This repository was archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGrafanaConverterHelper.java
More file actions
366 lines (293 loc) · 10 KB
/
Copy pathGrafanaConverterHelper.java
File metadata and controls
366 lines (293 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package com.wavefront.labs.convert.converter.grafana;
import com.wavefront.labs.convert.ExpressionBuilder;
import com.wavefront.labs.convert.converter.grafana.model.*;
import com.wavefront.rest.models.Chart;
import com.wavefront.rest.models.Chart.SummarizationEnum;
import com.wavefront.rest.models.ChartSettings;
import com.wavefront.rest.models.ChartSettings.*;
import com.wavefront.rest.models.ChartSourceQuery;
import java.util.ArrayList;
import java.util.List;
public class GrafanaConverterHelper {
private ExpressionBuilder expressionBuilder;
public GrafanaConverterHelper(ExpressionBuilder expressionBuilder) {
this.expressionBuilder = expressionBuilder;
}
public Chart buildChart(GrafanaPanel panel) {
switch (panel.getType()) {
case "graph":
return fromGraph(panel);
case "singlestat":
return fromSinglestat(panel);
case "table":
return fromTable(panel);
case "text":
return fromText(panel);
default:
return fromDefaultPanel(panel);
}
}
private Chart fromGraph(GrafanaPanel panel) {
Chart chart = new Chart();
chart.setName(panel.getTitle());
chart.setDescription(panel.getDescription());
chart.setSummarization(SummarizationEnum.MEAN);
ChartSettings chartSettings = new ChartSettings();
if (!panel.isLines()) {
chartSettings.setType(TypeEnum.SCATTERPLOT);
} else if (panel.isStack()) {
chartSettings.setType(TypeEnum.STACKED_AREA);
if (panel.isPercentage()) {
chartSettings.setStackType(StackTypeEnum.EXPAND);
}
} else {
chartSettings.setType(TypeEnum.LINE);
}
// Queries
chart.setSources(fromTargets(panel.getTargets()));
// Y-axes
if (panel.getYaxes() != null && panel.getYaxes().size() > 0) {
chart.setBase(panel.getYaxes().get(0).getLogBase());
GrafanaPanelYAxis y0 = panel.getYaxes().get(0);
AxisUnit y0AxisUnit = fromFormat(y0.getFormat());
chart.setUnits(y0AxisUnit.getUnit());
chartSettings.setY0UnitAutoscaling(y0AxisUnit.isDynamic());
chartSettings.setY0ScaleSIBy1024(y0AxisUnit.isBinary());
if (y0.getMin() != null && !y0.getMin().equals("")) {
chartSettings.setYmin(Double.valueOf(y0.getMin()));
}
if (y0.getMax() != null && !y0.getMax().equals("")) {
chartSettings.setYmax(Double.valueOf(y0.getMax()));
}
if (panel.getYaxes().size() > 1) {
GrafanaPanelYAxis y1 = panel.getYaxes().get(1);
AxisUnit y1AxisUnit = fromFormat(y1.getFormat());
chartSettings.setY1Units(y1AxisUnit.getUnit());
chartSettings.setY1UnitAutoscaling(y1AxisUnit.isDynamic());
chartSettings.setY1ScaleSIBy1024(y1AxisUnit.isBinary());
if (y1.getMin() != null && !y1.getMin().equals("")) {
chartSettings.setYmin(Double.valueOf(y1.getMin()));
}
if (y1.getMax() != null && !y1.getMax().equals("")) {
chartSettings.setYmax(Double.valueOf(y1.getMax()));
}
}
}
// Legend
GrafanaPanelLegend legend = panel.getLegend();
if (legend != null && legend.isShow()) {
if (legend.isCurrent()) {
chartSettings.addFixedLegendDisplayStatsItem("CURRENT");
}
if (legend.isAvg()) {
chartSettings.addFixedLegendDisplayStatsItem("MEAN");
}
if (legend.isMin()) {
chartSettings.addFixedLegendDisplayStatsItem("MIN");
}
if (legend.isMax()) {
chartSettings.addFixedLegendDisplayStatsItem("MAX");
}
if (legend.isTotal()) {
chartSettings.addFixedLegendDisplayStatsItem("SUM");
}
if (legend.isValues()) {
chartSettings.addFixedLegendDisplayStatsItem("COUNT");
}
if (chartSettings.getFixedLegendDisplayStats() != null && !chartSettings.getFixedLegendDisplayStats().isEmpty()) {
chartSettings.setFixedLegendEnabled(true);
if (legend.isRightSide()) {
chartSettings.setFixedLegendPosition(FixedLegendPositionEnum.RIGHT);
} else {
chartSettings.setFixedLegendPosition(FixedLegendPositionEnum.BOTTOM);
}
}
}
chart.setChartSettings(chartSettings);
return chart;
}
private Chart fromSinglestat(GrafanaPanel panel) {
Chart chart = new Chart();
chart.setName(panel.getTitle());
chart.setDescription(panel.getDescription());
chart.setSummarization(SummarizationEnum.MEAN);
ChartSettings chartSettings = new ChartSettings();
chartSettings.setType(TypeEnum.SPARKLINE);
// Queries
chart.setSources(fromTargets(panel.getTargets()));
// Format
AxisUnit axisUnit = fromFormat(panel.getFormat());
chart.setUnits(axisUnit.getUnit());
chartSettings.setY0UnitAutoscaling(axisUnit.isDynamic());
chartSettings.setY0ScaleSIBy1024(axisUnit.isBinary());
// Sparkline
if (panel.getSparkline() != null) {
GrafanaPanelSparkline sparkline = panel.getSparkline();
if (sparkline.isShow()) {
chartSettings.setSparklineFillColor(sparkline.getFillColor());
chartSettings.setSparklineLineColor(sparkline.getFillColor());
if (sparkline.isFull()) {
chartSettings.setSparklineSize(SparklineSizeEnum.BACKGROUND);
} else {
chartSettings.setSparklineSize(SparklineSizeEnum.BOTTOM);
}
} else {
chartSettings.setSparklineSize(SparklineSizeEnum.NONE);
}
} else {
chartSettings.setSparklineSize(SparklineSizeEnum.NONE);
}
// Color Map Colors
ArrayList<String> chartColors = new ArrayList();
if (panel.getColors() instanceof ArrayList) {
ArrayList<String> colors = (ArrayList<String>) panel.getColors();
chartColors.addAll(colors);
}
chartSettings.setSparklineValueColorMapColors(chartColors);
// Color Map Values
ArrayList<Float> values = new ArrayList();
if (panel.getThresholds() instanceof String) {
String[] valueStrs = ((String) panel.getThresholds()).split(",");
for (String value : valueStrs) {
try {
values.add(Float.valueOf(value));
} catch (NumberFormatException e) {
values.add(1f);
}
}
} else {
values.add(1f);
}
chartSettings.setSparklineValueColorMapValuesV2(values);
// Color applies to
if (panel.isColorBackground()) {
chartSettings.setSparklineValueColorMapApplyTo(SparklineValueColorMapApplyToEnum.BACKGROUND);
} else {
chartSettings.setSparklineValueColorMapApplyTo(SparklineValueColorMapApplyToEnum.TEXT);
}
// Decimal precision
if (panel.getDecimals() != null) {
chartSettings.setSparklineDecimalPrecision(panel.getDecimals());
}
// Fontsize
chartSettings.setSparklineDisplayFontSize(fromValueFontSize(panel.getValueFontSize()));
// Postfix / Prefix
chartSettings.setSparklineDisplayPostfix(panel.getPostfix());
chartSettings.setSparklineDisplayPrefix(panel.getPrefix());
chart.setChartSettings(chartSettings);
return chart;
}
private Chart fromTable(GrafanaPanel panel) {
Chart chart = new Chart();
chart.setName(panel.getTitle());
chart.setDescription(panel.getDescription());
ChartSettings chartSettings = new ChartSettings();
chartSettings.setType(TypeEnum.TABLE);
// Queries
chart.setSources(fromTargets(panel.getTargets()));
chart.setChartSettings(chartSettings);
return chart;
}
private Chart fromText(GrafanaPanel panel) {
Chart chart = fromDefaultPanel(panel);
chart.getChartSettings().setPlainMarkdownContent(panel.getContent());
return chart;
}
private Chart fromDefaultPanel(GrafanaPanel panel) {
Chart chart = new Chart();
chart.setName(panel.getTitle());
chart.setDescription(panel.getDescription());
ChartSettings chartSettings = new ChartSettings();
chartSettings.setType(TypeEnum.MARKDOWN_WIDGET);
chartSettings.setPlainMarkdownContent(panel.getType());
chart.setChartSettings(chartSettings);
return chart;
}
private List<ChartSourceQuery> fromTargets(List<GrafanaPanelTarget> targets) {
List<ChartSourceQuery> chartSourceQueries = new ArrayList();
if (targets != null) {
for (GrafanaPanelTarget target : targets) {
ChartSourceQuery chartSourceQuery = new ChartSourceQuery();
chartSourceQuery.setDisabled(target.isHide());
chartSourceQuery.setName(target.getRefId());
String query;
if (target.getExpr() != null && !target.getExpr().equals("")) {
query = target.getExpr();
} else if (target.getTargetFull() != null && !target.getTargetFull().equals("")) {
query = target.getTargetFull();
} else if (target.getTarget() != null && !target.getTarget().equals("")){
query = target.getTarget();
} else {
continue;
}
chartSourceQuery.setQuery(expressionBuilder.buildExpression(query));
chartSourceQueries.add(chartSourceQuery);
}
}
return chartSourceQueries;
}
private AxisUnit fromFormat(String format) {
if (format == null || format.equals("") || format.equals("none") || format.equals("short")) {
return new AxisUnit("", false, false);
}
format = format.toLowerCase();
if (format.equals("percent")) {
return new AxisUnit("%", false, false);
}
if (format.startsWith("dtduration")) {
return new AxisUnit(format.substring("dtduration".length()), true, false);
}
if (format.startsWith("dec")) {
format = format.substring("dec".length());
}
if (format.endsWith("bytes")) {
return new AxisUnit(format.substring(0, format.length() - 4).toUpperCase(), true, false);
}
if (format.endsWith("bits")) {
format = format.substring(0, format.length() - 3).toUpperCase();
if (format.length() > 1) {
format = format.charAt(0) + "i" + format.charAt(1);
}
return new AxisUnit(format, true, true);
}
return new AxisUnit(format, true, false);
}
private String fromValueFontSize(String valueFontSize) {
if (valueFontSize == null) {
return "100";
}
int value = Integer.parseInt(valueFontSize.replace("%", ""));
if (value >= 200) {
return "200";
} else if (value >= 175) {
return "175";
} else if (value >= 150) {
return "150";
} else if (value >= 125) {
return "125";
} else if (value >= 100) {
return "100";
} else {
return "75";
}
}
private class AxisUnit {
private String unit;
private boolean dynamic;
private boolean binary;
public AxisUnit(String unit, boolean dynamic, boolean binary) {
this.unit = unit;
this.dynamic = dynamic;
this.binary = binary;
}
public String getUnit() {
return unit;
}
public boolean isDynamic() {
return dynamic;
}
public boolean isBinary() {
return binary;
}
}
}