1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are made
5 * 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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.statistics
;
14 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
16 import java
.text
.Format
;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
20 import java
.util
.Map
.Entry
;
22 import org
.eclipse
.jdt
.annotation
.Nullable
;
23 import org
.eclipse
.jface
.action
.Action
;
24 import org
.eclipse
.jface
.action
.IAction
;
25 import org
.eclipse
.jface
.action
.IMenuManager
;
26 import org
.eclipse
.jface
.action
.MenuManager
;
27 import org
.eclipse
.jface
.viewers
.ISelection
;
28 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
29 import org
.eclipse
.jface
.viewers
.TreeViewer
;
30 import org
.eclipse
.jface
.viewers
.Viewer
;
31 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
32 import org
.eclipse
.swt
.SWT
;
33 import org
.eclipse
.swt
.widgets
.Composite
;
34 import org
.eclipse
.swt
.widgets
.Menu
;
35 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.statistics
.AbstractSegmentStatisticsAnalysis
;
36 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.statistics
.SegmentStoreStatistics
;
37 import org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.SubSecondTimeWithUnitFormat
;
38 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.Activator
;
39 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.views
.segmentstore
.statistics
.Messages
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.AbstractTmfTreeViewer
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.ITmfTreeColumnDataProvider
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.ITmfTreeViewerEntry
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.TmfTreeColumnData
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.TmfTreeViewerEntry
;
52 * An abstract tree viewer implementation for displaying segment store
55 * @author Bernd Hufmann
58 public abstract class AbstractSegmentStoreStatisticsViewer
extends AbstractTmfTreeViewer
{
60 private static final Format FORMATTER
= new SubSecondTimeWithUnitFormat();
63 private TmfAbstractAnalysisModule fModule
;
64 private MenuManager fTablePopupMenuManager
;
66 private static final String
[] COLUMN_NAMES
= new String
[] {
67 checkNotNull(Messages
.SegmentStoreStatistics_LevelLabel
),
68 checkNotNull(Messages
.SegmentStoreStatistics_Statistics_MinLabel
),
69 checkNotNull(Messages
.SegmentStoreStatistics_MaxLabel
),
70 checkNotNull(Messages
.SegmentStoreStatistics_AverageLabel
),
71 checkNotNull(Messages
.SegmentStoreStatisticsViewer_StandardDeviation
),
72 checkNotNull(Messages
.SegmentStoreStatisticsViewer_Count
),
73 checkNotNull(Messages
.SegmentStoreStatisticsViewer_Total
)
80 * the parent composite
82 public AbstractSegmentStoreStatisticsViewer(Composite parent
) {
84 setLabelProvider(new SegmentStoreStatisticsLabelProvider());
85 fTablePopupMenuManager
= new MenuManager();
86 fTablePopupMenuManager
.setRemoveAllWhenShown(true);
87 fTablePopupMenuManager
.addMenuListener(manager
-> {
88 TreeViewer viewer
= getTreeViewer();
89 ISelection selection
= viewer
.getSelection();
90 if (selection
instanceof IStructuredSelection
) {
91 IStructuredSelection sel
= (IStructuredSelection
) selection
;
92 if (manager
!= null) {
93 appendToTablePopupMenu(manager
, sel
);
97 Menu tablePopup
= fTablePopupMenuManager
.createContextMenu(getTreeViewer().getTree());
98 getTreeViewer().getTree().setMenu(tablePopup
);
101 /** Provides label for the Segment Store tree viewer cells */
102 protected static class SegmentStoreStatisticsLabelProvider
extends TreeLabelProvider
{
105 public String
getColumnText(@Nullable Object element
, int columnIndex
) {
106 String value
= ""; //$NON-NLS-1$
107 if (element
instanceof HiddenTreeViewerEntry
) {
108 if (columnIndex
== 0) {
109 value
= ((HiddenTreeViewerEntry
) element
).getName();
111 } else if (element
instanceof SegmentStoreStatisticsEntry
) {
112 SegmentStoreStatisticsEntry entry
= (SegmentStoreStatisticsEntry
) element
;
113 if (columnIndex
== 0) {
114 return String
.valueOf(entry
.getName());
116 if (entry
.getEntry().getNbSegments() > 0) {
117 if (columnIndex
== 1) {
118 value
= toFormattedString(entry
.getEntry().getMin());
119 } else if (columnIndex
== 2) {
120 value
= String
.valueOf(toFormattedString(entry
.getEntry().getMax()));
121 } else if (columnIndex
== 3) {
122 value
= String
.valueOf(toFormattedString(entry
.getEntry().getAverage()));
123 } else if (columnIndex
== 4) {
124 value
= String
.valueOf(toFormattedString(entry
.getEntry().getStdDev()));
125 } else if (columnIndex
== 5) {
126 value
= String
.valueOf(entry
.getEntry().getNbSegments());
127 } else if (columnIndex
== 6) {
128 value
= String
.valueOf(toFormattedString(entry
.getEntry().getTotal()));
132 return checkNotNull(value
);
137 * Creates the statistics analysis module
139 * @return the statistics analysis module
142 protected abstract TmfAbstractAnalysisModule
createStatisticsAnalysiModule();
145 * Gets the statistics analysis module
147 * @return the statistics analysis module
150 public TmfAbstractAnalysisModule
getStatisticsAnalysisModule() {
155 protected ITmfTreeColumnDataProvider
getColumnDataProvider() {
156 return new ITmfTreeColumnDataProvider() {
159 public List
<@Nullable TmfTreeColumnData
> getColumnData() {
160 /* All columns are sortable */
161 List
<@Nullable TmfTreeColumnData
> columns
= new ArrayList
<>();
162 TmfTreeColumnData column
= new TmfTreeColumnData(COLUMN_NAMES
[0]);
163 column
.setAlignment(SWT
.RIGHT
);
164 column
.setComparator(new ViewerComparator() {
166 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
167 if ((e1
== null) || (e2
== null)) {
171 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
172 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
174 return n1
.getName().compareTo(n2
.getName());
179 column
= new TmfTreeColumnData(COLUMN_NAMES
[1]);
180 column
.setAlignment(SWT
.RIGHT
);
181 column
.setComparator(new ViewerComparator() {
183 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
184 if ((e1
== null) || (e2
== null)) {
188 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
189 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
191 return Long
.compare(n1
.getEntry().getMin(), n2
.getEntry().getMin());
196 column
= new TmfTreeColumnData(COLUMN_NAMES
[2]);
197 column
.setAlignment(SWT
.RIGHT
);
198 column
.setComparator(new ViewerComparator() {
200 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
201 if ((e1
== null) || (e2
== null)) {
205 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
206 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
208 return Long
.compare(n1
.getEntry().getMax(), n2
.getEntry().getMax());
213 column
= new TmfTreeColumnData(COLUMN_NAMES
[3]);
214 column
.setAlignment(SWT
.RIGHT
);
215 column
.setComparator(new ViewerComparator() {
217 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
218 if ((e1
== null) || (e2
== null)) {
222 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
223 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
225 return Double
.compare(n1
.getEntry().getAverage(), n2
.getEntry().getAverage());
230 column
= new TmfTreeColumnData(COLUMN_NAMES
[4]);
231 column
.setAlignment(SWT
.RIGHT
);
232 column
.setComparator(new ViewerComparator() {
234 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
235 if ((e1
== null) || (e2
== null)) {
239 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
240 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
242 return Double
.compare(n1
.getEntry().getStdDev(), n2
.getEntry().getStdDev());
247 column
= new TmfTreeColumnData(COLUMN_NAMES
[5]);
248 column
.setAlignment(SWT
.RIGHT
);
249 column
.setComparator(new ViewerComparator() {
251 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
252 if ((e1
== null) || (e2
== null)) {
256 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
257 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
259 return Long
.compare(n1
.getEntry().getNbSegments(), n2
.getEntry().getNbSegments());
264 column
= new TmfTreeColumnData(COLUMN_NAMES
[6]);
265 column
.setAlignment(SWT
.RIGHT
);
266 column
.setComparator(new ViewerComparator() {
268 public int compare(@Nullable Viewer viewer
, @Nullable Object e1
, @Nullable Object e2
) {
269 if ((e1
== null) || (e2
== null)) {
273 SegmentStoreStatisticsEntry n1
= (SegmentStoreStatisticsEntry
) e1
;
274 SegmentStoreStatisticsEntry n2
= (SegmentStoreStatisticsEntry
) e2
;
276 return Double
.compare(n1
.getEntry().getTotal(), n2
.getEntry().getTotal());
281 column
= new TmfTreeColumnData(""); //$NON-NLS-1$
290 public void initializeDataSource() {
291 ITmfTrace trace
= getTrace();
293 TmfAbstractAnalysisModule module
= createStatisticsAnalysiModule();
294 if (module
== null) {
298 module
.setTrace(trace
);
301 } catch (TmfAnalysisException e
) {
302 Activator
.getDefault().logError("Error initializing statistics analysis module", e
); //$NON-NLS-1$
308 * Method to add commands to the context sensitive menu.
313 * the current selection
315 protected void appendToTablePopupMenu(IMenuManager manager
, IStructuredSelection sel
) {
316 Object element
= sel
.getFirstElement();
317 if ((element
instanceof SegmentStoreStatisticsEntry
) && !(element
instanceof HiddenTreeViewerEntry
)) {
318 final SegmentStoreStatisticsEntry segment
= (SegmentStoreStatisticsEntry
) element
;
319 IAction gotoStartTime
= new Action(Messages
.SegmentStoreStatisticsViewer_GotoMinAction
) {
322 long start
= segment
.getEntry().getMinSegment().getStart();
323 long end
= segment
.getEntry().getMinSegment().getEnd();
324 broadcast(new TmfSelectionRangeUpdatedSignal(AbstractSegmentStoreStatisticsViewer
.this, TmfTimestamp
.fromNanos(start
), TmfTimestamp
.fromNanos(end
)));
328 IAction gotoEndTime
= new Action(Messages
.SegmentStoreStatisticsViewer_GotoMaxAction
) {
331 long start
= segment
.getEntry().getMaxSegment().getStart();
332 long end
= segment
.getEntry().getMaxSegment().getEnd();
333 broadcast(new TmfSelectionRangeUpdatedSignal(AbstractSegmentStoreStatisticsViewer
.this, TmfTimestamp
.fromNanos(start
), TmfTimestamp
.fromNanos(end
)));
337 manager
.add(gotoStartTime
);
338 manager
.add(gotoEndTime
);
343 * Formats a double value string
347 * @return formatted value
349 protected static String
toFormattedString(double value
) {
350 // The cast to long is needed because the formatter cannot truncate the
352 String percentageString
= String
.format("%s", FORMATTER
.format(value
)); //$NON-NLS-1$
353 return percentageString
;
357 * Class for defining an entry in the statistics tree.
359 protected class SegmentStoreStatisticsEntry
extends TmfTreeViewerEntry
{
361 private final SegmentStoreStatistics fEntry
;
370 * segment store statistics object
372 public SegmentStoreStatisticsEntry(String name
, SegmentStoreStatistics entry
) {
378 * Gets the statistics object
380 * @return statistics object
382 public SegmentStoreStatistics
getEntry() {
389 protected @Nullable ITmfTreeViewerEntry
updateElements(long start
, long end
, boolean isSelection
) {
390 if (isSelection
|| (start
== end
)) {
394 TmfAbstractAnalysisModule analysisModule
= getStatisticsAnalysisModule();
396 if (getTrace() == null || !(analysisModule
instanceof AbstractSegmentStatisticsAnalysis
)) {
400 AbstractSegmentStatisticsAnalysis module
= (AbstractSegmentStatisticsAnalysis
) analysisModule
;
402 module
.waitForCompletion();
404 TmfTreeViewerEntry root
= new TmfTreeViewerEntry(""); //$NON-NLS-1$
405 final SegmentStoreStatistics entry
= module
.getTotalStats();
408 List
<ITmfTreeViewerEntry
> entryList
= root
.getChildren();
410 TmfTreeViewerEntry aggregateEntry
= new SegmentStoreStatisticsEntry(getTotalLabel(), entry
);
411 entryList
.add(aggregateEntry
);
412 HiddenTreeViewerEntry category
= new HiddenTreeViewerEntry(getTypeLabel());
413 aggregateEntry
.addChild(category
);
415 Map
<String
, SegmentStoreStatistics
> perSegmentStats
= module
.getPerSegmentTypeStats();
416 if (perSegmentStats
!= null) {
417 for (Entry
<String
, SegmentStoreStatistics
> statsEntry
: perSegmentStats
.entrySet()) {
418 category
.addChild(new SegmentStoreStatisticsEntry(statsEntry
.getKey(), statsEntry
.getValue()));
431 protected String
getTypeLabel() {
432 return checkNotNull(Messages
.AbstractSegmentStoreStatisticsViewer_types
);
436 * Get the total column label
438 * @return the totals column label
441 protected String
getTotalLabel() {
442 return checkNotNull(Messages
.AbstractSegmentStoreStatisticsViewer_total
);
446 * Class to define a level in the tree that doesn't have any values.
448 protected class HiddenTreeViewerEntry
extends SegmentStoreStatisticsEntry
{
453 * the name of the level
455 public HiddenTreeViewerEntry(String name
) {
456 super(name
, new SegmentStoreStatistics());