Explore.tsx 9.0 KB

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