grafana/public/app/features/explore/Explore.tsx

343 lines
10 KiB
TypeScript
Raw Normal View History

2019-01-17 10:59:47 -06:00
// Libraries
2019-02-04 04:25:07 -06:00
import React, { ComponentClass } from 'react';
2018-04-26 04:58:42 -05:00
import { hot } from 'react-hot-loader';
// @ts-ignore
2019-01-10 07:24:31 -06:00
import { connect } from 'react-redux';
import _ from 'lodash';
2019-01-11 11:26:56 -06:00
import { AutoSizer } from 'react-virtualized';
2019-01-17 10:59:47 -06:00
// Services & Utils
import store from 'app/core/store';
2019-01-17 10:59:47 -06:00
// Components
import { Alert } from './Error';
import ErrorBoundary from './ErrorBoundary';
import GraphContainer from './GraphContainer';
import LogsContainer from './LogsContainer';
import QueryRows from './QueryRows';
import TableContainer from './TableContainer';
import TimePicker, { parseTime } from './TimePicker';
2019-01-10 07:24:31 -06:00
2019-01-17 10:59:47 -06:00
// Actions
import {
changeSize,
changeTime,
initializeExplore,
modifyQueries,
scanStart,
setQueries,
refreshExplore,
reconnectDatasource,
} from './state/actions';
2018-04-26 04:58:42 -05:00
2019-01-17 10:59:47 -06:00
// Types
2019-02-05 02:32:42 -06:00
import { RawTimeRange, TimeRange, DataQuery, ExploreStartPageProps, ExploreDataSourceApi } from '@grafana/ui';
import { ExploreItemState, ExploreUrlState, RangeScanner, ExploreId, ExploreUpdateState } from 'app/types/explore';
2019-01-17 10:59:47 -06:00
import { StoreState } from 'app/types';
import { LAST_USED_DATASOURCE_KEY, ensureQueries, DEFAULT_RANGE, DEFAULT_UI_STATE } from 'app/core/utils/explore';
2019-01-17 10:59:47 -06:00
import { Emitter } from 'app/core/utils/emitter';
import { ExploreToolbar } from './ExploreToolbar';
import { scanStopAction } from './state/actionTypes';
import { NoDataSourceCallToAction } from './NoDataSourceCallToAction';
import { FadeIn } from 'app/core/components/Animations/FadeIn';
interface ExploreProps {
2019-02-04 04:25:07 -06:00
StartPage?: ComponentClass<ExploreStartPageProps>;
2019-01-11 11:26:56 -06:00
changeSize: typeof changeSize;
2019-01-10 07:24:31 -06:00
changeTime: typeof changeTime;
datasourceError: string;
2019-02-05 02:32:42 -06:00
datasourceInstance: ExploreDataSourceApi;
2019-01-10 07:24:31 -06:00
datasourceLoading: boolean | null;
datasourceMissing: boolean;
2019-01-11 11:26:56 -06:00
exploreId: ExploreId;
2019-01-10 07:24:31 -06:00
initializeExplore: typeof initializeExplore;
2019-01-12 16:22:28 -06:00
initialized: boolean;
2019-01-10 07:24:31 -06:00
modifyQueries: typeof modifyQueries;
range: RawTimeRange;
update: ExploreUpdateState;
reconnectDatasource: typeof reconnectDatasource;
refreshExplore: typeof refreshExplore;
2019-01-10 07:24:31 -06:00
scanner?: RangeScanner;
scanning?: boolean;
scanRange?: RawTimeRange;
scanStart: typeof scanStart;
scanStopAction: typeof scanStopAction;
setQueries: typeof setQueries;
split: boolean;
2019-01-10 07:24:31 -06:00
showingStartPage?: boolean;
supportsGraph: boolean | null;
supportsLogs: boolean | null;
supportsTable: boolean | null;
2019-02-04 06:41:29 -06:00
queryKeys: string[];
urlState: ExploreUrlState;
}
/**
* Explore provides an area for quick query iteration for a given datasource.
* Once a datasource is selected it populates the query section at the top.
* When queries are run, their results are being displayed in the main section.
* The datasource determines what kind of query editor it brings, and what kind
2019-01-12 16:44:24 -06:00
* of results viewers it supports. The state is managed entirely in Redux.
2018-11-22 05:00:41 -06:00
*
2019-01-12 16:44:24 -06:00
* SPLIT VIEW
2018-11-22 05:00:41 -06:00
*
2019-01-12 16:44:24 -06:00
* Explore can have two Explore areas side-by-side. This is handled in `Wrapper.tsx`.
* Since there can be multiple Explores (e.g., left and right) each action needs
* the `exploreId` as first parameter so that the reducer knows which Explore state
* is affected.
2018-11-22 05:00:41 -06:00
*
* DATASOURCE REQUESTS
*
* A click on Run Query creates transactions for all DataQueries for all expanded
* result viewers. New runs are discarding previous runs. Upon completion a transaction
* saves the result. The result viewers construct their data from the currently existing
* transactions.
*
* The result viewers determine some of the query options sent to the datasource, e.g.,
* `format`, to indicate eventual transformations by the datasources' result transformers.
*/
2019-01-11 11:26:56 -06:00
export class Explore extends React.PureComponent<ExploreProps> {
el: any;
2018-11-23 08:12:20 -06:00
exploreEvents: Emitter;
/**
* Timepicker to control scanning
*/
timepickerRef: React.RefObject<TimePicker>;
constructor(props: ExploreProps) {
2018-04-26 04:58:42 -05:00
super(props);
2018-11-23 08:12:20 -06:00
this.exploreEvents = new Emitter();
this.timepickerRef = React.createRef();
2018-04-26 04:58:42 -05:00
}
componentDidMount() {
const { exploreId, urlState, initialized } = this.props;
const { datasource, queries, range = DEFAULT_RANGE, ui = DEFAULT_UI_STATE } = (urlState || {}) as ExploreUrlState;
const initialDatasource = datasource || store.get(LAST_USED_DATASOURCE_KEY);
const initialQueries: DataQuery[] = ensureQueries(queries);
const initialRange = { from: parseTime(range.from), to: parseTime(range.to) };
const width = this.el ? this.el.offsetWidth : 0;
// initialize the whole explore first time we mount and if browser history contains a change in datasource
if (!initialized) {
2019-01-11 11:26:56 -06:00
this.props.initializeExplore(
exploreId,
initialDatasource,
initialQueries,
initialRange,
width,
this.exploreEvents,
ui
2019-01-11 11:26:56 -06:00
);
}
2018-04-26 04:58:42 -05:00
}
componentWillUnmount() {
this.exploreEvents.removeAllListeners();
}
componentDidUpdate(prevProps: ExploreProps) {
this.refreshExplore();
}
getRef = (el: any) => {
this.el = el;
};
2019-01-10 07:24:31 -06:00
onChangeTime = (range: TimeRange, changedByScanner?: boolean) => {
if (this.props.scanning && !changedByScanner) {
2018-11-27 09:35:37 -06:00
this.onStopScanning();
}
2019-01-11 11:26:56 -06:00
this.props.changeTime(this.props.exploreId, range);
};
// Use this in help pages to set page to a single query
2018-11-21 09:28:30 -06:00
onClickExample = (query: DataQuery) => {
this.props.setQueries(this.props.exploreId, [query]);
};
onClickLabel = (key: string, value: string) => {
this.onModifyQueries({ type: 'ADD_FILTER', key, value });
};
onModifyQueries = (action: any, index?: number) => {
2019-01-10 07:24:31 -06:00
const { datasourceInstance } = this.props;
if (datasourceInstance && datasourceInstance.modifyQuery) {
const modifier = (queries: DataQuery, modification: any) => datasourceInstance.modifyQuery(queries, modification);
2019-01-11 11:26:56 -06:00
this.props.modifyQueries(this.props.exploreId, action, index, modifier);
}
};
2019-01-11 11:26:56 -06:00
onResize = (size: { height: number; width: number }) => {
this.props.changeSize(this.props.exploreId, size);
2018-04-26 04:58:42 -05:00
};
2018-11-27 09:35:37 -06:00
onStartScanning = () => {
2019-01-10 07:24:31 -06:00
// Scanner will trigger a query
const scanner = this.scanPreviousRange;
2019-01-11 11:26:56 -06:00
this.props.scanStart(this.props.exploreId, scanner);
};
2019-01-10 07:24:31 -06:00
scanPreviousRange = (): RawTimeRange => {
// Calling move() on the timepicker will trigger this.onChangeTime()
return this.timepickerRef.current.move(-1, true);
};
2018-11-27 09:35:37 -06:00
onStopScanning = () => {
this.props.scanStopAction({ exploreId: this.props.exploreId });
};
refreshExplore = () => {
const { exploreId, update } = this.props;
if (update.queries || update.ui || update.range || update.datasource) {
this.props.refreshExplore(exploreId);
}
};
renderEmptyState = () => {
return (
<div className="explore-container">
<NoDataSourceCallToAction />
</div>
);
};
onReconnect = (event: React.MouseEvent<HTMLButtonElement>) => {
const { exploreId, reconnectDatasource } = this.props;
event.preventDefault();
reconnectDatasource(exploreId);
};
2018-04-26 04:58:42 -05:00
render() {
const {
2018-10-30 08:38:34 -05:00
StartPage,
2019-01-10 07:24:31 -06:00
datasourceInstance,
2018-04-26 04:58:42 -05:00
datasourceError,
datasourceLoading,
datasourceMissing,
2019-01-11 11:26:56 -06:00
exploreId,
showingStartPage,
2019-01-10 07:24:31 -06:00
split,
supportsGraph,
supportsLogs,
supportsTable,
2019-02-04 06:41:29 -06:00
queryKeys,
2019-01-10 07:24:31 -06:00
} = this.props;
const exploreClass = split ? 'explore explore-split' : 'explore';
2018-04-26 04:58:42 -05:00
return (
<div className={exploreClass} ref={this.getRef}>
<ExploreToolbar exploreId={exploreId} timepickerRef={this.timepickerRef} onChangeTime={this.onChangeTime} />
{datasourceLoading ? <div className="explore-container">Loading datasource...</div> : null}
{datasourceMissing ? this.renderEmptyState() : null}
<FadeIn duration={datasourceError ? 150 : 5} in={datasourceError ? true : false}>
<div className="explore-container">
<Alert
message={`Error connecting to datasource: ${datasourceError}`}
button={{ text: 'Reconnect', onClick: this.onReconnect }}
/>
</div>
</FadeIn>
{datasourceInstance && (
<div className="explore-container">
<QueryRows exploreEvents={this.exploreEvents} exploreId={exploreId} queryKeys={queryKeys} />
<AutoSizer onResize={this.onResize} disableHeight>
{({ width }) => {
if (width === 0) {
return null;
}
return (
<main className="m-t-2" style={{ width }}>
<ErrorBoundary>
{showingStartPage && <StartPage onClickExample={this.onClickExample} />}
{!showingStartPage && (
<>
{supportsGraph && !supportsLogs && <GraphContainer width={width} exploreId={exploreId} />}
{supportsTable && <TableContainer exploreId={exploreId} onClickCell={this.onClickLabel} />}
{supportsLogs && (
<LogsContainer
width={width}
exploreId={exploreId}
onChangeTime={this.onChangeTime}
onClickLabel={this.onClickLabel}
onStartScanning={this.onStartScanning}
onStopScanning={this.onStopScanning}
/>
)}
</>
)}
</ErrorBoundary>
</main>
);
}}
</AutoSizer>
</div>
)}
2018-04-26 04:58:42 -05:00
</div>
);
}
}
function mapStateToProps(state: StoreState, { exploreId }: ExploreProps) {
2019-01-11 11:26:56 -06:00
const explore = state.explore;
const { split } = explore;
const item: ExploreItemState = explore[exploreId];
2019-01-10 07:24:31 -06:00
const {
StartPage,
datasourceError,
datasourceInstance,
datasourceLoading,
datasourceMissing,
2019-01-12 16:22:28 -06:00
initialized,
2019-01-10 07:24:31 -06:00
range,
showingStartPage,
supportsGraph,
supportsLogs,
supportsTable,
2019-02-04 06:41:29 -06:00
queryKeys,
urlState,
update,
2019-01-11 11:26:56 -06:00
} = item;
2019-01-10 07:24:31 -06:00
return {
StartPage,
datasourceError,
datasourceInstance,
datasourceLoading,
datasourceMissing,
2019-01-12 16:22:28 -06:00
initialized,
2019-01-10 07:24:31 -06:00
range,
showingStartPage,
2019-01-11 11:26:56 -06:00
split,
2019-01-10 07:24:31 -06:00
supportsGraph,
supportsLogs,
supportsTable,
2019-02-04 06:41:29 -06:00
queryKeys,
urlState,
update,
2019-01-10 07:24:31 -06:00
};
}
const mapDispatchToProps = {
2019-01-11 11:26:56 -06:00
changeSize,
2019-01-10 07:24:31 -06:00
changeTime,
initializeExplore,
modifyQueries,
reconnectDatasource,
refreshExplore,
2019-01-10 07:24:31 -06:00
scanStart,
scanStopAction,
setQueries,
2019-01-10 07:24:31 -06:00
};
export default hot(module)(
connect(
mapStateToProps,
mapDispatchToProps
)(Explore)
);