PanelChrome.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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, PanelData } 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 {
  126. state: LoadingState.Done,
  127. series: this.hasPanelSnapshot ? getProcessedSeriesData(this.props.panel.snapshotData) : [],
  128. };
  129. }
  130. renderPanelPlugin(data: PanelData, width: number, height: number): JSX.Element {
  131. const { panel, plugin } = this.props;
  132. const { timeRange, renderCounter } = this.state;
  133. const PanelComponent = plugin.reactPlugin.panel;
  134. // This is only done to increase a counter that is used by backend
  135. // image rendering (phantomjs/headless chrome) to know when to capture image
  136. if (data.state === LoadingState.Done) {
  137. profiler.renderingCompleted(panel.id);
  138. }
  139. return (
  140. <div className="panel-content">
  141. <PanelComponent
  142. data={data}
  143. timeRange={timeRange}
  144. options={panel.getOptions(plugin.reactPlugin.defaults)}
  145. width={width - 2 * config.theme.panelPadding.horizontal}
  146. height={height - PANEL_HEADER_HEIGHT - config.theme.panelPadding.vertical}
  147. renderCounter={renderCounter}
  148. replaceVariables={this.replaceVariables}
  149. />
  150. </div>
  151. );
  152. }
  153. renderPanelBody = (width: number, height: number): JSX.Element => {
  154. const { panel } = this.props;
  155. const { refreshCounter, timeRange } = this.state;
  156. const { datasource, targets } = panel;
  157. return (
  158. <>
  159. {this.needsQueryExecution ? (
  160. <DataPanel
  161. panelId={panel.id}
  162. datasource={datasource}
  163. queries={targets}
  164. timeRange={timeRange}
  165. isVisible={this.isVisible}
  166. widthPixels={width}
  167. refreshCounter={refreshCounter}
  168. scopedVars={panel.scopedVars}
  169. onDataResponse={this.onDataResponse}
  170. onError={this.onDataError}
  171. >
  172. {({ data }) => {
  173. return this.renderPanelPlugin(data, width, height);
  174. }}
  175. </DataPanel>
  176. ) : (
  177. this.renderPanelPlugin(this.getDataForPanel, width, height)
  178. )}
  179. </>
  180. );
  181. };
  182. render() {
  183. const { dashboard, panel, isFullscreen } = this.props;
  184. const { errorMessage, timeInfo } = this.state;
  185. const { transparent } = panel;
  186. const containerClassNames = `panel-container panel-container--absolute ${transparent ? 'panel-transparent' : ''}`;
  187. return (
  188. <AutoSizer>
  189. {({ width, height }) => {
  190. if (width === 0) {
  191. return null;
  192. }
  193. return (
  194. <div className={containerClassNames}>
  195. <PanelHeader
  196. panel={panel}
  197. dashboard={dashboard}
  198. timeInfo={timeInfo}
  199. title={panel.title}
  200. description={panel.description}
  201. scopedVars={panel.scopedVars}
  202. links={panel.links}
  203. error={errorMessage}
  204. isFullscreen={isFullscreen}
  205. />
  206. <ErrorBoundary>
  207. {({ error, errorInfo }) => {
  208. if (errorInfo) {
  209. this.onPanelError(error.message || DEFAULT_PLUGIN_ERROR);
  210. return null;
  211. }
  212. return this.renderPanelBody(width, height);
  213. }}
  214. </ErrorBoundary>
  215. </div>
  216. );
  217. }}
  218. </AutoSizer>
  219. );
  220. }
  221. }