move toTableData to grafana/ui
This commit is contained in:
@@ -4,12 +4,12 @@ import isNumber from 'lodash/isNumber';
|
||||
import { colors } from './colors';
|
||||
|
||||
// Types
|
||||
import { TimeSeriesVMs, NullValueMode, TimeSeriesValue, TableData } from '../types';
|
||||
import { TimeSeriesVMs, NullValueMode, TimeSeriesValue, TableData, TimeSeries } from '../types';
|
||||
|
||||
interface Options {
|
||||
data: TableData[];
|
||||
xColumn?: number; // Time
|
||||
yColumn?: number; // Value
|
||||
xColumn?: number; // Time (or null to guess)
|
||||
yColumn?: number; // Value (or null to guess)
|
||||
nullValueMode: NullValueMode;
|
||||
}
|
||||
|
||||
@@ -190,3 +190,38 @@ export function processTimeSeries({ data, xColumn, yColumn, nullValueMode }: Opt
|
||||
|
||||
return vmSeries;
|
||||
}
|
||||
|
||||
export const toTableData = (results: any[]): TableData[] => {
|
||||
const tables: TableData[] = [];
|
||||
if (results) {
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
const data = results[i];
|
||||
if (data) {
|
||||
if (data.hasOwnProperty('columns')) {
|
||||
tables.push(data as TableData);
|
||||
} else if (data.hasOwnProperty('datapoints')) {
|
||||
const ts = data as TimeSeries;
|
||||
tables.push({
|
||||
type: 'timeseries',
|
||||
columns: [
|
||||
{
|
||||
text: ts.target,
|
||||
unit: ts.unit,
|
||||
type: 'number', // Is this really true?
|
||||
},
|
||||
{
|
||||
text: 'time',
|
||||
type: 'time',
|
||||
},
|
||||
],
|
||||
rows: ts.datapoints,
|
||||
} as TableData);
|
||||
} else {
|
||||
console.warn('Can not convert', data);
|
||||
throw new Error('Unsupported data format');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tables;
|
||||
};
|
||||
|
||||
@@ -14,10 +14,9 @@ import {
|
||||
TableData,
|
||||
TimeRange,
|
||||
ScopedVars,
|
||||
toTableData,
|
||||
} from '@grafana/ui';
|
||||
|
||||
import { toTableData } from '../utils/panel';
|
||||
|
||||
interface RenderProps {
|
||||
loading: LoadingState;
|
||||
data: TableData[];
|
||||
|
||||
@@ -11,14 +11,14 @@ import { DataPanel } from './DataPanel';
|
||||
import ErrorBoundary from '../../../core/components/ErrorBoundary/ErrorBoundary';
|
||||
|
||||
// Utils
|
||||
import { applyPanelTimeOverrides, snapshotDataToPanelData } from 'app/features/dashboard/utils/panel';
|
||||
import { applyPanelTimeOverrides } from 'app/features/dashboard/utils/panel';
|
||||
import { PANEL_HEADER_HEIGHT } from 'app/core/constants';
|
||||
import { profiler } from 'app/core/profiler';
|
||||
|
||||
// Types
|
||||
import { DashboardModel, PanelModel } from '../state';
|
||||
import { PanelPlugin } from 'app/types';
|
||||
import { DataQueryResponse, TimeRange, LoadingState, TableData, DataQueryError } from '@grafana/ui';
|
||||
import { DataQueryResponse, TimeRange, LoadingState, TableData, DataQueryError, toTableData } from '@grafana/ui';
|
||||
import { ScopedVars } from '@grafana/ui';
|
||||
|
||||
import variables from 'sass/_variables.generated.scss';
|
||||
@@ -139,7 +139,7 @@ export class PanelChrome extends PureComponent<Props, State> {
|
||||
}
|
||||
|
||||
get getDataForPanel() {
|
||||
return this.hasPanelSnapshot ? snapshotDataToPanelData(this.props.panel) : null;
|
||||
return this.hasPanelSnapshot ? toTableData(this.props.panel.snapshotData) : null;
|
||||
}
|
||||
|
||||
renderPanelPlugin(loading: LoadingState, data: TableData[], width: number, height: number): JSX.Element {
|
||||
|
||||
@@ -4,7 +4,7 @@ import store from 'app/core/store';
|
||||
// Models
|
||||
import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
|
||||
import { PanelModel } from 'app/features/dashboard/state/PanelModel';
|
||||
import { TableData, TimeRange, TimeSeries } from '@grafana/ui';
|
||||
import { TimeRange } from '@grafana/ui';
|
||||
|
||||
// Utils
|
||||
import { isString as _isString } from 'lodash';
|
||||
@@ -169,40 +169,3 @@ export function getResolution(panel: PanelModel): number {
|
||||
|
||||
return panel.maxDataPoints ? panel.maxDataPoints : Math.ceil(width * (panel.gridPos.w / 24));
|
||||
}
|
||||
|
||||
const isTimeSeries = (data: any): data is TimeSeries => data && data.hasOwnProperty('datapoints');
|
||||
const isTableData = (data: any): data is TableData => data && data.hasOwnProperty('columns');
|
||||
export const snapshotDataToPanelData = (panel: PanelModel): TableData[] => {
|
||||
return toTableData(panel.snapshotData);
|
||||
};
|
||||
|
||||
export const toTableData = (results: any[]): TableData[] => {
|
||||
if (!results) {
|
||||
return [];
|
||||
}
|
||||
return results.map(data => {
|
||||
if (isTableData(data)) {
|
||||
return data as TableData;
|
||||
}
|
||||
if (isTimeSeries(data)) {
|
||||
const ts = data as TimeSeries;
|
||||
return {
|
||||
type: 'timeseries',
|
||||
columns: [
|
||||
{
|
||||
text: ts.target,
|
||||
unit: ts.unit,
|
||||
type: 'number', // Is this really true?
|
||||
},
|
||||
{
|
||||
text: 'time',
|
||||
type: 'time',
|
||||
},
|
||||
],
|
||||
rows: ts.datapoints,
|
||||
} as TableData;
|
||||
}
|
||||
console.warn('Can not convert', data);
|
||||
throw new Error('Unsupported data format');
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user