Explore.tsx 10 KB

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