@cloudcare/guance-front-tools 1.0.13 → 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 +1678 -0
- package/lib/cjs/scripts/convert-grafana-dashboard.js +130 -0
- package/lib/cjs/scripts/grafana-covert-to-guance-core.js +2 -2
- package/lib/esm/scripts/convert-grafana-dashboard-core.js +1675 -0
- package/lib/esm/scripts/convert-grafana-dashboard.js +125 -0
- package/lib/esm/scripts/grafana-covert-to-guance-core.js +1 -1
- package/lib/scripts/convert-grafana-dashboard-core.js +1770 -0
- package/lib/scripts/convert-grafana-dashboard.js +147 -0
- package/lib/scripts/grafana-covert-to-guance-core.js +1 -1
- package/lib/scripts/grafana-covert-to-guance-core.ts +1 -1
- package/lib/scripts/grafana-covert-to-guance.js +3 -1
- package/lib/scripts/grafana-covert-to-guance.ts +3 -4
- package/package.json +3 -2
- package/scripts/sync-converter.mjs +57 -0
- package/skills/grafana-to-guance-dashboard/scripts/convert-grafana-dashboard-core.js +1675 -0
- package/skills/grafana-to-guance-dashboard/scripts/convert-grafana-dashboard.mjs +106 -1880
- package/test/cli.test.mjs +123 -1
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
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; } });
|
|
13
|
+
if (isDirectExecution()) {
|
|
14
|
+
main();
|
|
15
|
+
}
|
|
16
|
+
function isDirectExecution() {
|
|
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;
|
|
20
|
+
}
|
|
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
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function parseArgs(args) {
|
|
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
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (!inputPath) {
|
|
71
|
+
printHelp();
|
|
72
|
+
process.exit(1);
|
|
73
|
+
}
|
|
74
|
+
if (!outputPath) {
|
|
75
|
+
const parsed = path_1.default.parse(inputPath);
|
|
76
|
+
outputPath = path_1.default.join(parsed.dir, `${parsed.name}.guance.json`);
|
|
77
|
+
}
|
|
78
|
+
return { inputPath, outputPath, validateOutput, schemaId, guancePromqlCompatible, keepGrafanaMeta };
|
|
79
|
+
}
|
|
80
|
+
function printHelp() {
|
|
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]');
|
|
82
|
+
}
|
|
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);
|
|
103
|
+
}
|
|
104
|
+
function findProjectRoot() {
|
|
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;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function readJson(filePath) {
|
|
118
|
+
return JSON.parse(fs_1.default.readFileSync(filePath, 'utf8'));
|
|
119
|
+
}
|
|
120
|
+
function forEachFile(directoryPath, visitor) {
|
|
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);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -4,7 +4,7 @@ exports.covert = covert;
|
|
|
4
4
|
// This script is the shared conversion source for the published API and CLI.
|
|
5
5
|
// Keep it thin and delegate to the maintained converter implementation.
|
|
6
6
|
// @ts-ignore
|
|
7
|
-
var
|
|
7
|
+
var convert_grafana_dashboard_core_js_1 = require("./convert-grafana-dashboard-core.js");
|
|
8
8
|
function covert(grafanaData) {
|
|
9
|
-
return (0,
|
|
9
|
+
return (0, convert_grafana_dashboard_core_js_1.convertDashboard)(grafanaData);
|
|
10
10
|
}
|