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