DataPanel.tsx 5.1 KB

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