Explore.tsx 10 KB

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