Commit | Line | Data |
---|---|---|
ce8319b6 BH |
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; | |
53f46dc0 | 25 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.latency.statistics.SegmentStoreStatistics; |
ce8319b6 BH |
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 | /** | |
53f46dc0 | 36 | * An abstract tree viewer implementation for displaying segment store statistics |
ce8319b6 BH |
37 | * |
38 | * @author Bernd Hufmann | |
39 | * | |
40 | */ | |
3507ca88 | 41 | public abstract class AbstractSegmentStoreStatisticsViewer extends AbstractTmfTreeViewer { |
ce8319b6 BH |
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[] { | |
53f46dc0 BH |
48 | checkNotNull(Messages.SegmentStoreStatistics_LevelLabel), |
49 | checkNotNull(Messages.SegmentStoreStatistics_Statistics_MinLabel), | |
50 | checkNotNull(Messages.SegmentStoreStatistics_MaxLabel), | |
51 | checkNotNull(Messages.SegmentStoreStatistics_AverageLabel) | |
ce8319b6 BH |
52 | }; |
53 | ||
54 | /** | |
55 | * Constructor | |
56 | * | |
57 | * @param parent | |
58 | * the parent composite | |
59 | */ | |
3507ca88 | 60 | public AbstractSegmentStoreStatisticsViewer(Composite parent) { |
ce8319b6 | 61 | super(parent, false); |
53f46dc0 | 62 | setLabelProvider(new SegmentStoreStatisticsLabelProvider()); |
ce8319b6 BH |
63 | } |
64 | ||
53f46dc0 BH |
65 | /** Provides label for the Segment Store tree viewer cells */ |
66 | protected static class SegmentStoreStatisticsLabelProvider extends TreeLabelProvider { | |
ce8319b6 BH |
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 | } | |
53f46dc0 BH |
75 | } else if (element instanceof SegmentStoreStatisticsEntry) { |
76 | SegmentStoreStatisticsEntry entry = (SegmentStoreStatisticsEntry) element; | |
1198ba6e BH |
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()); | |
ce8319b6 | 83 | } else if (columnIndex == 2) { |
1198ba6e | 84 | value = String.valueOf(toFormattedString(entry.getEntry().getMax())); |
ce8319b6 | 85 | } else if (columnIndex == 3) { |
1198ba6e | 86 | value = String.valueOf(toFormattedString(entry.getEntry().getAverage())); |
ce8319b6 BH |
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 | |
4c4e2816 | 114 | public List<@Nullable TmfTreeColumnData> getColumnData() { |
ce8319b6 | 115 | /* All columns are sortable */ |
4c4e2816 | 116 | List<@Nullable TmfTreeColumnData> columns = new ArrayList<>(); |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
125 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
126 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
141 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
142 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
157 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
158 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
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 | ||
53f46dc0 BH |
173 | SegmentStoreStatisticsEntry n1 = (SegmentStoreStatisticsEntry) e1; |
174 | SegmentStoreStatisticsEntry n2 = (SegmentStoreStatisticsEntry) e2; | |
ce8319b6 BH |
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 | */ | |
53f46dc0 | 222 | protected class SegmentStoreStatisticsEntry extends TmfTreeViewerEntry { |
3507ca88 | 223 | |
53f46dc0 | 224 | private SegmentStoreStatistics fEntry; |
ce8319b6 BH |
225 | |
226 | /** | |
227 | * Constructor | |
228 | * | |
229 | * @param name | |
230 | * name of entry | |
231 | * | |
232 | * @param entry | |
53f46dc0 | 233 | * segment store statistics object |
ce8319b6 | 234 | */ |
53f46dc0 | 235 | public SegmentStoreStatisticsEntry(String name, SegmentStoreStatistics entry) { |
ce8319b6 BH |
236 | super(name); |
237 | fEntry = entry; | |
238 | } | |
239 | ||
240 | /** | |
241 | * Gets the statistics object | |
242 | * | |
243 | * @return statistics object | |
244 | */ | |
53f46dc0 | 245 | public SegmentStoreStatistics getEntry() { |
3507ca88 | 246 | return checkNotNull(fEntry); |
ce8319b6 BH |
247 | } |
248 | ||
249 | } | |
250 | ||
251 | /** | |
252 | * Class to define a level in the tree that doesn't have any values. | |
253 | */ | |
53f46dc0 | 254 | protected class HiddenTreeViewerEntry extends SegmentStoreStatisticsEntry { |
ce8319b6 BH |
255 | /** |
256 | * Constructor | |
257 | * | |
258 | * @param name | |
259 | * the name of the level | |
260 | */ | |
261 | public HiddenTreeViewerEntry(String name) { | |
53f46dc0 | 262 | super(name, new SegmentStoreStatistics()); |
ce8319b6 BH |
263 | } |
264 | } | |
265 | ||
266 | } |