timing.core: Add local statistics to the latency statistics
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.ui / src / org / eclipse / tracecompass / analysis / timing / ui / views / segmentstore / statistics / AbstractSegmentStoreStatisticsViewer.java
CommitLineData
ce8319b6 1/*******************************************************************************
658401c8 2 * Copyright (c) 2015, 2016 Ericsson
ce8319b6
BH
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
5b901f94 12package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.statistics;
ce8319b6
BH
13
14import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
15
91deb4e1 16import java.text.Format;
ce8319b6
BH
17import java.util.ArrayList;
18import java.util.List;
dda7186d
MK
19import java.util.Map;
20import java.util.Map.Entry;
ce8319b6 21
b405ad64
JCK
22import org.eclipse.core.runtime.IProgressMonitor;
23import org.eclipse.core.runtime.NullProgressMonitor;
24import org.eclipse.jdt.annotation.NonNull;
ce8319b6 25import org.eclipse.jdt.annotation.Nullable;
a1e4b7e8
BH
26import org.eclipse.jface.action.Action;
27import org.eclipse.jface.action.IAction;
28import org.eclipse.jface.action.IMenuManager;
29import org.eclipse.jface.action.MenuManager;
30import org.eclipse.jface.viewers.ISelection;
31import org.eclipse.jface.viewers.IStructuredSelection;
32import org.eclipse.jface.viewers.TreeViewer;
ce8319b6
BH
33import org.eclipse.jface.viewers.Viewer;
34import org.eclipse.jface.viewers.ViewerComparator;
a0acb044 35import org.eclipse.swt.SWT;
ce8319b6 36import org.eclipse.swt.widgets.Composite;
a1e4b7e8 37import org.eclipse.swt.widgets.Menu;
dda7186d 38import org.eclipse.tracecompass.analysis.timing.core.segmentstore.statistics.AbstractSegmentStatisticsAnalysis;
5b901f94 39import org.eclipse.tracecompass.analysis.timing.core.segmentstore.statistics.SegmentStoreStatistics;
91deb4e1 40import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.SubSecondTimeWithUnitFormat;
658401c8 41import org.eclipse.tracecompass.internal.analysis.timing.ui.Activator;
d57672d7 42import org.eclipse.tracecompass.internal.analysis.timing.ui.views.segmentstore.statistics.Messages;
ce8319b6
BH
43import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
44import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
a1e4b7e8 45import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
b405ad64
JCK
46import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
47import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal;
a1e4b7e8 48import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
ce8319b6
BH
49import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
50import org.eclipse.tracecompass.tmf.ui.viewers.tree.AbstractTmfTreeViewer;
51import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeColumnDataProvider;
dda7186d 52import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeViewerEntry;
ce8319b6
BH
53import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData;
54import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeViewerEntry;
55
56/**
a0acb044
MK
57 * An abstract tree viewer implementation for displaying segment store
58 * statistics
ce8319b6
BH
59 *
60 * @author Bernd Hufmann
61 *
62 */
3507ca88 63public abstract class AbstractSegmentStoreStatisticsViewer extends AbstractTmfTreeViewer {
ce8319b6 64
91deb4e1 65 private static final Format FORMATTER = new SubSecondTimeWithUnitFormat();
ce8319b6 66
b405ad64 67 private @Nullable TmfAbstractAnalysisModule fModule;
a1e4b7e8 68 private MenuManager fTablePopupMenuManager;
ce8319b6
BH
69
70 private static final String[] COLUMN_NAMES = new String[] {
53f46dc0
BH
71 checkNotNull(Messages.SegmentStoreStatistics_LevelLabel),
72 checkNotNull(Messages.SegmentStoreStatistics_Statistics_MinLabel),
73 checkNotNull(Messages.SegmentStoreStatistics_MaxLabel),
a0acb044 74 checkNotNull(Messages.SegmentStoreStatistics_AverageLabel),
8f6e9344 75 checkNotNull(Messages.SegmentStoreStatisticsViewer_StandardDeviation),
c7d7e74e
MAL
76 checkNotNull(Messages.SegmentStoreStatisticsViewer_Count),
77 checkNotNull(Messages.SegmentStoreStatisticsViewer_Total)
ce8319b6
BH
78 };
79
80 /**
81 * Constructor
82 *
83 * @param parent
84 * the parent composite
85 */
3507ca88 86 public AbstractSegmentStoreStatisticsViewer(Composite parent) {
ce8319b6 87 super(parent, false);
53f46dc0 88 setLabelProvider(new SegmentStoreStatisticsLabelProvider());
a1e4b7e8
BH
89 fTablePopupMenuManager = new MenuManager();
90 fTablePopupMenuManager.setRemoveAllWhenShown(true);
91 fTablePopupMenuManager.addMenuListener(manager -> {
92 TreeViewer viewer = getTreeViewer();
93 ISelection selection = viewer.getSelection();
94 if (selection instanceof IStructuredSelection) {
95 IStructuredSelection sel = (IStructuredSelection) selection;
96 if (manager != null) {
97 appendToTablePopupMenu(manager, sel);
98 }
99 }
100 });
101 Menu tablePopup = fTablePopupMenuManager.createContextMenu(getTreeViewer().getTree());
102 getTreeViewer().getTree().setMenu(tablePopup);
ce8319b6
BH
103 }
104
53f46dc0
BH
105 /** Provides label for the Segment Store tree viewer cells */
106 protected static class SegmentStoreStatisticsLabelProvider extends TreeLabelProvider {
ce8319b6
BH
107
108 @Override
109 public String getColumnText(@Nullable Object element, int columnIndex) {
a0acb044 110 String value = ""; //$NON-NLS-1$
ce8319b6
BH
111 if (element instanceof HiddenTreeViewerEntry) {
112 if (columnIndex == 0) {
113 value = ((HiddenTreeViewerEntry) element).getName();
114 }
53f46dc0
BH
115 } else if (element instanceof SegmentStoreStatisticsEntry) {
116 SegmentStoreStatisticsEntry entry = (SegmentStoreStatisticsEntry) element;
1198ba6e 117 if (columnIndex == 0) {
0e4f957e 118 return String.valueOf(entry.getName());
1198ba6e
BH
119 }
120 if (entry.getEntry().getNbSegments() > 0) {
121 if (columnIndex == 1) {
122 value = toFormattedString(entry.getEntry().getMin());
ce8319b6 123 } else if (columnIndex == 2) {
1198ba6e 124 value = String.valueOf(toFormattedString(entry.getEntry().getMax()));
ce8319b6 125 } else if (columnIndex == 3) {
1198ba6e 126 value = String.valueOf(toFormattedString(entry.getEntry().getAverage()));
a0acb044
MK
127 } else if (columnIndex == 4) {
128 value = String.valueOf(toFormattedString(entry.getEntry().getStdDev()));
8f6e9344
JCK
129 } else if (columnIndex == 5) {
130 value = String.valueOf(entry.getEntry().getNbSegments());
c7d7e74e
MAL
131 } else if (columnIndex == 6) {
132 value = String.valueOf(toFormattedString(entry.getEntry().getTotal()));
ce8319b6
BH
133 }
134 }
135 }
136 return checkNotNull(value);
137 }
138 }
139
140 /**
141 * Creates the statistics analysis module
142 *
143 * @return the statistics analysis module
144 */
a0acb044
MK
145 @Nullable
146 protected abstract TmfAbstractAnalysisModule createStatisticsAnalysiModule();
ce8319b6
BH
147
148 /**
149 * Gets the statistics analysis module
a0acb044 150 *
ce8319b6
BH
151 * @return the statistics analysis module
152 */
a0acb044
MK
153 @Nullable
154 public TmfAbstractAnalysisModule getStatisticsAnalysisModule() {
ce8319b6
BH
155 return fModule;
156 }
157
158 @Override
159 protected ITmfTreeColumnDataProvider getColumnDataProvider() {
160 return new ITmfTreeColumnDataProvider() {
161
162 @Override
4c4e2816 163 public List<@Nullable TmfTreeColumnData> getColumnData() {
ce8319b6 164 /* All columns are sortable */
4c4e2816 165 List<@Nullable TmfTreeColumnData> columns = new ArrayList<>();
ce8319b6 166 TmfTreeColumnData column = new TmfTreeColumnData(COLUMN_NAMES[0]);
a0acb044 167 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
168 column.setComparator(new ViewerComparator() {
169 @Override
170 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
171 if ((e1 == null) || (e2 == null)) {
172 return 0;
173 }
174
53f46dc0
BH
175 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
176 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
177
178 return n1.getName().compareTo(n2.getName());
179
180 }
181 });
182 columns.add(column);
183 column = new TmfTreeColumnData(COLUMN_NAMES[1]);
a0acb044 184 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
185 column.setComparator(new ViewerComparator() {
186 @Override
187 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
188 if ((e1 == null) || (e2 == null)) {
189 return 0;
190 }
191
53f46dc0
BH
192 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
193 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
194
195 return Long.compare(n1.getEntry().getMin(), n2.getEntry().getMin());
196
197 }
198 });
199 columns.add(column);
200 column = new TmfTreeColumnData(COLUMN_NAMES[2]);
a0acb044 201 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
202 column.setComparator(new ViewerComparator() {
203 @Override
204 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
205 if ((e1 == null) || (e2 == null)) {
206 return 0;
207 }
208
53f46dc0
BH
209 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
210 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
211
212 return Long.compare(n1.getEntry().getMax(), n2.getEntry().getMax());
213
214 }
215 });
216 columns.add(column);
217 column = new TmfTreeColumnData(COLUMN_NAMES[3]);
a0acb044 218 column.setAlignment(SWT.RIGHT);
ce8319b6
BH
219 column.setComparator(new ViewerComparator() {
220 @Override
221 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
222 if ((e1 == null) || (e2 == null)) {
223 return 0;
224 }
225
53f46dc0
BH
226 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
227 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6
BH
228
229 return Double.compare(n1.getEntry().getAverage(), n2.getEntry().getAverage());
230
231 }
232 });
233 columns.add(column);
a0acb044
MK
234 column = new TmfTreeColumnData(COLUMN_NAMES[4]);
235 column.setAlignment(SWT.RIGHT);
236 column.setComparator(new ViewerComparator() {
237 @Override
238 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
239 if ((e1 == null) || (e2 == null)) {
240 return 0;
241 }
242
243 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
244 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
ce8319b6 245
a0acb044
MK
246 return Double.compare(n1.getEntry().getStdDev(), n2.getEntry().getStdDev());
247
248 }
249 });
250 columns.add(column);
8f6e9344
JCK
251 column = new TmfTreeColumnData(COLUMN_NAMES[5]);
252 column.setAlignment(SWT.RIGHT);
253 column.setComparator(new ViewerComparator() {
254 @Override
255 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
256 if ((e1 == null) || (e2 == null)) {
257 return 0;
258 }
259
260 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
261 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
262
263 return Long.compare(n1.getEntry().getNbSegments(), n2.getEntry().getNbSegments());
264
265 }
266 });
267 columns.add(column);
c7d7e74e
MAL
268 column = new TmfTreeColumnData(COLUMN_NAMES[6]);
269 column.setAlignment(SWT.RIGHT);
270 column.setComparator(new ViewerComparator() {
271 @Override
272 public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) {
273 if ((e1 == null) || (e2 == null)) {
274 return 0;
275 }
276
277 SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1;
278 SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2;
279
280 return Double.compare(n1.getEntry().getTotal(), n2.getEntry().getTotal());
281
282 }
283 });
284 columns.add(column);
a0acb044
MK
285 column = new TmfTreeColumnData(""); //$NON-NLS-1$
286 columns.add(column);
ce8319b6
BH
287 return columns;
288 }
289
290 };
291 }
292
ce8319b6
BH
293 @Override
294 public void initializeDataSource() {
0b9fc6da
JCK
295 ITmfTrace trace = getTrace();
296 if (trace != null) {
297 TmfAbstractAnalysisModule module = createStatisticsAnalysiModule();
298 if (module == null) {
299 return;
300 }
301 try {
302 module.setTrace(trace);
303 module.schedule();
304 fModule = module;
305 } catch (TmfAnalysisException e) {
306 Activator.getDefault().logError("Error initializing statistics analysis module", e); //$NON-NLS-1$
307 }
ce8319b6
BH
308 }
309 }
310
a1e4b7e8
BH
311 /**
312 * Method to add commands to the context sensitive menu.
dda7186d 313 *
a1e4b7e8 314 * @param manager
dda7186d 315 * the menu manager
a1e4b7e8 316 * @param sel
dda7186d 317 * the current selection
a1e4b7e8
BH
318 */
319 protected void appendToTablePopupMenu(IMenuManager manager, IStructuredSelection sel) {
dda7186d 320 Object element = sel.getFirstElement();
a1e4b7e8
BH
321 if ((element instanceof SegmentStoreStatisticsEntry) && !(element instanceof HiddenTreeViewerEntry)) {
322 final SegmentStoreStatisticsEntry segment = (SegmentStoreStatisticsEntry) element;
323 IAction gotoStartTime = new Action(Messages.SegmentStoreStatisticsViewer_GotoMinAction) {
324 @Override
325 public void run() {
326 long start = segment.getEntry().getMinSegment().getStart();
327 long end = segment.getEntry().getMinSegment().getEnd();
328 broadcast(new TmfSelectionRangeUpdatedSignal(AbstractSegmentStoreStatisticsViewer.this, TmfTimestamp.fromNanos(start), TmfTimestamp.fromNanos(end)));
b405ad64 329 updateContent(start, end, true);
a1e4b7e8
BH
330 }
331 };
332
333 IAction gotoEndTime = new Action(Messages.SegmentStoreStatisticsViewer_GotoMaxAction) {
334 @Override
335 public void run() {
336 long start = segment.getEntry().getMaxSegment().getStart();
337 long end = segment.getEntry().getMaxSegment().getEnd();
338 broadcast(new TmfSelectionRangeUpdatedSignal(AbstractSegmentStoreStatisticsViewer.this, TmfTimestamp.fromNanos(start), TmfTimestamp.fromNanos(end)));
b405ad64 339 updateContent(start, end, true);
a1e4b7e8
BH
340 }
341 };
342
343 manager.add(gotoStartTime);
344 manager.add(gotoEndTime);
345 }
346 }
347
ce8319b6
BH
348 /**
349 * Formats a double value string
350 *
351 * @param value
352 * a value to format
353 * @return formatted value
354 */
355 protected static String toFormattedString(double value) {
a0acb044
MK
356 // The cast to long is needed because the formatter cannot truncate the
357 // number.
0e4f957e 358 String percentageString = String.format("%s", FORMATTER.format(value)); //$NON-NLS-1$
ce8319b6
BH
359 return percentageString;
360 }
361
362 /**
363 * Class for defining an entry in the statistics tree.
364 */
53f46dc0 365 protected class SegmentStoreStatisticsEntry extends TmfTreeViewerEntry {
3507ca88 366
5b901f94 367 private final SegmentStoreStatistics fEntry;
ce8319b6
BH
368
369 /**
370 * Constructor
371 *
372 * @param name
373 * name of entry
374 *
375 * @param entry
53f46dc0 376 * segment store statistics object
ce8319b6 377 */
53f46dc0 378 public SegmentStoreStatisticsEntry(String name, SegmentStoreStatistics entry) {
ce8319b6
BH
379 super(name);
380 fEntry = entry;
381 }
382
383 /**
384 * Gets the statistics object
385 *
386 * @return statistics object
387 */
53f46dc0 388 public SegmentStoreStatistics getEntry() {
0e4f957e 389 return fEntry;
ce8319b6
BH
390 }
391
392 }
393
dda7186d
MK
394 @Override
395 protected @Nullable ITmfTreeViewerEntry updateElements(long start, long end, boolean isSelection) {
dda7186d
MK
396
397 TmfAbstractAnalysisModule analysisModule = getStatisticsAnalysisModule();
398
399 if (getTrace() == null || !(analysisModule instanceof AbstractSegmentStatisticsAnalysis)) {
400 return null;
401 }
402
403 AbstractSegmentStatisticsAnalysis module = (AbstractSegmentStatisticsAnalysis) analysisModule;
404
405 module.waitForCompletion();
406
407 TmfTreeViewerEntry root = new TmfTreeViewerEntry(""); //$NON-NLS-1$
b405ad64 408 List<ITmfTreeViewerEntry> entryList = root.getChildren();
dda7186d 409
b405ad64
JCK
410 if (isSelection) {
411 setStats(start, end, entryList, module, true, new NullProgressMonitor());
412 }
413 setStats(start, end, entryList, module, false, new NullProgressMonitor());
414 return root;
415 }
416
417 private void setStats(long start, long end, List<ITmfTreeViewerEntry> entryList, AbstractSegmentStatisticsAnalysis module, boolean isSelection, IProgressMonitor monitor) {
418 String label = isSelection ? getSelectionLabel() : getTotalLabel();
419 final SegmentStoreStatistics entry = isSelection ? module.getTotalStatsForRange(start, end, monitor) : module.getTotalStats();
420 if (entry != null) {
dda7186d 421
b405ad64
JCK
422 if (entry.getNbSegments() == 0) {
423 return;
424 }
425 TmfTreeViewerEntry child = new SegmentStoreStatisticsEntry(checkNotNull(label), entry);
426 entryList.add(child);
dda7186d 427
b405ad64
JCK
428 final Map<@NonNull String, @NonNull SegmentStoreStatistics> perTypeStats = isSelection? module.getPerSegmentTypeStatsForRange(start, end, monitor) : module.getPerSegmentTypeStats();
429 if (perTypeStats != null) {
430 for (Entry<@NonNull String, @NonNull SegmentStoreStatistics> statsEntry : perTypeStats.entrySet()) {
431 child.addChild(new SegmentStoreStatisticsEntry(statsEntry.getKey(), statsEntry.getValue()));
dda7186d
MK
432 }
433 }
434 }
b405ad64
JCK
435 }
436
437 @Override
438 @TmfSignalHandler
439 public void windowRangeUpdated(@Nullable TmfWindowRangeUpdatedSignal signal) {
440 // Do nothing. We do not want to update the view and lose the selection
441 // if the window range is updated with current selection outside of this
442 // new range.
dda7186d
MK
443 }
444
445 /**
446 * Get the type label
447 *
448 * @return the label
f625ccd4 449 * @since 1.2
dda7186d
MK
450 */
451 protected String getTypeLabel() {
452 return checkNotNull(Messages.AbstractSegmentStoreStatisticsViewer_types);
453 }
454
455 /**
456 * Get the total column label
457 *
458 * @return the totals column label
f625ccd4 459 * @since 1.2
dda7186d
MK
460 */
461 protected String getTotalLabel() {
462 return checkNotNull(Messages.AbstractSegmentStoreStatisticsViewer_total);
463 }
464
b405ad64
JCK
465 /**
466 * Get the selection column label
467 *
468 * @return The selection column label
469 * @since 1.2
470 */
471 protected String getSelectionLabel() {
472 return checkNotNull(Messages.AbstractSegmentStoreStatisticsViewer_selection);
473 }
474
ce8319b6
BH
475 /**
476 * Class to define a level in the tree that doesn't have any values.
477 */
53f46dc0 478 protected class HiddenTreeViewerEntry extends SegmentStoreStatisticsEntry {
ce8319b6
BH
479 /**
480 * Constructor
481 *
482 * @param name
483 * the name of the level
484 */
485 public HiddenTreeViewerEntry(String name) {
53f46dc0 486 super(name, new SegmentStoreStatistics());
ce8319b6
BH
487 }
488 }
489
490}
This page took 0.060884 seconds and 5 git commands to generate.