DataPanel.tsx 5.0 KB

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