Browse Source

add models to parse data and options

merge-requests/3/head
vargburz 3 years ago
parent
commit
a01567f24a
  1. 50
      src/components/Panel.tsx
  2. 27
      src/models/options.ts
  3. 34
      src/models/series.ts

50
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<PanelOptions> {}
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
></div>
);
}
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,
};
});
}

27
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 }],
};
}
}

34
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,
};
});
}
}
Loading…
Cancel
Save