DataPanel.tsx 5.1 KB

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