Explore.tsx 9.8 KB

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