DataPanel.tsx 4.2 KB

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