Browse Source

upd types

pull/1/head
rozetko 2 years ago
parent
commit
fbf6c4949f
  1. 1
      src/index.ts
  2. 5
      src/metrics/metric.ts
  3. 13
      src/metrics/prometheus_metric.ts

1
src/index.ts

@ -1,3 +1,4 @@
export { Metric } from './metrics/metrics_factory'; export { Metric } from './metrics/metrics_factory';
export { Datasource } from './metrics/metric' export { Datasource } from './metrics/metric'
// TODO: move queryByMetric from Grafana service
export { queryByMetric, GrafanaUnavailable, DatasourceUnavailable } from './grafana_service'; export { queryByMetric, GrafanaUnavailable, DatasourceUnavailable } from './grafana_service';

5
src/metrics/metric.ts

@ -1,5 +1,6 @@
export declare type Datasource = { export declare type Datasource = {
url: string; url: string;
// TODO: type: enum
type: string; type: string;
params?: { params?: {
db: string; db: string;
@ -18,8 +19,8 @@ export type MetricQuery = {
} }
export type MetricResults = { export type MetricResults = {
values: any; values: (number | null)[][];
columns: any; columns: string[];
} }
export type MetricId = string; export type MetricId = string;

13
src/metrics/prometheus_metric.ts

@ -36,13 +36,13 @@ export class PrometheusMetric extends AbstractMetric {
} }
let result = res.data.data.result; let result = res.data.data.result;
let result_matrix = { let result_matrix: MetricResults = {
columns: ['timestamp'], columns: ['timestamp'],
values: [] values: []
}; };
result.map(r => { result.map(r => {
let keys = []; let keys: string[] = [];
for(let key in r.metric) { for(let key in r.metric) {
keys.push(`${key}=${r.metric[key]}`); keys.push(`${key}=${r.metric[key]}`);
} }
@ -51,8 +51,8 @@ export class PrometheusMetric extends AbstractMetric {
let values = result.map(r => r.values); let values = result.map(r => r.values);
let timestamps = []; let timestamps: (number | null)[] = [];
values.map(v => v.map(row => timestamps.push(row[0]))); values.forEach(v => v.forEach((row: number[]) => timestamps.push(row[0])));
timestamps = timestamps.filter(function(item, i, ar) { timestamps = timestamps.filter(function(item, i, ar) {
return ar.indexOf(item) === i; //uniq values return ar.indexOf(item) === i; //uniq values
}); });
@ -70,12 +70,11 @@ export class PrometheusMetric extends AbstractMetric {
if(currentTimestamp === t) { if(currentTimestamp === t) {
row.push(+currentValue); row.push(+currentValue);
v.shift(); v.shift();
} } else {
else {
row.push(null); row.push(null);
} }
}); });
row[0] = +row[0] * 1000; //convert timestamp to ms row[0] = +(row[0] as number) * 1000; //convert timestamp to ms
result_matrix.values.push(row); result_matrix.values.push(row);
}; };
return result_matrix; return result_matrix;

Loading…
Cancel
Save