DataPanel.tsx 4.4 KB

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