Some refactoring in histogram, added javadoc and better comments
[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 private HistogramContent histogramContent = null;
27
28 private int lastInterval = 0;
29 private long lastRangeTime = 0L;
30 private long nbEventsInInterval = 1;
31
32 private int nbIntervalNotEmpty = 1;
33 private int nbEventRead = 0;
34
35 private HistogramCanvas parentCanvas = null;
36
37 private boolean requestCompleted = false;
38
39 /**
40 * Constructor for HistogramRequest.<p>
41 * Prepare the request in TMF and reset the histogram content.
42 *
43 * @param range Range of the request.
44 * @param nbRequested Nb events requested. Can be "Infinity" for all.
45 * @param newParentCanvas HistogramCanvas related to the request.
46 * @param timeInterval Time interval to consider (i.e. : 1 interval is 1 bar in the histogram)
47 *
48 * @see org.eclipse.linuxtools.tmf.request.TmfEventRequest
49 */
50 public HistogramRequest(TmfTimeRange range, int nbRequested, HistogramCanvas newParentCanvas, Long timeInterval) {
51 super((Class<LttngEvent>)LttngEvent.class, range, nbRequested, HistogramConstant.MAX_EVENTS_PER_READ);
52
53 // *** FIXME ***
54 // This does not work! The request won't be processed or the number of events returned is wrong!
55 // We cannot use this !
56 //super((Class<LttngEvent>)dataType, range);
57
58 parentCanvas = newParentCanvas;
59 histogramContent = parentCanvas.getHistogramContent();
60
61 // Reset the content of the HistogramContent... the given data better be valid or this will fail.
62 histogramContent.resetContentData();
63 histogramContent.setStartTime(range.getStartTime().getValue());
64 histogramContent.setEndTime(range.getEndTime().getValue());
65 histogramContent.setIntervalTime(timeInterval);
66 histogramContent.resetTable();
67
68 lastRangeTime = histogramContent.getStartTime();
69 }
70
71 /**
72 * HandleData function : will be called by TMF each time a new event is receive for the request.<p>
73 * Calculation for the content is done here.
74 */
75 @Override
76 public void handleData() {
77 TmfEvent[] result = getData();
78 TmfEvent[] evt = new TmfEvent[1];
79
80 evt[0] = (result.length > 0) ? result[0] : null;
81
82 // *** FIXME ***
83 // *** EVIL BUG ***
84 // The request by timerange only does not work! (see constructor above)
85 // However, the request with number of events will loop until it reach its number or EOF
86 // We have to filter out ourself the extra useless events!
87 //
88 if ( (evt[0] != null) && (requestCompleted == false) ) {
89 LttngEvent tmpEvent = (LttngEvent)evt[0];
90
91 // This check is linked to the evil fix mentionned above
92 if ( tmpEvent.getTimestamp().getValue() <= histogramContent.getEndTime() ) {
93
94 // Distance (in time) between this event and the last one we read
95 long distance = ( tmpEvent.getTimestamp().getValue() - lastRangeTime );
96
97 // Check if we changed of interval (the distance is higher than the interval time)
98 if ( distance > histogramContent.getIntervalTime() ) {
99
100 histogramContent.getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval;
101 lastRangeTime = tmpEvent.getTimestamp().getValue();
102
103 // * NOTE *
104 // We can skip several interval at once, so we need to find what was our interval now
105 lastInterval = (int)((lastRangeTime - histogramContent.getStartTime()) / histogramContent.getIntervalTime() );
106
107 // *** HACK ***
108 // Because of the threads, weird phenomenons seem to happen here, like a position after the
109 // element range because another request was issued.
110 // This enforce the position but may result in slightly inconsistent result (i.e. a weird misplaced bar sometime).
111 if ( lastInterval < 0 ) {
112 lastInterval = 0;
113 }
114 else if ( lastInterval >= histogramContent.getNbElement() ) {
115 lastInterval = (histogramContent.getNbElement()-1);
116 }
117
118 // * NOTE *
119 // We save the time we have. This mean only the FIRST time read in an interval will be saved.
120 histogramContent.getElementByIndex(lastInterval).firstIntervalTimestamp = lastRangeTime;
121 histogramContent.setReadyUpToPosition(lastInterval);
122
123 nbIntervalNotEmpty++;
124 nbEventsInInterval = 1;
125 }
126 // We are still in the same interval, just keep counting
127 else {
128 nbEventsInInterval++;
129 if ( nbEventsInInterval > histogramContent.getHeighestEventCount() ) {
130 histogramContent.setHeighestEventCount(nbEventsInInterval);
131 }
132 }
133
134 nbEventRead++;
135
136 // Call an asynchronous redraw every REDRAW_EVERY_NB_EVENTS events
137 // That way we don't need to wait until to end to have something on the screen
138 if ( nbEventRead % HistogramConstant.REDRAW_EVERY_NB_EVENTS == 0 ) {
139 redrawAsyncronously();
140 }
141 }
142 else {
143 // *** FIXME ***
144 // *** EVIL FIX ***
145 // Because of the other evil bug (see above), we have to ignore extra useless events we will get
146 // However, we might be far away from the end so we better start a redraw now
147 redrawAsyncronously();
148 requestCompleted = true;
149
150 // Althought it won't do anything, try to call control functions to stop the request
151 done();
152 cancel();
153 fail();
154 }
155 }
156 }
157
158 /**
159 * Function that is called when the request completed (successful or not).<p>
160 * Update information and redraw the screen.
161 */
162 @Override
163 public void handleCompleted() {
164 parentCanvas.canvasRedrawer.asynchronousNotifyParentUpdatedInformation();
165 redrawAsyncronously();
166 }
167
168 /**
169 * Function that is called when the request completed successfully.<p>
170 */
171 @Override
172 public void handleSuccess() {
173 // Nothing different from completed.
174 }
175
176 /**
177 * Function that is called when the request completed in failure.<p>
178 */
179 @Override
180 public void handleFailure() {
181 // Nothing different from cancel.
182 }
183
184 /**
185 * Function that is called when the request was cancelled.<p>
186 * Redraw and set the requestCompleted flag to true;
187 */
188 @Override
189 public void handleCancel() {
190 redrawAsyncronously();
191 requestCompleted = true;
192 }
193
194 /**
195 * Update the HistogramContent with the latest information.<p>
196 * This will perform some calculation that might be a bit harsh so it shouldnt be called too often.
197 */
198 public void updateEventsInfo() {
199 int averageNumberOfEvents = nbEventRead / nbIntervalNotEmpty;
200 histogramContent.setAverageNumberOfEvents(averageNumberOfEvents);
201 histogramContent.recalculateEventHeight();
202 }
203
204 /**
205 * Perform an asynchonous redraw of the screen.
206 */
207 public void redrawAsyncronously() {
208 updateEventsInfo();
209 // Canvas redraw is already asynchronous
210 parentCanvas.redrawAsynchronously();
211 }
212
213 }
This page took 0.036584 seconds and 6 git commands to generate.