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