arreglso
This commit is contained in:
parent
617b44d90e
commit
98d2029582
|
@ -1,7 +1,17 @@
|
|||
{
|
||||
// Use IntelliSense para saber los atributos posibles.
|
||||
// Mantenga el puntero para ver las descripciones de los existentes atributos.
|
||||
// Para más información, visite: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": []
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "attach",
|
||||
"name": "Attach",
|
||||
"restart": true,
|
||||
"timeout": 50000
|
||||
}, {
|
||||
"type": "node",
|
||||
"request": "attach",
|
||||
"name": "Attach by process ID",
|
||||
"processId": "${command:PickProcess}"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
// Coloque su configuración en este archivo para sobrescribir la configuración predeterminada y de usuario.
|
||||
{
|
||||
// Carácter predeterminado de final de línea.
|
||||
"files.eol": "\n",
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll.eslint": true
|
||||
}
|
||||
}
|
124
main.js
124
main.js
|
@ -12,7 +12,8 @@ const conGrafana = mysql.createPool({
|
|||
host: "dev-db.verdnatura.es",
|
||||
user: "grafanaPermissions",
|
||||
password: "grafana",
|
||||
port : 3307
|
||||
port : 3307,
|
||||
queryTimeout: 100
|
||||
});
|
||||
|
||||
const conVicent = mysql.createPool({
|
||||
|
@ -38,15 +39,6 @@ console.log(
|
|||
`v${packageJson.version}`.cyan.bold
|
||||
);
|
||||
|
||||
const usage = {
|
||||
description: 'Utility to find strings in dashboards',
|
||||
params: {
|
||||
user: 'Grafana username',
|
||||
version: 'Display the version number and exit',
|
||||
help: 'Display this help message'
|
||||
}
|
||||
};
|
||||
|
||||
const opts = getopts(process.argv.slice(2), {
|
||||
alias: {
|
||||
version: 'v',
|
||||
|
@ -56,7 +48,7 @@ const opts = getopts(process.argv.slice(2), {
|
|||
'version',
|
||||
'help'
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
if (opts.version) {
|
||||
process.exit();
|
||||
|
@ -168,7 +160,7 @@ async function main(){
|
|||
} catch (notfound) {
|
||||
console.error(`${error}Server '${config.grafanaUrl}' not found`.red);
|
||||
process.exit(1);
|
||||
};
|
||||
}
|
||||
let AllOrganizations = await response.json();
|
||||
|
||||
if (AllOrganizations.message==='invalid username or password') {
|
||||
|
@ -237,7 +229,7 @@ async function main(){
|
|||
if (panel.targets)
|
||||
for (const target of panel.targets) {
|
||||
const firstWordAfterFrom = getFirstWordAfterFrom(target.rawSql);
|
||||
if(firstWordAfterFrom == 'ost_ticket') continue;
|
||||
if(firstWordAfterFrom == 'ost_ticket' || firstWordAfterFrom == 'osticket'|| firstWordAfterFrom == 'redmine') continue;
|
||||
|
||||
const sql = replaceVariables(target.rawSql);
|
||||
|
||||
|
@ -294,7 +286,7 @@ async function main(){
|
|||
if (!numberOfDashboards)
|
||||
console.log(`No results found\n`.green);
|
||||
numberOfDashboards = 0;
|
||||
};
|
||||
}
|
||||
|
||||
console.log(colors.green.bold(`──────── Have been found ${numberOfObjects} results in ${totalDashboards} dashboards ────────\n`));
|
||||
|
||||
|
@ -309,27 +301,6 @@ async function main(){
|
|||
|
||||
main();
|
||||
|
||||
function haveDatabaseInFrom(str){
|
||||
let fromIndex = str.indexOf("FROM") + 5;
|
||||
let firstWord = str.substring(fromIndex, str.indexOf(" ", fromIndex));
|
||||
|
||||
if (firstWord.indexOf(".") !== -1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function haveDatabaseInJoin(str){
|
||||
let fromIndex = str.indexOf("JOIN") + 5;
|
||||
let firstWord = str.substring(fromIndex, str.indexOf(" ", fromIndex));
|
||||
|
||||
if (firstWord.indexOf(".") !== -1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function getFirstWord(text) {
|
||||
const words = text.split(" ");
|
||||
|
@ -359,12 +330,15 @@ function getFirstWordBetweenQuotes(text) {
|
|||
function replaceVariables(text){
|
||||
let sql = text;
|
||||
|
||||
sql = sql.replaceAll('$__timeGroupAlias', 'util.mock2');
|
||||
sql = sql.replaceAll('$__timeGroup', 'util.mock2');
|
||||
sql = sql.replaceAll('$__timeFilter', 'util.mock');
|
||||
sql = sql.replaceAll('$__timeFrom()', 1);
|
||||
sql = sql.replaceAll('$__timeTo()', 1);
|
||||
sql = sql.replaceAll('$__timeFrom()', `'2022-01-01'`);
|
||||
sql = sql.replaceAll('$__timeTo()', `'2022-01-01'`);
|
||||
sql = sql.replaceAll('${__from}', `'2022-01-01'`);
|
||||
sql = sql.replaceAll('${__to}', `'2022-01-01'`);
|
||||
sql = sql.replaceAll(/\$\w+/g, 1);
|
||||
sql = sql.replaceAll('${__from}', 1);
|
||||
sql = sql.replaceAll('${__to}', 1);
|
||||
|
||||
|
||||
return sql;
|
||||
}
|
||||
|
@ -376,33 +350,43 @@ function getFirstWordAfterFrom(text) {
|
|||
return match[1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function addVnInFom(query) {
|
||||
return query.replace(/(FROM)\s/g, '$1 vn.');
|
||||
}
|
||||
|
||||
function addVnInJoin(query) {
|
||||
return query.replace(/(JOIN)\s/g, '$1 vn.');
|
||||
}
|
||||
|
||||
// function addVnInFunctions(query) {
|
||||
// return query.replace(/(firstDayOfWeek|lastDayOfWeek)\s*\(\s*(\w+)\s*\)/g, function(match, functionName, argument) {
|
||||
// if (!/\./.test(argument)) {
|
||||
// return `vn.${functionName}(${argument})`;
|
||||
// }
|
||||
// return match;
|
||||
// });
|
||||
// }
|
||||
|
||||
function addVn(query) {
|
||||
return query.replace(/(FROM|JOIN)\s+(?!\w+\.)(\w+)/g, '$1 vn.$2');
|
||||
}
|
||||
|
||||
function addVnInFunctions(str){
|
||||
let words = str.split(" ");
|
||||
|
||||
for (let i = 0; i < words.length; i++) {
|
||||
if (words[i].startsWith("firstDayOfWeek") || words[i].startsWith("lastDayOfWeek")) {
|
||||
words[i] = "vn." + words[i];
|
||||
}
|
||||
}
|
||||
|
||||
str = words.join(" ");
|
||||
return str;
|
||||
}
|
||||
|
||||
function errorFn() {
|
||||
throw 'hola'
|
||||
}
|
||||
async function queryAndGrant(sql){
|
||||
console.log(sql);
|
||||
let entry;
|
||||
try{
|
||||
await conGrafana.query(sql);
|
||||
console.log('ENTRY ', new Date())
|
||||
entry = new Date().getTime();
|
||||
//const timeout = setTimeout(errorFn, 1000)
|
||||
const value = await conGrafana.query({
|
||||
sql: sql,
|
||||
timeout: 1000 // 1 second
|
||||
});
|
||||
//clearTimeout(timeout)
|
||||
console.log('VALUE ', value, ' ', new Date().getTime() - entry, 's')
|
||||
console.log('STOP ', new Date())
|
||||
|
||||
} catch (err) {
|
||||
if(err.code == 'ER_TABLEACCESS_DENIED_ERROR') {
|
||||
const table = getLastWord(err.sqlMessage); // p.e: `account`.`user`
|
||||
|
@ -411,7 +395,7 @@ async function queryAndGrant(sql){
|
|||
const grant = `GRANT SELECT ON ${table} TO '${user}'@'%';`;
|
||||
console.log(grant);
|
||||
try{
|
||||
await conVicent.query(grant)
|
||||
await conVicent.query(grant);
|
||||
} catch (err){
|
||||
// console.log(err);
|
||||
if(err.code != 'ER_NO_SUCH_TABLE') await queryAndGrant(sql);
|
||||
|
@ -425,37 +409,29 @@ async function queryAndGrant(sql){
|
|||
const grant = `GRANT EXECUTE ON FUNCTION ${test} TO '${user}'@'%';`;
|
||||
console.log(grant);
|
||||
try{
|
||||
await conVicent.query(grant)
|
||||
await conVicent.query(grant);
|
||||
} catch (err){
|
||||
// console.log(err);
|
||||
await queryAndGrant(sql)
|
||||
await queryAndGrant(sql);
|
||||
}
|
||||
} else if(err.code == 'ER_NO_DB_ERROR'){
|
||||
console.log(err);
|
||||
sql = addVn(sql)
|
||||
await queryAndGrant(sql);
|
||||
if (!err.sql.includes("users")) {
|
||||
sql = addVn(sql);
|
||||
await queryAndGrant(sql);
|
||||
}
|
||||
} else if(err.code == 'ER_SP_DOES_NOT_EXIST'){
|
||||
console.log(err);
|
||||
const sqlComplete = addVnInFunctions(sql);
|
||||
await queryAndGrant(sqlComplete);
|
||||
} else if(err.code == 'PROTOCOL_SEQUENCE_TIMEOUT'){
|
||||
console.log('NEXT POR TIMEOUT', new Date().getTime() - entry, 's')
|
||||
} else {
|
||||
console.log(err);
|
||||
throw 'ELSE ' + err;
|
||||
}
|
||||
}
|
||||
|
||||
function addVnInFunctions(str){
|
||||
let words = str.split(" ");
|
||||
|
||||
for (let i = 0; i < words.length; i++) {
|
||||
if (words[i].startsWith("firstDayOfWeek") || words[i].startsWith("lastDayOfWeek")) {
|
||||
words[i] = "vn." + words[i];
|
||||
}
|
||||
}
|
||||
|
||||
str = words.join(" ");
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -6,6 +6,7 @@
|
|||
"license": "GPL-3.0",
|
||||
"dependencies": {
|
||||
"colors": "^1.4.0",
|
||||
"eslint-config-google": "^0.14.0",
|
||||
"getopts": "^2.3.0",
|
||||
"mysql": "^2.18.1",
|
||||
"mysql2": "^3.1.0",
|
||||
|
@ -16,5 +17,8 @@
|
|||
"grafana-find": "main.js",
|
||||
"gfind": "main.js"
|
||||
},
|
||||
"main": "main.js"
|
||||
"main": "main.js",
|
||||
"devDependencies": {
|
||||
"eslint": "^8.33.0"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue