DataPanel.tsx 5.4 KB

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