Explore.tsx 12 KB

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