HistogramView update, can now resize
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / HistogramRequest.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * William Bourque - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.linuxtools.lttng.ui.views.histogram;
13
14 import org.eclipse.linuxtools.lttng.event.LttngEvent;
15 import org.eclipse.linuxtools.tmf.event.TmfEvent;
16 import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
17 import org.eclipse.linuxtools.tmf.request.TmfEventRequest;
18
19 /**
20 * <b><u>HistogramRequest</u></b>
21 * <p>
22 * Request class, to perform a request to TMF for the histograms.
23 * <p>
24 */
25 public class HistogramRequest extends TmfEventRequest<LttngEvent> {
26 protected HistogramContent histogramContent = null;
27
28 protected Integer lastInterval = 0;
29 protected Long lastRangeTime = 0L;
30 protected Long nbEventsInInterval = 1L;
31
32 protected Integer nbIntervalNotEmpty = 1;
33 protected Integer nbEventRead = 0;
34
35 protected Integer lastDrawPosition = 0;
36
37 protected Boolean requestCompleted = false;
38
39 protected HistogramCanvas parentCanvas = null;
40
41 /**
42 * Constructor for HistogramRequest.<p>
43 * Prepare the request in TMF and reset the histogram content.
44 *
45 * @param range Range of the request.
46 * @param nbRequested Nb events requested. Can be "Infinity" for all.
47 * @param newParentCanvas HistogramCanvas related to the request.
48 * @param timeInterval Time interval to consider (i.e. : 1 interval is 1 bar in the histogram)
49 *
50 * @see org.eclipse.linuxtools.tmf.request.TmfEventRequest
51 */
52 public HistogramRequest(TmfTimeRange range, Integer nbRequested, HistogramCanvas newParentCanvas, Long timeInterval) {
53 super((Class<LttngEvent>)LttngEvent.class, range, nbRequested, HistogramConstant.MAX_EVENTS_PER_READ);
54
55 // *** FIXME ***
56 // This does not work! The request won't be processed or the number of events returned is wrong!
57 // We cannot use this !
58 //super((Class<LttngEvent>)dataType, range);
59
60 parentCanvas = newParentCanvas;
61 histogramContent = parentCanvas.getHistogramContent();
62
63 // Reset the content of the HistogramContent... the given data better be valid or this will fail.
64 histogramContent.clearContentData();
65 histogramContent.resetTable(range.getStartTime().getValue(), range.getEndTime().getValue(), timeInterval);
66
67 lastRangeTime = range.getStartTime().getValue();
68
69 // Notify the UI even before the request started, so we set the timestamp already.
70 parentCanvas.notifyParentUpdatedInformationAsynchronously();
71 }
72
73 /**
74 * HandleData function : will be called by TMF each time a new event is receive for the request.<p>
75 * Calculation for the content is done here.
76 */
77 @Override
78 public void handleData() {
79 TmfEvent[] result = getData();
80 TmfEvent[] evt = new TmfEvent[1];
81
82 evt[0] = (result.length > 0) ? result[0] : null;
83
84 // *** FIXME ***
85 // *** EVIL BUG ***
86 // The request by timerange only does not work! (see constructor above)
87 // However, the request with number of events will loop until it reach its number or EOF
88 // We have to filter out ourself the extra useless events!
89 //
90 if ( (evt[0] != null) && (requestCompleted == false) ) {
91 LttngEvent tmpEvent = (LttngEvent)evt[0];
92
93 // This check is linked to the evil fix mentionned above
94 if ( ( tmpEvent.getTimestamp().getValue() >= histogramContent.getStartTime() ) &&
95 ( tmpEvent.getTimestamp().getValue() <= histogramContent.getEndTime() ) )
96 {
97
98 // Distance (in time) between this event and the last one we read
99 long distance = ( tmpEvent.getTimestamp().getValue() - lastRangeTime );
100
101 // Check if we changed of interval (the distance is higher than the interval time)
102 if ( distance > histogramContent.getElementsTimeInterval() ) {
103
104 histogramContent.getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval;
105 lastRangeTime = tmpEvent.getTimestamp().getValue();
106
107 // * NOTE *
108 // We can skip several interval at once, so we need to find what was our interval now
109 lastInterval = (int)((lastRangeTime - histogramContent.getStartTime()) / histogramContent.getElementsTimeInterval() );
110
111 // *** HACK ***
112 // Because of the threads, weird phenomenons seem to happen here, like a position after the
113 // element range because another request was issued.
114 // This enforce the position but may result in slightly inconsistent result (i.e. a weird misplaced bar sometime).
115 if ( lastInterval < 0 ) {
116 lastInterval = 0;
117 }
118 else if ( lastInterval >= histogramContent.getNbElement() ) {
119 lastInterval = (histogramContent.getNbElement()-1);
120 }
121
122 // * NOTE *
123 // We save the time we have here. This mean only the FIRST time read in an interval will be saved.
124 histogramContent.getElementByIndex(lastInterval).firstIntervalTimestamp = lastRangeTime;
125 histogramContent.setReadyUpToPosition(lastInterval);
126
127 nbIntervalNotEmpty++;
128 nbEventsInInterval = 1L;
129 }
130 // We are still in the same interval, just keep counting
131 else {
132 nbEventsInInterval++;
133 if ( nbEventsInInterval > histogramContent.getHeighestEventCount() ) {
134 histogramContent.setHeighestEventCount(nbEventsInInterval);
135 }
136 }
137
138 nbEventRead++;
139
140 // Call an asynchronous redraw every REDRAW_EVERY_NB_EVENTS events
141 // That way we don't need to wait until to end to have something on the screen
142 if ( nbEventRead % HistogramConstant.REDRAW_EVERY_NB_EVENTS == 0 ) {
143 redrawAsyncronously();
144 }
145 }
146 else {
147 //System.out.println("Requested Timerange is : " + histogramContent.getStartTime() + " / " + histogramContent.getEndTime());
148 //System.out.println("Time is : " + tmpEvent.getTimestamp().getValue());
149 // *** FIXME ***
150 // *** EVIL FIX ***
151 // Because of the other evil bug (see above), we have to ignore extra useless events we will get
152 // However, we might be far away from the end so we better start a redraw now
153 if (tmpEvent.getTimestamp().getValue() >= histogramContent.getEndTime()) {
154 redrawAsyncronously();
155 requestCompleted = true;
156 }
157 }
158 }
159 }
160
161 /**
162 * Function that is called when the request completed (successful or not).<p>
163 * Update information and redraw the screen.
164 */
165 @Override
166 public void handleCompleted() {
167 parentCanvas.notifyParentUpdatedInformationAsynchronously();
168 redrawAsyncronously();
169 }
170
171 /**
172 * Function that is called when the request completed successfully.<p>
173 */
174 @Override
175 public void handleSuccess() {
176 // Nothing different from completed.
177 }
178
179 /**
180 * Function that is called when the request completed in failure.<p>
181 */
182 @Override
183 public void handleFailure() {
184 // Nothing different from cancel.
185 }
186
187 /**
188 * Function that is called when the request was cancelled.<p>
189 * Redraw and set the requestCompleted flag to true;
190 */
191 @Override
192 public void handleCancel() {
193 redrawAsyncronously();
194 requestCompleted = true;
195 }
196
197 /**
198 * Update the HistogramContent with the latest information.<p>
199 * This will perform some calculation that might be a bit harsh so it should'nt be called too often.
200 */
201 public void updateEventsInfo() {
202 // *** Note ***
203 // The average number of event is calculated while skipping empty interval if asked
204 int averageNumberOfEvents = 0;
205 if ( HistogramConstant.SKIP_EMPTY_INTERVALS_WHEN_CALCULATING_AVERAGE ) {
206 averageNumberOfEvents = nbEventRead / nbIntervalNotEmpty;
207 }
208 else {
209 averageNumberOfEvents = nbEventRead / histogramContent.getNbElement();
210 }
211 histogramContent.setAverageNumberOfEvents(averageNumberOfEvents);
212
213 // It is possible that the height factor didn't change;
214 // If not, we only need to redraw the updated section, no the whole content
215 // Save the actual height, recalculate the height and check if there was any changes
216 double previousHeightFactor = histogramContent.getHeightFactor();
217 histogramContent.recalculateHeightFactor();
218 if ( histogramContent.getHeightFactor() != previousHeightFactor ) {
219 histogramContent.recalculateEventHeight();
220 }
221 else {
222 histogramContent.recalculateEventHeightInInterval(lastDrawPosition, histogramContent.getReadyUpToPosition());
223 }
224
225 lastDrawPosition = histogramContent.getReadyUpToPosition();
226 }
227
228 /**
229 * Perform an asynchonous redraw of the screen.
230 */
231 public void redrawAsyncronously() {
232 updateEventsInfo();
233 // Canvas redraw is already asynchronous
234 parentCanvas.redrawAsynchronously();
235 }
236
237 }
This page took 0.037196 seconds and 6 git commands to generate.