DataPanel.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 { DataQueryOptions, DataQueryResponse, LoadingState, TimeRange, TimeSeries } 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. onDataResponse?: (data: DataQueryResponse) => void;
  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 {
  72. isVisible,
  73. queries,
  74. datasource,
  75. panelId,
  76. dashboardId,
  77. timeRange,
  78. widthPixels,
  79. maxDataPoints,
  80. onDataResponse,
  81. } = this.props;
  82. if (!isVisible) {
  83. return;
  84. }
  85. if (!queries.length) {
  86. this.setState({ loading: LoadingState.Done });
  87. return;
  88. }
  89. this.setState({ loading: LoadingState.Loading, errorMessage: '' });
  90. try {
  91. const ds = await this.dataSourceSrv.get(datasource);
  92. // TODO interpolate variables
  93. const minInterval = this.props.minInterval || ds.interval;
  94. const intervalRes = kbn.calculateInterval(timeRange, widthPixels, minInterval);
  95. const queryOptions: DataQueryOptions = {
  96. timezone: 'browser',
  97. panelId: panelId,
  98. dashboardId: dashboardId,
  99. range: timeRange,
  100. rangeRaw: timeRange.raw,
  101. interval: intervalRes.interval,
  102. intervalMs: intervalRes.intervalMs,
  103. targets: queries,
  104. maxDataPoints: maxDataPoints || widthPixels,
  105. scopedVars: {},
  106. cacheTimeout: null,
  107. };
  108. console.log('Issuing DataPanel query', queryOptions);
  109. const resp = await ds.query(queryOptions);
  110. console.log('Issuing DataPanel query Resp', resp);
  111. if (this.isUnmounted) {
  112. return;
  113. }
  114. if (onDataResponse) {
  115. onDataResponse(resp);
  116. }
  117. this.setState({
  118. loading: LoadingState.Done,
  119. response: resp,
  120. isFirstLoad: false,
  121. });
  122. } catch (err) {
  123. console.log('Loading error', err);
  124. this.onError('Request Error');
  125. }
  126. };
  127. onError = (errorMessage: string) => {
  128. if (this.state.loading !== LoadingState.Error || this.state.errorMessage !== errorMessage) {
  129. this.setState({
  130. loading: LoadingState.Error,
  131. isFirstLoad: false,
  132. errorMessage: errorMessage,
  133. });
  134. }
  135. };
  136. render() {
  137. const { queries } = this.props;
  138. const { response, loading, isFirstLoad } = this.state;
  139. const timeSeries = response.data;
  140. if (isFirstLoad && loading === LoadingState.Loading) {
  141. return this.renderLoadingStates();
  142. }
  143. if (!queries.length) {
  144. return (
  145. <div className="panel-empty">
  146. <p>Add a query to get some data!</p>
  147. </div>
  148. );
  149. }
  150. return (
  151. <>
  152. {this.renderLoadingStates()}
  153. <ErrorBoundary>
  154. {({ error, errorInfo }) => {
  155. if (errorInfo) {
  156. this.onError(error.message || DEFAULT_PLUGIN_ERROR);
  157. return null;
  158. }
  159. return (
  160. <>
  161. {this.props.children({
  162. timeSeries,
  163. loading,
  164. })}
  165. </>
  166. );
  167. }}
  168. </ErrorBoundary>
  169. </>
  170. );
  171. }
  172. private renderLoadingStates(): JSX.Element {
  173. const { loading, errorMessage } = this.state;
  174. if (loading === LoadingState.Loading) {
  175. return (
  176. <div className="panel-loading">
  177. <i className="fa fa-spinner fa-spin" />
  178. </div>
  179. );
  180. } else if (loading === LoadingState.Error) {
  181. return (
  182. <Tooltip content={errorMessage} placement="bottom-start" theme="error">
  183. <div className="panel-info-corner panel-info-corner--error">
  184. <i className="fa" />
  185. <span className="panel-info-corner-inner" />
  186. </div>
  187. </Tooltip>
  188. );
  189. }
  190. return null;
  191. }
  192. }