grafana/public/app/features/dashboard/dashgrid/DataPanel.tsx

210 lines
4.4 KiB
TypeScript
Raw Normal View History

// Library
2018-10-14 22:31:20 +08:00
import React, { Component } from 'react';
2019-01-09 17:33:20 +08:00
// Services
2019-01-30 17:39:42 +08:00
import { DatasourceSrv, getDatasourceSrv } from 'app/features/plugins/datasource_srv';
// Utils
import kbn from 'app/core/utils/kbn';
// Types
2019-01-31 16:59:21 +08:00
import {
DataQueryOptions,
DataQueryResponse,
DataQueryError,
LoadingState,
2019-01-31 16:59:21 +08:00
PanelData,
TableData,
TimeRange,
TimeSeries,
} from '@grafana/ui';
2018-06-28 19:31:55 +08:00
interface RenderProps {
loading: LoadingState;
2019-01-31 16:59:21 +08:00
panelData: PanelData;
2018-06-28 19:31:55 +08:00
}
export interface Props {
datasource: string | null;
queries: any[];
panelId: number;
dashboardId?: number;
isVisible?: boolean;
timeRange?: TimeRange;
2018-11-14 01:50:12 +08:00
widthPixels: number;
refreshCounter: number;
minInterval?: string;
maxDataPoints?: number;
children: (r: RenderProps) => JSX.Element;
2019-01-30 17:39:42 +08:00
onDataResponse?: (data: DataQueryResponse) => void;
onError: (message: string, error: DataQueryError) => void;
2018-06-28 19:31:55 +08:00
}
export interface State {
isFirstLoad: boolean;
loading: LoadingState;
2018-10-15 00:19:49 +08:00
response: DataQueryResponse;
2018-06-28 19:31:55 +08:00
}
2018-10-14 22:31:20 +08:00
export class DataPanel extends Component<Props, State> {
static defaultProps = {
isVisible: true,
dashboardId: 1,
};
2018-07-01 23:34:42 +08:00
2018-11-14 20:20:19 +08:00
dataSourceSrv: DatasourceSrv = getDatasourceSrv();
isUnmounted = false;
constructor(props: Props) {
super(props);
2018-07-01 23:34:42 +08:00
this.state = {
loading: LoadingState.NotStarted,
2018-10-15 00:19:49 +08:00
response: {
data: [],
},
isFirstLoad: true,
};
}
2018-07-01 23:34:42 +08:00
componentDidMount() {
this.issueQueries();
2018-10-14 22:31:20 +08:00
}
2018-11-14 20:20:19 +08:00
componentWillUnmount() {
this.isUnmounted = true;
}
2018-10-14 22:31:20 +08:00
async componentDidUpdate(prevProps: Props) {
if (!this.hasPropsChanged(prevProps)) {
return;
}
this.issueQueries();
}
2018-07-01 23:34:42 +08:00
2018-10-14 22:31:20 +08:00
hasPropsChanged(prevProps: Props) {
return this.props.refreshCounter !== prevProps.refreshCounter;
2018-10-14 22:31:20 +08:00
}
2018-11-14 20:20:19 +08:00
private issueQueries = async () => {
2019-01-30 17:39:42 +08:00
const {
isVisible,
queries,
datasource,
panelId,
dashboardId,
timeRange,
widthPixels,
maxDataPoints,
onDataResponse,
onError,
2019-01-30 17:39:42 +08:00
} = this.props;
if (!isVisible) {
return;
}
if (!queries.length) {
2018-10-15 00:19:49 +08:00
this.setState({ loading: LoadingState.Done });
return;
}
this.setState({ loading: LoadingState.Loading });
try {
2018-11-14 01:50:12 +08:00
const ds = await this.dataSourceSrv.get(datasource);
// TODO interpolate variables
const minInterval = this.props.minInterval || ds.interval;
const intervalRes = kbn.calculateInterval(timeRange, widthPixels, minInterval);
2018-10-16 03:52:24 +08:00
const queryOptions: DataQueryOptions = {
timezone: 'browser',
panelId: panelId,
dashboardId: dashboardId,
range: timeRange,
rangeRaw: timeRange.raw,
interval: intervalRes.interval,
intervalMs: intervalRes.intervalMs,
targets: queries,
maxDataPoints: maxDataPoints || widthPixels,
scopedVars: {},
cacheTimeout: null,
};
const resp = await ds.query(queryOptions);
2019-01-31 16:59:21 +08:00
2018-11-14 20:20:19 +08:00
if (this.isUnmounted) {
return;
}
2019-01-30 17:39:42 +08:00
if (onDataResponse) {
onDataResponse(resp);
}
2018-10-15 00:19:49 +08:00
this.setState({
loading: LoadingState.Done,
response: resp,
2018-10-22 15:53:40 +08:00
isFirstLoad: false,
2018-10-15 00:19:49 +08:00
});
} catch (err) {
console.log('DataPanel error', err);
let message = 'Query error';
if (err.message) {
message = err.message;
} else if (err.data && err.data.message) {
message = err.data.message;
} else if (err.data && err.data.error) {
message = err.data.error;
}
onError(message, err);
this.setState({ isFirstLoad: false });
}
};
2019-01-31 16:59:21 +08:00
getPanelData = () => {
const { response } = this.state;
if (response.data.length > 0 && (response.data[0] as TableData).type === 'table') {
return {
2019-01-31 20:59:25 +08:00
tableData: response.data[0] as TableData,
timeSeries: null,
2019-01-31 16:59:21 +08:00
};
}
return {
2019-01-31 20:59:25 +08:00
timeSeries: response.data as TimeSeries[],
tableData: null,
2019-01-31 16:59:21 +08:00
};
};
render() {
const { queries } = this.props;
2019-01-31 16:59:21 +08:00
const { loading, isFirstLoad } = this.state;
const panelData = this.getPanelData();
2018-11-14 20:20:19 +08:00
if (isFirstLoad && loading === LoadingState.Loading) {
return (
<div className="panel-loading">
<i className="fa fa-spinner fa-spin" />
</div>
);
}
2018-07-01 23:34:42 +08:00
if (!queries.length) {
return (
<div className="panel-empty">
<p>Add a query to get some data!</p>
</div>
);
2018-07-01 23:34:42 +08:00
}
2018-06-28 19:31:55 +08:00
return this.props.children({
loading,
panelData,
});
}
}