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

549 lines
18 KiB
TypeScript
Raw Normal View History

2018-04-26 04:58:42 -05:00
import React from 'react';
import { hot } from 'react-hot-loader';
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-10 07:24:31 -06:00
import { RawTimeRange, TimeRange } from '@grafana/ui';
2019-01-10 07:24:31 -06:00
import { DataSourceSelectItem } from 'app/types/datasources';
2019-01-11 11:26:56 -06:00
import { ExploreUrlState, HistoryItem, QueryTransaction, RangeScanner, ExploreId } from 'app/types/explore';
import { DataQuery } from 'app/types/series';
2019-01-11 11:26:56 -06:00
import { StoreState } from 'app/types';
import store from 'app/core/store';
2019-01-11 11:26:56 -06:00
import { LAST_USED_DATASOURCE_KEY, ensureQueries, DEFAULT_RANGE } from 'app/core/utils/explore';
2018-12-14 07:05:47 -06:00
import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker';
2018-11-23 08:12:20 -06:00
import { Emitter } from 'app/core/utils/emitter';
2019-01-10 07:24:31 -06:00
import {
addQueryRow,
changeDatasource,
changeQuery,
changeSize,
changeTime,
clickClear,
2019-01-11 11:26:56 -06:00
clickCloseSplit,
2019-01-10 07:24:31 -06:00
clickExample,
clickGraphButton,
clickLogsButton,
2019-01-11 11:26:56 -06:00
clickSplit,
2019-01-10 07:24:31 -06:00
clickTableButton,
highlightLogsExpression,
initializeExplore,
modifyQueries,
removeQueryRow,
runQueries,
scanStart,
scanStop,
} from './state/actions';
2019-01-11 11:26:56 -06:00
import { ExploreItemState } from './state/reducers';
2018-04-26 04:58:42 -05:00
import Panel from './Panel';
2018-10-30 08:38:34 -05:00
import QueryRows from './QueryRows';
import Graph from './Graph';
import Logs from './Logs';
import Table from './Table';
import ErrorBoundary from './ErrorBoundary';
import { Alert } from './Error';
import TimePicker, { parseTime } from './TimePicker';
2019-01-10 07:24:31 -06:00
import { LogsModel } from 'app/core/logs_model';
import TableModel from 'app/core/table_model';
interface ExploreProps {
2019-01-10 07:24:31 -06:00
StartPage?: any;
addQueryRow: typeof addQueryRow;
changeDatasource: typeof changeDatasource;
changeQuery: typeof changeQuery;
2019-01-11 11:26:56 -06:00
changeSize: typeof changeSize;
2019-01-10 07:24:31 -06:00
changeTime: typeof changeTime;
clickClear: typeof clickClear;
2019-01-11 11:26:56 -06:00
clickCloseSplit: typeof clickCloseSplit;
2019-01-10 07:24:31 -06:00
clickExample: typeof clickExample;
clickGraphButton: typeof clickGraphButton;
clickLogsButton: typeof clickLogsButton;
2019-01-11 11:26:56 -06:00
clickSplit: typeof clickSplit;
2019-01-10 07:24:31 -06:00
clickTableButton: typeof clickTableButton;
datasourceError: string;
datasourceInstance: any;
datasourceLoading: boolean | null;
datasourceMissing: boolean;
exploreDatasources: DataSourceSelectItem[];
2019-01-11 11:26:56 -06:00
exploreId: ExploreId;
2019-01-10 07:24:31 -06:00
graphResult?: any[];
highlightLogsExpression: typeof highlightLogsExpression;
history: HistoryItem[];
initialDatasource?: string;
initialQueries: DataQuery[];
initializeExplore: typeof initializeExplore;
logsHighlighterExpressions?: string[];
logsResult?: LogsModel;
modifyQueries: typeof modifyQueries;
queryTransactions: QueryTransaction[];
removeQueryRow: typeof removeQueryRow;
range: RawTimeRange;
runQueries: typeof runQueries;
scanner?: RangeScanner;
scanning?: boolean;
scanRange?: RawTimeRange;
scanStart: typeof scanStart;
scanStop: typeof scanStop;
split: boolean;
2019-01-10 07:24:31 -06:00
showingGraph: boolean;
showingLogs: boolean;
showingStartPage?: boolean;
showingTable: boolean;
supportsGraph: boolean | null;
supportsLogs: boolean | null;
supportsTable: boolean | null;
tableResult?: TableModel;
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
* of results viewers it supports.
2018-11-22 05:00:41 -06:00
*
* QUERY HANDLING
*
* TLDR: to not re-render Explore during edits, query editing is not "controlled"
* in a React sense: values need to be pushed down via `initialQueries`, while
* edits travel up via `this.modifiedQueries`.
*
* By default the query rows start without prior state: `initialQueries` will
* contain one empty DataQuery. While the user modifies the DataQuery, the
* modifications are being tracked in `this.modifiedQueries`, which need to be
* used whenever a query is sent to the datasource to reflect what the user sees
2018-12-05 03:17:22 -06:00
* on the screen. Query"react-popper": "^0.7.5", rows can be initialized or reset using `initialQueries`,
* by giving the respec"react-popper": "^0.7.5",tive row a new key. This wipes the old row and its state.
* This property is als"react-popper": "^0.7.5",o used to govern how many query rows there are (minimum 1).
2018-11-22 05:00:41 -06:00
*
* This flow makes sure that a query row can be arbitrarily complex without the
* fear of being wiped or re-initialized via props. The query row is free to keep
* its own state while the user edits or builds a query. Valid queries can be sent
* up to Explore via the `onChangeQuery` prop.
*
* 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>;
2018-04-26 04:58:42 -05:00
constructor(props) {
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
}
async componentDidMount() {
2019-01-11 11:26:56 -06:00
const { exploreId, split, urlState } = this.props;
if (!split) {
// Load URL state and parse range
const { datasource, queries, range = DEFAULT_RANGE } = (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;
this.props.initializeExplore(
exploreId,
initialDatasource,
initialQueries,
initialRange,
width,
this.exploreEvents
);
}
2018-04-26 04:58:42 -05:00
}
componentWillUnmount() {
this.exploreEvents.removeAllListeners();
}
getRef = el => {
this.el = el;
};
onAddQueryRow = index => {
2019-01-11 11:26:56 -06:00
this.props.addQueryRow(this.props.exploreId, index);
};
onChangeDatasource = async option => {
2019-01-11 11:26:56 -06:00
this.props.changeDatasource(this.props.exploreId, option.value);
};
2019-01-10 07:24:31 -06:00
onChangeQuery = (query: DataQuery, index: number, override?: boolean) => {
2019-01-11 11:26:56 -06:00
const { changeQuery, datasourceInstance, exploreId } = this.props;
2019-01-11 11:26:56 -06:00
changeQuery(exploreId, query, index, override);
2019-01-10 07:24:31 -06:00
if (query && !override && datasourceInstance.getHighlighterExpression && index === 0) {
// Live preview of log search matches. Only use on first row for now
this.updateLogsHighlights(query);
}
};
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);
};
onClickClear = () => {
2019-01-11 11:26:56 -06:00
this.props.clickClear(this.props.exploreId);
};
onClickCloseSplit = () => {
2019-01-11 11:26:56 -06:00
this.props.clickCloseSplit();
};
onClickGraphButton = () => {
2019-01-11 11:26:56 -06:00
this.props.clickGraphButton(this.props.exploreId);
2018-04-26 04:58:42 -05:00
};
onClickLogsButton = () => {
2019-01-11 11:26:56 -06:00
this.props.clickLogsButton(this.props.exploreId);
};
// Use this in help pages to set page to a single query
2018-11-21 09:28:30 -06:00
onClickExample = (query: DataQuery) => {
2019-01-11 11:26:56 -06:00
this.props.clickExample(this.props.exploreId, query);
};
onClickSplit = () => {
2019-01-11 11:26:56 -06:00
this.props.clickSplit();
};
onClickTableButton = () => {
2019-01-11 11:26:56 -06:00
this.props.clickTableButton(this.props.exploreId);
2018-04-26 04:58:42 -05:00
};
onClickLabel = (key: string, value: string) => {
this.onModifyQueries({ type: 'ADD_FILTER', key, value });
};
onModifyQueries = (action, index?: number) => {
2019-01-10 07:24:31 -06:00
const { datasourceInstance } = this.props;
if (datasourceInstance && datasourceInstance.modifyQuery) {
const modifier = (queries: DataQuery, action: any) => datasourceInstance.modifyQuery(queries, action);
2019-01-11 11:26:56 -06:00
this.props.modifyQueries(this.props.exploreId, action, index, modifier);
}
};
onRemoveQueryRow = index => {
2019-01-11 11:26:56 -06:00
this.props.removeQueryRow(this.props.exploreId, index);
};
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 = () => {
2019-01-11 11:26:56 -06:00
this.props.scanStop(this.props.exploreId);
};
onSubmit = () => {
2019-01-11 11:26:56 -06:00
this.props.runQueries(this.props.exploreId);
2018-04-26 04:58:42 -05:00
};
2019-01-10 07:24:31 -06:00
updateLogsHighlights = _.debounce((value: DataQuery) => {
const { datasourceInstance } = this.props;
if (datasourceInstance.getHighlighterExpression) {
const expressions = [datasourceInstance.getHighlighterExpression(value)];
2019-01-11 11:26:56 -06:00
this.props.highlightLogsExpression(this.props.exploreId, expressions);
2018-11-23 08:12:20 -06:00
}
}, 500);
2019-01-10 07:24:31 -06:00
// saveState = () => {
// const { stateKey, onSaveState } = this.props;
// onSaveState(stateKey, this.cloneState());
// };
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,
exploreDatasources,
2019-01-11 11:26:56 -06:00
exploreId,
graphResult,
history,
2018-11-21 09:28:30 -06:00
initialQueries,
logsHighlighterExpressions,
logsResult,
queryTransactions,
range,
scanning,
2018-11-27 09:35:37 -06:00
scanRange,
2018-04-26 04:58:42 -05:00
showingGraph,
showingLogs,
showingStartPage,
2018-04-26 04:58:42 -05:00
showingTable,
2019-01-10 07:24:31 -06:00
split,
supportsGraph,
supportsLogs,
supportsTable,
tableResult,
2019-01-10 07:24:31 -06:00
} = this.props;
const graphHeight = showingGraph && showingTable ? '200px' : '400px';
const exploreClass = split ? 'explore explore-split' : 'explore';
2019-01-10 07:24:31 -06:00
const selectedDatasource = datasourceInstance
? exploreDatasources.find(d => d.name === datasourceInstance.name)
: undefined;
const graphLoading = queryTransactions.some(qt => qt.resultType === 'Graph' && !qt.done);
const tableLoading = queryTransactions.some(qt => qt.resultType === 'Table' && !qt.done);
const logsLoading = queryTransactions.some(qt => qt.resultType === 'Logs' && !qt.done);
const loading = queryTransactions.some(qt => !qt.done);
2018-04-26 04:58:42 -05:00
return (
<div className={exploreClass} ref={this.getRef}>
<div className="navbar">
2019-01-11 11:26:56 -06:00
{exploreId === 'left' ? (
<div>
<a className="navbar-page-btn">
<i className="fa fa-rocket" />
Explore
</a>
</div>
) : (
2018-10-30 08:38:34 -05:00
<div className="navbar-buttons explore-first-button">
<button className="btn navbar-button" onClick={this.onClickCloseSplit}>
Close Split
</button>
2018-10-30 08:38:34 -05:00
</div>
)}
{!datasourceMissing ? (
<div className="navbar-buttons">
2018-12-14 07:05:47 -06:00
<DataSourcePicker
onChange={this.onChangeDatasource}
2018-12-14 07:05:47 -06:00
datasources={exploreDatasources}
current={selectedDatasource}
/>
</div>
) : null}
<div className="navbar__spacer" />
2019-01-11 11:26:56 -06:00
{exploreId === 'left' && !split ? (
<div className="navbar-buttons">
<button className="btn navbar-button" onClick={this.onClickSplit}>
Split
</button>
</div>
) : null}
<TimePicker ref={this.timepickerRef} range={range} onChangeTime={this.onChangeTime} />
<div className="navbar-buttons">
<button className="btn navbar-button navbar-button--no-icon" onClick={this.onClickClear}>
Clear All
</button>
</div>
<div className="navbar-buttons relative">
2018-12-16 02:58:37 -06:00
<button className="btn navbar-button navbar-button--primary" onClick={this.onSubmit}>
Run Query{' '}
{loading ? <i className="fa fa-spinner fa-fw fa-spin run-icon" /> : <i className="fa fa-level-down fa-fw run-icon" />}
</button>
</div>
</div>
{datasourceLoading ? <div className="explore-container">Loading datasource...</div> : null}
{datasourceMissing ? (
<div className="explore-container">Please add a datasource that supports Explore (e.g., Prometheus).</div>
) : null}
{datasourceError && (
<div className="explore-container">
<Alert message={`Error connecting to datasource: ${datasourceError}`} />
</div>
)}
2019-01-11 11:26:56 -06:00
{datasourceInstance &&
!datasourceError && (
<div className="explore-container">
<QueryRows
datasource={datasourceInstance}
history={history}
initialQueries={initialQueries}
onAddQueryRow={this.onAddQueryRow}
onChangeQuery={this.onChangeQuery}
onClickHintFix={this.onModifyQueries}
onExecuteQuery={this.onSubmit}
onRemoveQueryRow={this.onRemoveQueryRow}
transactions={queryTransactions}
exploreEvents={this.exploreEvents}
range={range}
/>
<AutoSizer onResize={this.onResize} disableHeight>
{({ width }) => (
<main className="m-t-2" style={{ width }}>
<ErrorBoundary>
{showingStartPage && <StartPage onClickExample={this.onClickExample} />}
{!showingStartPage && (
<>
{supportsGraph && (
<Panel
label="Graph"
isOpen={showingGraph}
loading={graphLoading}
onToggle={this.onClickGraphButton}
>
<Graph
data={graphResult}
height={graphHeight}
id={`explore-graph-${exploreId}`}
onChangeTime={this.onChangeTime}
range={range}
split={split}
/>
</Panel>
)}
{supportsTable && (
<Panel
label="Table"
loading={tableLoading}
isOpen={showingTable}
onToggle={this.onClickTableButton}
>
<Table data={tableResult} loading={tableLoading} onClickCell={this.onClickLabel} />
</Panel>
)}
{supportsLogs && (
<Panel
label="Logs"
loading={logsLoading}
isOpen={showingLogs}
onToggle={this.onClickLogsButton}
>
<Logs
data={logsResult}
exploreId={exploreId}
key={logsResult.id}
highlighterExpressions={logsHighlighterExpressions}
loading={logsLoading}
onChangeTime={this.onChangeTime}
onClickLabel={this.onClickLabel}
onStartScanning={this.onStartScanning}
onStopScanning={this.onStopScanning}
range={range}
scanning={scanning}
scanRange={scanRange}
/>
</Panel>
)}
</>
)}
</ErrorBoundary>
</main>
)}
2019-01-11 11:26:56 -06:00
</AutoSizer>
</div>
)}
2018-04-26 04:58:42 -05:00
</div>
);
}
}
2019-01-11 11:26:56 -06:00
function mapStateToProps(state: StoreState, { exploreId }) {
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,
exploreDatasources,
graphResult,
initialDatasource,
initialQueries,
history,
logsHighlighterExpressions,
logsResult,
queryTransactions,
range,
scanning,
scanRange,
showingGraph,
showingLogs,
showingStartPage,
showingTable,
supportsGraph,
supportsLogs,
supportsTable,
tableResult,
2019-01-11 11:26:56 -06:00
} = item;
2019-01-10 07:24:31 -06:00
return {
StartPage,
datasourceError,
datasourceInstance,
datasourceLoading,
datasourceMissing,
exploreDatasources,
graphResult,
initialDatasource,
initialQueries,
history,
logsHighlighterExpressions,
logsResult,
queryTransactions,
range,
scanning,
scanRange,
showingGraph,
showingLogs,
showingStartPage,
showingTable,
2019-01-11 11:26:56 -06:00
split,
2019-01-10 07:24:31 -06:00
supportsGraph,
supportsLogs,
supportsTable,
tableResult,
};
}
const mapDispatchToProps = {
addQueryRow,
changeDatasource,
changeQuery,
2019-01-11 11:26:56 -06:00
changeSize,
2019-01-10 07:24:31 -06:00
changeTime,
clickClear,
2019-01-11 11:26:56 -06:00
clickCloseSplit,
2019-01-10 07:24:31 -06:00
clickExample,
clickGraphButton,
clickLogsButton,
2019-01-11 11:26:56 -06:00
clickSplit,
2019-01-10 07:24:31 -06:00
clickTableButton,
highlightLogsExpression,
initializeExplore,
modifyQueries,
removeQueryRow,
runQueries,
scanStart,
scanStop,
};
2019-01-11 11:26:56 -06:00
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(Explore));