mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
cleanup and guess all columns
This commit is contained in:
60
public/app/features/dashboard/dashgrid/DataPanel.test.tsx
Normal file
60
public/app/features/dashboard/dashgrid/DataPanel.test.tsx
Normal file
@@ -0,0 +1,60 @@
|
||||
// Library
|
||||
import React from 'react';
|
||||
|
||||
import { DataPanel, getProcessedTableData } from './DataPanel';
|
||||
|
||||
describe('DataPanel', () => {
|
||||
let dataPanel: DataPanel;
|
||||
|
||||
beforeEach(() => {
|
||||
dataPanel = new DataPanel({
|
||||
queries: [],
|
||||
panelId: 1,
|
||||
widthPixels: 100,
|
||||
refreshCounter: 1,
|
||||
datasource: 'xxx',
|
||||
children: r => {
|
||||
return <div>hello</div>;
|
||||
},
|
||||
onError: (message, error) => {},
|
||||
});
|
||||
});
|
||||
|
||||
it('starts with unloaded state', () => {
|
||||
expect(dataPanel.state.isFirstLoad).toBe(true);
|
||||
});
|
||||
|
||||
it('converts timeseries to table skipping nulls', () => {
|
||||
const input1 = {
|
||||
target: 'Field Name',
|
||||
datapoints: [[100, 1], [200, 2]],
|
||||
};
|
||||
const input2 = {
|
||||
// without target
|
||||
target: '',
|
||||
datapoints: [[100, 1], [200, 2]],
|
||||
};
|
||||
const data = getProcessedTableData([null, input1, input2, null, null]);
|
||||
expect(data.length).toBe(2);
|
||||
expect(data[0].columns[0].text).toBe(input1.target);
|
||||
expect(data[0].rows).toBe(input1.datapoints);
|
||||
|
||||
// Default name
|
||||
expect(data[1].columns[0].text).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();
|
||||
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([]);
|
||||
});
|
||||
});
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
TimeRange,
|
||||
ScopedVars,
|
||||
toTableData,
|
||||
guessColumnTypes,
|
||||
} from '@grafana/ui';
|
||||
|
||||
interface RenderProps {
|
||||
@@ -46,6 +47,25 @@ export interface State {
|
||||
data?: TableData[];
|
||||
}
|
||||
|
||||
/**
|
||||
* All panels will be passed tables that have our best guess at colum type set
|
||||
*
|
||||
* This is also used by PanelChrome for snapshot support
|
||||
*/
|
||||
export function getProcessedTableData(results?: any[]): TableData[] {
|
||||
if (!results) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const tables: TableData[] = [];
|
||||
for (const r of results) {
|
||||
if (r) {
|
||||
tables.push(guessColumnTypes(toTableData(r)));
|
||||
}
|
||||
}
|
||||
return tables;
|
||||
}
|
||||
|
||||
export class DataPanel extends Component<Props, State> {
|
||||
static defaultProps = {
|
||||
isVisible: true,
|
||||
@@ -147,7 +167,7 @@ export class DataPanel extends Component<Props, State> {
|
||||
this.setState({
|
||||
loading: LoadingState.Done,
|
||||
response: resp,
|
||||
data: toTableData(resp.data),
|
||||
data: getProcessedTableData(resp.data),
|
||||
isFirstLoad: false,
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
@@ -19,11 +19,13 @@ import config from 'app/core/config';
|
||||
// Types
|
||||
import { DashboardModel, PanelModel } from '../state';
|
||||
import { PanelPlugin } from 'app/types';
|
||||
import { DataQueryResponse, TimeRange, LoadingState, TableData, DataQueryError, toTableData } from '@grafana/ui';
|
||||
import { DataQueryResponse, TimeRange, LoadingState, TableData, DataQueryError } from '@grafana/ui';
|
||||
import { ScopedVars } from '@grafana/ui';
|
||||
|
||||
import templateSrv from 'app/features/templating/template_srv';
|
||||
|
||||
import { getProcessedTableData } from './DataPanel';
|
||||
|
||||
const DEFAULT_PLUGIN_ERROR = 'Error in plugin';
|
||||
|
||||
export interface Props {
|
||||
@@ -139,7 +141,7 @@ export class PanelChrome extends PureComponent<Props, State> {
|
||||
}
|
||||
|
||||
get getDataForPanel() {
|
||||
return this.hasPanelSnapshot ? toTableData(this.props.panel.snapshotData) : null;
|
||||
return this.hasPanelSnapshot ? getProcessedTableData(this.props.panel.snapshotData) : null;
|
||||
}
|
||||
|
||||
renderPanelPlugin(loading: LoadingState, data: TableData[], width: number, height: number): JSX.Element {
|
||||
|
||||
Reference in New Issue
Block a user