@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.
@@ -0,0 +1,125 @@
1
+ #!/usr/bin/env node
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import { pathToFileURL } from 'url';
5
+ import Ajv from 'ajv';
6
+ import { convertDashboard } from './convert-grafana-dashboard-core.js';
7
+ if (isDirectExecution()) {
8
+ main();
9
+ }
10
+ function isDirectExecution() {
11
+ if (!process.argv[1])
12
+ return false;
13
+ return import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href;
14
+ }
15
+ export function main() {
16
+ const { inputPath, outputPath, validateOutput, schemaId, guancePromqlCompatible, keepGrafanaMeta } = parseArgs(process.argv.slice(2));
17
+ const grafanaDashboard = readJson(inputPath);
18
+ const guanceDashboard = convertDashboard(grafanaDashboard, { guancePromqlCompatible, keepGrafanaMeta });
19
+ fs.mkdirSync(path.dirname(outputPath), { recursive: true });
20
+ fs.writeFileSync(outputPath, `${JSON.stringify(guanceDashboard, null, 2)}\n`, 'utf8');
21
+ console.log(`Converted ${inputPath} -> ${outputPath}`);
22
+ if (validateOutput) {
23
+ validateDashboardFile(outputPath, schemaId);
24
+ }
25
+ }
26
+ function parseArgs(args) {
27
+ let inputPath = '';
28
+ let outputPath = '';
29
+ let validateOutput = false;
30
+ let schemaId = 'dashboard-schema.json';
31
+ let guancePromqlCompatible = false;
32
+ let keepGrafanaMeta = false;
33
+ for (let index = 0; index < args.length; index++) {
34
+ const value = args[index];
35
+ if ((value === '-i' || value === '--input') && args[index + 1]) {
36
+ inputPath = path.resolve(args[++index]);
37
+ continue;
38
+ }
39
+ if ((value === '-o' || value === '--output') && args[index + 1]) {
40
+ outputPath = path.resolve(args[++index]);
41
+ continue;
42
+ }
43
+ if (value === '--validate') {
44
+ validateOutput = true;
45
+ continue;
46
+ }
47
+ if (value === '--schema' && args[index + 1]) {
48
+ schemaId = args[++index];
49
+ continue;
50
+ }
51
+ if (value === '--guance-promql-compatible') {
52
+ guancePromqlCompatible = true;
53
+ continue;
54
+ }
55
+ if (value === '--keep-grafana-meta') {
56
+ keepGrafanaMeta = true;
57
+ continue;
58
+ }
59
+ if (value === '-h' || value === '--help') {
60
+ printHelp();
61
+ process.exit(0);
62
+ }
63
+ }
64
+ if (!inputPath) {
65
+ printHelp();
66
+ process.exit(1);
67
+ }
68
+ if (!outputPath) {
69
+ const parsed = path.parse(inputPath);
70
+ outputPath = path.join(parsed.dir, `${parsed.name}.guance.json`);
71
+ }
72
+ return { inputPath, outputPath, validateOutput, schemaId, guancePromqlCompatible, keepGrafanaMeta };
73
+ }
74
+ function printHelp() {
75
+ console.error('Usage: node convert-grafana-dashboard.mjs --input <grafana.json> [--output <guance.json>] [--validate] [--schema <schema-id>] [--guance-promql-compatible] [--keep-grafana-meta]');
76
+ }
77
+ export function validateDashboardFile(filePath, schemaId) {
78
+ const projectRoot = findProjectRoot();
79
+ const schemasDirectory = path.join(projectRoot, 'schemas');
80
+ const ajv = new Ajv({ allErrors: true });
81
+ forEachFile(schemasDirectory, (schemaPath) => {
82
+ if (!schemaPath.endsWith('.json'))
83
+ return;
84
+ ajv.addSchema(readJson(schemaPath));
85
+ });
86
+ const valid = ajv.validate(schemaId, readJson(filePath));
87
+ if (valid) {
88
+ console.log(`Validated ${filePath} against ${schemaId}`);
89
+ return;
90
+ }
91
+ console.error(`Validation failed for ${filePath} against ${schemaId}:`);
92
+ for (const error of ajv.errors || []) {
93
+ const instancePath = error.instancePath || '/';
94
+ console.error(`- ${instancePath} ${error.message}`);
95
+ }
96
+ process.exit(1);
97
+ }
98
+ function findProjectRoot() {
99
+ let currentDirectory = process.cwd();
100
+ while (true) {
101
+ if (fs.existsSync(path.join(currentDirectory, 'schemas', 'dashboard-schema.json'))) {
102
+ return currentDirectory;
103
+ }
104
+ const parentDirectory = path.dirname(currentDirectory);
105
+ if (parentDirectory === currentDirectory) {
106
+ throw new Error('Could not locate project root containing schemas/dashboard-schema.json');
107
+ }
108
+ currentDirectory = parentDirectory;
109
+ }
110
+ }
111
+ function readJson(filePath) {
112
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
113
+ }
114
+ function forEachFile(directoryPath, visitor) {
115
+ const entries = fs.readdirSync(directoryPath, { withFileTypes: true });
116
+ for (const entry of entries) {
117
+ const entryPath = path.join(directoryPath, entry.name);
118
+ if (entry.isDirectory()) {
119
+ forEachFile(entryPath, visitor);
120
+ continue;
121
+ }
122
+ visitor(entryPath);
123
+ }
124
+ }
125
+ export { convertDashboard };
@@ -1,7 +1,7 @@
1
1
  // This script is the shared conversion source for the published API and CLI.
2
2
  // Keep it thin and delegate to the maintained converter implementation.
3
3
  // @ts-ignore
4
- import { convertDashboard } from '../../skills/grafana-to-guance-dashboard/scripts/convert-grafana-dashboard.mjs';
4
+ import { convertDashboard } from './convert-grafana-dashboard-core.js';
5
5
  export function covert(grafanaData) {
6
6
  return convertDashboard(grafanaData);
7
7
  }