@cloudcare/guance-front-tools 1.0.11 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,3 +17,11 @@ $ grafanaCovertToGuance
17
17
  # run task
18
18
  $ grafanaCovertToGuance -d examples/grafana.json -o examples/guance.json
19
19
  ```
20
+
21
+ Module usage:
22
+
23
+ ```ts
24
+ import { covert } from '@cloudcare/guance-front-tools'
25
+
26
+ const guanceDashboard = covert(grafanaDashboard)
27
+ ```
@@ -0,0 +1,4 @@
1
+ import type { DashboardData as GuanceDashboardType } from '../generated/dashboardCharts';
2
+ import type { DashboardData as GrafanaDashboardType, VariableModel, Panel, RowPanel } from './grafana-dashbord';
3
+ export declare function covert(grafanaData: GrafanaDashboardType): GuanceDashboardType;
4
+ export type { GrafanaDashboardType, Panel as GrafanaPanel, RowPanel as GrafanaRowPanel, VariableModel as GrafanaVariableModel };
@@ -0,0 +1,294 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.covert = covert;
4
+ var grafanaPanelTypeToGuanceChartMap = {
5
+ stat: 'singlestat',
6
+ singlestat: 'singlestat',
7
+ barchart: 'bar',
8
+ timeseries: 'sequence',
9
+ graph: 'sequence',
10
+ piechart: 'pie',
11
+ histogram: 'histogram',
12
+ bargauge: 'toplist',
13
+ gauge: 'gauge',
14
+ table: 'table',
15
+ text: 'text',
16
+ heatmap: 'heatmap',
17
+ treemap: 'treemap',
18
+ };
19
+ var GRAFANA_KEYWORKD = ['__interval'];
20
+ var VARIABLE_MAP = {
21
+ query: 'PROMQL_QUERY',
22
+ custom: 'CUSTOM_LIST',
23
+ };
24
+ var VARIABLE_DATASOURCE_MAP = {
25
+ query: 'dataflux',
26
+ custom: 'custom',
27
+ };
28
+ function isSupportedVariableType(type) {
29
+ return Object.prototype.hasOwnProperty.call(VARIABLE_MAP, type);
30
+ }
31
+ function replaceVariableStr(grafanaExpr) {
32
+ return grafanaExpr.replace(/\$\{?([\d_\w]+)\}?/g, function (match, variable) {
33
+ if (GRAFANA_KEYWORKD.indexOf(variable) > -1)
34
+ return match;
35
+ return "#{".concat(variable, "}");
36
+ });
37
+ }
38
+ function sortPanelItemsByRowCol(panels) {
39
+ return panels.slice(0).sort(function (panelA, panelB) {
40
+ var posA = panelA.gridPos;
41
+ var posB = panelB.gridPos;
42
+ if (!posA || !posB)
43
+ return -1;
44
+ if (posA.y === posB.y && posA.x === posB.x && posA.w > posB.w) {
45
+ return -1;
46
+ }
47
+ if (posA.y === posB.y && posA.x === posB.x) {
48
+ return 0;
49
+ }
50
+ if (posA.y > posB.y || (posA.y === posB.y && posA.x > posB.x)) {
51
+ return 1;
52
+ }
53
+ return -1;
54
+ });
55
+ }
56
+ function tenToTweenty(source) {
57
+ if (source === void 0) { source = 1; }
58
+ var numArr = [];
59
+ source--;
60
+ do {
61
+ numArr.push(source % 26);
62
+ source = Math.floor(source / 26);
63
+ } while (source > 0);
64
+ return numArr
65
+ .reverse()
66
+ .map(function (item, index) {
67
+ return String.fromCharCode(item + 97 + (index === numArr.length - 1 ? 0 : -1));
68
+ })
69
+ .join('')
70
+ .toLowerCase();
71
+ }
72
+ function getGridH(h, rowHeight, margin) {
73
+ return Math.round(h * rowHeight + Math.max(0, 2 * (h - 1)) * margin);
74
+ }
75
+ function getGuanceHByGrafanaH(granfanH) {
76
+ return (getGridH(granfanH, 30, 4) + 10) / 20;
77
+ }
78
+ function covertPanelToGuanceChart(grafanaPanel, rowPanel) {
79
+ var gridPos = grafanaPanel.gridPos, title = grafanaPanel.title, type = grafanaPanel.type, targets = grafanaPanel.targets, options = grafanaPanel.options;
80
+ var chartType = grafanaPanelTypeToGuanceChartMap[type];
81
+ var pos = {
82
+ x: gridPos.x,
83
+ w: gridPos.w,
84
+ y: gridPos.y,
85
+ h: gridPos.h,
86
+ };
87
+ if (rowPanel) {
88
+ var rowGridPos = rowPanel.gridPos;
89
+ if (rowGridPos && gridPos && !rowPanel.collapsed) {
90
+ pos = {
91
+ x: gridPos.x,
92
+ w: gridPos.w,
93
+ y: gridPos.y - rowGridPos.y,
94
+ h: gridPos.h,
95
+ };
96
+ }
97
+ }
98
+ var queries = [];
99
+ if (targets && targets.length) {
100
+ var currentIndex_1 = 0;
101
+ targets.forEach(function (_target) {
102
+ var queryStr = _target.expr || _target.query || _target.queryText;
103
+ if (!queryStr)
104
+ return;
105
+ currentIndex_1++;
106
+ var queryItem = {
107
+ datasource: 'dataflux',
108
+ qtype: 'promql',
109
+ type: chartType,
110
+ query: {
111
+ q: replaceVariableStr(queryStr),
112
+ type: 'promql',
113
+ code: tenToTweenty(currentIndex_1),
114
+ promqlCode: currentIndex_1,
115
+ },
116
+ };
117
+ queries.push(queryItem);
118
+ });
119
+ }
120
+ var settings = {};
121
+ if (options) {
122
+ switch (chartType) {
123
+ case 'text':
124
+ queries.push({
125
+ query: {
126
+ content: options.content,
127
+ },
128
+ });
129
+ break;
130
+ case 'toplist':
131
+ settings = {
132
+ showTopSize: true,
133
+ chartType: 'bar',
134
+ };
135
+ break;
136
+ default:
137
+ break;
138
+ }
139
+ }
140
+ return {
141
+ extend: {
142
+ settings: settings,
143
+ },
144
+ group: {
145
+ name: rowPanel ? rowPanel.title : null,
146
+ },
147
+ pos: {
148
+ x: pos.x,
149
+ y: getGuanceHByGrafanaH(pos.y),
150
+ h: getGuanceHByGrafanaH(pos.h),
151
+ w: pos.w,
152
+ },
153
+ name: replaceVariableStr(title || ''),
154
+ queries: queries,
155
+ type: chartType,
156
+ };
157
+ }
158
+ function covertPanelsToCharts(grafanaPanelData, rowPanel) {
159
+ var guanceCharts = [];
160
+ grafanaPanelData.forEach(function (grafanaPanel) {
161
+ if (!grafanaPanel.gridPos)
162
+ return;
163
+ guanceCharts.push(covertPanelToGuanceChart(grafanaPanel, rowPanel));
164
+ });
165
+ return guanceCharts;
166
+ }
167
+ function covert(grafanaData) {
168
+ var _a, _b, _c;
169
+ var covertGuanceResult = {};
170
+ covertGuanceResult.title = grafanaData.title;
171
+ var guanceVars = [];
172
+ (_b = (_a = grafanaData.templating) === null || _a === void 0 ? void 0 : _a.list) === null || _b === void 0 ? void 0 : _b.forEach(function (_variable, index) {
173
+ var current = _variable.current, type = _variable.type, allValue = _variable.allValue;
174
+ var variableType = String(type);
175
+ if (!isSupportedVariableType(variableType))
176
+ return;
177
+ var defaultVal = {
178
+ label: '',
179
+ value: '',
180
+ };
181
+ if (current) {
182
+ var labels = [];
183
+ var values = [];
184
+ if (Array.isArray(current.text)) {
185
+ labels = current.text;
186
+ }
187
+ else if (typeof current.text === 'string') {
188
+ labels = [current.text];
189
+ }
190
+ if (Array.isArray(current.value)) {
191
+ values = current.value;
192
+ }
193
+ else if (typeof current.value === 'string') {
194
+ values = [current.value];
195
+ }
196
+ labels = labels.map(function (label) {
197
+ if (label === 'All') {
198
+ if (allValue === '.*') {
199
+ return '*';
200
+ }
201
+ return 'all values';
202
+ }
203
+ return label;
204
+ });
205
+ values = values.map(function (value) {
206
+ if (value === '$__all') {
207
+ if (allValue === '.*') {
208
+ return '*';
209
+ }
210
+ return '__all__';
211
+ }
212
+ return value;
213
+ });
214
+ defaultVal = {
215
+ label: labels.join(','),
216
+ value: values.join(','),
217
+ };
218
+ }
219
+ var value = _variable.query;
220
+ if (value && typeof value === 'object' && value.query) {
221
+ value = replaceVariableStr(value.query);
222
+ }
223
+ else if (value && typeof value === 'string') {
224
+ value = replaceVariableStr(_variable.query);
225
+ }
226
+ else {
227
+ return;
228
+ }
229
+ var guanceVariableItem = {
230
+ type: VARIABLE_MAP[variableType],
231
+ datasource: VARIABLE_DATASOURCE_MAP[variableType],
232
+ name: _variable.label || _variable.name || '',
233
+ seq: index,
234
+ hide: _variable.hide ? 1 : 0,
235
+ multiple: _variable.multi !== undefined ? _variable.multi : true,
236
+ includeStar: _variable.includeAll !== undefined ? _variable.includeAll : true,
237
+ valueSort: 'desc',
238
+ code: _variable.name,
239
+ definition: {
240
+ value: value,
241
+ defaultVal: defaultVal,
242
+ },
243
+ };
244
+ guanceVars.push(guanceVariableItem);
245
+ });
246
+ var guanceGroups = [];
247
+ var guanceExpand = {};
248
+ var guanceCharts = [];
249
+ var lastRowPanel;
250
+ var lastPanels = [];
251
+ var grafanaCharts = ((_c = grafanaData.panels) === null || _c === void 0 ? void 0 : _c.filter(function (_panel) { return _panel.type === 'row' || grafanaPanelTypeToGuanceChartMap[_panel.type]; })) || [];
252
+ grafanaCharts = sortPanelItemsByRowCol(grafanaCharts);
253
+ grafanaCharts.forEach(function (_panel) {
254
+ var _a;
255
+ if (_panel.type === 'row') {
256
+ var _rowPanel = _panel;
257
+ if (_rowPanel.title) {
258
+ guanceGroups.push({
259
+ name: _rowPanel.title,
260
+ });
261
+ guanceExpand[_rowPanel.title] = !_rowPanel.collapsed;
262
+ }
263
+ if (lastPanels.length) {
264
+ guanceCharts.push.apply(guanceCharts, covertPanelsToCharts(lastPanels, lastRowPanel));
265
+ lastPanels = [];
266
+ lastRowPanel = undefined;
267
+ }
268
+ if (_rowPanel.collapsed) {
269
+ var subPanels = ((_a = _rowPanel.panels) === null || _a === void 0 ? void 0 : _a.filter(function (_childPanel) {
270
+ return _childPanel.type === 'row' || grafanaPanelTypeToGuanceChartMap[_childPanel.type];
271
+ })) || [];
272
+ guanceCharts.push.apply(guanceCharts, covertPanelsToCharts(subPanels, _rowPanel));
273
+ }
274
+ else {
275
+ lastRowPanel = _rowPanel;
276
+ }
277
+ }
278
+ else {
279
+ lastPanels.push(_panel);
280
+ }
281
+ });
282
+ if (lastPanels.length) {
283
+ guanceCharts.push.apply(guanceCharts, covertPanelsToCharts(lastPanels, lastRowPanel));
284
+ }
285
+ covertGuanceResult.dashboardExtend = {
286
+ groupUnfoldStatus: guanceExpand,
287
+ };
288
+ covertGuanceResult.main = {
289
+ vars: guanceVars,
290
+ charts: guanceCharts,
291
+ groups: guanceGroups,
292
+ };
293
+ return covertGuanceResult;
294
+ }