Browse Source

it works

pull/1/head
rozetko 6 years ago
parent
commit
3d4fa76c23
  1. 3
      .gitignore
  2. 21
      src/grafana-datasource-kit/grafana_service.ts
  3. 7
      src/routes/tasks.ts
  4. 39
      src/target.ts

3
.gitignore vendored

@ -1,6 +1,5 @@
node_modules/ node_modules/
dist/ dist/
exported/*.csv exported/
exported/*.json
api-keys.json api-keys.json
package-lock.json package-lock.json

21
src/grafana-datasource-kit/grafana_service.ts

@ -13,7 +13,7 @@ const CHUNK_SIZE = 50000;
*/ */
export async function queryByMetric( export async function queryByMetric(
metric: GrafanaMetric, panelUrl: string, from: number, to: number metric: GrafanaMetric, panelUrl: string, from: number, to: number
): Promise<[number, number][]> { ) {
let datasource = metric.datasource; let datasource = metric.datasource;
@ -21,14 +21,20 @@ export async function queryByMetric(
let url = `${origin}/${datasource.url}`; let url = `${origin}/${datasource.url}`;
let params = datasource.params let params = datasource.params
let data = []; let data = {
values: [],
columns: []
};
let chunkParams = Object.assign({}, params); let chunkParams = Object.assign({}, params);
while(true) { while(true) {
chunkParams.q = metric.metricQuery.getQuery(from, to, CHUNK_SIZE, data.length); chunkParams.q = metric.metricQuery.getQuery(from, to, CHUNK_SIZE, data.values.length);
var chunk = await queryGrafana(url, chunkParams); var chunk = await queryGrafana(url, chunkParams);
data = data.concat(chunk); let values = chunk.values;
if(chunk.length < CHUNK_SIZE) { data.values = data.values.concat(values);
data.columns = chunk.columns;
if(values.length < CHUNK_SIZE) {
// because if we get less that we could, then there is nothing more // because if we get less that we could, then there is nothing more
break; break;
} }
@ -38,7 +44,8 @@ export async function queryByMetric(
} }
async function queryGrafana(url: string, params: any) { async function queryGrafana(url: string, params: any) {
let headers = { Authorization: `Bearer ${getApiKey(url)}` }; let origin = new URL(url).origin;
let headers = { Authorization: `Bearer ${getApiKey(origin)}` };
try { try {
var res = await axios.get(url, { params, headers }); var res = await axios.get(url, { params, headers });
@ -59,5 +66,5 @@ async function queryGrafana(url: string, params: any) {
return []; return [];
} }
return results.series[0].values as [number, number][]; return results.series[0];
} }

7
src/routes/tasks.ts

@ -7,6 +7,10 @@ async function addTask(req, res) {
let body = req.body; let body = req.body;
let from = parseInt(body.from); let from = parseInt(body.from);
let to = parseInt(body.to); let to = parseInt(body.to);
let panelUrl = body.panelUrl;
let targets = [body.target];
let datasource = body.datasourceRequest;
let user = body.user;
if(isNaN(from) || isNaN(to)) { if(isNaN(from) || isNaN(to)) {
res.status(500).send('Range error: please fill both "from" and "to" fields'); res.status(500).send('Range error: please fill both "from" and "to" fields');
@ -14,8 +18,7 @@ async function addTask(req, res) {
res.status(500).send('Range error: "from" should be less than "to"'); res.status(500).send('Range error: "from" should be less than "to"');
} else { } else {
res.status(200).send('Task added'); res.status(200).send('Task added');
let grafanaUrl = req.get('origin'); let target = new Target(panelUrl, user, datasource, targets, from, to);
let target = new Target(grafanaUrl, body.user, body.datasource, body.measurement, body.query, from, to);
target.export(); target.export();
} }
} }

39
src/target.ts

@ -1,5 +1,5 @@
import { queryByMetric } from './grafana-datasource-kit/grafana_service'; import { queryByMetric } from './grafana-datasource-kit/grafana_service';
import { GrafanaDatasource } from './grafana-datasource-kit/grafana_metric_model'; import { GrafanaDatasource, GrafanaMetric } from './grafana-datasource-kit/grafana_metric_model';
import * as csv from 'fast-csv'; import * as csv from 'fast-csv';
import * as path from 'path'; import * as path from 'path';
@ -14,26 +14,17 @@ export class Target {
private days: number; private days: number;
private day: number; private day: number;
private csvStream: any; private csvStream: any;
private _datasource: GrafanaDatasource; private metric: GrafanaMetric;
constructor( constructor(
panelUrl: string, private panelUrl: string,
private user: string, private user: string,
datasource: string, datasource: GrafanaDatasource,
private measurement: string, targets: Array<Object>,
private query: string,
private from: number, private from: number,
private to: number private to: number
) { ) {
this._datasource = { this.metric = new GrafanaMetric(datasource, targets);
url: panelUrl,
type: type,
params: {
db: string,
q: string,
epoch: string
}
}
} }
public updateStatus(status) { public updateStatus(status) {
@ -41,8 +32,6 @@ export class Target {
let data = { let data = {
time, time,
user: this.user, user: this.user,
datasource: this.datasource,
measurement: this.measurement,
exportedRows: this.exportedRows, exportedRows: this.exportedRows,
progress: (this.day / this.days).toLocaleString('en', { style: 'percent' }), progress: (this.day / this.days).toLocaleString('en', { style: 'percent' }),
status status
@ -75,11 +64,9 @@ export class Target {
console.log(`${this.day} day: ${from}ms -> ${to}ms`); console.log(`${this.day} day: ${from}ms -> ${to}ms`);
let currentQuery = this.query.replace('$timeFilter', `time >= ${from}ms AND time <= ${to}ms`).replace('$__interval', '1s'); let metrics = await queryByMetric(this.metric, this.panelUrl, from, to);
let metrics = await queryByMetric(this.datasource, currentQuery);
console.log(metrics); if(metrics.values.length > 0) {
if(metrics.length > 0) {
if(metrics !== undefined) { if(metrics !== undefined) {
this.writeCsv(metrics); this.writeCsv(metrics);
} }
@ -103,22 +90,20 @@ export class Target {
} }
private writeCsv(series) { private writeCsv(series) {
for(let serie of series) { for(let val of series.values) {
for(let val of serie.values) {
if(val[1] !== null) { if(val[1] !== null) {
let row = {}; let row = {};
for(let col in serie.columns) { for(let col in series.columns) {
row[serie.columns[col]] = val[col]; row[series.columns[col]] = val[col];
} }
this.csvStream.write(row); this.csvStream.write(row);
this.exportedRows++; this.exportedRows++;
} }
} }
} }
}
private getFilename(extension) { private getFilename(extension) {
return `${this.datasource}.${this.measurement}.${this.from}-${this.to}.${extension}`; return `${this.from}-${this.to}.${extension}`;
} }
private getFilePath(extension) { private getFilePath(extension) {

Loading…
Cancel
Save