Explore.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // Libraries
  2. import React, { ComponentClass } from 'react';
  3. import { hot } from 'react-hot-loader';
  4. // @ts-ignore
  5. import { connect } from 'react-redux';
  6. import _ from 'lodash';
  7. import { AutoSizer } from 'react-virtualized';
  8. // Services & Utils
  9. import store from 'app/core/store';
  10. // Components
  11. import { Alert } from './Error';
  12. import ErrorBoundary from './ErrorBoundary';
  13. import GraphContainer from './GraphContainer';
  14. import LogsContainer from './LogsContainer';
  15. import QueryRows from './QueryRows';
  16. import TableContainer from './TableContainer';
  17. import TimePicker from './TimePicker';
  18. // Actions
  19. import {
  20. changeSize,
  21. changeTime,
  22. initializeExplore,
  23. modifyQueries,
  24. scanStart,
  25. setQueries,
  26. refreshExplore,
  27. reconnectDatasource,
  28. } from './state/actions';
  29. // Types
  30. import { RawTimeRange, DataQuery, ExploreStartPageProps, ExploreDataSourceApi } from '@grafana/ui';
  31. import {
  32. ExploreItemState,
  33. ExploreUrlState,
  34. RangeScanner,
  35. ExploreId,
  36. ExploreUpdateState,
  37. ExploreUIState,
  38. } from 'app/types/explore';
  39. import { StoreState } from 'app/types';
  40. import {
  41. LAST_USED_DATASOURCE_KEY,
  42. ensureQueries,
  43. DEFAULT_RANGE,
  44. DEFAULT_UI_STATE,
  45. getTimeRangeFromUrl,
  46. } from 'app/core/utils/explore';
  47. import { Emitter } from 'app/core/utils/emitter';
  48. import { ExploreToolbar } from './ExploreToolbar';
  49. import { scanStopAction } from './state/actionTypes';
  50. import { NoDataSourceCallToAction } from './NoDataSourceCallToAction';
  51. import { FadeIn } from 'app/core/components/Animations/FadeIn';
  52. import { getTimeZone } from '../profile/state/selectors';
  53. interface ExploreProps {
  54. StartPage?: ComponentClass<ExploreStartPageProps>;
  55. changeSize: typeof changeSize;
  56. changeTime: typeof changeTime;
  57. datasourceError: string;
  58. datasourceInstance: ExploreDataSourceApi;
  59. datasourceLoading: boolean | null;
  60. datasourceMissing: boolean;
  61. exploreId: ExploreId;
  62. initializeExplore: typeof initializeExplore;
  63. initialized: boolean;
  64. modifyQueries: typeof modifyQueries;
  65. update: ExploreUpdateState;
  66. reconnectDatasource: typeof reconnectDatasource;
  67. refreshExplore: typeof refreshExplore;
  68. scanner?: RangeScanner;
  69. scanning?: boolean;
  70. scanRange?: RawTimeRange;
  71. scanStart: typeof scanStart;
  72. scanStopAction: typeof scanStopAction;
  73. setQueries: typeof setQueries;
  74. split: boolean;
  75. showingStartPage?: boolean;
  76. supportsGraph: boolean | null;
  77. supportsLogs: boolean | null;
  78. supportsTable: boolean | null;
  79. queryKeys: string[];
  80. initialDatasource: string;
  81. initialQueries: DataQuery[];
  82. initialRange: RawTimeRange;
  83. initialUI: ExploreUIState;
  84. }
  85. /**
  86. * Explore provides an area for quick query iteration for a given datasource.
  87. * Once a datasource is selected it populates the query section at the top.
  88. * When queries are run, their results are being displayed in the main section.
  89. * The datasource determines what kind of query editor it brings, and what kind
  90. * of results viewers it supports. The state is managed entirely in Redux.
  91. *
  92. * SPLIT VIEW
  93. *
  94. * Explore can have two Explore areas side-by-side. This is handled in `Wrapper.tsx`.
  95. * Since there can be multiple Explores (e.g., left and right) each action needs
  96. * the `exploreId` as first parameter so that the reducer knows which Explore state
  97. * is affected.
  98. *
  99. * DATASOURCE REQUESTS
  100. *
  101. * A click on Run Query creates transactions for all DataQueries for all expanded
  102. * result viewers. New runs are discarding previous runs. Upon completion a transaction
  103. * saves the result. The result viewers construct their data from the currently existing
  104. * transactions.
  105. *
  106. * The result viewers determine some of the query options sent to the datasource, e.g.,
  107. * `format`, to indicate eventual transformations by the datasources' result transformers.
  108. */
  109. export class Explore extends React.PureComponent<ExploreProps> {
  110. el: any;
  111. exploreEvents: Emitter;
  112. /**
  113. * Timepicker to control scanning
  114. */
  115. timepickerRef: React.RefObject<TimePicker>;
  116. constructor(props: ExploreProps) {
  117. super(props);
  118. this.exploreEvents = new Emitter();
  119. this.timepickerRef = React.createRef();
  120. }
  121. componentDidMount() {
  122. const { initialized, exploreId, initialDatasource, initialQueries, initialRange, initialUI } = this.props;
  123. const width = this.el ? this.el.offsetWidth : 0;
  124. // initialize the whole explore first time we mount and if browser history contains a change in datasource
  125. if (!initialized) {
  126. this.props.initializeExplore(
  127. exploreId,
  128. initialDatasource,
  129. initialQueries,
  130. initialRange,
  131. width,
  132. this.exploreEvents,
  133. initialUI
  134. );
  135. }
  136. }
  137. componentWillUnmount() {
  138. this.exploreEvents.removeAllListeners();
  139. }
  140. componentDidUpdate(prevProps: ExploreProps) {
  141. this.refreshExplore();
  142. }
  143. getRef = (el: any) => {
  144. this.el = el;
  145. };
  146. onChangeTime = (range: RawTimeRange, changedByScanner?: boolean) => {
  147. if (this.props.scanning && !changedByScanner) {
  148. this.onStopScanning();
  149. }
  150. this.props.changeTime(this.props.exploreId, range);
  151. };
  152. // Use this in help pages to set page to a single query
  153. onClickExample = (query: DataQuery) => {
  154. this.props.setQueries(this.props.exploreId, [query]);
  155. };
  156. onClickLabel = (key: string, value: string) => {
  157. this.onModifyQueries({ type: 'ADD_FILTER', key, value });
  158. };
  159. onModifyQueries = (action: any, index?: number) => {
  160. const { datasourceInstance } = this.props;
  161. if (datasourceInstance && datasourceInstance.modifyQuery) {
  162. const modifier = (queries: DataQuery, modification: any) => datasourceInstance.modifyQuery(queries, modification);
  163. this.props.modifyQueries(this.props.exploreId, action, index, modifier);
  164. }
  165. };
  166. onResize = (size: { height: number; width: number }) => {
  167. this.props.changeSize(this.props.exploreId, size);
  168. };
  169. onStartScanning = () => {
  170. // Scanner will trigger a query
  171. const scanner = this.scanPreviousRange;
  172. this.props.scanStart(this.props.exploreId, scanner);
  173. };
  174. scanPreviousRange = (): RawTimeRange => {
  175. // Calling move() on the timepicker will trigger this.onChangeTime()
  176. return this.timepickerRef.current.move(-1, true);
  177. };
  178. onStopScanning = () => {
  179. this.props.scanStopAction({ exploreId: this.props.exploreId });
  180. };
  181. refreshExplore = () => {
  182. const { exploreId, update } = this.props;
  183. if (update.queries || update.ui || update.range || update.datasource) {
  184. this.props.refreshExplore(exploreId);
  185. }
  186. };
  187. renderEmptyState = () => {
  188. return (
  189. <div className="explore-container">
  190. <NoDataSourceCallToAction />
  191. </div>
  192. );
  193. };
  194. onReconnect = (event: React.MouseEvent<HTMLButtonElement>) => {
  195. const { exploreId, reconnectDatasource } = this.props;
  196. event.preventDefault();
  197. reconnectDatasource(exploreId);
  198. };
  199. render() {
  200. const {
  201. StartPage,
  202. datasourceInstance,
  203. datasourceError,
  204. datasourceLoading,
  205. datasourceMissing,
  206. exploreId,
  207. showingStartPage,
  208. split,
  209. supportsGraph,
  210. supportsLogs,
  211. supportsTable,
  212. queryKeys,
  213. } = this.props;
  214. const exploreClass = split ? 'explore explore-split' : 'explore';
  215. return (
  216. <div className={exploreClass} ref={this.getRef}>
  217. <ExploreToolbar exploreId={exploreId} timepickerRef={this.timepickerRef} onChangeTime={this.onChangeTime} />
  218. {datasourceLoading ? <div className="explore-container">Loading datasource...</div> : null}
  219. {datasourceMissing ? this.renderEmptyState() : null}
  220. <FadeIn duration={datasourceError ? 150 : 5} in={datasourceError ? true : false}>
  221. <div className="explore-container">
  222. <Alert
  223. message={`Error connecting to datasource: ${datasourceError}`}
  224. button={{ text: 'Reconnect', onClick: this.onReconnect }}
  225. />
  226. </div>
  227. </FadeIn>
  228. {datasourceInstance && (
  229. <div className="explore-container">
  230. <QueryRows exploreEvents={this.exploreEvents} exploreId={exploreId} queryKeys={queryKeys} />
  231. <AutoSizer onResize={this.onResize} disableHeight>
  232. {({ width }) => {
  233. if (width === 0) {
  234. return null;
  235. }
  236. return (
  237. <main className="m-t-2" style={{ width }}>
  238. <ErrorBoundary>
  239. {showingStartPage && <StartPage onClickExample={this.onClickExample} />}
  240. {!showingStartPage && (
  241. <>
  242. {supportsGraph && !supportsLogs && <GraphContainer width={width} exploreId={exploreId} />}
  243. {supportsTable && <TableContainer exploreId={exploreId} onClickCell={this.onClickLabel} />}
  244. {supportsLogs && (
  245. <LogsContainer
  246. width={width}
  247. exploreId={exploreId}
  248. onChangeTime={this.onChangeTime}
  249. onClickLabel={this.onClickLabel}
  250. onStartScanning={this.onStartScanning}
  251. onStopScanning={this.onStopScanning}
  252. />
  253. )}
  254. </>
  255. )}
  256. </ErrorBoundary>
  257. </main>
  258. );
  259. }}
  260. </AutoSizer>
  261. </div>
  262. )}
  263. </div>
  264. );
  265. }
  266. }
  267. function mapStateToProps(state: StoreState, { exploreId }: ExploreProps) {
  268. const explore = state.explore;
  269. const { split } = explore;
  270. const item: ExploreItemState = explore[exploreId];
  271. const timeZone = getTimeZone(state.user);
  272. const {
  273. StartPage,
  274. datasourceError,
  275. datasourceInstance,
  276. datasourceLoading,
  277. datasourceMissing,
  278. initialized,
  279. showingStartPage,
  280. supportsGraph,
  281. supportsLogs,
  282. supportsTable,
  283. queryKeys,
  284. urlState,
  285. update,
  286. } = item;
  287. const { datasource, queries, range: urlRange, ui } = (urlState || {}) as ExploreUrlState;
  288. const initialDatasource = datasource || store.get(LAST_USED_DATASOURCE_KEY);
  289. const initialQueries: DataQuery[] = ensureQueries(queries);
  290. const initialRange = urlRange ? getTimeRangeFromUrl(urlRange, timeZone).raw : DEFAULT_RANGE;
  291. const initialUI = ui || DEFAULT_UI_STATE;
  292. return {
  293. StartPage,
  294. datasourceError,
  295. datasourceInstance,
  296. datasourceLoading,
  297. datasourceMissing,
  298. initialized,
  299. showingStartPage,
  300. split,
  301. supportsGraph,
  302. supportsLogs,
  303. supportsTable,
  304. queryKeys,
  305. update,
  306. initialDatasource,
  307. initialQueries,
  308. initialRange,
  309. initialUI,
  310. };
  311. }
  312. const mapDispatchToProps = {
  313. changeSize,
  314. changeTime,
  315. initializeExplore,
  316. modifyQueries,
  317. reconnectDatasource,
  318. refreshExplore,
  319. scanStart,
  320. scanStopAction,
  321. setQueries,
  322. };
  323. export default hot(module)(
  324. connect(
  325. mapStateToProps,
  326. mapDispatchToProps
  327. )(Explore)
  328. );