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