DataPanel.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Library
  2. import React, { Component } from 'react';
  3. // Services
  4. import { getDatasourceSrv } 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. dataSourceSrv = getDatasourceSrv();
  33. static defaultProps = {
  34. isVisible: true,
  35. panelId: 1,
  36. dashboardId: 1,
  37. };
  38. constructor(props: Props) {
  39. super(props);
  40. this.state = {
  41. loading: LoadingState.NotStarted,
  42. response: {
  43. data: [],
  44. },
  45. isFirstLoad: true,
  46. };
  47. }
  48. componentDidMount() {
  49. this.issueQueries();
  50. }
  51. async componentDidUpdate(prevProps: Props) {
  52. if (!this.hasPropsChanged(prevProps)) {
  53. return;
  54. }
  55. this.issueQueries();
  56. }
  57. hasPropsChanged(prevProps: Props) {
  58. return this.props.refreshCounter !== prevProps.refreshCounter;
  59. }
  60. issueQueries = async () => {
  61. const { isVisible, queries, datasource, panelId, dashboardId, timeRange, widthPixels, maxDataPoints } = this.props;
  62. if (!isVisible) {
  63. return;
  64. }
  65. if (!queries.length) {
  66. this.setState({ loading: LoadingState.Done });
  67. return;
  68. }
  69. this.setState({ loading: LoadingState.Loading });
  70. try {
  71. const ds = await this.dataSourceSrv.get(datasource);
  72. // TODO interpolate variables
  73. const minInterval = this.props.minInterval || ds.interval;
  74. const intervalRes = kbn.calculateInterval(timeRange, widthPixels, minInterval);
  75. const queryOptions: DataQueryOptions = {
  76. timezone: 'browser',
  77. panelId: panelId,
  78. dashboardId: dashboardId,
  79. range: timeRange,
  80. rangeRaw: timeRange.raw,
  81. interval: intervalRes.interval,
  82. intervalMs: intervalRes.intervalMs,
  83. targets: queries,
  84. maxDataPoints: maxDataPoints || widthPixels,
  85. scopedVars: {},
  86. cacheTimeout: null,
  87. };
  88. console.log('Issuing DataPanel query', queryOptions);
  89. const resp = await ds.query(queryOptions);
  90. console.log('Issuing DataPanel query Resp', resp);
  91. this.setState({
  92. loading: LoadingState.Done,
  93. response: resp,
  94. isFirstLoad: false,
  95. });
  96. } catch (err) {
  97. console.log('Loading error', err);
  98. this.setState({ loading: LoadingState.Error, isFirstLoad: false });
  99. }
  100. };
  101. render() {
  102. const { response, loading } = this.state;
  103. const timeSeries = response.data;
  104. console.log('data panel render');
  105. return (
  106. <>
  107. {this.loadingSpinner}
  108. {this.props.children({
  109. timeSeries,
  110. loading,
  111. })}
  112. </>
  113. );
  114. }
  115. private get loadingSpinner(): JSX.Element {
  116. const { loading } = this.state;
  117. if (loading === LoadingState.Loading) {
  118. return (
  119. <div className="panel-loading">
  120. <i className="fa fa-spinner fa-spin" />
  121. </div>
  122. );
  123. }
  124. return null;
  125. }
  126. }