DataPanel.tsx 3.9 KB

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