You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.2 KiB

import { Aggregation } from './types';
import * as _ from 'lodash';
export function filterMetricListByAlias(list: any[], alias: string | undefined, option: string): any[] {
3 years ago
const filteredSeries = _.filter(list, (serie) => serie.alias === alias);
if (filteredSeries.length === 0) {
throw new Error(`${option}: Can't find metric for ${alias} name.`);
}
3 years ago
if (filteredSeries.length > 1) {
throw new Error(`${option}: Get ${filteredSeries.length} metrics for ${alias} name. Please choose one.`);
}
return filteredSeries;
}
3 years ago
export function getAggregatedValueFromSerie(
serie: any,
aggregation = Aggregation.LAST,
valueIdx: 0 | 1 = 0
): number | null {
// series types { datapoints: [number, number][]}
// valueIdx === 0 for Grafana series, valueIdx === 1 for Chartwerk series
3 years ago
if (serie === undefined) {
return null;
}
3 years ago
if (serie.datapoints.length === 0) {
return null;
}
3 years ago
switch (aggregation) {
case Aggregation.LAST:
3 years ago
const lastRow = _.last(serie.datapoints as Array<[number, number]>);
// @ts-ignore
return !_.isEmpty(lastRow) ? lastRow[valueIdx] : null;
default:
3 years ago
throw new Error(`Unknown aggregation type: ${aggregation}`);
}
}