DataPanel.tsx 3.9 KB

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