mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
refactor(data models): Renamed TableData to SeriesData (#16185)
This commit is contained in:
committed by
Torkel Ödegaard
parent
c7d108264d
commit
77b3da3e8b
@@ -9,6 +9,7 @@ interface MutableColumn extends Column {
|
||||
title?: string;
|
||||
sort?: boolean;
|
||||
desc?: boolean;
|
||||
type?: string;
|
||||
}
|
||||
|
||||
export default class TableModel implements TableData {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Library
|
||||
import React from 'react';
|
||||
|
||||
import { DataPanel, getProcessedTableData } from './DataPanel';
|
||||
import { DataPanel, getProcessedSeriesData } from './DataPanel';
|
||||
|
||||
describe('DataPanel', () => {
|
||||
let dataPanel: DataPanel;
|
||||
@@ -34,27 +34,27 @@ describe('DataPanel', () => {
|
||||
target: '',
|
||||
datapoints: [[100, 1], [200, 2]],
|
||||
};
|
||||
const data = getProcessedTableData([null, input1, input2, null, null]);
|
||||
const data = getProcessedSeriesData([null, input1, input2, null, null]);
|
||||
expect(data.length).toBe(2);
|
||||
expect(data[0].columns[0].text).toBe(input1.target);
|
||||
expect(data[0].fields[0].name).toBe(input1.target);
|
||||
expect(data[0].rows).toBe(input1.datapoints);
|
||||
|
||||
// Default name
|
||||
expect(data[1].columns[0].text).toEqual('Value');
|
||||
expect(data[1].fields[0].name).toEqual('Value');
|
||||
|
||||
// Every colun should have a name and a type
|
||||
for (const table of data) {
|
||||
for (const column of table.columns) {
|
||||
expect(column.text).toBeDefined();
|
||||
for (const column of table.fields) {
|
||||
expect(column.name).toBeDefined();
|
||||
expect(column.type).toBeDefined();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('supports null values from query OK', () => {
|
||||
expect(getProcessedTableData([null, null, null, null])).toEqual([]);
|
||||
expect(getProcessedTableData(undefined)).toEqual([]);
|
||||
expect(getProcessedTableData((null as unknown) as any[])).toEqual([]);
|
||||
expect(getProcessedTableData([])).toEqual([]);
|
||||
expect(getProcessedSeriesData([null, null, null, null])).toEqual([]);
|
||||
expect(getProcessedSeriesData(undefined)).toEqual([]);
|
||||
expect(getProcessedSeriesData((null as unknown) as any[])).toEqual([]);
|
||||
expect(getProcessedSeriesData([])).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,16 +11,16 @@ import {
|
||||
DataQueryResponse,
|
||||
DataQueryError,
|
||||
LoadingState,
|
||||
TableData,
|
||||
SeriesData,
|
||||
TimeRange,
|
||||
ScopedVars,
|
||||
toTableData,
|
||||
guessColumnTypes,
|
||||
toSeriesData,
|
||||
guessFieldTypes,
|
||||
} from '@grafana/ui';
|
||||
|
||||
interface RenderProps {
|
||||
loading: LoadingState;
|
||||
data: TableData[];
|
||||
data: SeriesData[];
|
||||
}
|
||||
|
||||
export interface Props {
|
||||
@@ -44,7 +44,7 @@ export interface State {
|
||||
isFirstLoad: boolean;
|
||||
loading: LoadingState;
|
||||
response: DataQueryResponse;
|
||||
data?: TableData[];
|
||||
data?: SeriesData[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,18 +52,18 @@ export interface State {
|
||||
*
|
||||
* This is also used by PanelChrome for snapshot support
|
||||
*/
|
||||
export function getProcessedTableData(results?: any[]): TableData[] {
|
||||
export function getProcessedSeriesData(results?: any[]): SeriesData[] {
|
||||
if (!results) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const tables: TableData[] = [];
|
||||
const series: SeriesData[] = [];
|
||||
for (const r of results) {
|
||||
if (r) {
|
||||
tables.push(guessColumnTypes(toTableData(r)));
|
||||
series.push(guessFieldTypes(toSeriesData(r)));
|
||||
}
|
||||
}
|
||||
return tables;
|
||||
return series;
|
||||
}
|
||||
|
||||
export class DataPanel extends Component<Props, State> {
|
||||
@@ -167,7 +167,7 @@ export class DataPanel extends Component<Props, State> {
|
||||
this.setState({
|
||||
loading: LoadingState.Done,
|
||||
response: resp,
|
||||
data: getProcessedTableData(resp.data),
|
||||
data: getProcessedSeriesData(resp.data),
|
||||
isFirstLoad: false,
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
@@ -19,12 +19,12 @@ import config from 'app/core/config';
|
||||
// Types
|
||||
import { DashboardModel, PanelModel } from '../state';
|
||||
import { PanelPlugin } from 'app/types';
|
||||
import { DataQueryResponse, TimeRange, LoadingState, TableData, DataQueryError } from '@grafana/ui';
|
||||
import { DataQueryResponse, TimeRange, LoadingState, DataQueryError, SeriesData } from '@grafana/ui';
|
||||
import { ScopedVars } from '@grafana/ui';
|
||||
|
||||
import templateSrv from 'app/features/templating/template_srv';
|
||||
|
||||
import { getProcessedTableData } from './DataPanel';
|
||||
import { getProcessedSeriesData } from './DataPanel';
|
||||
|
||||
const DEFAULT_PLUGIN_ERROR = 'Error in plugin';
|
||||
|
||||
@@ -141,10 +141,10 @@ export class PanelChrome extends PureComponent<Props, State> {
|
||||
}
|
||||
|
||||
get getDataForPanel() {
|
||||
return this.hasPanelSnapshot ? getProcessedTableData(this.props.panel.snapshotData) : null;
|
||||
return this.hasPanelSnapshot ? getProcessedSeriesData(this.props.panel.snapshotData) : null;
|
||||
}
|
||||
|
||||
renderPanelPlugin(loading: LoadingState, data: TableData[], width: number, height: number): JSX.Element {
|
||||
renderPanelPlugin(loading: LoadingState, data: SeriesData[], width: number, height: number): JSX.Element {
|
||||
const { panel, plugin } = this.props;
|
||||
const { timeRange, renderCounter } = this.state;
|
||||
const PanelComponent = plugin.exports.reactPanel.panel;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import _ from 'lodash';
|
||||
import TableModel from 'app/core/table_model';
|
||||
import { ColumnType } from '@grafana/ui';
|
||||
import { FieldType } from '@grafana/ui';
|
||||
|
||||
export default class InfluxSeries {
|
||||
series: any;
|
||||
@@ -157,7 +157,7 @@ export default class InfluxSeries {
|
||||
// Check that the first column is indeed 'time'
|
||||
if (series.columns[0] === 'time') {
|
||||
// Push this now before the tags and with the right type
|
||||
table.columns.push({ text: 'Time', type: ColumnType.time });
|
||||
table.columns.push({ text: 'Time', type: FieldType.time });
|
||||
j++;
|
||||
}
|
||||
_.each(_.keys(series.tags), key => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import _ from 'lodash';
|
||||
import TableModel from 'app/core/table_model';
|
||||
import { TimeSeries, ColumnType } from '@grafana/ui';
|
||||
import { TimeSeries, FieldType } from '@grafana/ui';
|
||||
|
||||
export class ResultTransformer {
|
||||
constructor(private templateSrv) {}
|
||||
@@ -98,7 +98,7 @@ export class ResultTransformer {
|
||||
|
||||
// Sort metric labels, create columns for them and record their index
|
||||
const sortedLabels = _.keys(metricLabels).sort();
|
||||
table.columns.push({ text: 'Time', type: ColumnType.time });
|
||||
table.columns.push({ text: 'Time', type: FieldType.time });
|
||||
_.each(sortedLabels, (label, labelIndex) => {
|
||||
metricLabels[label] = labelIndex + 1;
|
||||
table.columns.push({ text: label, filterable: true });
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import _ from 'lodash';
|
||||
import React, { PureComponent } from 'react';
|
||||
|
||||
import { Graph, PanelProps, NullValueMode, colors, TimeSeriesVMs, ColumnType, getFirstTimeColumn } from '@grafana/ui';
|
||||
import { Graph, PanelProps, NullValueMode, colors, TimeSeriesVMs, FieldType, getFirstTimeField } from '@grafana/ui';
|
||||
import { Options } from './types';
|
||||
import { getFlotPairs } from '@grafana/ui/src/utils/flotPairs';
|
||||
|
||||
@@ -15,16 +15,16 @@ export class GraphPanel extends PureComponent<Props> {
|
||||
|
||||
const vmSeries: TimeSeriesVMs = [];
|
||||
for (const table of data) {
|
||||
const timeColumn = getFirstTimeColumn(table);
|
||||
const timeColumn = getFirstTimeField(table);
|
||||
if (timeColumn < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (let i = 0; i < table.columns.length; i++) {
|
||||
const column = table.columns[i];
|
||||
for (let i = 0; i < table.fields.length; i++) {
|
||||
const column = table.fields[i];
|
||||
|
||||
// Show all numeric columns
|
||||
if (column.type === ColumnType.number) {
|
||||
if (column.type === FieldType.number) {
|
||||
// Use external calculator just to make sure it works :)
|
||||
const points = getFlotPairs({
|
||||
rows: table.rows,
|
||||
@@ -34,7 +34,7 @@ export class GraphPanel extends PureComponent<Props> {
|
||||
});
|
||||
|
||||
vmSeries.push({
|
||||
label: column.text,
|
||||
label: column.name,
|
||||
data: points,
|
||||
color: colors[vmSeries.length % colors.length],
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import React, { PureComponent, CSSProperties } from 'react';
|
||||
// Types
|
||||
import { SingleStatOptions, SingleStatBaseOptions } from './types';
|
||||
|
||||
import { DisplayValue, PanelProps, NullValueMode, ColumnType, calculateStats } from '@grafana/ui';
|
||||
import { DisplayValue, PanelProps, NullValueMode, FieldType, calculateStats } from '@grafana/ui';
|
||||
import { config } from 'app/core/config';
|
||||
import { getDisplayProcessor } from '@grafana/ui';
|
||||
import { ProcessedValuesRepeater } from './ProcessedValuesRepeater';
|
||||
@@ -26,19 +26,19 @@ export const getSingleStatValues = (props: PanelProps<SingleStatBaseOptions>): D
|
||||
|
||||
const values: DisplayValue[] = [];
|
||||
|
||||
for (const table of data) {
|
||||
for (const series of data) {
|
||||
if (stat === 'name') {
|
||||
values.push(display(table.name));
|
||||
values.push(display(series.name));
|
||||
}
|
||||
|
||||
for (let i = 0; i < table.columns.length; i++) {
|
||||
const column = table.columns[i];
|
||||
for (let i = 0; i < series.fields.length; i++) {
|
||||
const column = series.fields[i];
|
||||
|
||||
// Show all columns that are not 'time'
|
||||
if (column.type === ColumnType.number) {
|
||||
if (column.type === FieldType.number) {
|
||||
const stats = calculateStats({
|
||||
table,
|
||||
columnIndex: i,
|
||||
series,
|
||||
fieldIndex: i,
|
||||
stats: [stat], // The stats to calculate
|
||||
nullValueMode: NullValueMode.Null,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user