Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / HistogramRequest.java
index 56a585e02eea91c1f33b4093d621baff7ded09e8..2b88329570eabb9bade5ffd75ecc725bf35c8357 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009 Ericsson
+ * Copyright (c) 2009, 2011 Ericsson
  * 
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
  * 
  * Contributors:
  *   William Bourque - Initial API and implementation
+ *   Yuriy Vashchuk - Heritage correction.
+ *   Francois Chouinard - Cleanup and refactoring
  *******************************************************************************/
+
 package org.eclipse.linuxtools.lttng.ui.views.histogram;
 
-import org.eclipse.linuxtools.lttng.event.LttngEvent;
-import org.eclipse.linuxtools.tmf.event.TmfEvent;
-import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
-import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;
-import org.eclipse.linuxtools.tmf.request.TmfEventRequest;
+import org.eclipse.linuxtools.lttng.core.LttngConstants;
+import org.eclipse.linuxtools.lttng.core.event.LttngEvent;
+import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
+import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
+import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
 
 /**
  * <b><u>HistogramRequest</u></b>
  * <p>
- * Request class, to perform a request to TMF for the histograms.
- * <p>
  */
 public class HistogramRequest extends TmfEventRequest<LttngEvent> {
-       protected HistogramContent histogramContent = null;
-       
-       protected int   lastInterval = 0;
-       protected long          lastRangeTime = 0L;
-       protected long          nbEventsInInterval = 0L;
-       
-       protected int   nbIntervalNotEmpty = 1;
-       protected int   nbEventRead = 0;
-       
-       protected int   lastDrawPosition = 0;
-       
-       protected HistogramCanvas parentCanvas = null;
-       
-       /**
-        * Constructor for HistogramRequest.<p>
-        * Prepare the request in TMF and reset the histogram content.
-        * 
-        * @param range                         Range of the request.
-        * @param nbRequested           Nb events requested. Can be "Infinity" for all.
-        * @param newParentCanvas       HistogramCanvas related to the request.
-        * @param timeInterval          Time interval to consider (i.e. : 1 interval is 1 bar in the histogram)
-        * 
-        * @see org.eclipse.linuxtools.tmf.request.TmfEventRequest
-        */
-       public HistogramRequest(TmfTimeRange range, int nbRequested, HistogramCanvas newParentCanvas, long timeInterval, ITmfDataRequest.ExecutionType execType) {
-        super((Class<LttngEvent>)LttngEvent.class, range, nbRequested, HistogramConstant.MAX_EVENTS_PER_READ, execType);
-        
-        // *** FIXME ***
-        // This does not work! The request won't be processed or the number of events returned is wrong!
-        // We cannot use this !
-               //super((Class<LttngEvent>)dataType, range);
-        
-        parentCanvas = newParentCanvas;
-        histogramContent = parentCanvas.getHistogramContent();
-        
-        // Reset the content of the HistogramContent... the given data better be valid or this will fail.
-        histogramContent.clearContentData();
-        histogramContent.resetTable(range.getStartTime().getValue(), range.getEndTime().getValue(), timeInterval);
-        
-        lastRangeTime = range.getStartTime().getValue();
-        
-        // Notify the UI even before the request started, so we set the timestamp already.
-        parentCanvas.notifyParentUpdatedInformationAsynchronously();
+
+    // ------------------------------------------------------------------------
+    // Attributes
+    // ------------------------------------------------------------------------
+
+    private final Histogram fHistogram;
+
+    // ------------------------------------------------------------------------
+    // Constructor
+    // ------------------------------------------------------------------------
+
+    public HistogramRequest(Histogram histogram, TmfTimeRange range, int rank, int nbEvents, ITmfDataRequest.ExecutionType execType) {
+        super(LttngEvent.class, range, rank, nbEvents, LttngConstants.DEFAULT_BLOCK_SIZE, execType);
+        fHistogram = histogram;
     }
-       
-       /**
-        * HandleData function : will be called by TMF each time a new event is receive for the request.<p>
-        * Calculation for the content is done here.
-        */
-       @Override
-    public void handleData() {
-        TmfEvent[] result = getData();
-        TmfEvent[] evt = new TmfEvent[1];
-        
-        evt[0] = (result.length > 0) ? result[0] : null;
-        
-        // *** FIXME ***
-       // *** EVIL BUG ***
-        // The request by timerange only does not work! (see constructor above) 
-       //      However, the request with number of events will loop until it reach its number or EOF
-       //  We have to filter out ourself the extra useless events!
-       //
-        if (evt[0] != null) {
-        
-               LttngEvent tmpEvent = (LttngEvent)evt[0];
-               
-               // This check is linked to the evil fix mentionned above
-               if ( ( tmpEvent.getTimestamp().getValue() >= histogramContent.getStartTime() ) &&
-                        ( tmpEvent.getTimestamp().getValue() <= histogramContent.getEndTime() ) )
-               {
-                       
-                       // Distance (in time) between this event and the last one we read
-                       long distance = ( tmpEvent.getTimestamp().getValue() - lastRangeTime );
-                               
-                       // Check if we changed of interval (the distance is higher than the interval time)
-                               if  ( distance > histogramContent.getElementsTimeInterval() ) {
-                                       
-                                       histogramContent.getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval;
-                                       lastRangeTime = tmpEvent.getTimestamp().getValue();
-                                       
-                                       // * NOTE *
-                                       // We can skip several interval at once, so we need to find what was our interval now
-                                       lastInterval = (int)((lastRangeTime - histogramContent.getStartTime()) / histogramContent.getElementsTimeInterval() );
-                                       
-                                       // *** HACK ***
-                                       // Because of the threads, weird phenomenons seem to happen here, like a position after the 
-                                       //       element range because another request was issued.
-                                       // This enforce the position but may result in slightly inconsistent result (i.e. a weird misplaced bar sometime).
-                                       if ( lastInterval < 0 ) {
-                                               lastInterval = 0;
-                                       }
-                                       else if ( lastInterval >= histogramContent.getNbElement() ) {
-                                               lastInterval = (histogramContent.getNbElement()-1);
-                                       }
-                                       
-                                       // * NOTE * 
-                                       // We save the time we have here. This mean only the FIRST time read in an interval will be saved. 
-                                       histogramContent.getElementByIndex(lastInterval).firstIntervalTimestamp = lastRangeTime;
-                                       histogramContent.setReadyUpToPosition(lastInterval);
-                                       
-                                       nbIntervalNotEmpty++;
-                                       nbEventsInInterval = 1L;
-                               }
-                               // We are still in the same interval, just keep counting
-                               else {
-                                       nbEventsInInterval++;
-                               }
-                               
-                               if ( nbEventsInInterval > histogramContent.getHeighestEventCount() ) {
-                                       histogramContent.setHeighestEventCount(nbEventsInInterval);
-                               }
-                               nbEventRead++;
-                               
-                               // Call an asynchronous redraw every REDRAW_EVERY_NB_EVENTS events
-                               // That way we don't need to wait until to end to have something on the screen
-                               if ( nbEventRead % HistogramConstant.REDRAW_EVERY_NB_EVENTS == 0 ) {
-                                       redrawAsyncronously();
-                               }
-               }
-               }
-        // We got a null event! This mean we reach the end of the request. 
-        // Save the last interval we had, so we won't miss the very last events at the end. 
-        else {
-               // Save the last events
-               histogramContent.getElementByIndex(lastInterval).intervalNbEvents = nbEventsInInterval;
-               // We reached the end of the request, so assume we fill up the content as well
-                       histogramContent.setReadyUpToPosition(histogramContent.getNbElement());
-                       
-                       // If the interval wasn't null, count this as a "non empty" interval
-                       if (nbEventsInInterval > 0) {
-                               nbIntervalNotEmpty++;
-                       }
-        }
+
+    public HistogramRequest(Histogram histogram, TmfTimeRange range, ITmfDataRequest.ExecutionType execType) {
+        this(histogram, range, 0, ALL_DATA, execType);
     }
-       
-       /**
-        * Function that is called when the request completed (successful or not).<p>
-        * Update information and redraw the screen.
-        */
-    @Override
-    public void handleCompleted() {
-       parentCanvas.notifyParentUpdatedInformationAsynchronously();
-               redrawAsyncronously();
+
+    public HistogramRequest(Histogram histogram, TmfTimeRange range, int rank, ITmfDataRequest.ExecutionType execType) {
+        this(histogram, range, rank, ALL_DATA, execType);
     }
-    
-    /**
-        * Function that is called when the request completed successfully.<p>
-        */
+
+    // ------------------------------------------------------------------------
+    // TmfEventRequest
+    // ------------------------------------------------------------------------
+
     @Override
-    public void handleSuccess() {
-       // Nothing different from completed.
+    public void handleData(LttngEvent event) {
+        super.handleData(event);
+        if (event != null) {
+            long timestamp = event.getTimestamp().getValue();
+            fHistogram.countEvent(timestamp);
+        }
     }
-    
-    /**
-        * Function that is called when the request completed in failure.<p>
-        */
+
     @Override
-    public void handleFailure() {
-       // Nothing different from cancel.
+    public void handleCompleted() {
+        fHistogram.refresh();
+        super.handleCompleted();
     }
-    
-    /**
-        * Function that is called when the request was cancelled.<p>
-        * Redraw and set the requestCompleted flag to true;
-        */
+
     @Override
     public void handleCancel() {
-       redrawAsyncronously();
-    }
-       
-    /**
-        * Update the HistogramContent with the latest information.<p>
-        * This will perform some calculation that might be a bit harsh so it should'nt be called too often.
-        */
-    public void updateEventsInfo() {
-       // *** Note *** 
-       // The average number of event is calculated while skipping empty interval if asked
-       int averageNumberOfEvents = 0;
-       if ( HistogramConstant.SKIP_EMPTY_INTERVALS_WHEN_CALCULATING_AVERAGE ) {
-               averageNumberOfEvents = (int)Math.ceil((double)nbEventRead / (double)nbIntervalNotEmpty);
-       }
-       else {
-               averageNumberOfEvents = (int)Math.ceil((double)nbEventRead / (double)histogramContent.getNbElement());
-       }
-       
-       histogramContent.setAverageNumberOfEvents(averageNumberOfEvents);
-       
-       // It is possible that the height factor didn't change; 
-       //              If not, we only need to redraw the updated section, no the whole content
-       // Save the actual height, recalculate the height and check if there was any changes
-       double previousHeightFactor = histogramContent.getHeightFactor();
-       histogramContent.recalculateHeightFactor();
-       if ( histogramContent.getHeightFactor() != previousHeightFactor ) {
-                       histogramContent.recalculateEventHeight();
-       }
-       else {
-               histogramContent.recalculateEventHeightInInterval(lastDrawPosition, histogramContent.getReadyUpToPosition());
-       }
-       
-       lastDrawPosition = histogramContent.getReadyUpToPosition();
+        fHistogram.refresh();
+        super.handleCancel();
     }
-    
-    /**
-        * Perform an asynchonous redraw of the screen.
-        */
-    public void redrawAsyncronously() {
-       updateEventsInfo();
-       // Canvas redraw is already asynchronous
-       parentCanvas.redrawAsynchronously();
-    }
-    
+
 }
This page took 0.026976 seconds and 5 git commands to generate.