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.

60 lines
2.0 KiB

import { PanelData, TimeRange } from '@grafana/data';
import { DataProcessor } from '../grafana/data_processor';
import { ExtremumOptions } from 'types';
import * as _ from 'lodash';
// Convert Grafana options into Chartwerk options
export class Series {
private processor;
private _seriesList;
private _selectedSerieName;
constructor(grafanaData: PanelData, timeRange: TimeRange, private gaugeValueOptions: ExtremumOptions) {
if(this._isSerieOneValue()) {
this._seriesList = [{ datapoints: [[0, gaugeValueOptions.value]] }];
return;
}
if(this.gaugeValueOptions.useMetric && _.isEmpty(this.gaugeValueOptions.metricName)) {
throw new Error(`Value or metric is not selected.`);
}
this._selectedSerieName = this.gaugeValueOptions.metricName;
this.processor = new DataProcessor({});
const series = grafanaData.series;
const seriesList = this.processor.getSeriesList({
dataList: series,
range: timeRange,
});
console.log('_seriesList', seriesList);
const filteredSeries = _.filter(seriesList, serie => serie.alias === this._selectedSerieName);
if(filteredSeries.length === 0) {
throw new Error(`Can't find metric for ${this._selectedSerieName} name`);
}
if(filteredSeries.length > 1) {
throw new Error(`Get ${filteredSeries.length} metrics for ${this._selectedSerieName} name. Please choose one`);
}
console.log('filteredSeries', filteredSeries);
this._seriesList = this._updateSeriesListWithChartwerkParams(filteredSeries);
}
public getChartwerkSeries(): any[] {
return this._seriesList;
}
private _updateSeriesListWithChartwerkParams(series: any[]): any[] {
return _.map(series, (serie: any, idx: number) => {
return {
target: serie.alias,
color: serie.color,
datapoints: _.map(serie.datapoints, (row) => _.reverse(row)),
alias: serie.label,
};
});
}
private _isSerieOneValue(): boolean {
return !this.gaugeValueOptions.useMetric;
}
}