Browse Source
Models to parse data See merge request chartwerk/grafana-chartwerk-panel!3merge-requests/4/merge
Alexander Velikiy
3 years ago
5 changed files with 136 additions and 59 deletions
@ -0,0 +1,27 @@
|
||||
import { PanelOptions } from 'types'; |
||||
|
||||
// Convert Grafana options into Chartwerk options
|
||||
export class Options { |
||||
constructor(private grafanaOptions: PanelOptions) {} |
||||
|
||||
public getChartwerkOptions(): any { |
||||
return { |
||||
maxValue: this.grafanaOptions.gauge.max.value || 0, |
||||
minValue: this.grafanaOptions.gauge.min.value || 0, |
||||
valueFormatter: (val: any) => val.toFixed(2), |
||||
defaultColor: 'green', |
||||
stops: [ |
||||
{ |
||||
color: 'green', |
||||
value: 100, |
||||
}, |
||||
{ |
||||
color: 'orange', |
||||
value: 140, |
||||
}, |
||||
], |
||||
// @ts-ignore
|
||||
icons: [{ src: 'https://cityhost.ua/upload_img/blog5ef308ea5529c_trash2-01.jpg', position: 'middle', size: 30 }], |
||||
}; |
||||
} |
||||
} |
@ -0,0 +1,59 @@
|
||||
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 seriesList = this.processor.getSeriesList({ |
||||
dataList: grafanaData.series, |
||||
range: timeRange, |
||||
}); |
||||
|
||||
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`); |
||||
} |
||||
|
||||
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; |
||||
} |
||||
} |
Loading…
Reference in new issue