grafana-find/main.js

66 lines
1.9 KiB
JavaScript

const fetch = require('node-fetch');
const colors = require('colors');
const findAll = "vn.client";
const urlDashboards = "https://grafana.verdnatura.es/api/search";
const urlUID = "https://grafana.verdnatura.es/api/dashboards/uid/";
const apiKey = "Bearer eyJrIjoiaHczdnZsOGd5M0xNUHJwZ3V4MGpvQzV0QVNGblpySloiLCJuIjoiZmluZEFsbF9HcmFmYW5hIiwiaWQiOjF9";
let numberOfResults = 0;
const regexRawSQL = new RegExp(findAll, 'i');
async function main(){
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;
let response = await fetch(url, {
method: "GET",
headers: {
"Authorization": apiKey,
}
});
let data = await response.json();
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;
}
}
if (isFound) {
console.log(colors.randomColor('https://grafana.verdnatura.es/d/' + allUID[i].uid));
numberOfResults++;
}
}
if (numberOfResults==0)
console.log("No se han encontrado resultados".red);
else
console.log(colors.green.bold("---- Se han encontrado " + numberOfResults + " resultados ----"));
}
main();