PanelChrome.tsx 8.4 KB

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