From a01567f24a7ec64ff8139e42f4e8bea783cf8a15 Mon Sep 17 00:00:00 2001 From: vargburz Date: Tue, 3 May 2022 20:16:18 +0300 Subject: [PATCH] add models to parse data and options --- src/components/Panel.tsx | 50 +++++++++------------------------------- src/models/options.ts | 27 ++++++++++++++++++++++ src/models/series.ts | 34 +++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 39 deletions(-) create mode 100644 src/models/options.ts create mode 100644 src/models/series.ts diff --git a/src/components/Panel.tsx b/src/components/Panel.tsx index 101c7bf..f796534 100644 --- a/src/components/Panel.tsx +++ b/src/components/Panel.tsx @@ -1,4 +1,6 @@ import { DataProcessor } from '../grafana/data_processor'; +import { Options } from '../models/options'; +import { Series } from '../models/series'; import { PanelOptions } from '../types'; @@ -14,40 +16,21 @@ interface Props extends PanelProps {} export function Panel({ options, data, width, height, timeZone, timeRange, onChangeTimeRange }: Props) { console.log('options', options); - console.log('data', data); - const processor = new DataProcessor({}); - const seriesList = processor.getSeriesList({ - dataList: data.series, - range: timeRange, - }); - const chwSeriesList = updateSeriesListWithChartwerkParams(seriesList); - console.log('seriesList', chwSeriesList); - let chartContainer = useRef(null); - // TODO: use models to parse options and series + const series = new Series(data, timeRange).getChartwerkSeries(); + console.log('series', series); + const chartwerkOptions = new Options(options).getChartwerkOptions(); + let chartContainer = useRef(null); // we request animation frame here because at the moment we need an existing DOM-element at the moment we render the pod window.requestAnimationFrame(() => { // TODO: pass datapoints // TODO: pass options // TODO: switch / case pod type - const pod = new ChartwerkGaugePod((chartContainer as any).current, chwSeriesList, { - maxValue: options.gauge.max.value || 0, - minValue: options.gauge.min.value || 0, - valueFormatter: (val) => 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 }], - }); + const pod = new ChartwerkGaugePod( + (chartContainer as any).current, + series, + chartwerkOptions + ); pod.render(); }); return ( @@ -60,14 +43,3 @@ export function Panel({ options, data, width, height, timeZone, timeRange, onCha > ); } - -function 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, - }; - }); -} diff --git a/src/models/options.ts b/src/models/options.ts new file mode 100644 index 0000000..01d32d3 --- /dev/null +++ b/src/models/options.ts @@ -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 }], + }; + } +} diff --git a/src/models/series.ts b/src/models/series.ts new file mode 100644 index 0000000..cf83f5f --- /dev/null +++ b/src/models/series.ts @@ -0,0 +1,34 @@ +import { PanelData, TimeRange } from '@grafana/data'; +import { DataProcessor } from '../grafana/data_processor'; + +import * as _ from 'lodash'; + +// Convert Grafana options into Chartwerk options +export class Series { + private processor; + private _seriesList; + + constructor(private grafanaData: PanelData, timeRange: TimeRange) { + this.processor = new DataProcessor({}); + const seriesList = this.processor.getSeriesList({ + dataList: grafanaData.series, + range: timeRange, + }); + this._seriesList = this._updateSeriesListWithChartwerkParams(seriesList); + } + + 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, + }; + }); + } +}