| 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 | |
| 20 | import org.eclipse.jdt.annotation.Nullable; |
| 21 | import org.eclipse.jface.viewers.Viewer; |
| 22 | import org.eclipse.jface.viewers.ViewerComparator; |
| 23 | import org.eclipse.swt.SWT; |
| 24 | import org.eclipse.swt.widgets.Composite; |
| 25 | import org.eclipse.tracecompass.analysis.timing.core.segmentstore.statistics.SegmentStoreStatistics; |
| 26 | import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.SubSecondTimeWithUnitFormat; |
| 27 | import org.eclipse.tracecompass.internal.analysis.timing.ui.Activator; |
| 28 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule; |
| 29 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; |
| 30 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 31 | import org.eclipse.tracecompass.tmf.ui.viewers.tree.AbstractTmfTreeViewer; |
| 32 | import org.eclipse.tracecompass.tmf.ui.viewers.tree.ITmfTreeColumnDataProvider; |
| 33 | import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeColumnData; |
| 34 | import org.eclipse.tracecompass.tmf.ui.viewers.tree.TmfTreeViewerEntry; |
| 35 | |
| 36 | /** |
| 37 | * An abstract tree viewer implementation for displaying segment store |
| 38 | * statistics |
| 39 | * |
| 40 | * @author Bernd Hufmann |
| 41 | * |
| 42 | */ |
| 43 | public abstract class AbstractSegmentStoreStatisticsViewer extends AbstractTmfTreeViewer { |
| 44 | |
| 45 | private static final Format FORMATTER = new SubSecondTimeWithUnitFormat(); |
| 46 | |
| 47 | @Nullable |
| 48 | private TmfAbstractAnalysisModule fModule; |
| 49 | |
| 50 | private static final String[] COLUMN_NAMES = new String[] { |
| 51 | checkNotNull(Messages.SegmentStoreStatistics_LevelLabel), |
| 52 | checkNotNull(Messages.SegmentStoreStatistics_Statistics_MinLabel), |
| 53 | checkNotNull(Messages.SegmentStoreStatistics_MaxLabel), |
| 54 | checkNotNull(Messages.SegmentStoreStatistics_AverageLabel), |
| 55 | checkNotNull(Messages.SegmentStoreStatisticsViewer_StandardDeviation), |
| 56 | checkNotNull(Messages.SegmentStoreStatisticsViewer_Count) |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * Constructor |
| 61 | * |
| 62 | * @param parent |
| 63 | * the parent composite |
| 64 | */ |
| 65 | public AbstractSegmentStoreStatisticsViewer(Composite parent) { |
| 66 | super(parent, false); |
| 67 | setLabelProvider(new SegmentStoreStatisticsLabelProvider()); |
| 68 | } |
| 69 | |
| 70 | /** Provides label for the Segment Store tree viewer cells */ |
| 71 | protected static class SegmentStoreStatisticsLabelProvider extends TreeLabelProvider { |
| 72 | |
| 73 | @Override |
| 74 | public String getColumnText(@Nullable Object element, int columnIndex) { |
| 75 | String value = ""; //$NON-NLS-1$ |
| 76 | if (element instanceof HiddenTreeViewerEntry) { |
| 77 | if (columnIndex == 0) { |
| 78 | value = ((HiddenTreeViewerEntry) element).getName(); |
| 79 | } |
| 80 | } else if (element instanceof SegmentStoreStatisticsEntry) { |
| 81 | SegmentStoreStatisticsEntry entry = (SegmentStoreStatisticsEntry) element; |
| 82 | if (columnIndex == 0) { |
| 83 | return String.valueOf(entry.getName()); |
| 84 | } |
| 85 | if (entry.getEntry().getNbSegments() > 0) { |
| 86 | if (columnIndex == 1) { |
| 87 | value = toFormattedString(entry.getEntry().getMin()); |
| 88 | } else if (columnIndex == 2) { |
| 89 | value = String.valueOf(toFormattedString(entry.getEntry().getMax())); |
| 90 | } else if (columnIndex == 3) { |
| 91 | value = String.valueOf(toFormattedString(entry.getEntry().getAverage())); |
| 92 | } else if (columnIndex == 4) { |
| 93 | value = String.valueOf(toFormattedString(entry.getEntry().getStdDev())); |
| 94 | } else if (columnIndex == 5) { |
| 95 | value = String.valueOf(entry.getEntry().getNbSegments()); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | return checkNotNull(value); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Creates the statistics analysis module |
| 105 | * |
| 106 | * @return the statistics analysis module |
| 107 | */ |
| 108 | @Nullable |
| 109 | protected abstract TmfAbstractAnalysisModule createStatisticsAnalysiModule(); |
| 110 | |
| 111 | /** |
| 112 | * Gets the statistics analysis module |
| 113 | * |
| 114 | * @return the statistics analysis module |
| 115 | */ |
| 116 | @Nullable |
| 117 | public TmfAbstractAnalysisModule getStatisticsAnalysisModule() { |
| 118 | return fModule; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | protected ITmfTreeColumnDataProvider getColumnDataProvider() { |
| 123 | return new ITmfTreeColumnDataProvider() { |
| 124 | |
| 125 | @Override |
| 126 | public List<@Nullable TmfTreeColumnData> getColumnData() { |
| 127 | /* All columns are sortable */ |
| 128 | List<@Nullable TmfTreeColumnData> columns = new ArrayList<>(); |
| 129 | TmfTreeColumnData column = new TmfTreeColumnData(COLUMN_NAMES[0]); |
| 130 | column.setAlignment(SWT.RIGHT); |
| 131 | column.setComparator(new ViewerComparator() { |
| 132 | @Override |
| 133 | public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) { |
| 134 | if ((e1 == null) || (e2 == null)) { |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
| 139 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; |
| 140 | |
| 141 | return n1.getName().compareTo(n2.getName()); |
| 142 | |
| 143 | } |
| 144 | }); |
| 145 | columns.add(column); |
| 146 | column = new TmfTreeColumnData(COLUMN_NAMES[1]); |
| 147 | column.setAlignment(SWT.RIGHT); |
| 148 | column.setComparator(new ViewerComparator() { |
| 149 | @Override |
| 150 | public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) { |
| 151 | if ((e1 == null) || (e2 == null)) { |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
| 156 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; |
| 157 | |
| 158 | return Long.compare(n1.getEntry().getMin(), n2.getEntry().getMin()); |
| 159 | |
| 160 | } |
| 161 | }); |
| 162 | columns.add(column); |
| 163 | column = new TmfTreeColumnData(COLUMN_NAMES[2]); |
| 164 | column.setAlignment(SWT.RIGHT); |
| 165 | column.setComparator(new ViewerComparator() { |
| 166 | @Override |
| 167 | public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) { |
| 168 | if ((e1 == null) || (e2 == null)) { |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
| 173 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; |
| 174 | |
| 175 | return Long.compare(n1.getEntry().getMax(), n2.getEntry().getMax()); |
| 176 | |
| 177 | } |
| 178 | }); |
| 179 | columns.add(column); |
| 180 | column = new TmfTreeColumnData(COLUMN_NAMES[3]); |
| 181 | column.setAlignment(SWT.RIGHT); |
| 182 | column.setComparator(new ViewerComparator() { |
| 183 | @Override |
| 184 | public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) { |
| 185 | if ((e1 == null) || (e2 == null)) { |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
| 190 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; |
| 191 | |
| 192 | return Double.compare(n1.getEntry().getAverage(), n2.getEntry().getAverage()); |
| 193 | |
| 194 | } |
| 195 | }); |
| 196 | columns.add(column); |
| 197 | column = new TmfTreeColumnData(COLUMN_NAMES[4]); |
| 198 | column.setAlignment(SWT.RIGHT); |
| 199 | column.setComparator(new ViewerComparator() { |
| 200 | @Override |
| 201 | public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) { |
| 202 | if ((e1 == null) || (e2 == null)) { |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
| 207 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; |
| 208 | |
| 209 | return Double.compare(n1.getEntry().getStdDev(), n2.getEntry().getStdDev()); |
| 210 | |
| 211 | } |
| 212 | }); |
| 213 | columns.add(column); |
| 214 | column = new TmfTreeColumnData(COLUMN_NAMES[5]); |
| 215 | column.setAlignment(SWT.RIGHT); |
| 216 | column.setComparator(new ViewerComparator() { |
| 217 | @Override |
| 218 | public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) { |
| 219 | if ((e1 == null) || (e2 == null)) { |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
| 224 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; |
| 225 | |
| 226 | return Long.compare(n1.getEntry().getNbSegments(), n2.getEntry().getNbSegments()); |
| 227 | |
| 228 | } |
| 229 | }); |
| 230 | columns.add(column); |
| 231 | column = new TmfTreeColumnData(""); //$NON-NLS-1$ |
| 232 | columns.add(column); |
| 233 | return columns; |
| 234 | } |
| 235 | |
| 236 | }; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void initializeDataSource() { |
| 241 | ITmfTrace trace = getTrace(); |
| 242 | if (trace != null) { |
| 243 | TmfAbstractAnalysisModule module = createStatisticsAnalysiModule(); |
| 244 | if (module == null) { |
| 245 | return; |
| 246 | } |
| 247 | try { |
| 248 | module.setTrace(trace); |
| 249 | module.schedule(); |
| 250 | fModule = module; |
| 251 | } catch (TmfAnalysisException e) { |
| 252 | Activator.getDefault().logError("Error initializing statistics analysis module", e); //$NON-NLS-1$ |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Formats a double value string |
| 259 | * |
| 260 | * @param value |
| 261 | * a value to format |
| 262 | * @return formatted value |
| 263 | */ |
| 264 | protected static String toFormattedString(double value) { |
| 265 | // The cast to long is needed because the formatter cannot truncate the |
| 266 | // number. |
| 267 | String percentageString = String.format("%s", FORMATTER.format(value)); //$NON-NLS-1$ |
| 268 | return percentageString; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Class for defining an entry in the statistics tree. |
| 273 | */ |
| 274 | protected class SegmentStoreStatisticsEntry extends TmfTreeViewerEntry { |
| 275 | |
| 276 | private final SegmentStoreStatistics fEntry; |
| 277 | |
| 278 | /** |
| 279 | * Constructor |
| 280 | * |
| 281 | * @param name |
| 282 | * name of entry |
| 283 | * |
| 284 | * @param entry |
| 285 | * segment store statistics object |
| 286 | */ |
| 287 | public SegmentStoreStatisticsEntry(String name, SegmentStoreStatistics entry) { |
| 288 | super(name); |
| 289 | fEntry = entry; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Gets the statistics object |
| 294 | * |
| 295 | * @return statistics object |
| 296 | */ |
| 297 | public SegmentStoreStatistics getEntry() { |
| 298 | return fEntry; |
| 299 | } |
| 300 | |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Class to define a level in the tree that doesn't have any values. |
| 305 | */ |
| 306 | protected class HiddenTreeViewerEntry extends SegmentStoreStatisticsEntry { |
| 307 | /** |
| 308 | * Constructor |
| 309 | * |
| 310 | * @param name |
| 311 | * the name of the level |
| 312 | */ |
| 313 | public HiddenTreeViewerEntry(String name) { |
| 314 | super(name, new SegmentStoreStatistics()); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | } |