@cloudcare/guance-front-tools 1.0.15 → 1.0.16
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/lib/cjs/scripts/convert-grafana-dashboard-core.js +1512 -1604
- package/lib/cjs/scripts/convert-grafana-dashboard.js +108 -125
- package/lib/esm/scripts/convert-grafana-dashboard-core.js +1508 -1603
- package/lib/esm/scripts/convert-grafana-dashboard.js +101 -123
- package/package.json +1 -1
- package/scripts/sync-converter.mjs +17 -1
- package/skills/grafana-to-guance-dashboard/scripts/convert-grafana-dashboard-core.js +1508 -1603
- package/skills/grafana-to-guance-dashboard/scripts/convert-grafana-dashboard.mjs +101 -123
- package/test/cli.test.mjs +3 -10
|
@@ -1,147 +1,130 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.convertDashboard = void 0;
|
|
5
|
+
exports.main = main;
|
|
6
|
+
exports.validateDashboardFile = validateDashboardFile;
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const url_1 = require("url");
|
|
10
|
+
const ajv_1 = require("ajv");
|
|
11
|
+
const convert_grafana_dashboard_core_js_1 = require("./convert-grafana-dashboard-core.js");
|
|
12
|
+
Object.defineProperty(exports, "convertDashboard", { enumerable: true, get: function () { return convert_grafana_dashboard_core_js_1.convertDashboard; } });
|
|
9
13
|
if (isDirectExecution()) {
|
|
10
|
-
|
|
14
|
+
main();
|
|
11
15
|
}
|
|
12
|
-
|
|
13
16
|
function isDirectExecution() {
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
if (!process.argv[1])
|
|
18
|
+
return false;
|
|
19
|
+
return import.meta.url === (0, url_1.pathToFileURL)(path_1.default.resolve(process.argv[1])).href;
|
|
16
20
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (validateOutput) {
|
|
28
|
-
validateDashboardFile(outputPath, schemaId)
|
|
29
|
-
}
|
|
21
|
+
function main() {
|
|
22
|
+
const { inputPath, outputPath, validateOutput, schemaId, guancePromqlCompatible, keepGrafanaMeta } = parseArgs(process.argv.slice(2));
|
|
23
|
+
const grafanaDashboard = readJson(inputPath);
|
|
24
|
+
const guanceDashboard = (0, convert_grafana_dashboard_core_js_1.convertDashboard)(grafanaDashboard, { guancePromqlCompatible, keepGrafanaMeta });
|
|
25
|
+
fs_1.default.mkdirSync(path_1.default.dirname(outputPath), { recursive: true });
|
|
26
|
+
fs_1.default.writeFileSync(outputPath, `${JSON.stringify(guanceDashboard, null, 2)}\n`, 'utf8');
|
|
27
|
+
console.log(`Converted ${inputPath} -> ${outputPath}`);
|
|
28
|
+
if (validateOutput) {
|
|
29
|
+
validateDashboardFile(outputPath, schemaId);
|
|
30
|
+
}
|
|
30
31
|
}
|
|
31
|
-
|
|
32
32
|
function parseArgs(args) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
33
|
+
let inputPath = '';
|
|
34
|
+
let outputPath = '';
|
|
35
|
+
let validateOutput = false;
|
|
36
|
+
let schemaId = 'dashboard-schema.json';
|
|
37
|
+
let guancePromqlCompatible = false;
|
|
38
|
+
let keepGrafanaMeta = false;
|
|
39
|
+
for (let index = 0; index < args.length; index++) {
|
|
40
|
+
const value = args[index];
|
|
41
|
+
if ((value === '-i' || value === '--input') && args[index + 1]) {
|
|
42
|
+
inputPath = path_1.default.resolve(args[++index]);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if ((value === '-o' || value === '--output') && args[index + 1]) {
|
|
46
|
+
outputPath = path_1.default.resolve(args[++index]);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (value === '--validate') {
|
|
50
|
+
validateOutput = true;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (value === '--schema' && args[index + 1]) {
|
|
54
|
+
schemaId = args[++index];
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (value === '--guance-promql-compatible') {
|
|
58
|
+
guancePromqlCompatible = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (value === '--keep-grafana-meta') {
|
|
62
|
+
keepGrafanaMeta = true;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (value === '-h' || value === '--help') {
|
|
66
|
+
printHelp();
|
|
67
|
+
process.exit(0);
|
|
68
|
+
}
|
|
57
69
|
}
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
if (!inputPath) {
|
|
71
|
+
printHelp();
|
|
72
|
+
process.exit(1);
|
|
61
73
|
}
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
|
|
74
|
+
if (!outputPath) {
|
|
75
|
+
const parsed = path_1.default.parse(inputPath);
|
|
76
|
+
outputPath = path_1.default.join(parsed.dir, `${parsed.name}.guance.json`);
|
|
65
77
|
}
|
|
66
|
-
|
|
67
|
-
printHelp()
|
|
68
|
-
process.exit(0)
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (!inputPath) {
|
|
73
|
-
printHelp()
|
|
74
|
-
process.exit(1)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (!outputPath) {
|
|
78
|
-
const parsed = path.parse(inputPath)
|
|
79
|
-
outputPath = path.join(parsed.dir, `${parsed.name}.guance.json`)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return { inputPath, outputPath, validateOutput, schemaId, guancePromqlCompatible, keepGrafanaMeta }
|
|
78
|
+
return { inputPath, outputPath, validateOutput, schemaId, guancePromqlCompatible, keepGrafanaMeta };
|
|
83
79
|
}
|
|
84
|
-
|
|
85
80
|
function printHelp() {
|
|
86
|
-
|
|
87
|
-
'Usage: node convert-grafana-dashboard.mjs --input <grafana.json> [--output <guance.json>] [--validate] [--schema <schema-id>] [--guance-promql-compatible] [--keep-grafana-meta]'
|
|
88
|
-
)
|
|
81
|
+
console.error('Usage: node convert-grafana-dashboard.mjs --input <grafana.json> [--output <guance.json>] [--validate] [--schema <schema-id>] [--guance-promql-compatible] [--keep-grafana-meta]');
|
|
89
82
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
console.error(`- ${instancePath} ${error.message}`)
|
|
111
|
-
}
|
|
112
|
-
process.exit(1)
|
|
83
|
+
function validateDashboardFile(filePath, schemaId) {
|
|
84
|
+
const projectRoot = findProjectRoot();
|
|
85
|
+
const schemasDirectory = path_1.default.join(projectRoot, 'schemas');
|
|
86
|
+
const ajv = new ajv_1.default({ allErrors: true });
|
|
87
|
+
forEachFile(schemasDirectory, (schemaPath) => {
|
|
88
|
+
if (!schemaPath.endsWith('.json'))
|
|
89
|
+
return;
|
|
90
|
+
ajv.addSchema(readJson(schemaPath));
|
|
91
|
+
});
|
|
92
|
+
const valid = ajv.validate(schemaId, readJson(filePath));
|
|
93
|
+
if (valid) {
|
|
94
|
+
console.log(`Validated ${filePath} against ${schemaId}`);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
console.error(`Validation failed for ${filePath} against ${schemaId}:`);
|
|
98
|
+
for (const error of ajv.errors || []) {
|
|
99
|
+
const instancePath = error.instancePath || '/';
|
|
100
|
+
console.error(`- ${instancePath} ${error.message}`);
|
|
101
|
+
}
|
|
102
|
+
process.exit(1);
|
|
113
103
|
}
|
|
114
|
-
|
|
115
104
|
function findProjectRoot() {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
105
|
+
let currentDirectory = process.cwd();
|
|
106
|
+
while (true) {
|
|
107
|
+
if (fs_1.default.existsSync(path_1.default.join(currentDirectory, 'schemas', 'dashboard-schema.json'))) {
|
|
108
|
+
return currentDirectory;
|
|
109
|
+
}
|
|
110
|
+
const parentDirectory = path_1.default.dirname(currentDirectory);
|
|
111
|
+
if (parentDirectory === currentDirectory) {
|
|
112
|
+
throw new Error('Could not locate project root containing schemas/dashboard-schema.json');
|
|
113
|
+
}
|
|
114
|
+
currentDirectory = parentDirectory;
|
|
126
115
|
}
|
|
127
|
-
currentDirectory = parentDirectory
|
|
128
|
-
}
|
|
129
116
|
}
|
|
130
|
-
|
|
131
117
|
function readJson(filePath) {
|
|
132
|
-
|
|
118
|
+
return JSON.parse(fs_1.default.readFileSync(filePath, 'utf8'));
|
|
133
119
|
}
|
|
134
|
-
|
|
135
120
|
function forEachFile(directoryPath, visitor) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
121
|
+
const entries = fs_1.default.readdirSync(directoryPath, { withFileTypes: true });
|
|
122
|
+
for (const entry of entries) {
|
|
123
|
+
const entryPath = path_1.default.join(directoryPath, entry.name);
|
|
124
|
+
if (entry.isDirectory()) {
|
|
125
|
+
forEachFile(entryPath, visitor);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
visitor(entryPath);
|
|
142
129
|
}
|
|
143
|
-
visitor(entryPath)
|
|
144
|
-
}
|
|
145
130
|
}
|
|
146
|
-
|
|
147
|
-
export { convertDashboard }
|