DataPanel.tsx 5.1 KB

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