DataPanel.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Library
  2. import React, { Component } from 'react';
  3. import { Tooltip } from '@grafana/ui';
  4. import ErrorBoundary from 'app/core/components/ErrorBoundary/ErrorBoundary';
  5. // Services
  6. import { DatasourceSrv, getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  7. // Utils
  8. import kbn from 'app/core/utils/kbn';
  9. // Types
  10. import {
  11. DataQueryOptions,
  12. DataQueryResponse,
  13. LoadingState,
  14. PanelData,
  15. TableData,
  16. TimeRange,
  17. TimeSeries,
  18. } from '@grafana/ui';
  19. const DEFAULT_PLUGIN_ERROR = 'Error in plugin';
  20. interface RenderProps {
  21. loading: LoadingState;
  22. panelData: PanelData;
  23. }
  24. export interface Props {
  25. datasource: string | null;
  26. queries: any[];
  27. panelId?: number;
  28. dashboardId?: number;
  29. isVisible?: boolean;
  30. timeRange?: TimeRange;
  31. widthPixels: number;
  32. refreshCounter: number;
  33. minInterval?: string;
  34. maxDataPoints?: number;
  35. children: (r: RenderProps) => JSX.Element;
  36. onDataResponse?: (data: DataQueryResponse) => void;
  37. }
  38. export interface State {
  39. isFirstLoad: boolean;
  40. loading: LoadingState;
  41. errorMessage: string;
  42. response: DataQueryResponse;
  43. }
  44. export class DataPanel extends Component<Props, State> {
  45. static defaultProps = {
  46. isVisible: true,
  47. panelId: 1,
  48. dashboardId: 1,
  49. };
  50. dataSourceSrv: DatasourceSrv = getDatasourceSrv();
  51. isUnmounted = false;
  52. constructor(props: Props) {
  53. super(props);
  54. this.state = {
  55. loading: LoadingState.NotStarted,
  56. errorMessage: '',
  57. response: {
  58. data: [],
  59. },
  60. isFirstLoad: true,
  61. };
  62. }
  63. componentDidMount() {
  64. this.issueQueries();
  65. }
  66. componentWillUnmount() {
  67. this.isUnmounted = true;
  68. }
  69. async componentDidUpdate(prevProps: Props) {
  70. if (!this.hasPropsChanged(prevProps)) {
  71. return;
  72. }
  73. this.issueQueries();
  74. }
  75. hasPropsChanged(prevProps: Props) {
  76. return this.props.refreshCounter !== prevProps.refreshCounter;
  77. }
  78. private issueQueries = async () => {
  79. const {
  80. isVisible,
  81. queries,
  82. datasource,
  83. panelId,
  84. dashboardId,
  85. timeRange,
  86. widthPixels,
  87. maxDataPoints,
  88. onDataResponse,
  89. } = this.props;
  90. if (!isVisible) {
  91. return;
  92. }
  93. if (!queries.length) {
  94. this.setState({ loading: LoadingState.Done });
  95. return;
  96. }
  97. this.setState({ loading: LoadingState.Loading, errorMessage: '' });
  98. try {
  99. const ds = await this.dataSourceSrv.get(datasource);
  100. // TODO interpolate variables
  101. const minInterval = this.props.minInterval || ds.interval;
  102. const intervalRes = kbn.calculateInterval(timeRange, widthPixels, minInterval);
  103. const queryOptions: DataQueryOptions = {
  104. timezone: 'browser',
  105. panelId: panelId,
  106. dashboardId: dashboardId,
  107. range: timeRange,
  108. rangeRaw: timeRange.raw,
  109. interval: intervalRes.interval,
  110. intervalMs: intervalRes.intervalMs,
  111. targets: queries,
  112. maxDataPoints: maxDataPoints || widthPixels,
  113. scopedVars: {},
  114. cacheTimeout: null,
  115. };
  116. console.log('Issuing DataPanel query', queryOptions);
  117. const resp = await ds.query(queryOptions);
  118. console.log('Issuing DataPanel query Resp', resp);
  119. if (this.isUnmounted) {
  120. return;
  121. }
  122. if (onDataResponse) {
  123. onDataResponse(resp);
  124. }
  125. this.setState({
  126. loading: LoadingState.Done,
  127. response: resp,
  128. isFirstLoad: false,
  129. });
  130. } catch (err) {
  131. console.log('Loading error', err);
  132. this.onError('Request Error');
  133. }
  134. };
  135. onError = (errorMessage: string) => {
  136. if (this.state.loading !== LoadingState.Error || this.state.errorMessage !== errorMessage) {
  137. this.setState({
  138. loading: LoadingState.Error,
  139. isFirstLoad: false,
  140. errorMessage: errorMessage,
  141. });
  142. }
  143. };
  144. getPanelData = () => {
  145. const { response } = this.state;
  146. if (response.data.length > 0 && (response.data[0] as TableData).type === 'table') {
  147. return {
  148. tableData: response.data[0] as TableData,
  149. timeSeries: null,
  150. };
  151. }
  152. return {
  153. timeSeries: response.data as TimeSeries[],
  154. tableData: null,
  155. };
  156. };
  157. render() {
  158. const { queries } = this.props;
  159. const { loading, isFirstLoad } = this.state;
  160. const panelData = this.getPanelData();
  161. if (isFirstLoad && loading === LoadingState.Loading) {
  162. return this.renderLoadingStates();
  163. }
  164. if (!queries.length) {
  165. return (
  166. <div className="panel-empty">
  167. <p>Add a query to get some data!</p>
  168. </div>
  169. );
  170. }
  171. return (
  172. <>
  173. {this.renderLoadingStates()}
  174. <ErrorBoundary>
  175. {({ error, errorInfo }) => {
  176. if (errorInfo) {
  177. this.onError(error.message || DEFAULT_PLUGIN_ERROR);
  178. return null;
  179. }
  180. return (
  181. <>
  182. {this.props.children({
  183. loading,
  184. panelData,
  185. })}
  186. </>
  187. );
  188. }}
  189. </ErrorBoundary>
  190. </>
  191. );
  192. }
  193. private renderLoadingStates(): JSX.Element {
  194. const { loading, errorMessage } = this.state;
  195. if (loading === LoadingState.Loading) {
  196. return (
  197. <div className="panel-loading">
  198. <i className="fa fa-spinner fa-spin" />
  199. </div>
  200. );
  201. } else if (loading === LoadingState.Error) {
  202. return (
  203. <Tooltip content={errorMessage} placement="bottom-start" theme="error">
  204. <div className="panel-info-corner panel-info-corner--error">
  205. <i className="fa" />
  206. <span className="panel-info-corner-inner" />
  207. </div>
  208. </Tooltip>
  209. );
  210. }
  211. return null;
  212. }
  213. }