PanelChrome.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Services
  4. import { getTimeSrv, TimeSrv } from '../services/TimeSrv';
  5. // Components
  6. import { PanelHeader } from './PanelHeader/PanelHeader';
  7. import ErrorBoundary from 'app/core/components/ErrorBoundary/ErrorBoundary';
  8. // Utils
  9. import { applyPanelTimeOverrides } from 'app/features/dashboard/utils/panel';
  10. import { PANEL_HEADER_HEIGHT } from 'app/core/constants';
  11. import { profiler } from 'app/core/profiler';
  12. import config from 'app/core/config';
  13. // Types
  14. import { DashboardModel, PanelModel } from '../state';
  15. import { PanelPluginMeta, LoadingState, PanelData } from '@grafana/ui';
  16. import { ScopedVars } from '@grafana/ui';
  17. import templateSrv from 'app/features/templating/template_srv';
  18. import { getProcessedSeriesData } from '../state/PanelQueryState';
  19. import { Unsubscribable } from 'rxjs';
  20. const DEFAULT_PLUGIN_ERROR = 'Error in plugin';
  21. export interface Props {
  22. panel: PanelModel;
  23. dashboard: DashboardModel;
  24. plugin: PanelPluginMeta;
  25. isFullscreen: boolean;
  26. width: number;
  27. height: number;
  28. }
  29. export interface State {
  30. isFirstLoad: boolean;
  31. renderCounter: number;
  32. errorMessage: string | null;
  33. // Current state of all events
  34. data: PanelData;
  35. }
  36. export class PanelChrome extends PureComponent<Props, State> {
  37. timeSrv: TimeSrv = getTimeSrv();
  38. querySubscription: Unsubscribable;
  39. delayedStateUpdate: Partial<State>;
  40. constructor(props: Props) {
  41. super(props);
  42. this.state = {
  43. isFirstLoad: true,
  44. renderCounter: 0,
  45. errorMessage: null,
  46. data: {
  47. state: LoadingState.NotStarted,
  48. series: [],
  49. },
  50. };
  51. }
  52. componentDidMount() {
  53. const { panel, dashboard } = this.props;
  54. panel.events.on('refresh', this.onRefresh);
  55. panel.events.on('render', this.onRender);
  56. dashboard.panelInitialized(this.props.panel);
  57. // Move snapshot data into the query response
  58. if (this.hasPanelSnapshot) {
  59. this.setState({
  60. data: {
  61. state: LoadingState.Done,
  62. series: getProcessedSeriesData(panel.snapshotData),
  63. },
  64. isFirstLoad: false,
  65. });
  66. } else if (!this.wantsQueryExecution) {
  67. this.setState({ isFirstLoad: false });
  68. }
  69. }
  70. componentWillUnmount() {
  71. this.props.panel.events.off('refresh', this.onRefresh);
  72. if (this.querySubscription) {
  73. this.querySubscription.unsubscribe();
  74. this.querySubscription = null;
  75. }
  76. }
  77. // Updates the response with information from the stream
  78. // The next is outside a react synthetic event so setState is not batched
  79. // So in this context we can only do a single call to setState
  80. panelDataObserver = {
  81. next: (data: PanelData) => {
  82. let { errorMessage, isFirstLoad } = this.state;
  83. if (data.state === LoadingState.Error) {
  84. const { error } = data;
  85. if (error) {
  86. if (this.state.errorMessage !== error.message) {
  87. errorMessage = error.message;
  88. }
  89. }
  90. } else {
  91. errorMessage = null;
  92. }
  93. if (data.state === LoadingState.Done) {
  94. // If we are doing a snapshot save data in panel model
  95. if (this.props.dashboard.snapshot) {
  96. this.props.panel.snapshotData = data.series;
  97. }
  98. if (this.state.isFirstLoad) {
  99. isFirstLoad = false;
  100. }
  101. }
  102. const stateUpdate = { isFirstLoad, errorMessage, data };
  103. if (this.isVisible) {
  104. this.setState(stateUpdate);
  105. } else {
  106. // if we are getting data while another panel is in fullscreen / edit mode
  107. // we need to store the data but not update state yet
  108. this.delayedStateUpdate = stateUpdate;
  109. }
  110. },
  111. };
  112. onRefresh = () => {
  113. console.log('onRefresh');
  114. if (!this.isVisible) {
  115. return;
  116. }
  117. const { panel, width } = this.props;
  118. const timeData = applyPanelTimeOverrides(panel, this.timeSrv.timeRange());
  119. // Issue Query
  120. if (this.wantsQueryExecution) {
  121. if (width < 0) {
  122. console.log('Refresh skippted, no width yet... wait till we know');
  123. return;
  124. }
  125. const queryRunner = panel.getQueryRunner();
  126. if (!this.querySubscription) {
  127. this.querySubscription = queryRunner.subscribe(this.panelDataObserver);
  128. }
  129. queryRunner.run({
  130. datasource: panel.datasource,
  131. queries: panel.targets,
  132. panelId: panel.id,
  133. dashboardId: this.props.dashboard.id,
  134. timezone: this.props.dashboard.timezone,
  135. timeRange: timeData.timeRange,
  136. timeInfo: timeData.timeInfo,
  137. widthPixels: width,
  138. maxDataPoints: panel.maxDataPoints,
  139. minInterval: panel.interval,
  140. scopedVars: panel.scopedVars,
  141. cacheTimeout: panel.cacheTimeout,
  142. });
  143. }
  144. };
  145. onRender = () => {
  146. const stateUpdate = { renderCounter: this.state.renderCounter + 1 };
  147. // If we have received a data update while hidden copy over that state as well
  148. if (this.delayedStateUpdate) {
  149. Object.assign(stateUpdate, this.delayedStateUpdate);
  150. this.delayedStateUpdate = null;
  151. }
  152. this.setState(stateUpdate);
  153. };
  154. onOptionsChange = (options: any) => {
  155. this.props.panel.updateOptions(options);
  156. };
  157. replaceVariables = (value: string, extraVars?: ScopedVars, format?: string) => {
  158. let vars = this.props.panel.scopedVars;
  159. if (extraVars) {
  160. vars = vars ? { ...vars, ...extraVars } : extraVars;
  161. }
  162. return templateSrv.replace(value, vars, format);
  163. };
  164. onPanelError = (message: string) => {
  165. if (this.state.errorMessage !== message) {
  166. this.setState({ errorMessage: message });
  167. }
  168. };
  169. get isVisible() {
  170. return !this.props.dashboard.otherPanelInFullscreen(this.props.panel);
  171. }
  172. get hasPanelSnapshot() {
  173. const { panel } = this.props;
  174. return panel.snapshotData && panel.snapshotData.length;
  175. }
  176. get wantsQueryExecution() {
  177. return this.props.plugin.dataFormats.length > 0 && !this.hasPanelSnapshot;
  178. }
  179. renderPanel(width: number, height: number): JSX.Element {
  180. const { panel, plugin } = this.props;
  181. const { renderCounter, data, isFirstLoad } = this.state;
  182. const PanelComponent = plugin.panelPlugin.panel;
  183. // This is only done to increase a counter that is used by backend
  184. // image rendering (phantomjs/headless chrome) to know when to capture image
  185. const loading = data.state;
  186. if (loading === LoadingState.Done) {
  187. profiler.renderingCompleted(panel.id);
  188. }
  189. // do not render component until we have first data
  190. if (isFirstLoad && (loading === LoadingState.Loading || loading === LoadingState.NotStarted)) {
  191. return this.renderLoadingState();
  192. }
  193. return (
  194. <>
  195. {loading === LoadingState.Loading && this.renderLoadingState()}
  196. <div className="panel-content">
  197. <PanelComponent
  198. data={data}
  199. timeRange={data.request ? data.request.range : this.timeSrv.timeRange()}
  200. options={panel.getOptions(plugin.panelPlugin.defaults)}
  201. width={width - 2 * config.theme.panelPadding.horizontal}
  202. height={height - PANEL_HEADER_HEIGHT - config.theme.panelPadding.vertical}
  203. renderCounter={renderCounter}
  204. replaceVariables={this.replaceVariables}
  205. onOptionsChange={this.onOptionsChange}
  206. />
  207. </div>
  208. </>
  209. );
  210. }
  211. private renderLoadingState(): JSX.Element {
  212. return (
  213. <div className="panel-loading">
  214. <i className="fa fa-spinner fa-spin" />
  215. </div>
  216. );
  217. }
  218. render() {
  219. const { dashboard, panel, isFullscreen, width, height } = this.props;
  220. const { errorMessage, data } = this.state;
  221. const { transparent } = panel;
  222. const containerClassNames = `panel-container panel-container--absolute ${transparent ? 'panel-transparent' : ''}`;
  223. return (
  224. <div className={containerClassNames}>
  225. <PanelHeader
  226. panel={panel}
  227. dashboard={dashboard}
  228. timeInfo={data.request ? data.request.timeInfo : null}
  229. title={panel.title}
  230. description={panel.description}
  231. scopedVars={panel.scopedVars}
  232. links={panel.links}
  233. error={errorMessage}
  234. isFullscreen={isFullscreen}
  235. />
  236. <ErrorBoundary>
  237. {({ error, errorInfo }) => {
  238. if (errorInfo) {
  239. this.onPanelError(error.message || DEFAULT_PLUGIN_ERROR);
  240. return null;
  241. }
  242. return this.renderPanel(width, height);
  243. }}
  244. </ErrorBoundary>
  245. </div>
  246. );
  247. }
  248. }