Explore.tsx 12 KB

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