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