@cloudcare/guance-front-tools 1.0.0
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/.prettierignore +3 -0
- package/.prettierrc +6 -0
- package/LICENSE +21 -0
- package/README.md +31 -0
- package/cli.js +16 -0
- package/lib/cjs/generated/browserSessionReplay.d.ts +703 -0
- package/lib/cjs/generated/browserSessionReplay.js +6 -0
- package/lib/cjs/generated/dashboardCharts.d.ts +1372 -0
- package/lib/cjs/generated/dashboardCharts.js +6 -0
- package/lib/cjs/generated/mobileSessionReplay.d.ts +653 -0
- package/lib/cjs/generated/mobileSessionReplay.js +6 -0
- package/lib/cjs/generated/sessionReplay.d.ts +1190 -0
- package/lib/cjs/generated/sessionReplay.js +6 -0
- package/lib/cjs/src/dashboard-charts.d.ts +1 -0
- package/lib/cjs/src/dashboard-charts.js +17 -0
- package/lib/cjs/src/index.d.ts +2 -0
- package/lib/cjs/src/index.js +18 -0
- package/lib/cjs/src/session-replay-browser.d.ts +58 -0
- package/lib/cjs/src/session-replay-browser.js +69 -0
- package/lib/cjs/src/session-replay-mobile.d.ts +30 -0
- package/lib/cjs/src/session-replay-mobile.js +42 -0
- package/lib/cjs/src/session-replay.d.ts +30 -0
- package/lib/cjs/src/session-replay.js +49 -0
- package/lib/esm/generated/browserSessionReplay.d.ts +703 -0
- package/lib/esm/generated/browserSessionReplay.js +5 -0
- package/lib/esm/generated/dashboardCharts.d.ts +1372 -0
- package/lib/esm/generated/dashboardCharts.js +5 -0
- package/lib/esm/generated/mobileSessionReplay.d.ts +653 -0
- package/lib/esm/generated/mobileSessionReplay.js +5 -0
- package/lib/esm/generated/sessionReplay.d.ts +1190 -0
- package/lib/esm/generated/sessionReplay.js +5 -0
- package/lib/esm/src/dashboard-charts.d.ts +1 -0
- package/lib/esm/src/dashboard-charts.js +1 -0
- package/lib/esm/src/index.d.ts +2 -0
- package/lib/esm/src/index.js +2 -0
- package/lib/esm/src/session-replay-browser.d.ts +58 -0
- package/lib/esm/src/session-replay-browser.js +52 -0
- package/lib/esm/src/session-replay-mobile.d.ts +30 -0
- package/lib/esm/src/session-replay-mobile.js +25 -0
- package/lib/esm/src/session-replay.d.ts +30 -0
- package/lib/esm/src/session-replay.js +24 -0
- package/lib/example/grafana-dashboard.json +2205 -0
- package/lib/example/guance-covert-test.json +629 -0
- package/lib/example/guance-dashboard-1.json +672 -0
- package/lib/example/guance-dashboard.json +2841 -0
- package/lib/scripts/grafana-covert-to-guance.d.ts +1 -0
- package/lib/scripts/grafana-covert-to-guance.js +367 -0
- package/lib/scripts/grafana-covert-to-guance.ts +374 -0
- package/lib/scripts/grafana-dashbord.d.ts +2220 -0
- package/lib/scripts/grafana-dashbord.js +1 -0
- package/lib/scripts/grafana-dashbord.ts +2427 -0
- package/lib/src/dashboard-charts.ts +1 -0
- package/lib/src/index.ts +2 -0
- package/lib/src/session-replay-browser.ts +98 -0
- package/lib/src/session-replay-mobile.ts +54 -0
- package/lib/src/session-replay.ts +61 -0
- package/package.json +45 -0
- package/samples/dashboard/dashboard.json +2842 -0
- package/samples/dashboard/promql-dashboard.json +2175 -0
- package/samples/session-replay/mobile/record/full-snapshot-record.json +51 -0
- package/samples/session-replay/mobile/record/has-focus-record.json +7 -0
- package/samples/session-replay/mobile/record/incremental-snapshot-record.json +74 -0
- package/samples/session-replay/mobile/record/metadata-record.json +8 -0
- package/samples/session-replay/mobile/record/view-end-record.json +4 -0
- package/samples/session-replay/mobile/segment/segment.json +110 -0
- package/schemas/charts/chart-schema.json +73 -0
- package/schemas/charts/common/common-chart-types-schema.json +37 -0
- package/schemas/charts/common/common-vars-item-schema.json +117 -0
- package/schemas/charts/common/common-vars-schema.json +10 -0
- package/schemas/charts/dashboard-schema.json +73 -0
- package/schemas/charts/query/queries-schema.json +10 -0
- package/schemas/charts/query/query-item-schema.json +42 -0
- package/schemas/charts/settings/_common-settings-connect-null-schema.json +7 -0
- package/schemas/charts/settings/_common-settings-gradual-color-schema.json +72 -0
- package/schemas/charts/settings/_common-settings-groupby-schema.json +22 -0
- package/schemas/charts/settings/_common-settings-legend-position-schema.json +14 -0
- package/schemas/charts/settings/_common-settings-legend-values-schema.json +19 -0
- package/schemas/charts/settings/_common-settings-open-stack-schema.json +8 -0
- package/schemas/charts/settings/_common-settings-show-label-schema.json +8 -0
- package/schemas/charts/settings/_common-settings-show-line-schema.json +8 -0
- package/schemas/charts/settings/_common-settings-stack-content-schema.json +12 -0
- package/schemas/charts/settings/_common-settings-stack-type-schema.json +11 -0
- package/schemas/charts/settings/chart-alarm-settings-schema.json +48 -0
- package/schemas/charts/settings/chart-bar-settings-schema.json +69 -0
- package/schemas/charts/settings/chart-chinamap-settings-schema.json +102 -0
- package/schemas/charts/settings/chart-funnel-settings-schema.json +34 -0
- package/schemas/charts/settings/chart-gauge-settings-schema.json +65 -0
- package/schemas/charts/settings/chart-heatmap-settings-schema.json +50 -0
- package/schemas/charts/settings/chart-hexgon-settings-schema.json +90 -0
- package/schemas/charts/settings/chart-histogram-settings-schema.json +47 -0
- package/schemas/charts/settings/chart-iframe-settings-schema.json +24 -0
- package/schemas/charts/settings/chart-log-settings-schema.json +84 -0
- package/schemas/charts/settings/chart-object-settings-schema.json +52 -0
- package/schemas/charts/settings/chart-picture-settings-schema.json +46 -0
- package/schemas/charts/settings/chart-pie-settings-schema.json +90 -0
- package/schemas/charts/settings/chart-sankey-settings-schema.json +34 -0
- package/schemas/charts/settings/chart-scatter-bubble-settings-schema.json +31 -0
- package/schemas/charts/settings/chart-sequence-settings-schema.json +76 -0
- package/schemas/charts/settings/chart-singlestat-settings-schema.json +163 -0
- package/schemas/charts/settings/chart-table-settings-schema.json +184 -0
- package/schemas/charts/settings/chart-text-settings-schema.json +20 -0
- package/schemas/charts/settings/chart-toplist-settings-schema.json +117 -0
- package/schemas/charts/settings/chart-topology-settings-schema.json +71 -0
- package/schemas/charts/settings/chart-treemap-settings-schema.json +66 -0
- package/schemas/charts/settings/chart-worldmap-settings-schema.json +102 -0
- package/schemas/charts/settings/common-settings-schema.json +82 -0
- package/schemas/charts/settings/settings-alias-schema.json +47 -0
- package/schemas/charts/settings/settings-chart-type-schema.json +27 -0
- package/schemas/charts/settings/settings-colors-schema.json +46 -0
- package/schemas/charts/settings/settings-compare-schema.json +58 -0
- package/schemas/charts/settings/settings-fixed-time-schema.json +49 -0
- package/schemas/charts/settings/settings-levels-schema.json +33 -0
- package/schemas/charts/settings/settings-schema.json +79 -0
- package/schemas/charts/settings/settings-time-schema.json +47 -0
- package/schemas/charts/settings/settings-unit-items-schema.json +226 -0
- package/schemas/charts/settings/settings-units-schema.json +60 -0
- package/schemas/dashboard-schema.json +5 -0
- package/schemas/session-replay/browser/attribute-mutation-schema.json +21 -0
- package/schemas/session-replay/browser/attributes-schema.json +10 -0
- package/schemas/session-replay/browser/cdata-node-schema.json +19 -0
- package/schemas/session-replay/browser/creation-reason-schema.json +17 -0
- package/schemas/session-replay/browser/document-fragment-node-schema.json +35 -0
- package/schemas/session-replay/browser/document-node-schema.json +30 -0
- package/schemas/session-replay/browser/document-type-node-schema.json +28 -0
- package/schemas/session-replay/browser/element-node-schema.json +33 -0
- package/schemas/session-replay/browser/frustration-record-schema.json +49 -0
- package/schemas/session-replay/browser/full-snapshot-record-schema.json +26 -0
- package/schemas/session-replay/browser/incremental-data-schema.json +36 -0
- package/schemas/session-replay/browser/incremental-snapshot-record-schema.json +29 -0
- package/schemas/session-replay/browser/input-data-schema.json +27 -0
- package/schemas/session-replay/browser/input-state-schema.json +27 -0
- package/schemas/session-replay/browser/media-interaction-data-schema.json +23 -0
- package/schemas/session-replay/browser/media-interaction-schema.json +20 -0
- package/schemas/session-replay/browser/mouse-interaction-data-schema.json +23 -0
- package/schemas/session-replay/browser/mouse-interaction-schema.json +47 -0
- package/schemas/session-replay/browser/mouse-move-data-schema.json +23 -0
- package/schemas/session-replay/browser/mouse-position-schema.json +26 -0
- package/schemas/session-replay/browser/mutation-data-schema.json +23 -0
- package/schemas/session-replay/browser/mutation-payload-schema.json +42 -0
- package/schemas/session-replay/browser/node-added-mutation-schema.json +41 -0
- package/schemas/session-replay/browser/node-removed-mutation-schema.json +18 -0
- package/schemas/session-replay/browser/node-schema.json +28 -0
- package/schemas/session-replay/browser/record-schema.json +30 -0
- package/schemas/session-replay/browser/scroll-data-schema.json +23 -0
- package/schemas/session-replay/browser/scroll-position-schema.json +22 -0
- package/schemas/session-replay/browser/segment-metadata-schema.json +28 -0
- package/schemas/session-replay/browser/segment-schema.json +25 -0
- package/schemas/session-replay/browser/serialized-node-schema.json +15 -0
- package/schemas/session-replay/browser/serialized-node-with-id-schema.json +21 -0
- package/schemas/session-replay/browser/stylesheet-rule-add-schema.json +28 -0
- package/schemas/session-replay/browser/stylesheet-rule-data-schema.json +23 -0
- package/schemas/session-replay/browser/stylesheet-rule-delete-schema.json +24 -0
- package/schemas/session-replay/browser/stylesheet-rule-schema.json +29 -0
- package/schemas/session-replay/browser/stylesheet-schema.json +30 -0
- package/schemas/session-replay/browser/text-mutation-schema.json +27 -0
- package/schemas/session-replay/browser/text-node-schema.json +23 -0
- package/schemas/session-replay/common/_common-record-schema.json +14 -0
- package/schemas/session-replay/common/_common-segment-metadata-schema.json +30 -0
- package/schemas/session-replay/common/_slot-supported-common-record-schema.json +21 -0
- package/schemas/session-replay/common/focus-record-schema.json +34 -0
- package/schemas/session-replay/common/meta-record-schema.json +42 -0
- package/schemas/session-replay/common/pointer-interaction-data-schema.json +23 -0
- package/schemas/session-replay/common/pointer-interaction-schema.json +34 -0
- package/schemas/session-replay/common/segment-context-schema.json +46 -0
- package/schemas/session-replay/common/view-end-record-schema.json +23 -0
- package/schemas/session-replay/common/viewport-resize-data-schema.json +23 -0
- package/schemas/session-replay/common/viewport-resize-dimension-schema.json +18 -0
- package/schemas/session-replay/common/visual-viewport-record-schema.json +50 -0
- package/schemas/session-replay/full-snapshot-record-schema.json +14 -0
- package/schemas/session-replay/incremental-data-schema.json +14 -0
- package/schemas/session-replay/incremental-snapshot-record-schema.json +14 -0
- package/schemas/session-replay/mobile/_common-shape-wireframe-schema.json +22 -0
- package/schemas/session-replay/mobile/_common-shape-wireframe-update-schema.json +22 -0
- package/schemas/session-replay/mobile/_common-wireframe-schema.json +38 -0
- package/schemas/session-replay/mobile/_common-wireframe-update-schema.json +38 -0
- package/schemas/session-replay/mobile/full-snapshot-record-schema.json +38 -0
- package/schemas/session-replay/mobile/image-wireframe-schema.json +43 -0
- package/schemas/session-replay/mobile/image-wireframe-update-schema.json +43 -0
- package/schemas/session-replay/mobile/incremental-data-schema.json +21 -0
- package/schemas/session-replay/mobile/incremental-snapshot-record-schema.json +26 -0
- package/schemas/session-replay/mobile/mutation-data-schema.json +23 -0
- package/schemas/session-replay/mobile/mutation-payload-schema.json +55 -0
- package/schemas/session-replay/mobile/placeholder-wireframe-schema.json +28 -0
- package/schemas/session-replay/mobile/placeholder-wireframe-update-schema.json +28 -0
- package/schemas/session-replay/mobile/record-schema.json +27 -0
- package/schemas/session-replay/mobile/segment-metadata-schema.json +25 -0
- package/schemas/session-replay/mobile/segment-schema.json +25 -0
- package/schemas/session-replay/mobile/shape-border-schema.json +25 -0
- package/schemas/session-replay/mobile/shape-style-schema.json +29 -0
- package/schemas/session-replay/mobile/shape-wireframe-schema.json +23 -0
- package/schemas/session-replay/mobile/shape-wireframe-update-schema.json +23 -0
- package/schemas/session-replay/mobile/text-position-schema.json +57 -0
- package/schemas/session-replay/mobile/text-style-schema.json +31 -0
- package/schemas/session-replay/mobile/text-wireframe-schema.json +34 -0
- package/schemas/session-replay/mobile/text-wireframe-update-schema.json +34 -0
- package/schemas/session-replay/mobile/touch-data-schema.json +51 -0
- package/schemas/session-replay/mobile/webview-wireframe-schema.json +33 -0
- package/schemas/session-replay/mobile/webview-wireframe-update-schema.json +33 -0
- package/schemas/session-replay/mobile/wireframe-clip-schema.json +29 -0
- package/schemas/session-replay/mobile/wireframe-schema.json +24 -0
- package/schemas/session-replay/mobile/wireframe-update-mutation-schema.json +24 -0
- package/schemas/session-replay/mutation-data-schema.json +14 -0
- package/schemas/session-replay/mutation-payload-schema.json +14 -0
- package/schemas/session-replay/record-schema.json +14 -0
- package/schemas/session-replay/segment-metadata-schema.json +14 -0
- package/schemas/session-replay/segment-schema.json +15 -0
- package/schemas/session-replay-browser-schema.json +5 -0
- package/schemas/session-replay-mobile-schema.json +5 -0
- package/schemas/session-replay-schema.json +33 -0
- package/scripts/generate.mjs +96 -0
- package/scripts/utils.mjs +16 -0
- package/scripts/validate.mjs +144 -0
- package/tsconfig.cjs.json +7 -0
- package/tsconfig.esm.json +7 -0
- package/tsconfig.json +13 -0
- package/tsconfig.script.json +15 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import * as fs from 'fs'
|
|
2
|
+
import * as path from 'path'
|
|
3
|
+
import yargs from 'yargs'
|
|
4
|
+
import type {
|
|
5
|
+
DashboardData as GuanceDashboardType,
|
|
6
|
+
ChartVarsSettings,
|
|
7
|
+
ChartVarsItem,
|
|
8
|
+
ChartData as GuanceChartData,
|
|
9
|
+
ChartType1 as GuanceChartType,
|
|
10
|
+
ChartQueries as GuanceChartQueries,
|
|
11
|
+
ChartQueryItem as GuanceChartQueryItem,
|
|
12
|
+
ChartSettings as GuanceChartSettings,
|
|
13
|
+
ChartTextSettings,
|
|
14
|
+
} from '../generated/dashboardCharts'
|
|
15
|
+
import type { DashboardData as GrafanaDashboardType, VariableModel, Panel, RowPanel } from './grafana-dashbord'
|
|
16
|
+
|
|
17
|
+
// 1. Graph (折线图) - 用于展示时间序列数据的线图或柱状图。
|
|
18
|
+
// 2. Stat (统计) - 用于显示单个数值,如当前值、最小值或最大值。
|
|
19
|
+
// 3. Gauge (仪表盘) - 用于显示单个数值,并通过仪表盘样式进行可视化。
|
|
20
|
+
// 4. Bar Gauge (柱状仪表盘) - 类似于仪表盘,但以柱状方式显示。
|
|
21
|
+
// 5. Table (表格) - 用于以表格形式展示数据,支持多列、多行显示。
|
|
22
|
+
// 6. Singlestat (单数值) - 适合展示单个重要的数值(现已被 Stat 面板替代)。
|
|
23
|
+
// 7. Heatmap (热力图) - 用于显示数据密度或强度的热力图。
|
|
24
|
+
// 8. Alert List (告警列表) - 用于展示触发的告警和状态。
|
|
25
|
+
// 9. Logs (日志) - 用于展示和查询日志数据,尤其是通过 Loki 数据源。
|
|
26
|
+
// 10. Pie Chart (饼图) - 用于显示数据的饼图。
|
|
27
|
+
// 11. Text (文本) - 用于展示静态文本,支持 Markdown、HTML 格式。
|
|
28
|
+
// 12. Gauge (Gauge 仪表) - 显示环形图表用于单个值的展示。
|
|
29
|
+
// 13. Bar Chart (柱状图) - 显示条形图或柱状图。
|
|
30
|
+
// 14. Timeseries (时间序列) - 最新的时间序列图表,提供更多功能和配置选项。
|
|
31
|
+
// 15. Geomap (地理图) - 基于地图的地理数据可视化。
|
|
32
|
+
// 16. Histogram (直方图) - 展示数据分布的图表。
|
|
33
|
+
// 17. Status History (状态历史) - 显示状态随时间变化的历史记录。
|
|
34
|
+
// 18. Canvas (画布) - 允许自定义绘制的可视化。
|
|
35
|
+
// 19. Node Graph (节点图) - 可视化连接点之间的网络关系。
|
|
36
|
+
// 20. XY Chart (XY 图表) - 显示二维数据的 XY 图表。
|
|
37
|
+
// | 'group'
|
|
38
|
+
// | 'sequence'
|
|
39
|
+
// | 'singlestat'
|
|
40
|
+
// | 'pie'
|
|
41
|
+
// | 'bar'
|
|
42
|
+
// | 'histogram'
|
|
43
|
+
// | 'slo'
|
|
44
|
+
// | 'toplist'
|
|
45
|
+
// | 'gauge'
|
|
46
|
+
// | 'scatter'
|
|
47
|
+
// | 'bubble'
|
|
48
|
+
// | 'table'
|
|
49
|
+
// | 'treemap'
|
|
50
|
+
// | 'funnel'
|
|
51
|
+
// | 'chinamap'
|
|
52
|
+
// | 'worldmap'
|
|
53
|
+
// | 'hexgon'
|
|
54
|
+
// | 'heatmap'
|
|
55
|
+
// | 'topology'
|
|
56
|
+
// | 'sankey'
|
|
57
|
+
// | 'log'
|
|
58
|
+
// | 'object'
|
|
59
|
+
// | 'alarm'
|
|
60
|
+
// | 'text'
|
|
61
|
+
// | 'video'
|
|
62
|
+
// | 'picture'
|
|
63
|
+
// | 'command'
|
|
64
|
+
// | 'iframe'
|
|
65
|
+
const grafanaPanelTypeToGuanceChartMap: { [key: string]: GuanceChartType } = {
|
|
66
|
+
stat: 'singlestat',
|
|
67
|
+
barchart: 'bar',
|
|
68
|
+
timeseries: 'sequence',
|
|
69
|
+
piechart: 'pie',
|
|
70
|
+
histogram: 'histogram',
|
|
71
|
+
bargauge: 'toplist',
|
|
72
|
+
gauge: 'gauge',
|
|
73
|
+
table: 'table',
|
|
74
|
+
text: 'text',
|
|
75
|
+
heatmap: 'heatmap',
|
|
76
|
+
treemap: 'treemap',
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function replaceVariableStr(grafanaExpr: string): string {
|
|
80
|
+
return grafanaExpr.replace(/\$([\d_\w]+)/g, (match, $0) => {
|
|
81
|
+
return `#{${$0}}`
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Get layout items sorted from top left to right and down.
|
|
86
|
+
*
|
|
87
|
+
* @return {Array} Array of layout objects.
|
|
88
|
+
* @return {Array} Layout, sorted static items first.
|
|
89
|
+
*/
|
|
90
|
+
function sortPanelItemsByRowCol(panels: Panel[]): Panel[] {
|
|
91
|
+
return panels.slice(0).sort(function (panelA, panelB) {
|
|
92
|
+
// 宽度长的在前面
|
|
93
|
+
const { gridPos: posA } = panelA
|
|
94
|
+
const { gridPos: posB } = panelB
|
|
95
|
+
if (!posA || !posB) return -1
|
|
96
|
+
if (posA.y === posB.y && posA.x === posB.x && posA.w > posB.w) {
|
|
97
|
+
return -1
|
|
98
|
+
}
|
|
99
|
+
if (posA.y === posB.y && posA.x === posB.x) {
|
|
100
|
+
return 0
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (posA.y > posB.y || (posA.y === posB.y && posA.x > posB.x)) {
|
|
104
|
+
return 1
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return -1
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
function getGridH(h: number, rowHeight: number, margin: number) {
|
|
111
|
+
return Math.round(h * rowHeight + Math.max(0, 2 * (h - 1)) * margin)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function getGuanceHByGrafanaH(granfanH: number) {
|
|
115
|
+
return (getGridH(granfanH, 30, 4) + 10) / 20
|
|
116
|
+
}
|
|
117
|
+
function covertPanelToGuanceChart(grafanaPanel: Panel, rowPanel: RowPanel | undefined): GuanceChartData {
|
|
118
|
+
const { gridPos, title, type, targets, options } = grafanaPanel
|
|
119
|
+
// const { gridPos: rowGridPos } = rowPanel
|
|
120
|
+
const chartType: GuanceChartType = grafanaPanelTypeToGuanceChartMap[type]
|
|
121
|
+
let pos = {
|
|
122
|
+
x: gridPos!.x,
|
|
123
|
+
w: gridPos!.w,
|
|
124
|
+
y: gridPos!.y,
|
|
125
|
+
h: gridPos!.h,
|
|
126
|
+
}
|
|
127
|
+
if (rowPanel) {
|
|
128
|
+
const { gridPos: rowGridPos } = rowPanel
|
|
129
|
+
if (rowGridPos && gridPos) {
|
|
130
|
+
pos = {
|
|
131
|
+
x: gridPos.x,
|
|
132
|
+
w: gridPos.w,
|
|
133
|
+
y: gridPos.y - rowGridPos.y,
|
|
134
|
+
h: gridPos.h,
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const queries: GuanceChartQueries = []
|
|
139
|
+
if (targets && targets.length) {
|
|
140
|
+
targets.forEach((_target) => {
|
|
141
|
+
if (!_target.expr) return
|
|
142
|
+
const queryItem: GuanceChartQueryItem = {
|
|
143
|
+
datasource: 'dataflux',
|
|
144
|
+
qtype: 'promql',
|
|
145
|
+
type: chartType,
|
|
146
|
+
query: {
|
|
147
|
+
q: replaceVariableStr(_target.expr as string),
|
|
148
|
+
type: 'promql',
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
queries.push(queryItem)
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
const settings: GuanceChartSettings = {}
|
|
155
|
+
if (options) {
|
|
156
|
+
switch (chartType) {
|
|
157
|
+
case 'text':
|
|
158
|
+
const queryItem: GuanceChartQueryItem = {
|
|
159
|
+
query: {
|
|
160
|
+
content: options.content,
|
|
161
|
+
},
|
|
162
|
+
}
|
|
163
|
+
queries.push(queryItem)
|
|
164
|
+
break
|
|
165
|
+
default:
|
|
166
|
+
break
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const guanceChart: GuanceChartData = {
|
|
170
|
+
extend: {
|
|
171
|
+
settings: settings,
|
|
172
|
+
},
|
|
173
|
+
group: {
|
|
174
|
+
name: rowPanel ? rowPanel.title : 'default',
|
|
175
|
+
},
|
|
176
|
+
pos: {
|
|
177
|
+
x: pos.x,
|
|
178
|
+
y: getGuanceHByGrafanaH(pos.y),
|
|
179
|
+
h: getGuanceHByGrafanaH(pos.h),
|
|
180
|
+
w: pos.w,
|
|
181
|
+
},
|
|
182
|
+
name: title || '',
|
|
183
|
+
queries: queries,
|
|
184
|
+
type: chartType,
|
|
185
|
+
}
|
|
186
|
+
return guanceChart
|
|
187
|
+
}
|
|
188
|
+
const covertPanelsToCharts = (grafanaPanelData: Panel[], rowPanel: RowPanel | undefined): GuanceChartData[] => {
|
|
189
|
+
const guanceCharts: GuanceChartData[] = []
|
|
190
|
+
grafanaPanelData.forEach((grafanaPanel) => {
|
|
191
|
+
if (!grafanaPanel.gridPos) return
|
|
192
|
+
guanceCharts.push(covertPanelToGuanceChart(grafanaPanel, rowPanel))
|
|
193
|
+
})
|
|
194
|
+
return guanceCharts
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const VARIABLE_MAP = {
|
|
198
|
+
query: 'PROMQL_QUERY',
|
|
199
|
+
custom: 'CUSTOM_LIST',
|
|
200
|
+
}
|
|
201
|
+
const VARIABLE_DATASOURCE_MAP = {
|
|
202
|
+
query: 'dataflux',
|
|
203
|
+
custom: 'custom',
|
|
204
|
+
}
|
|
205
|
+
const covert = (grafanaData: GrafanaDashboardType): GuanceDashboardType => {
|
|
206
|
+
const covertGuanceResult: GuanceDashboardType = {}
|
|
207
|
+
// 标题
|
|
208
|
+
covertGuanceResult.title = grafanaData.title
|
|
209
|
+
// 视图变量
|
|
210
|
+
const guanceVars: ChartVarsSettings = []
|
|
211
|
+
grafanaData.templating?.list?.forEach((_variable: VariableModel, index: number) => {
|
|
212
|
+
const { current, type, allValue } = _variable
|
|
213
|
+
if (!VARIABLE_MAP[type]) return
|
|
214
|
+
let defaultVal = {
|
|
215
|
+
label: '',
|
|
216
|
+
value: '',
|
|
217
|
+
}
|
|
218
|
+
if (current) {
|
|
219
|
+
let labels: string[] = []
|
|
220
|
+
let values: string[] = []
|
|
221
|
+
if (Array.isArray(current.text)) {
|
|
222
|
+
labels = current.text
|
|
223
|
+
} else if (typeof current.text === 'string') {
|
|
224
|
+
labels = [current.text]
|
|
225
|
+
}
|
|
226
|
+
if (Array.isArray(current.value)) {
|
|
227
|
+
values = current.value
|
|
228
|
+
} else if (typeof current.value === 'string') {
|
|
229
|
+
values = [current.value]
|
|
230
|
+
}
|
|
231
|
+
labels = labels.map((label) => {
|
|
232
|
+
// all 的情况需要适配观测云
|
|
233
|
+
if (label === 'All') {
|
|
234
|
+
if (allValue === '.*') {
|
|
235
|
+
return '*'
|
|
236
|
+
} else {
|
|
237
|
+
return 'all values'
|
|
238
|
+
}
|
|
239
|
+
} else {
|
|
240
|
+
return label
|
|
241
|
+
}
|
|
242
|
+
})
|
|
243
|
+
values = values.map((value) => {
|
|
244
|
+
// all 的情况需要适配观测云
|
|
245
|
+
if (value === '$__all') {
|
|
246
|
+
if (allValue === '.*') {
|
|
247
|
+
return '*'
|
|
248
|
+
} else {
|
|
249
|
+
return '__all__'
|
|
250
|
+
}
|
|
251
|
+
} else {
|
|
252
|
+
return value
|
|
253
|
+
}
|
|
254
|
+
})
|
|
255
|
+
defaultVal = {
|
|
256
|
+
label: labels.join(','),
|
|
257
|
+
value: values.join(','),
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const guanceVariableItem: ChartVarsItem = {
|
|
261
|
+
type: VARIABLE_MAP[type],
|
|
262
|
+
datasource: VARIABLE_DATASOURCE_MAP[type],
|
|
263
|
+
name: _variable.label,
|
|
264
|
+
seq: index,
|
|
265
|
+
hide: _variable.hide ? 1 : 0,
|
|
266
|
+
multiple: _variable.multi,
|
|
267
|
+
includeStar: _variable.includeAll,
|
|
268
|
+
valueSort: 'desc',
|
|
269
|
+
code: _variable.name,
|
|
270
|
+
definition: {
|
|
271
|
+
value: replaceVariableStr(_variable.query as string),
|
|
272
|
+
defaultVal: defaultVal,
|
|
273
|
+
},
|
|
274
|
+
}
|
|
275
|
+
guanceVars.push(guanceVariableItem)
|
|
276
|
+
})
|
|
277
|
+
// 分组
|
|
278
|
+
const guanceGroups: {
|
|
279
|
+
name?: string
|
|
280
|
+
}[] = []
|
|
281
|
+
// 分组展开信息
|
|
282
|
+
const guanceExpand: {
|
|
283
|
+
[key: string]: boolean
|
|
284
|
+
} = {}
|
|
285
|
+
|
|
286
|
+
// 图表
|
|
287
|
+
const guanceCharts: GuanceChartData[] = []
|
|
288
|
+
// 前一次分组,也就是当前图表的分组
|
|
289
|
+
let lastRowPanel: RowPanel | undefined
|
|
290
|
+
// 当前分组的 panel 列表
|
|
291
|
+
let lastPanels: Panel[] = []
|
|
292
|
+
let grafanaCharts =
|
|
293
|
+
grafanaData.panels?.filter(
|
|
294
|
+
(_panel: Panel | RowPanel) => _panel.type === 'row' || grafanaPanelTypeToGuanceChartMap[_panel.type]
|
|
295
|
+
) || []
|
|
296
|
+
grafanaCharts = sortPanelItemsByRowCol(grafanaCharts)
|
|
297
|
+
grafanaCharts.forEach((_panel) => {
|
|
298
|
+
if (_panel.type === 'row') {
|
|
299
|
+
const _rowPanel = _panel as RowPanel
|
|
300
|
+
guanceGroups.push({
|
|
301
|
+
name: _rowPanel.title,
|
|
302
|
+
})
|
|
303
|
+
if (_rowPanel.title) {
|
|
304
|
+
// 分组展开收起数据
|
|
305
|
+
guanceExpand[_rowPanel.title] = !_rowPanel.collapsed
|
|
306
|
+
}
|
|
307
|
+
if (lastPanels.length) {
|
|
308
|
+
// 上一个分组的图表转换,
|
|
309
|
+
guanceCharts.push(...covertPanelsToCharts(lastPanels, lastRowPanel))
|
|
310
|
+
//清空上一个分组列表
|
|
311
|
+
lastPanels = []
|
|
312
|
+
//清空上一个分组
|
|
313
|
+
lastRowPanel = undefined
|
|
314
|
+
}
|
|
315
|
+
if (_rowPanel.collapsed) {
|
|
316
|
+
// 收起
|
|
317
|
+
const subPanels =
|
|
318
|
+
_rowPanel.panels?.filter(
|
|
319
|
+
(_panel: Panel | RowPanel) => _panel.type === 'row' || grafanaPanelTypeToGuanceChartMap[_panel.type]
|
|
320
|
+
) || []
|
|
321
|
+
guanceCharts.push(...covertPanelsToCharts(subPanels, _rowPanel))
|
|
322
|
+
} else {
|
|
323
|
+
lastRowPanel = _rowPanel
|
|
324
|
+
// 展开
|
|
325
|
+
}
|
|
326
|
+
} else {
|
|
327
|
+
lastPanels.push(_panel)
|
|
328
|
+
}
|
|
329
|
+
})
|
|
330
|
+
// 最后一个分组
|
|
331
|
+
if (lastPanels.length) {
|
|
332
|
+
guanceCharts.push(...covertPanelsToCharts(lastPanels, lastRowPanel))
|
|
333
|
+
}
|
|
334
|
+
covertGuanceResult.dashboardExtend = {
|
|
335
|
+
groupUnfoldStatus: guanceExpand,
|
|
336
|
+
}
|
|
337
|
+
covertGuanceResult.main = {
|
|
338
|
+
vars: guanceVars,
|
|
339
|
+
charts: guanceCharts,
|
|
340
|
+
groups: guanceGroups,
|
|
341
|
+
}
|
|
342
|
+
return covertGuanceResult
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export async function run(args) {
|
|
346
|
+
const { argv } = yargs(args)
|
|
347
|
+
.usage('Convert grafana dashboard json template to guance dashboard json template.')
|
|
348
|
+
|
|
349
|
+
.demand('d')
|
|
350
|
+
.alias('d', 'input')
|
|
351
|
+
.describe('d', 'path to grafana dashboard json file path')
|
|
352
|
+
.coerce('d', (d) => {
|
|
353
|
+
const resolved = d && path.resolve(d)
|
|
354
|
+
if (fs.existsSync(resolved) && /\.json$/.test(resolved)) {
|
|
355
|
+
return resolved
|
|
356
|
+
}
|
|
357
|
+
throw new Error(`Input Grafana JSON File "${d}" is not a JSON!`)
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
.alias('o', 'out')
|
|
361
|
+
.describe('o', 'path to output json file path')
|
|
362
|
+
.default('o', path.resolve(path.join('.', 'guance-dashboard.json')))
|
|
363
|
+
.coerce('o', (o) => path.resolve(o))
|
|
364
|
+
const grafanaJsonPath = argv.d
|
|
365
|
+
const outGuanceJsonPath = argv.o
|
|
366
|
+
|
|
367
|
+
try {
|
|
368
|
+
const grafanaJSONData = JSON.parse(fs.readFileSync(grafanaJsonPath, 'utf-8'))
|
|
369
|
+
const covertResult = covert(grafanaJSONData)
|
|
370
|
+
fs.writeFileSync(outGuanceJsonPath, JSON.stringify(covertResult), 'utf-8')
|
|
371
|
+
} catch (err) {
|
|
372
|
+
throw new Error(err)
|
|
373
|
+
}
|
|
374
|
+
}
|