PanelChrome.tsx 7.3 KB

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