From fbf6c4949f0af5b5aa1d5dd91b2c1cbeb24c43f8 Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 12 Aug 2022 19:39:00 +0300 Subject: [PATCH] upd types --- src/index.ts | 1 + src/metrics/metric.ts | 5 +++-- src/metrics/prometheus_metric.ts | 13 ++++++------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7bc1800..3a6ed8a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ export { Metric } from './metrics/metrics_factory'; export { Datasource } from './metrics/metric' +// TODO: move queryByMetric from Grafana service export { queryByMetric, GrafanaUnavailable, DatasourceUnavailable } from './grafana_service'; diff --git a/src/metrics/metric.ts b/src/metrics/metric.ts index 9df4b8b..f5bdd5e 100644 --- a/src/metrics/metric.ts +++ b/src/metrics/metric.ts @@ -1,5 +1,6 @@ export declare type Datasource = { url: string; + // TODO: type: enum type: string; params?: { db: string; @@ -18,8 +19,8 @@ export type MetricQuery = { } export type MetricResults = { - values: any; - columns: any; + values: (number | null)[][]; + columns: string[]; } export type MetricId = string; diff --git a/src/metrics/prometheus_metric.ts b/src/metrics/prometheus_metric.ts index dd32488..bea8dbd 100644 --- a/src/metrics/prometheus_metric.ts +++ b/src/metrics/prometheus_metric.ts @@ -36,13 +36,13 @@ export class PrometheusMetric extends AbstractMetric { } let result = res.data.data.result; - let result_matrix = { + let result_matrix: MetricResults = { columns: ['timestamp'], values: [] }; result.map(r => { - let keys = []; + let keys: string[] = []; for(let key in r.metric) { keys.push(`${key}=${r.metric[key]}`); } @@ -51,8 +51,8 @@ export class PrometheusMetric extends AbstractMetric { let values = result.map(r => r.values); - let timestamps = []; - values.map(v => v.map(row => timestamps.push(row[0]))); + let timestamps: (number | null)[] = []; + values.forEach(v => v.forEach((row: number[]) => timestamps.push(row[0]))); timestamps = timestamps.filter(function(item, i, ar) { return ar.indexOf(item) === i; //uniq values }); @@ -70,12 +70,11 @@ export class PrometheusMetric extends AbstractMetric { if(currentTimestamp === t) { row.push(+currentValue); v.shift(); - } - else { + } else { 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); }; return result_matrix;