Commit | Line | Data |
---|---|---|
ce8319b6 | 1 | /******************************************************************************* |
658401c8 | 2 | * Copyright (c) 2015, 2016 Ericsson |
ce8319b6 BH |
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 | *******************************************************************************/ | |
5b901f94 | 12 | package org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.statistics; |
ce8319b6 BH |
13 | |
14 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
15 | ||
91deb4e1 | 16 | import java.text.Format; |
ce8319b6 BH |
17 | import java.util.ArrayList; |
18 | import java.util.List; | |
ce8319b6 BH |
19 | |
20 | import org.eclipse.jdt.annotation.Nullable; | |
21 | import org.eclipse.jface.viewers.Viewer; | |
22 | import org.eclipse.jface.viewers.ViewerComparator; | |
a0acb044 | 23 | import org.eclipse.swt.SWT; |
ce8319b6 | 24 | import org.eclipse.swt.widgets.Composite; |
5b901f94 | 25 | import org.eclipse.tracecompass.analysis.timing.core.segmentstore.statistics.SegmentStoreStatistics; |
91deb4e1 | 26 | import org.eclipse.tracecompass.analysis.timing.ui.views.segmentstore.SubSecondTimeWithUnitFormat; |
658401c8 | 27 | import org.eclipse.tracecompass.internal.analysis.timing.ui.Activator; |
ce8319b6 BH |
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 | /** | |
a0acb044 MK |
37 | * An abstract tree viewer implementation for displaying segment store |
38 | * statistics | |
ce8319b6 BH |
39 | * |
40 | * @author Bernd Hufmann | |
41 | * | |
42 | */ | |
3507ca88 | 43 | public abstract class AbstractSegmentStoreStatisticsViewer extends AbstractTmfTreeViewer { |
ce8319b6 | 44 | |
91deb4e1 | 45 | private static final Format FORMATTER = new SubSecondTimeWithUnitFormat(); |
ce8319b6 | 46 | |
a0acb044 MK |
47 | @Nullable |
48 | private TmfAbstractAnalysisModule fModule; | |
ce8319b6 BH |
49 | |
50 | private static final String[] COLUMN_NAMES = new String[] { | |
53f46dc0 BH |
51 | checkNotNull(Messages.SegmentStoreStatistics_LevelLabel), |
52 | checkNotNull(Messages.SegmentStoreStatistics_Statistics_MinLabel), | |
53 | checkNotNull(Messages.SegmentStoreStatistics_MaxLabel), | |
a0acb044 | 54 | checkNotNull(Messages.SegmentStoreStatistics_AverageLabel), |
8f6e9344 JCK |
55 | checkNotNull(Messages.SegmentStoreStatisticsViewer_StandardDeviation), |
56 | checkNotNull(Messages.SegmentStoreStatisticsViewer_Count) | |
ce8319b6 BH |
57 | }; |
58 | ||
59 | /** | |
60 | * Constructor | |
61 | * | |
62 | * @param parent | |
63 | * the parent composite | |
64 | */ | |
3507ca88 | 65 | public AbstractSegmentStoreStatisticsViewer(Composite parent) { |
ce8319b6 | 66 | super(parent, false); |
53f46dc0 | 67 | setLabelProvider(new SegmentStoreStatisticsLabelProvider()); |
ce8319b6 BH |
68 | } |
69 | ||
53f46dc0 BH |
70 | /** Provides label for the Segment Store tree viewer cells */ |
71 | protected static class SegmentStoreStatisticsLabelProvider extends TreeLabelProvider { | |
ce8319b6 BH |
72 | |
73 | @Override | |
74 | public String getColumnText(@Nullable Object element, int columnIndex) { | |
a0acb044 | 75 | String value = ""; //$NON-NLS-1$ |
ce8319b6 BH |
76 | if (element instanceof HiddenTreeViewerEntry) { |
77 | if (columnIndex == 0) { | |
78 | value = ((HiddenTreeViewerEntry) element).getName(); | |
79 | } | |
53f46dc0 BH |
80 | } else if (element instanceof SegmentStoreStatisticsEntry) { |
81 | SegmentStoreStatisticsEntry entry = (SegmentStoreStatisticsEntry) element; | |
1198ba6e | 82 | if (columnIndex == 0) { |
0e4f957e | 83 | return String.valueOf(entry.getName()); |
1198ba6e BH |
84 | } |
85 | if (entry.getEntry().getNbSegments() > 0) { | |
86 | if (columnIndex == 1) { | |
87 | value = toFormattedString(entry.getEntry().getMin()); | |
ce8319b6 | 88 | } else if (columnIndex == 2) { |
1198ba6e | 89 | value = String.valueOf(toFormattedString(entry.getEntry().getMax())); |
ce8319b6 | 90 | } else if (columnIndex == 3) { |
1198ba6e | 91 | value = String.valueOf(toFormattedString(entry.getEntry().getAverage())); |
a0acb044 MK |
92 | } else if (columnIndex == 4) { |
93 | value = String.valueOf(toFormattedString(entry.getEntry().getStdDev())); | |
8f6e9344 JCK |
94 | } else if (columnIndex == 5) { |
95 | value = String.valueOf(entry.getEntry().getNbSegments()); | |
ce8319b6 BH |
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 | */ | |
a0acb044 MK |
108 | @Nullable |
109 | protected abstract TmfAbstractAnalysisModule createStatisticsAnalysiModule(); | |
ce8319b6 BH |
110 | |
111 | /** | |
112 | * Gets the statistics analysis module | |
a0acb044 | 113 | * |
ce8319b6 BH |
114 | * @return the statistics analysis module |
115 | */ | |
a0acb044 MK |
116 | @Nullable |
117 | public TmfAbstractAnalysisModule getStatisticsAnalysisModule() { | |
ce8319b6 BH |
118 | return fModule; |
119 | } | |
120 | ||
121 | @Override | |
122 | protected ITmfTreeColumnDataProvider getColumnDataProvider() { | |
123 | return new ITmfTreeColumnDataProvider() { | |
124 | ||
125 | @Override | |
4c4e2816 | 126 | public List<@Nullable TmfTreeColumnData> getColumnData() { |
ce8319b6 | 127 | /* All columns are sortable */ |
4c4e2816 | 128 | List<@Nullable TmfTreeColumnData> columns = new ArrayList<>(); |
ce8319b6 | 129 | TmfTreeColumnData column = new TmfTreeColumnData(COLUMN_NAMES[0]); |
a0acb044 | 130 | column.setAlignment(SWT.RIGHT); |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
138 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
139 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
140 | |
141 | return n1.getName().compareTo(n2.getName()); | |
142 | ||
143 | } | |
144 | }); | |
145 | columns.add(column); | |
146 | column = new TmfTreeColumnData(COLUMN_NAMES[1]); | |
a0acb044 | 147 | column.setAlignment(SWT.RIGHT); |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
155 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
156 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
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]); | |
a0acb044 | 164 | column.setAlignment(SWT.RIGHT); |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
172 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
173 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
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]); | |
a0acb044 | 181 | column.setAlignment(SWT.RIGHT); |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
189 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
190 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
191 | |
192 | return Double.compare(n1.getEntry().getAverage(), n2.getEntry().getAverage()); | |
193 | ||
194 | } | |
195 | }); | |
196 | columns.add(column); | |
a0acb044 MK |
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; | |
ce8319b6 | 208 | |
a0acb044 MK |
209 | return Double.compare(n1.getEntry().getStdDev(), n2.getEntry().getStdDev()); |
210 | ||
211 | } | |
212 | }); | |
213 | columns.add(column); | |
8f6e9344 JCK |
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); | |
a0acb044 MK |
231 | column = new TmfTreeColumnData(""); //$NON-NLS-1$ |
232 | columns.add(column); | |
ce8319b6 BH |
233 | return columns; |
234 | } | |
235 | ||
236 | }; | |
237 | } | |
238 | ||
ce8319b6 BH |
239 | @Override |
240 | public void initializeDataSource() { | |
0b9fc6da JCK |
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 | } | |
ce8319b6 BH |
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) { | |
a0acb044 MK |
265 | // The cast to long is needed because the formatter cannot truncate the |
266 | // number. | |
0e4f957e | 267 | String percentageString = String.format("%s", FORMATTER.format(value)); //$NON-NLS-1$ |
ce8319b6 BH |
268 | return percentageString; |
269 | } | |
270 | ||
271 | /** | |
272 | * Class for defining an entry in the statistics tree. | |
273 | */ | |
53f46dc0 | 274 | protected class SegmentStoreStatisticsEntry extends TmfTreeViewerEntry { |
3507ca88 | 275 | |
5b901f94 | 276 | private final SegmentStoreStatistics fEntry; |
ce8319b6 BH |
277 | |
278 | /** | |
279 | * Constructor | |
280 | * | |
281 | * @param name | |
282 | * name of entry | |
283 | * | |
284 | * @param entry | |
53f46dc0 | 285 | * segment store statistics object |
ce8319b6 | 286 | */ |
53f46dc0 | 287 | public SegmentStoreStatisticsEntry(String name, SegmentStoreStatistics entry) { |
ce8319b6 BH |
288 | super(name); |
289 | fEntry = entry; | |
290 | } | |
291 | ||
292 | /** | |
293 | * Gets the statistics object | |
294 | * | |
295 | * @return statistics object | |
296 | */ | |
53f46dc0 | 297 | public SegmentStoreStatistics getEntry() { |
0e4f957e | 298 | return fEntry; |
ce8319b6 BH |
299 | } |
300 | ||
301 | } | |
302 | ||
303 | /** | |
304 | * Class to define a level in the tree that doesn't have any values. | |
305 | */ | |
53f46dc0 | 306 | protected class HiddenTreeViewerEntry extends SegmentStoreStatisticsEntry { |
ce8319b6 BH |
307 | /** |
308 | * Constructor | |
309 | * | |
310 | * @param name | |
311 | * the name of the level | |
312 | */ | |
313 | public HiddenTreeViewerEntry(String name) { | |
53f46dc0 | 314 | super(name, new SegmentStoreStatistics()); |
ce8319b6 BH |
315 | } |
316 | } | |
317 | ||
318 | } |