| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015 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.os.linux.ui.views.latency.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.os.linux.core.latency.statistics.LatencyStatistics; |
| 26 | import org.eclipse.tracecompass.internal.analysis.os.linux.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 latency statistics |
| 37 | * |
| 38 | * @author Bernd Hufmann |
| 39 | * |
| 40 | */ |
| 41 | public abstract class AbstractLatencyStatisticsViewer 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.LatencyStatistics_LevelLabel), |
| 49 | checkNotNull(Messages.LatencyStatistics_MinLabel), |
| 50 | checkNotNull(Messages.LatencyStatistics_MaxLabel), |
| 51 | checkNotNull(Messages.LatencyStatistics_AverageLabel) |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * Constructor |
| 56 | * |
| 57 | * @param parent |
| 58 | * the parent composite |
| 59 | */ |
| 60 | public AbstractLatencyStatisticsViewer(Composite parent) { |
| 61 | super(parent, false); |
| 62 | setLabelProvider(new LatencyLabelProvider()); |
| 63 | } |
| 64 | |
| 65 | /** Provides label for the Latency tree viewer cells */ |
| 66 | protected static class LatencyLabelProvider 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 { |
| 76 | LatencyTreeViewerEntry obj = (LatencyTreeViewerEntry) element; |
| 77 | if (obj != null) { |
| 78 | if (columnIndex == 0) { |
| 79 | value = String.valueOf(obj.getName()); |
| 80 | } else if (columnIndex == 1) { |
| 81 | value = toFormattedString(obj.getEntry().getMin()); |
| 82 | } else if (columnIndex == 2) { |
| 83 | value = String.valueOf(toFormattedString(obj.getEntry().getMax())); |
| 84 | } else if (columnIndex == 3) { |
| 85 | value = String.valueOf(toFormattedString(obj.getEntry().getAverage())); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return checkNotNull(value); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Creates the statistics analysis module |
| 95 | * |
| 96 | * @return the statistics analysis module |
| 97 | */ |
| 98 | @Nullable protected abstract TmfAbstractAnalysisModule createStatisticsAnalysiModule(); |
| 99 | |
| 100 | /** |
| 101 | * Gets the statistics analysis module |
| 102 | * @return the statistics analysis module |
| 103 | */ |
| 104 | @Nullable public TmfAbstractAnalysisModule getStatisticsAnalysisModule() { |
| 105 | return fModule; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected ITmfTreeColumnDataProvider getColumnDataProvider() { |
| 110 | return new ITmfTreeColumnDataProvider() { |
| 111 | |
| 112 | @Override |
| 113 | public List<TmfTreeColumnData> getColumnData() { |
| 114 | /* All columns are sortable */ |
| 115 | List<TmfTreeColumnData> columns = new ArrayList<>(); |
| 116 | TmfTreeColumnData column = new TmfTreeColumnData(COLUMN_NAMES[0]); |
| 117 | column.setComparator(new ViewerComparator() { |
| 118 | @Override |
| 119 | public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) { |
| 120 | if ((e1 == null) || (e2 == null)) { |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | LatencyTreeViewerEntry n1 = (LatencyTreeViewerEntry) e1; |
| 125 | LatencyTreeViewerEntry n2 = (LatencyTreeViewerEntry) e2; |
| 126 | |
| 127 | return n1.getName().compareTo(n2.getName()); |
| 128 | |
| 129 | } |
| 130 | }); |
| 131 | columns.add(column); |
| 132 | column = new TmfTreeColumnData(COLUMN_NAMES[1]); |
| 133 | column.setComparator(new ViewerComparator() { |
| 134 | @Override |
| 135 | public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) { |
| 136 | if ((e1 == null) || (e2 == null)) { |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | LatencyTreeViewerEntry n1 = (LatencyTreeViewerEntry) e1; |
| 141 | LatencyTreeViewerEntry n2 = (LatencyTreeViewerEntry) e2; |
| 142 | |
| 143 | return Long.compare(n1.getEntry().getMin(), n2.getEntry().getMin()); |
| 144 | |
| 145 | } |
| 146 | }); |
| 147 | columns.add(column); |
| 148 | column = new TmfTreeColumnData(COLUMN_NAMES[2]); |
| 149 | column.setComparator(new ViewerComparator() { |
| 150 | @Override |
| 151 | public int compare(@Nullable Viewer viewer, @Nullable Object e1, @Nullable Object e2) { |
| 152 | if ((e1 == null) || (e2 == null)) { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | LatencyTreeViewerEntry n1 = (LatencyTreeViewerEntry) e1; |
| 157 | LatencyTreeViewerEntry n2 = (LatencyTreeViewerEntry) e2; |
| 158 | |
| 159 | return Long.compare(n1.getEntry().getMax(), n2.getEntry().getMax()); |
| 160 | |
| 161 | } |
| 162 | }); |
| 163 | columns.add(column); |
| 164 | column = new TmfTreeColumnData(COLUMN_NAMES[3]); |
| 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 | LatencyTreeViewerEntry n1 = (LatencyTreeViewerEntry) e1; |
| 173 | LatencyTreeViewerEntry n2 = (LatencyTreeViewerEntry) e2; |
| 174 | |
| 175 | return Double.compare(n1.getEntry().getAverage(), n2.getEntry().getAverage()); |
| 176 | |
| 177 | } |
| 178 | }); |
| 179 | columns.add(column); |
| 180 | |
| 181 | return columns; |
| 182 | } |
| 183 | |
| 184 | }; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | @Override |
| 189 | public void initializeDataSource() { |
| 190 | /* Should not be called while trace is still null */ |
| 191 | ITmfTrace trace = checkNotNull(getTrace()); |
| 192 | TmfAbstractAnalysisModule module = createStatisticsAnalysiModule(); |
| 193 | if (module == null) { |
| 194 | return; |
| 195 | } |
| 196 | try { |
| 197 | module.setTrace(trace); |
| 198 | module.schedule(); |
| 199 | fModule = module; |
| 200 | } catch (TmfAnalysisException e) { |
| 201 | Activator.getDefault().logError("Error initializing statistics analysis module", e); //$NON-NLS-1$ |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Formats a double value string |
| 207 | * |
| 208 | * @param value |
| 209 | * a value to format |
| 210 | * @return formatted value |
| 211 | */ |
| 212 | protected static String toFormattedString(double value) { |
| 213 | // The cast to long is needed because the formatter cannot truncate the number. |
| 214 | String percentageString = checkNotNull(String.format("%s", FORMATTER.format(value))); //$NON-NLS-1$ |
| 215 | return percentageString; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Class for defining an entry in the statistics tree. |
| 220 | */ |
| 221 | protected class LatencyTreeViewerEntry extends TmfTreeViewerEntry { |
| 222 | private LatencyStatistics fEntry; |
| 223 | |
| 224 | /** |
| 225 | * Constructor |
| 226 | * |
| 227 | * @param name |
| 228 | * name of entry |
| 229 | * |
| 230 | * @param entry |
| 231 | * latency statistics object |
| 232 | */ |
| 233 | public LatencyTreeViewerEntry(String name, LatencyStatistics entry) { |
| 234 | super(name); |
| 235 | fEntry = entry; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Gets the statistics object |
| 240 | * |
| 241 | * @return statistics object |
| 242 | */ |
| 243 | public LatencyStatistics getEntry() { |
| 244 | return fEntry; |
| 245 | } |
| 246 | |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Class to define a level in the tree that doesn't have any values. |
| 251 | */ |
| 252 | protected class HiddenTreeViewerEntry extends LatencyTreeViewerEntry { |
| 253 | /** |
| 254 | * Constructor |
| 255 | * |
| 256 | * @param name |
| 257 | * the name of the level |
| 258 | */ |
| 259 | public HiddenTreeViewerEntry(String name) { |
| 260 | super(name, new LatencyStatistics()); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | } |