| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015, 2016 Ericsson |
| 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 | *******************************************************************************/ |
| 12 | package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.statistics; |
| 13 | |
| 14 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 15 | |
| 16 | import java.text.Format; |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | import java.util.Map; |
| 20 | import java.util.Map.Entry; |
| 21 | |
| 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; |
| 50 | |
| 51 | /** |
| 52 | * An abstract tree viewer implementation for displaying segment store |
| 53 | * statistics |
| 54 | * |
| 55 | * @author Bernd Hufmann |
| 56 | * |
| 57 | */ |
| 58 | public abstract class AbstractSegmentStoreStatisticsViewer extends AbstractTmfTreeViewer { |
| 59 | |
| 60 | private static final Format FORMATTER = new SubSecondTimeWithUnitFormat(); |
| 61 | |
| 62 | @Nullable |
| 63 | private TmfAbstractAnalysisModule fModule; |
| 64 | private MenuManager fTablePopupMenuManager; |
| 65 | |
| 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) |
| 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * Constructor |
| 78 | * |
| 79 | * @param parent |
| 80 | * the parent composite |
| 81 | */ |
| 82 | public AbstractSegmentStoreStatisticsViewer(Composite parent) { |
| 83 | super(parent, false); |
| 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); |
| 94 | } |
| 95 | } |
| 96 | }); |
| 97 | Menu tablePopup = fTablePopupMenuManager.createContextMenu(getTreeViewer().getTree()); |
| 98 | getTreeViewer().getTree().setMenu(tablePopup); |
| 99 | } |
| 100 | |
| 101 | /** Provides label for the Segment Store tree viewer cells */ |
| 102 | protected static class SegmentStoreStatisticsLabelProvider extends TreeLabelProvider { |
| 103 | |
| 104 | @Override |
| 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(); |
| 110 | } |
| 111 | } else if (element instanceof SegmentStoreStatisticsEntry) { |
| 112 | SegmentStoreStatisticsEntry entry = (SegmentStoreStatisticsEntry) element; |
| 113 | if (columnIndex == 0) { |
| 114 | return String.valueOf(entry.getName()); |
| 115 | } |
| 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())); |
| 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 | */ |
| 141 | @Nullable |
| 142 | protected abstract TmfAbstractAnalysisModule createStatisticsAnalysiModule(); |
| 143 | |
| 144 | /** |
| 145 | * Gets the statistics analysis module |
| 146 | * |
| 147 | * @return the statistics analysis module |
| 148 | */ |
| 149 | @Nullable |
| 150 | public TmfAbstractAnalysisModule getStatisticsAnalysisModule() { |
| 151 | return fModule; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected ITmfTreeColumnDataProvider getColumnDataProvider() { |
| 156 | return new ITmfTreeColumnDataProvider() { |
| 157 | |
| 158 | @Override |
| 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() { |
| 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 | |
| 171 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
| 172 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; |
| 173 | |
| 174 | return n1.getName().compareTo(n2.getName()); |
| 175 | |
| 176 | } |
| 177 | }); |
| 178 | columns.add(column); |
| 179 | column = new TmfTreeColumnData(COLUMN_NAMES[1]); |
| 180 | column.setAlignment(SWT.RIGHT); |
| 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 | |
| 188 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
| 189 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; |
| 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]); |
| 197 | column.setAlignment(SWT.RIGHT); |
| 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 | |
| 205 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
| 206 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; |
| 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]); |
| 214 | column.setAlignment(SWT.RIGHT); |
| 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 | |
| 222 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
| 223 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; |
| 224 | |
| 225 | return Double.compare(n1.getEntry().getAverage(), n2.getEntry().getAverage()); |
| 226 | |
| 227 | } |
| 228 | }); |
| 229 | columns.add(column); |
| 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; |
| 241 | |
| 242 | return Double.compare(n1.getEntry().getStdDev(), n2.getEntry().getStdDev()); |
| 243 | |
| 244 | } |
| 245 | }); |
| 246 | columns.add(column); |
| 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); |
| 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); |
| 281 | column = new TmfTreeColumnData(""); //$NON-NLS-1$ |
| 282 | columns.add(column); |
| 283 | return columns; |
| 284 | } |
| 285 | |
| 286 | }; |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public void initializeDataSource() { |
| 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 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Method to add commands to the context sensitive menu. |
| 309 | * |
| 310 | * @param manager |
| 311 | * the menu manager |
| 312 | * @param sel |
| 313 | * the current selection |
| 314 | */ |
| 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) { |
| 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 | |
| 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) { |
| 350 | // The cast to long is needed because the formatter cannot truncate the |
| 351 | // number. |
| 352 | String percentageString = String.format("%s", FORMATTER.format(value)); //$NON-NLS-1$ |
| 353 | return percentageString; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Class for defining an entry in the statistics tree. |
| 358 | */ |
| 359 | protected class SegmentStoreStatisticsEntry extends TmfTreeViewerEntry { |
| 360 | |
| 361 | private final SegmentStoreStatistics fEntry; |
| 362 | |
| 363 | /** |
| 364 | * Constructor |
| 365 | * |
| 366 | * @param name |
| 367 | * name of entry |
| 368 | * |
| 369 | * @param entry |
| 370 | * segment store statistics object |
| 371 | */ |
| 372 | public SegmentStoreStatisticsEntry(String name, SegmentStoreStatistics entry) { |
| 373 | super(name); |
| 374 | fEntry = entry; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Gets the statistics object |
| 379 | * |
| 380 | * @return statistics object |
| 381 | */ |
| 382 | public SegmentStoreStatistics getEntry() { |
| 383 | return fEntry; |
| 384 | } |
| 385 | |
| 386 | } |
| 387 | |
| 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.2 |
| 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.2 |
| 440 | */ |
| 441 | protected String getTotalLabel() { |
| 442 | return checkNotNull(Messages.AbstractSegmentStoreStatisticsViewer_total); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Class to define a level in the tree that doesn't have any values. |
| 447 | */ |
| 448 | protected class HiddenTreeViewerEntry extends SegmentStoreStatisticsEntry { |
| 449 | /** |
| 450 | * Constructor |
| 451 | * |
| 452 | * @param name |
| 453 | * the name of the level |
| 454 | */ |
| 455 | public HiddenTreeViewerEntry(String name) { |
| 456 | super(name, new SegmentStoreStatistics()); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | } |