-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathdownload_dashboards.mjs
executable file
·109 lines (93 loc) · 3.21 KB
/
download_dashboards.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint-disable
@typescript-eslint/explicit-function-return-type,
@typescript-eslint/naming-convention,
import/no-extraneous-dependencies,
no-console
*/
import fs from "node:fs";
import path from "node:path";
import axios from "axios";
import dotenv from "dotenv";
import {lintGrafanaDashboard, readGrafanaDashboard, writeGrafanaDashboard} from "./lint-grafana-dashboard.mjs";
// Usage:
//
// Create a file `.secrets.env` with envs
// ```
// GRAFANA_API_KEY=$token
// GRAFANA_URL=https://yourgrafanaapi.io
// ```
//
// Run
// ```
// node scripts/download_dashboards.mjs
// ```
//
// Check git diff of resulting files, commit, push and open PR
// load environment variables from .env file
dotenv.config({path: ".secrets.env"});
const OUTDIR = "./dashboards";
const UID_PREFIX_WHITELIST = "lodestar_";
const {GRAFANA_API_KEY, GRAFANA_URL} = process.env;
if (!GRAFANA_API_KEY) throw Error("GRAFANA_API_KEY not set");
if (!GRAFANA_URL) throw Error("GRAFANA_URL not set");
// Fetch all dashboard uids
/** @type {{data: DashboardMeta[]}} */
const dashboardListRes = await axios.get(`${GRAFANA_URL}/api/search`, {
headers: {Authorization: `Bearer ${GRAFANA_API_KEY}`},
});
// Iterate through each dashboard uid and download the dashboard data
for (const dashboardMeta of dashboardListRes.data) {
if (!dashboardMeta.uid.startsWith(UID_PREFIX_WHITELIST)) {
continue;
}
// Note, currently Grafana API does NOT support returning a dashboard with the
// "Export for sharing externally" toggle turned on.
// https://community.grafana.com/t/export-dashboard-for-external-use-via-http-api/50716
/** @type {{data: DashboardGet}} */
const dashboardDataRes = await axios.get(`${GRAFANA_URL}/api/dashboards/uid/${dashboardMeta.uid}`, {
headers: {Authorization: `Bearer ${GRAFANA_API_KEY}`},
});
const outpath = path.join(OUTDIR, `${dashboardMeta.uid}.json`);
// Only update dashboards that exist locally. Sometimes developers duplicate dashboards on the Grafana server
// to test some new panels, with names like $uid_2.json. This script ignores those duplicates.
// >> To add a new dashboard touch a file with filename `$uid.json`
if (fs.existsSync(outpath)) {
const prevDashboard = readGrafanaDashboard(outpath);
// Lint dashboard to match target format
const newDashboard = lintGrafanaDashboard(dashboardDataRes.data.dashboard);
// Set version to same to reduce diff
newDashboard.version = prevDashboard.version;
// Save dashboard data to a JSON file
writeGrafanaDashboard(outpath, newDashboard);
console.log(`saved ${outpath}`);
}
}
// {
// id: 39,
// uid: '1iQudJZVk',
// title: 'Alerts',
// uri: 'db/alerts',
// url: '/dashboards/f/1iQudJZVk/alerts',
// slug: '',
// type: 'dash-folder',
// tags: [],
// isStarred: false,
// sortMeta: 0
// },
/**
* @typedef {Object} DashboardMeta
* @property {number} id
* @property {string} uid
* @property {string} title
* @property {string} uri
* @property {string} url
* @property {string} slug
* @property {string} type
* @property {string[]} tags
* @property {boolean} isStarred
* @property {number} sortMeta
*/
/**
* @typedef {Object} DashboardGet
* @property {import('./lint-grafana-dashboard.mjs').Dashboard} dashboard
*/