Explore.tsx 10.0 KB

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