Browse Source

move toTableData to grafana/ui

ryan 7 years ago
parent
commit
439b044204

+ 38 - 3
packages/grafana-ui/src/utils/processTimeSeries.ts

@@ -4,12 +4,12 @@ import isNumber from 'lodash/isNumber';
 import { colors } from './colors';
 import { colors } from './colors';
 
 
 // Types
 // Types
-import { TimeSeriesVMs, NullValueMode, TimeSeriesValue, TableData } from '../types';
+import { TimeSeriesVMs, NullValueMode, TimeSeriesValue, TableData, TimeSeries } from '../types';
 
 
 interface Options {
 interface Options {
   data: TableData[];
   data: TableData[];
-  xColumn?: number; // Time
-  yColumn?: number; // Value
+  xColumn?: number; // Time (or null to guess)
+  yColumn?: number; // Value (or null to guess)
   nullValueMode: NullValueMode;
   nullValueMode: NullValueMode;
 }
 }
 
 
@@ -190,3 +190,38 @@ export function processTimeSeries({ data, xColumn, yColumn, nullValueMode }: Opt
 
 
   return vmSeries;
   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;
+};

+ 1 - 2
public/app/features/dashboard/dashgrid/DataPanel.tsx

@@ -14,10 +14,9 @@ import {
   TableData,
   TableData,
   TimeRange,
   TimeRange,
   ScopedVars,
   ScopedVars,
+  toTableData,
 } from '@grafana/ui';
 } from '@grafana/ui';
 
 
-import { toTableData } from '../utils/panel';
-
 interface RenderProps {
 interface RenderProps {
   loading: LoadingState;
   loading: LoadingState;
   data: TableData[];
   data: TableData[];

+ 3 - 3
public/app/features/dashboard/dashgrid/PanelChrome.tsx

@@ -11,14 +11,14 @@ import { DataPanel } from './DataPanel';
 import ErrorBoundary from '../../../core/components/ErrorBoundary/ErrorBoundary';
 import ErrorBoundary from '../../../core/components/ErrorBoundary/ErrorBoundary';
 
 
 // Utils
 // 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 { PANEL_HEADER_HEIGHT } from 'app/core/constants';
 import { profiler } from 'app/core/profiler';
 import { profiler } from 'app/core/profiler';
 
 
 // Types
 // Types
 import { DashboardModel, PanelModel } from '../state';
 import { DashboardModel, PanelModel } from '../state';
 import { PanelPlugin } from 'app/types';
 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 { ScopedVars } from '@grafana/ui';
 
 
 import variables from 'sass/_variables.generated.scss';
 import variables from 'sass/_variables.generated.scss';
@@ -139,7 +139,7 @@ export class PanelChrome extends PureComponent<Props, State> {
   }
   }
 
 
   get getDataForPanel() {
   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 {
   renderPanelPlugin(loading: LoadingState, data: TableData[], width: number, height: number): JSX.Element {

+ 1 - 38
public/app/features/dashboard/utils/panel.ts

@@ -4,7 +4,7 @@ import store from 'app/core/store';
 // Models
 // Models
 import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
 import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
 import { PanelModel } from 'app/features/dashboard/state/PanelModel';
 import { PanelModel } from 'app/features/dashboard/state/PanelModel';
-import { TableData, TimeRange, TimeSeries } from '@grafana/ui';
+import { TimeRange } from '@grafana/ui';
 
 
 // Utils
 // Utils
 import { isString as _isString } from 'lodash';
 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));
   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');
-  });
-};