grafana-find/main.js

66 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-06-15 06:32:29 +00:00
const fetch = require('node-fetch');
2022-06-16 11:07:29 +00:00
const colors = require('colors');
2022-06-14 12:57:03 +00:00
2022-06-16 11:07:29 +00:00
const findAll = "vn.client";
2022-06-15 08:21:55 +00:00
2022-06-17 06:13:54 +00:00
const urlDashboards = "https://grafana.verdnatura.es/api/search";
const urlUID = "https://grafana.verdnatura.es/api/dashboards/uid/";
const apiKey = "Bearer eyJrIjoiaHczdnZsOGd5M0xNUHJwZ3V4MGpvQzV0QVNGblpySloiLCJuIjoiZmluZEFsbF9HcmFmYW5hIiwiaWQiOjF9";
let numberOfResults = 0;
2022-06-16 11:07:29 +00:00
const regexRawSQL = new RegExp(findAll, 'i');
2022-06-14 12:57:03 +00:00
2022-06-15 06:32:29 +00:00
async function main(){
2022-06-17 06:13:54 +00:00
console.clear();
let responseAllUID = await fetch(urlDashboards, {
method: "GET",
headers: {
"Authorization": apiKey,
}
});
let allUID = await responseAllUID.json();
for (let i=0; i < allUID.length; i++) {
let url = urlUID + allUID[i].uid;
2022-06-15 08:21:55 +00:00
let response = await fetch(url, {
2022-06-15 06:32:29 +00:00
method: "GET",
headers: {
2022-06-17 06:13:54 +00:00
"Authorization": apiKey,
2022-06-15 08:21:55 +00:00
}
});
let data = await response.json();
2022-06-16 11:07:29 +00:00
let isFound = false;
const dashboard = data.dashboard;
if (dashboard) {
if (dashboard.panels)
panelsLoop: for (const panel of dashboard.panels)
if (panel.targets)
for (const target of panel.targets) {
isFound = regexRawSQL.test(target.rawSql);
if (isFound) break panelsLoop;
}
if (dashboard.templating && !isFound)
for (const list of dashboard.templating.list) {
isFound = regexRawSQL.test(list.query);
if (isFound) break;
}
}
2022-06-17 06:13:54 +00:00
if (isFound) {
console.log(colors.randomColor('https://grafana.verdnatura.es/d/' + allUID[i].uid));
numberOfResults++;
}
2022-06-15 06:32:29 +00:00
}
2022-06-17 06:13:54 +00:00
if (numberOfResults==0)
console.log("No se han encontrado resultados".red);
else
console.log(colors.green.bold("---- Se han encontrado " + numberOfResults + " resultados ----"));
2022-06-14 12:57:03 +00:00
}
2022-06-16 11:07:29 +00:00
main();