PanelChrome.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import { AutoSizer } from 'react-virtualized';
  4. // Services
  5. import { getTimeSrv, TimeSrv } from '../services/TimeSrv';
  6. // Components
  7. import { PanelHeader } from './PanelHeader/PanelHeader';
  8. import { DataPanel } from './DataPanel';
  9. import ErrorBoundary from '../../../core/components/ErrorBoundary/ErrorBoundary';
  10. // Utils
  11. import { applyPanelTimeOverrides } from 'app/features/dashboard/utils/panel';
  12. import { PANEL_HEADER_HEIGHT } from 'app/core/constants';
  13. import { profiler } from 'app/core/profiler';
  14. import config from 'app/core/config';
  15. // Types
  16. import { DashboardModel, PanelModel } from '../state';
  17. import { PanelPlugin } from 'app/types';
  18. import { DataQueryResponse, TimeRange, LoadingState, DataQueryError, SeriesData } from '@grafana/ui';
  19. import { ScopedVars } from '@grafana/ui';
  20. import templateSrv from 'app/features/templating/template_srv';
  21. import { getProcessedSeriesData } from './DataPanel';
  22. const DEFAULT_PLUGIN_ERROR = 'Error in plugin';
  23. export interface Props {
  24. panel: PanelModel;
  25. dashboard: DashboardModel;
  26. plugin: PanelPlugin;
  27. isFullscreen: boolean;
  28. }
  29. export interface State {
  30. refreshCounter: number;
  31. renderCounter: number;
  32. timeInfo?: string;
  33. timeRange?: TimeRange;
  34. errorMessage: string | null;
  35. }
  36. export class PanelChrome extends PureComponent<Props, State> {
  37. timeSrv: TimeSrv = getTimeSrv();
  38. constructor(props) {
  39. super(props);
  40. this.state = {
  41. refreshCounter: 0,
  42. renderCounter: 0,
  43. errorMessage: null,
  44. };
  45. }
  46. componentDidMount() {
  47. this.props.panel.events.on('refresh', this.onRefresh);
  48. this.props.panel.events.on('render', this.onRender);
  49. this.props.dashboard.panelInitialized(this.props.panel);
  50. }
  51. componentWillUnmount() {
  52. this.props.panel.events.off('refresh', this.onRefresh);
  53. }
  54. onRefresh = () => {
  55. console.log('onRefresh');
  56. if (!this.isVisible) {
  57. return;
  58. }
  59. const { panel } = this.props;
  60. const timeData = applyPanelTimeOverrides(panel, this.timeSrv.timeRange());
  61. this.setState({
  62. refreshCounter: this.state.refreshCounter + 1,
  63. timeRange: timeData.timeRange,
  64. timeInfo: timeData.timeInfo,
  65. });
  66. };
  67. onRender = () => {
  68. this.setState({
  69. renderCounter: this.state.renderCounter + 1,
  70. });
  71. };
  72. replaceVariables = (value: string, extraVars?: ScopedVars, format?: string) => {
  73. let vars = this.props.panel.scopedVars;
  74. if (extraVars) {
  75. vars = vars ? { ...vars, ...extraVars } : extraVars;
  76. }
  77. return templateSrv.replace(value, vars, format);
  78. };
  79. onDataResponse = (dataQueryResponse: DataQueryResponse) => {
  80. if (this.props.dashboard.isSnapshot()) {
  81. this.props.panel.snapshotData = dataQueryResponse.data;
  82. }
  83. // clear error state (if any)
  84. this.clearErrorState();
  85. // This event is used by old query editors and panel editor options
  86. this.props.panel.events.emit('data-received', dataQueryResponse.data);
  87. };
  88. onDataError = (message: string, error: DataQueryError) => {
  89. if (this.state.errorMessage !== message) {
  90. this.setState({ errorMessage: message });
  91. }
  92. // this event is used by old query editors
  93. this.props.panel.events.emit('data-error', error);
  94. };
  95. onPanelError = (message: string) => {
  96. if (this.state.errorMessage !== message) {
  97. this.setState({ errorMessage: message });
  98. }
  99. };
  100. clearErrorState() {
  101. if (this.state.errorMessage) {
  102. this.setState({ errorMessage: null });
  103. }
  104. }
  105. get isVisible() {
  106. return !this.props.dashboard.otherPanelInFullscreen(this.props.panel);
  107. }
  108. get hasPanelSnapshot() {
  109. const { panel } = this.props;
  110. return panel.snapshotData && panel.snapshotData.length;
  111. }
  112. get needsQueryExecution() {
  113. return this.hasPanelSnapshot || this.props.plugin.dataFormats.length > 0;
  114. }
  115. get getDataForPanel() {
  116. return this.hasPanelSnapshot ? getProcessedSeriesData(this.props.panel.snapshotData) : null;
  117. }
  118. renderPanelPlugin(loading: LoadingState, data: SeriesData[], width: number, height: number): JSX.Element {
  119. const { panel, plugin } = this.props;
  120. const { timeRange, renderCounter } = this.state;
  121. const PanelComponent = plugin.exports.reactPanel.panel;
  122. // This is only done to increase a counter that is used by backend
  123. // image rendering (phantomjs/headless chrome) to know when to capture image
  124. if (loading === LoadingState.Done) {
  125. profiler.renderingCompleted(panel.id);
  126. }
  127. return (
  128. <div className="panel-content">
  129. <PanelComponent
  130. loading={loading}
  131. data={data}
  132. timeRange={timeRange}
  133. options={panel.getOptions(plugin.exports.reactPanel.defaults)}
  134. width={width - 2 * config.theme.panelPadding.horizontal}
  135. height={height - PANEL_HEADER_HEIGHT - config.theme.panelPadding.vertical}
  136. renderCounter={renderCounter}
  137. replaceVariables={this.replaceVariables}
  138. />
  139. </div>
  140. );
  141. }
  142. renderPanelBody = (width: number, height: number): JSX.Element => {
  143. const { panel } = this.props;
  144. const { refreshCounter, timeRange } = this.state;
  145. const { datasource, targets } = panel;
  146. return (
  147. <>
  148. {this.needsQueryExecution ? (
  149. <DataPanel
  150. panelId={panel.id}
  151. datasource={datasource}
  152. queries={targets}
  153. timeRange={timeRange}
  154. isVisible={this.isVisible}
  155. widthPixels={width}
  156. refreshCounter={refreshCounter}
  157. scopedVars={panel.scopedVars}
  158. onDataResponse={this.onDataResponse}
  159. onError={this.onDataError}
  160. >
  161. {({ loading, data }) => {
  162. return this.renderPanelPlugin(loading, data, width, height);
  163. }}
  164. </DataPanel>
  165. ) : (
  166. this.renderPanelPlugin(LoadingState.Done, this.getDataForPanel, width, height)
  167. )}
  168. </>
  169. );
  170. };
  171. render() {
  172. const { dashboard, panel, isFullscreen } = this.props;
  173. const { errorMessage, timeInfo } = this.state;
  174. const { transparent } = panel;
  175. const containerClassNames = `panel-container panel-container--absolute ${transparent ? 'panel-transparent' : ''}`;
  176. return (
  177. <AutoSizer>
  178. {({ width, height }) => {
  179. if (width === 0) {
  180. return null;
  181. }
  182. return (
  183. <div className={containerClassNames}>
  184. <PanelHeader
  185. panel={panel}
  186. dashboard={dashboard}
  187. timeInfo={timeInfo}
  188. title={panel.title}
  189. description={panel.description}
  190. scopedVars={panel.scopedVars}
  191. links={panel.links}
  192. error={errorMessage}
  193. isFullscreen={isFullscreen}
  194. />
  195. <ErrorBoundary>
  196. {({ error, errorInfo }) => {
  197. if (errorInfo) {
  198. this.onPanelError(error.message || DEFAULT_PLUGIN_ERROR);
  199. return null;
  200. }
  201. return this.renderPanelBody(width, height);
  202. }}
  203. </ErrorBoundary>
  204. </div>
  205. );
  206. }}
  207. </AutoSizer>
  208. );
  209. }
  210. }