tmf: Fix IllegalStateException in HistogramDataModel
authorPatrick Tasse <patrick.tasse@gmail.com>
Tue, 19 May 2015 21:12:05 +0000 (17:12 -0400)
committerPatrick Tasse <patrick.tasse@gmail.com>
Wed, 20 May 2015 13:45:56 +0000 (09:45 -0400)
It should be allowed to have a last bucket set to 0 in the histogram
data model. This can happen if all trace events have the same timestamp.

The histogram time range is fixed to display even if the start and end
times are equal, as long as there is at least one event in the model.

The scaling is updated to make sure that these single-timestamp events
are spread over the whole width of the histogram. The scaled model will
have a bucket duration of 0 when it represents a single timestamp.

Change-Id: I5341aa6a158a3b4c1b3d4edee982ed67558e8a51
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/48224
Reviewed-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/histogram/HistogramDataModelTest.java
org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/Histogram.java
org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/views/histogram/HistogramDataModel.java

index 007b5c0b57baf43e230c437ef505cd3bb80ce542..51f17088c1625c23c8c718adfbbe50424c75f02d 100644 (file)
@@ -130,7 +130,7 @@ public class HistogramDataModelTest {
      * {@link HistogramDataModel#countEvent(long,long, ITmfTrace)} and
      * {@link HistogramDataModel#scaleTo(int,int,int)}.
      */
-    @Test(expected = IllegalStateException.class)
+    @Test
     public void testCountEvent_2() {
         final int nbBuckets = 100;
         final int maxHeight = 10;
@@ -139,9 +139,11 @@ public class HistogramDataModelTest {
         model.countEvent(0, 1, null);
 
         HistogramScaledData result = model.scaleTo(nbBuckets, maxHeight, 1);
-        assertEquals(_1, result.fData[0]);
 
-        assertArrayEqualsInt(1, result.fData, 1);
+        for (int i = 0; i < result.fData.length - 1; i++) {
+            assertEquals(_1, result.fData[i]);
+        }
+        assertEquals(_0, result.fData[result.fData.length - 1]);
 
         testModelConsistency(model, nbBuckets, 1, 1, 1, 1, 1, nbBuckets + 1);
     }
index aa5adf40d29ec2e9c96e5f5bf51aec37015a4e61..bbe0b401cff5c8a8e92c59440b80308e40b81990 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2014 Ericsson
+ * Copyright (c) 2011, 2015 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -724,7 +724,7 @@ public abstract class Histogram implements ControlListener, PaintListener, KeyLi
      * Update the range text controls
      */
     private void updateRangeTextControls() {
-        if (fDataModel.getStartTime() < fDataModel.getEndTime()) {
+        if (fDataModel.getNbEvents() != 0) {
             fTimeRangeStartLabel.setText(TmfTimestampFormat.getDefaulTimeFormat().format(fDataModel.getStartTime()));
             fTimeRangeEndLabel.setText(TmfTimestampFormat.getDefaulTimeFormat().format(fDataModel.getEndTime()));
         } else {
@@ -1020,7 +1020,7 @@ public abstract class Histogram implements ControlListener, PaintListener, KeyLi
 
     @Override
     public void mouseHover(final MouseEvent event) {
-        if (fDataModel.getStartTime() < fDataModel.getEndTime() && fScaledData != null) {
+        if (fDataModel.getNbEvents() != 0 && fScaledData != null) {
             final String tooltip = formatToolTipLabel(event.x - fOffset);
             fCanvas.setToolTipText(tooltip);
             return;
index b617c4cf6f3bc958e73779ba5a90e3fdec454712..db187897a6fe24800e41f1c14a89521a87cdca95 100644 (file)
@@ -623,7 +623,7 @@ public class HistogramDataModel implements IHistogramDataModel {
         if ((width <= 0) || (height <= 0) || (barWidth <= 0)) {
             throw new AssertionError("Invalid histogram dimensions (" + width + "x" + height + ", barWidth=" + barWidth + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
         }
-        if (fLastBucket == 0 || fBucketDuration == 0) {
+        if (fBucketDuration == 0) {
             throw new IllegalStateException("Bucket width is 0, that should be impossible"); //$NON-NLS-1$
         }
 
@@ -637,10 +637,16 @@ public class HistogramDataModel implements IHistogramDataModel {
         double bucketsPerBar = ((double) fLastBucket / nbBars);
         final long modelBucketStartTime = fFirstBucketTime;
         final long modelBucketEndTime = fEndTime;
-        result.fBucketDuration = (modelBucketEndTime - modelBucketStartTime) / (double) nbBars;
+        /*
+         * If there is only one model bucket, use a duration of 1 to spread the
+         * value over the scaled width, but store a scaled bucket duration of 0
+         * to prevent the half-bucket offset in the bucket time calculations.
+         */
+        double bucketDuration = Math.max(modelBucketEndTime - modelBucketStartTime, 1) / (double) nbBars;
+        result.fBucketDuration = fLastBucket == 0 ? 0 : bucketDuration;
         int scaledCount = 0;
         int scaledCountLostEvent = 0;
-        int offset = (int) (0.5 / result.fBucketDuration);
+        int offset = (int) (0.5 / bucketDuration);
         for (int i = 0; i < result.fData.length; i++) {
             result.fData[i] = new HistogramBucket(getNbTraces());
         }
This page took 0.028236 seconds and 5 git commands to generate.