diff --git a/packages/grafana-ui/src/utils/processTimeSeries.ts b/packages/grafana-ui/src/utils/processTimeSeries.ts index 4b64ae3dd99..3b2d1bd05aa 100644 --- a/packages/grafana-ui/src/utils/processTimeSeries.ts +++ b/packages/grafana-ui/src/utils/processTimeSeries.ts @@ -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; +}; diff --git a/public/app/features/dashboard/dashgrid/DataPanel.tsx b/public/app/features/dashboard/dashgrid/DataPanel.tsx index 1dd62c58e5c..872e2823553 100644 --- a/public/app/features/dashboard/dashgrid/DataPanel.tsx +++ b/public/app/features/dashboard/dashgrid/DataPanel.tsx @@ -14,10 +14,9 @@ import { TableData, TimeRange, ScopedVars, + toTableData, } from '@grafana/ui'; -import { toTableData } from '../utils/panel'; - interface RenderProps { loading: LoadingState; data: TableData[]; diff --git a/public/app/features/dashboard/dashgrid/PanelChrome.tsx b/public/app/features/dashboard/dashgrid/PanelChrome.tsx index 0dd82b7dad9..af58202dfa9 100644 --- a/public/app/features/dashboard/dashgrid/PanelChrome.tsx +++ b/public/app/features/dashboard/dashgrid/PanelChrome.tsx @@ -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 { } 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 { diff --git a/public/app/features/dashboard/utils/panel.ts b/public/app/features/dashboard/utils/panel.ts index 3058e2b9db6..57f4b81a0e0 100644 --- a/public/app/features/dashboard/utils/panel.ts +++ b/public/app/features/dashboard/utils/panel.ts @@ -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'); - }); -};