Explore.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 } 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 } = (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. );
  115. }
  116. }
  117. componentWillUnmount() {
  118. this.exploreEvents.removeAllListeners();
  119. }
  120. getRef = el => {
  121. this.el = el;
  122. };
  123. onChangeTime = (range: TimeRange, changedByScanner?: boolean) => {
  124. if (this.props.scanning && !changedByScanner) {
  125. this.onStopScanning();
  126. }
  127. this.props.changeTime(this.props.exploreId, range);
  128. };
  129. // Use this in help pages to set page to a single query
  130. onClickExample = (query: DataQuery) => {
  131. this.props.setQueries(this.props.exploreId, [query]);
  132. };
  133. onClickLabel = (key: string, value: string) => {
  134. this.onModifyQueries({ type: 'ADD_FILTER', key, value });
  135. };
  136. onModifyQueries = (action, index?: number) => {
  137. const { datasourceInstance } = this.props;
  138. if (datasourceInstance && datasourceInstance.modifyQuery) {
  139. const modifier = (queries: DataQuery, modification: any) => datasourceInstance.modifyQuery(queries, modification);
  140. this.props.modifyQueries(this.props.exploreId, action, index, modifier);
  141. }
  142. };
  143. onResize = (size: { height: number; width: number }) => {
  144. this.props.changeSize(this.props.exploreId, size);
  145. };
  146. onStartScanning = () => {
  147. // Scanner will trigger a query
  148. const scanner = this.scanPreviousRange;
  149. this.props.scanStart(this.props.exploreId, scanner);
  150. };
  151. scanPreviousRange = (): RawTimeRange => {
  152. // Calling move() on the timepicker will trigger this.onChangeTime()
  153. return this.timepickerRef.current.move(-1, true);
  154. };
  155. onStopScanning = () => {
  156. this.props.scanStop(this.props.exploreId);
  157. };
  158. render() {
  159. const {
  160. StartPage,
  161. datasourceInstance,
  162. datasourceError,
  163. datasourceLoading,
  164. datasourceMissing,
  165. exploreId,
  166. initialQueries,
  167. showingStartPage,
  168. split,
  169. supportsGraph,
  170. supportsLogs,
  171. supportsTable,
  172. } = this.props;
  173. const exploreClass = split ? 'explore explore-split' : 'explore';
  174. return (
  175. <div className={exploreClass} ref={this.getRef}>
  176. <ExploreToolbar exploreId={exploreId} timepickerRef={this.timepickerRef} onChangeTime={this.onChangeTime} />
  177. {datasourceLoading ? <div className="explore-container">Loading datasource...</div> : null}
  178. {datasourceMissing ? (
  179. <div className="explore-container">Please add a datasource that supports Explore (e.g., Prometheus).</div>
  180. ) : null}
  181. {datasourceError && (
  182. <div className="explore-container">
  183. <Alert message={`Error connecting to datasource: ${datasourceError}`} />
  184. </div>
  185. )}
  186. {datasourceInstance &&
  187. !datasourceError && (
  188. <div className="explore-container">
  189. <QueryRows exploreEvents={this.exploreEvents} exploreId={exploreId} initialQueries={initialQueries} />
  190. <AutoSizer onResize={this.onResize} disableHeight>
  191. {({ width }) => (
  192. <main className="m-t-2" style={{ width }}>
  193. <ErrorBoundary>
  194. {showingStartPage && <StartPage onClickExample={this.onClickExample} />}
  195. {!showingStartPage && (
  196. <>
  197. {supportsGraph && <GraphContainer exploreId={exploreId} />}
  198. {supportsTable && <TableContainer exploreId={exploreId} onClickCell={this.onClickLabel} />}
  199. {supportsLogs && (
  200. <LogsContainer
  201. exploreId={exploreId}
  202. onChangeTime={this.onChangeTime}
  203. onClickLabel={this.onClickLabel}
  204. onStartScanning={this.onStartScanning}
  205. onStopScanning={this.onStopScanning}
  206. />
  207. )}
  208. </>
  209. )}
  210. </ErrorBoundary>
  211. </main>
  212. )}
  213. </AutoSizer>
  214. </div>
  215. )}
  216. </div>
  217. );
  218. }
  219. }
  220. function mapStateToProps(state: StoreState, { exploreId }) {
  221. const explore = state.explore;
  222. const { split } = explore;
  223. const item: ExploreItemState = explore[exploreId];
  224. const {
  225. StartPage,
  226. datasourceError,
  227. datasourceInstance,
  228. datasourceLoading,
  229. datasourceMissing,
  230. initialQueries,
  231. initialized,
  232. range,
  233. showingStartPage,
  234. supportsGraph,
  235. supportsLogs,
  236. supportsTable,
  237. } = item;
  238. return {
  239. StartPage,
  240. datasourceError,
  241. datasourceInstance,
  242. datasourceLoading,
  243. datasourceMissing,
  244. initialQueries,
  245. initialized,
  246. range,
  247. showingStartPage,
  248. split,
  249. supportsGraph,
  250. supportsLogs,
  251. supportsTable,
  252. };
  253. }
  254. const mapDispatchToProps = {
  255. changeSize,
  256. changeTime,
  257. initializeExplore,
  258. modifyQueries,
  259. scanStart,
  260. scanStop,
  261. setQueries,
  262. };
  263. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(Explore));