Commit 5324bb4f by Ryan McKinley Committed by Torkel Ödegaard

Refactor: Rename TimeSeriesVM to GraphSeriesXY (#16216)

* Rename TimeSeriesVM to Trace

* remove TimeSeriesVM from types

* rename to GraphSeriesVM

* rename again

* now GraphSeriesXY
parent 04b3afcd
...@@ -3,11 +3,11 @@ import $ from 'jquery'; ...@@ -3,11 +3,11 @@ import $ from 'jquery';
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
// Types // Types
import { TimeRange, TimeSeriesVMs } from '../../types'; import { TimeRange, GraphSeriesXY } from '../../types';
interface GraphProps { interface GraphProps {
timeSeries: TimeSeriesVMs; series: GraphSeriesXY[];
timeRange: TimeRange; timeRange: TimeRange; // NOTE: we should aim to make `time` a property of the axis, not force it for all graphs
showLines?: boolean; showLines?: boolean;
showPoints?: boolean; showPoints?: boolean;
showBars?: boolean; showBars?: boolean;
...@@ -37,7 +37,7 @@ export class Graph extends PureComponent<GraphProps> { ...@@ -37,7 +37,7 @@ export class Graph extends PureComponent<GraphProps> {
return; return;
} }
const { width, timeSeries, timeRange, showLines, showBars, showPoints } = this.props; const { width, series, timeRange, showLines, showBars, showPoints } = this.props;
if (!width) { if (!width) {
return; return;
...@@ -95,9 +95,9 @@ export class Graph extends PureComponent<GraphProps> { ...@@ -95,9 +95,9 @@ export class Graph extends PureComponent<GraphProps> {
try { try {
console.log('Graph render'); console.log('Graph render');
$.plot(this.element, timeSeries, flotOptions); $.plot(this.element, series, flotOptions);
} catch (err) { } catch (err) {
console.log('Graph rendering error', err, flotOptions, timeSeries); console.log('Graph rendering error', err, flotOptions, series);
throw new Error('Error rendering panel'); throw new Error('Error rendering panel');
} }
} }
......
...@@ -53,24 +53,12 @@ export interface TimeSeries { ...@@ -53,24 +53,12 @@ export interface TimeSeries {
unit?: string; unit?: string;
} }
/** View model projection of a time series */
export interface TimeSeriesVM {
label: string;
color: string;
data: TimeSeriesValue[][];
allIsNull: boolean;
allIsZero: boolean;
}
export enum NullValueMode { export enum NullValueMode {
Null = 'null', Null = 'null',
Ignore = 'connected', Ignore = 'connected',
AsZero = 'null as zero', AsZero = 'null as zero',
} }
/** View model projection of many time series */
export type TimeSeriesVMs = TimeSeriesVM[];
export interface AnnotationEvent { export interface AnnotationEvent {
annotation?: any; annotation?: any;
dashboardId?: number; dashboardId?: number;
......
import { DisplayValue } from './displayValue';
export type GraphSeriesValue = number | null;
/** View model projection of a series */
export interface GraphSeriesXY {
label: string;
color: string;
data: GraphSeriesValue[][]; // [x,y][]
info?: DisplayValue[]; // Legend info
}
...@@ -4,6 +4,7 @@ export * from './panel'; ...@@ -4,6 +4,7 @@ export * from './panel';
export * from './plugin'; export * from './plugin';
export * from './datasource'; export * from './datasource';
export * from './theme'; export * from './theme';
export * from './graph';
export * from './threshold'; export * from './threshold';
export * from './input'; export * from './input';
export * from './displayValue'; export * from './displayValue';
import { getFlotPairs } from './flotPairs'; import { getFlotPairs } from './flotPairs';
describe('getFlotPairs', () => { describe('getFlotPairs', () => {
const table = { const series = {
fields: [],
rows: [[1, 100, 'a'], [2, 200, 'b'], [3, 300, 'c']], rows: [[1, 100, 'a'], [2, 200, 'b'], [3, 300, 'c']],
}; };
it('should get X and y', () => { it('should get X and y', () => {
const pairs = getFlotPairs({ rows: table.rows, xIndex: 0, yIndex: 1 }); const pairs = getFlotPairs({ series, xIndex: 0, yIndex: 1 });
expect(pairs.length).toEqual(3); expect(pairs.length).toEqual(3);
expect(pairs[0].length).toEqual(2); expect(pairs[0].length).toEqual(2);
...@@ -14,7 +15,7 @@ describe('getFlotPairs', () => { ...@@ -14,7 +15,7 @@ describe('getFlotPairs', () => {
}); });
it('should work with strings', () => { it('should work with strings', () => {
const pairs = getFlotPairs({ rows: table.rows, xIndex: 0, yIndex: 2 }); const pairs = getFlotPairs({ series, xIndex: 0, yIndex: 2 });
expect(pairs.length).toEqual(3); expect(pairs.length).toEqual(3);
expect(pairs[0].length).toEqual(2); expect(pairs[0].length).toEqual(2);
......
// Types // Types
import { NullValueMode } from '../types/index'; import { NullValueMode, GraphSeriesValue, SeriesData } from '../types/index';
export interface FloatPairsOptions { export interface FlotPairsOptions {
rows: any[][]; series: SeriesData;
xIndex: number; xIndex: number;
yIndex: number; yIndex: number;
nullValueMode?: NullValueMode; nullValueMode?: NullValueMode;
} }
export function getFlotPairs({ rows, xIndex, yIndex, nullValueMode }: FloatPairsOptions): any[][] { export function getFlotPairs({ series, xIndex, yIndex, nullValueMode }: FlotPairsOptions): GraphSeriesValue[][] {
const rows = series.rows;
const ignoreNulls = nullValueMode === NullValueMode.Ignore; const ignoreNulls = nullValueMode === NullValueMode.Ignore;
const nullAsZero = nullValueMode === NullValueMode.AsZero; const nullAsZero = nullValueMode === NullValueMode.AsZero;
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import _ from 'lodash'; import _ from 'lodash';
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { Graph, PanelProps, NullValueMode, colors, TimeSeriesVMs, FieldType, getFirstTimeField } from '@grafana/ui'; import { Graph, PanelProps, NullValueMode, colors, GraphSeriesXY, FieldType, getFirstTimeField } from '@grafana/ui';
import { Options } from './types'; import { Options } from './types';
import { getFlotPairs } from '@grafana/ui/src/utils/flotPairs'; import { getFlotPairs } from '@grafana/ui/src/utils/flotPairs';
...@@ -13,42 +13,40 @@ export class GraphPanel extends PureComponent<Props> { ...@@ -13,42 +13,40 @@ export class GraphPanel extends PureComponent<Props> {
const { data, timeRange, width, height } = this.props; const { data, timeRange, width, height } = this.props;
const { showLines, showBars, showPoints } = this.props.options; const { showLines, showBars, showPoints } = this.props.options;
const vmSeries: TimeSeriesVMs = []; const graphs: GraphSeriesXY[] = [];
for (const table of data) { for (const series of data) {
const timeColumn = getFirstTimeField(table); const timeColumn = getFirstTimeField(series);
if (timeColumn < 0) { if (timeColumn < 0) {
continue; continue;
} }
for (let i = 0; i < table.fields.length; i++) { for (let i = 0; i < series.fields.length; i++) {
const column = table.fields[i]; const field = series.fields[i];
// Show all numeric columns // Show all numeric columns
if (column.type === FieldType.number) { if (field.type === FieldType.number) {
// Use external calculator just to make sure it works :) // Use external calculator just to make sure it works :)
const points = getFlotPairs({ const points = getFlotPairs({
rows: table.rows, series,
xIndex: timeColumn, xIndex: timeColumn,
yIndex: i, yIndex: i,
nullValueMode: NullValueMode.Null, nullValueMode: NullValueMode.Null,
}); });
vmSeries.push({ if (points.length > 0) {
label: column.name, graphs.push({
data: points, label: field.name,
color: colors[vmSeries.length % colors.length], data: points,
color: colors[graphs.length % colors.length],
// TODO (calculate somewhere) });
allIsNull: false, }
allIsZero: false,
});
} }
} }
} }
return ( return (
<Graph <Graph
timeSeries={vmSeries} series={graphs}
timeRange={timeRange} timeRange={timeRange}
showLines={showLines} showLines={showLines}
showPoints={showPoints} showPoints={showPoints}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment