PanelChrome.tsx 6.8 KB

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