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 MK |
54 | checkNotNull(Messages.SegmentStoreStatistics_AverageLabel), |
55 | checkNotNull(Messages.SegmentStoreStatisticsViewer_StandardDeviation) | |
ce8319b6 BH |
56 | }; |
57 | ||
58 | /** | |
59 | * Constructor | |
60 | * | |
61 | * @param parent | |
62 | * the parent composite | |
63 | */ | |
3507ca88 | 64 | public AbstractSegmentStoreStatisticsViewer(Composite parent) { |
ce8319b6 | 65 | super(parent, false); |
53f46dc0 | 66 | setLabelProvider(new SegmentStoreStatisticsLabelProvider()); |
ce8319b6 BH |
67 | } |
68 | ||
53f46dc0 BH |
69 | /** Provides label for the Segment Store tree viewer cells */ |
70 | protected static class SegmentStoreStatisticsLabelProvider extends TreeLabelProvider { | |
ce8319b6 BH |
71 | |
72 | @Override | |
73 | public String getColumnText(@Nullable Object element, int columnIndex) { | |
a0acb044 | 74 | String value = ""; //$NON-NLS-1$ |
ce8319b6 BH |
75 | if (element instanceof HiddenTreeViewerEntry) { |
76 | if (columnIndex == 0) { | |
77 | value = ((HiddenTreeViewerEntry) element).getName(); | |
78 | } | |
53f46dc0 BH |
79 | } else if (element instanceof SegmentStoreStatisticsEntry) { |
80 | SegmentStoreStatisticsEntry entry = (SegmentStoreStatisticsEntry) element; | |
1198ba6e | 81 | if (columnIndex == 0) { |
0e4f957e | 82 | return String.valueOf(entry.getName()); |
1198ba6e BH |
83 | } |
84 | if (entry.getEntry().getNbSegments() > 0) { | |
85 | if (columnIndex == 1) { | |
86 | value = toFormattedString(entry.getEntry().getMin()); | |
ce8319b6 | 87 | } else if (columnIndex == 2) { |
1198ba6e | 88 | value = String.valueOf(toFormattedString(entry.getEntry().getMax())); |
ce8319b6 | 89 | } else if (columnIndex == 3) { |
1198ba6e | 90 | value = String.valueOf(toFormattedString(entry.getEntry().getAverage())); |
a0acb044 MK |
91 | } else if (columnIndex == 4) { |
92 | value = String.valueOf(toFormattedString(entry.getEntry().getStdDev())); | |
ce8319b6 BH |
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 | */ | |
a0acb044 MK |
105 | @Nullable |
106 | protected abstract TmfAbstractAnalysisModule createStatisticsAnalysiModule(); | |
ce8319b6 BH |
107 | |
108 | /** | |
109 | * Gets the statistics analysis module | |
a0acb044 | 110 | * |
ce8319b6 BH |
111 | * @return the statistics analysis module |
112 | */ | |
a0acb044 MK |
113 | @Nullable |
114 | public TmfAbstractAnalysisModule getStatisticsAnalysisModule() { | |
ce8319b6 BH |
115 | return fModule; |
116 | } | |
117 | ||
118 | @Override | |
119 | protected ITmfTreeColumnDataProvider getColumnDataProvider() { | |
120 | return new ITmfTreeColumnDataProvider() { | |
121 | ||
122 | @Override | |
4c4e2816 | 123 | public List<@Nullable TmfTreeColumnData> getColumnData() { |
ce8319b6 | 124 | /* All columns are sortable */ |
4c4e2816 | 125 | List<@Nullable TmfTreeColumnData> columns = new ArrayList<>(); |
ce8319b6 | 126 | TmfTreeColumnData column = new TmfTreeColumnData(COLUMN_NAMES[0]); |
a0acb044 | 127 | column.setAlignment(SWT.RIGHT); |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
135 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
136 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
137 | |
138 | return n1.getName().compareTo(n2.getName()); | |
139 | ||
140 | } | |
141 | }); | |
142 | columns.add(column); | |
143 | column = new TmfTreeColumnData(COLUMN_NAMES[1]); | |
a0acb044 | 144 | column.setAlignment(SWT.RIGHT); |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
152 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
153 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
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]); | |
a0acb044 | 161 | column.setAlignment(SWT.RIGHT); |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
169 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
170 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
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]); | |
a0acb044 | 178 | column.setAlignment(SWT.RIGHT); |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
186 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
187 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
188 | |
189 | return Double.compare(n1.getEntry().getAverage(), n2.getEntry().getAverage()); | |
190 | ||
191 | } | |
192 | }); | |
193 | columns.add(column); | |
a0acb044 MK |
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; | |
ce8319b6 | 205 | |
a0acb044 MK |
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); | |
ce8319b6 BH |
213 | return columns; |
214 | } | |
215 | ||
216 | }; | |
217 | } | |
218 | ||
ce8319b6 BH |
219 | @Override |
220 | public void initializeDataSource() { | |
0b9fc6da JCK |
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 | } | |
ce8319b6 BH |
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) { | |
a0acb044 MK |
245 | // The cast to long is needed because the formatter cannot truncate the |
246 | // number. | |
0e4f957e | 247 | String percentageString = String.format("%s", FORMATTER.format(value)); //$NON-NLS-1$ |
ce8319b6 BH |
248 | return percentageString; |
249 | } | |
250 | ||
251 | /** | |
252 | * Class for defining an entry in the statistics tree. | |
253 | */ | |
53f46dc0 | 254 | protected class SegmentStoreStatisticsEntry extends TmfTreeViewerEntry { |
3507ca88 | 255 | |
5b901f94 | 256 | private final SegmentStoreStatistics fEntry; |
ce8319b6 BH |
257 | |
258 | /** | |
259 | * Constructor | |
260 | * | |
261 | * @param name | |
262 | * name of entry | |
263 | * | |
264 | * @param entry | |
53f46dc0 | 265 | * segment store statistics object |
ce8319b6 | 266 | */ |
53f46dc0 | 267 | public SegmentStoreStatisticsEntry(String name, SegmentStoreStatistics entry) { |
ce8319b6 BH |
268 | super(name); |
269 | fEntry = entry; | |
270 | } | |
271 | ||
272 | /** | |
273 | * Gets the statistics object | |
274 | * | |
275 | * @return statistics object | |
276 | */ | |
53f46dc0 | 277 | public SegmentStoreStatistics getEntry() { |
0e4f957e | 278 | return fEntry; |
ce8319b6 BH |
279 | } |
280 | ||
281 | } | |
282 | ||
283 | /** | |
284 | * Class to define a level in the tree that doesn't have any values. | |
285 | */ | |
53f46dc0 | 286 | protected class HiddenTreeViewerEntry extends SegmentStoreStatisticsEntry { |
ce8319b6 BH |
287 | /** |
288 | * Constructor | |
289 | * | |
290 | * @param name | |
291 | * the name of the level | |
292 | */ | |
293 | public HiddenTreeViewerEntry(String name) { | |
53f46dc0 | 294 | super(name, new SegmentStoreStatistics()); |
ce8319b6 BH |
295 | } |
296 | } | |
297 | ||
298 | } |