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

515 lines
16 KiB
TypeScript
Raw Normal View History

import React from 'react';
2018-04-26 04:58:42 -05:00
import { hot } from 'react-hot-loader';
import { css, cx } from 'emotion';
import { compose } from 'redux';
2019-01-10 07:24:31 -06:00
import { connect } from 'react-redux';
import AutoSizer from 'react-virtualized-auto-sizer';
import memoizeOne from 'memoize-one';
import { selectors } from '@grafana/e2e-selectors';
import { ErrorBoundaryAlert, stylesFactory, withTheme } from '@grafana/ui';
import {
AbsoluteTimeRange,
DataQuery,
DataSourceApi,
GrafanaTheme,
GraphSeriesXY,
LoadingState,
PanelData,
RawTimeRange,
TimeRange,
TimeZone,
ExploreUrlState,
LogsModel,
} from '@grafana/data';
import store from 'app/core/store';
2019-01-17 10:59:47 -06:00
import LogsContainer from './LogsContainer';
import QueryRows from './QueryRows';
import TableContainer from './TableContainer';
import RichHistoryContainer from './RichHistory/RichHistoryContainer';
import ExploreQueryInspector from './ExploreQueryInspector';
import {
addQueryRow,
changeSize,
initializeExplore,
modifyQueries,
refreshExplore,
scanStart,
setQueries,
updateTimeRange,
} from './state/actions';
import { ExploreId, ExploreItemState, ExploreUpdateState } from 'app/types/explore';
2019-01-17 10:59:47 -06:00
import { StoreState } from 'app/types';
import {
DEFAULT_RANGE,
ensureQueries,
getFirstNonQueryRowSpecificError,
getTimeRange,
getTimeRangeFromUrl,
lastUsedDatasourceKeyForOrgId,
} 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 { NoDataSourceCallToAction } from './NoDataSourceCallToAction';
import { getTimeZone } from '../profile/state/selectors';
import { ErrorContainer } from './ErrorContainer';
import { scanStopAction } from './state/actionTypes';
import { ExploreGraphPanel } from './ExploreGraphPanel';
//TODO:unification
Tracing: Adds header and minimap (#23315) * Add integration with Jeager Add Jaeger datasource and modify derived fields in loki to allow for opening a trace in Jager in separate split. Modifies build so that this branch docker images are pushed to docker hub Add a traceui dir with docker-compose and provision files for demoing.:wq * Enable docker logger plugin to send logs to loki * Add placeholder zipkin datasource * Fixed rebase issues, added enhanceDataFrame to non-legacy code path * Trace selector for jaeger query field * Fix logs default mode for Loki * Fix loading jaeger query field services on split * Updated grafana image in traceui/compose file * Fix prettier error * Hide behind feature flag, clean up unused code. * Fix tests * Fix tests * Cleanup code and review feedback * Remove traceui directory * Remove circle build changes * Fix feature toggles object * Fix merge issues * Add trace ui in Explore * WIP * WIP * WIP * Make jaeger datasource return trace data instead of link * Allow js in jest tests * Return data from Jaeger datasource * Take yarn.lock from master * Fix missing component * Update yarn lock * Fix some ts and lint errors * Fix merge * Fix type errors * Make tests pass again * Add tests * Fix es5 compatibility * Add header with minimap * Fix sizing issue due to column resizer handle * Fix issues with sizing, search functionality, duplicate react, tests * Refactor TraceView component, fix tests * Fix type errors * Add tests for hooks Co-authored-by: David Kaltschmidt <david.kaltschmidt@gmail.com>
2020-04-08 10:16:22 -05:00
import { TraceView } from './TraceView/TraceView';
import { SecondaryActions } from './SecondaryActions';
import { FILTER_FOR_OPERATOR, FILTER_OUT_OPERATOR, FilterItem } from '@grafana/ui/src/components/Table/types';
const getStyles = stylesFactory((theme: GrafanaTheme) => {
return {
logsMain: css`
label: logsMain;
// Is needed for some transition animations to work.
position: relative;
margin-top: 21px;
`,
button: css`
label: button;
margin: 1em 4px 0 0;
`,
queryContainer: css`
label: queryContainer;
// Need to override normal css class and don't want to count on ordering of the classes in html.
height: auto !important;
flex: unset !important;
padding: ${theme.panelPadding}px;
`,
};
});
export interface ExploreProps {
2019-01-11 11:26:56 -06:00
changeSize: typeof changeSize;
datasourceInstance: DataSourceApi | null;
2019-01-10 07:24:31 -06:00
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;
update: ExploreUpdateState;
refreshExplore: typeof refreshExplore;
2019-01-10 07:24:31 -06:00
scanning?: boolean;
scanRange?: RawTimeRange;
scanStart: typeof scanStart;
scanStopAction: typeof scanStopAction;
setQueries: typeof setQueries;
split: boolean;
2019-02-04 06:41:29 -06:00
queryKeys: string[];
initialDatasource: string;
initialQueries: DataQuery[];
initialRange: TimeRange;
isLive: boolean;
syncedTimes: boolean;
updateTimeRange: typeof updateTimeRange;
graphResult?: GraphSeriesXY[] | null;
logsResult?: LogsModel;
loading?: boolean;
absoluteRange: AbsoluteTimeRange;
timeZone?: TimeZone;
onHiddenSeriesChanged?: (hiddenSeries: string[]) => void;
queryResponse: PanelData;
originPanelId: number;
addQueryRow: typeof addQueryRow;
theme: GrafanaTheme;
showMetrics: boolean;
showTable: boolean;
showLogs: boolean;
showTrace: boolean;
}
enum ExploreDrawer {
RichHistory,
QueryInspector,
}
interface ExploreState {
openDrawer?: ExploreDrawer;
}
/**
* 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.
*/
export class Explore extends React.PureComponent<ExploreProps, ExploreState> {
el: any;
2018-11-23 08:12:20 -06:00
exploreEvents: Emitter;
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.state = {
openDrawer: undefined,
};
2018-04-26 04:58:42 -05:00
}
componentDidMount() {
const { initialized, exploreId, initialDatasource, initialQueries, initialRange, originPanelId } = this.props;
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,
originPanelId
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;
};
onChangeTime = (rawRange: RawTimeRange) => {
const { updateTimeRange, exploreId } = this.props;
updateTimeRange({ exploreId, rawRange });
};
// 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]);
};
onCellFilterAdded = (filter: FilterItem) => {
const { value, key, operator } = filter;
if (operator === FILTER_FOR_OPERATOR) {
this.onClickFilterLabel(key, value);
}
if (operator === FILTER_OUT_OPERATOR) {
this.onClickFilterOutLabel(key, value);
}
};
onClickFilterLabel = (key: string, value: string) => {
this.onModifyQueries({ type: 'ADD_FILTER', key, value });
};
onClickFilterOutLabel = (key: string, value: string) => {
this.onModifyQueries({ type: 'ADD_FILTER_OUT', key, value });
};
onClickAddQueryRowButton = () => {
const { exploreId, queryKeys } = this.props;
this.props.addQueryRow(exploreId, queryKeys.length);
};
onModifyQueries = (action: any, index?: number) => {
2019-01-10 07:24:31 -06:00
const { datasourceInstance } = this.props;
if (datasourceInstance?.modifyQuery) {
const modifier = (queries: DataQuery, modification: any) =>
datasourceInstance.modifyQuery!(queries, modification);
this.props.modifyQueries(this.props.exploreId, action, modifier, index);
}
};
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
this.props.scanStart(this.props.exploreId);
};
2018-11-27 09:35:37 -06:00
onStopScanning = () => {
this.props.scanStopAction({ exploreId: this.props.exploreId });
};
onUpdateTimeRange = (absoluteRange: AbsoluteTimeRange) => {
const { exploreId, updateTimeRange } = this.props;
updateTimeRange({ exploreId, absoluteRange });
};
toggleShowRichHistory = () => {
this.setState(state => {
return {
openDrawer: state.openDrawer === ExploreDrawer.RichHistory ? undefined : ExploreDrawer.RichHistory,
};
});
};
toggleShowQueryInspector = () => {
this.setState(state => {
return {
openDrawer: state.openDrawer === ExploreDrawer.QueryInspector ? undefined : ExploreDrawer.QueryInspector,
};
});
};
refreshExplore = () => {
const { exploreId, update } = this.props;
if (update.queries || update.range || update.datasource || update.mode) {
this.props.refreshExplore(exploreId);
}
};
renderEmptyState = () => {
return (
<div className="explore-container">
<NoDataSourceCallToAction />
</div>
);
};
2018-04-26 04:58:42 -05:00
render() {
const {
2019-01-10 07:24:31 -06:00
datasourceInstance,
datasourceMissing,
2019-01-11 11:26:56 -06:00
exploreId,
2019-01-10 07:24:31 -06:00
split,
2019-02-04 06:41:29 -06:00
queryKeys,
graphResult,
loading,
absoluteRange,
timeZone,
queryResponse,
syncedTimes,
isLive,
theme,
showMetrics,
showTable,
showLogs,
showTrace,
2019-01-10 07:24:31 -06:00
} = this.props;
const { openDrawer } = this.state;
const exploreClass = split ? 'explore explore-split' : 'explore';
const styles = getStyles(theme);
const StartPage = datasourceInstance?.components?.ExploreStartPage;
const showStartPage = !queryResponse || queryResponse.state === LoadingState.NotStarted;
// gets an error without a refID, so non-query-row-related error, like a connection error
const queryErrors = queryResponse.error ? [queryResponse.error] : undefined;
const queryError = getFirstNonQueryRowSpecificError(queryErrors);
const showRichHistory = openDrawer === ExploreDrawer.RichHistory;
const showQueryInspector = openDrawer === ExploreDrawer.QueryInspector;
2018-04-26 04:58:42 -05:00
return (
<div className={exploreClass} ref={this.getRef} aria-label={selectors.pages.Explore.General.container}>
<ExploreToolbar exploreId={exploreId} onChangeTime={this.onChangeTime} />
{datasourceMissing ? this.renderEmptyState() : null}
{datasourceInstance && (
<div className="explore-container">
<div className={cx('panel-container', styles.queryContainer)}>
<QueryRows exploreEvents={this.exploreEvents} exploreId={exploreId} queryKeys={queryKeys} />
<SecondaryActions
addQueryRowButtonDisabled={isLive}
// We cannot show multiple traces at the same time right now so we do not show add query button.
//TODO:unification
addQueryRowButtonHidden={false}
richHistoryButtonActive={showRichHistory}
queryInspectorButtonActive={showQueryInspector}
onClickAddQueryRowButton={this.onClickAddQueryRowButton}
onClickRichHistoryButton={this.toggleShowRichHistory}
onClickQueryInspectorButton={this.toggleShowQueryInspector}
/>
</div>
<ErrorContainer queryError={queryError} />
Tracing: Adds header and minimap (#23315) * Add integration with Jeager Add Jaeger datasource and modify derived fields in loki to allow for opening a trace in Jager in separate split. Modifies build so that this branch docker images are pushed to docker hub Add a traceui dir with docker-compose and provision files for demoing.:wq * Enable docker logger plugin to send logs to loki * Add placeholder zipkin datasource * Fixed rebase issues, added enhanceDataFrame to non-legacy code path * Trace selector for jaeger query field * Fix logs default mode for Loki * Fix loading jaeger query field services on split * Updated grafana image in traceui/compose file * Fix prettier error * Hide behind feature flag, clean up unused code. * Fix tests * Fix tests * Cleanup code and review feedback * Remove traceui directory * Remove circle build changes * Fix feature toggles object * Fix merge issues * Add trace ui in Explore * WIP * WIP * WIP * Make jaeger datasource return trace data instead of link * Allow js in jest tests * Return data from Jaeger datasource * Take yarn.lock from master * Fix missing component * Update yarn lock * Fix some ts and lint errors * Fix merge * Fix type errors * Make tests pass again * Add tests * Fix es5 compatibility * Add header with minimap * Fix sizing issue due to column resizer handle * Fix issues with sizing, search functionality, duplicate react, tests * Refactor TraceView component, fix tests * Fix type errors * Add tests for hooks Co-authored-by: David Kaltschmidt <david.kaltschmidt@gmail.com>
2020-04-08 10:16:22 -05:00
<AutoSizer onResize={this.onResize} disableHeight>
{({ width }) => {
if (width === 0) {
return null;
}
return (
Tracing: Adds header and minimap (#23315) * Add integration with Jeager Add Jaeger datasource and modify derived fields in loki to allow for opening a trace in Jager in separate split. Modifies build so that this branch docker images are pushed to docker hub Add a traceui dir with docker-compose and provision files for demoing.:wq * Enable docker logger plugin to send logs to loki * Add placeholder zipkin datasource * Fixed rebase issues, added enhanceDataFrame to non-legacy code path * Trace selector for jaeger query field * Fix logs default mode for Loki * Fix loading jaeger query field services on split * Updated grafana image in traceui/compose file * Fix prettier error * Hide behind feature flag, clean up unused code. * Fix tests * Fix tests * Cleanup code and review feedback * Remove traceui directory * Remove circle build changes * Fix feature toggles object * Fix merge issues * Add trace ui in Explore * WIP * WIP * WIP * Make jaeger datasource return trace data instead of link * Allow js in jest tests * Return data from Jaeger datasource * Take yarn.lock from master * Fix missing component * Update yarn lock * Fix some ts and lint errors * Fix merge * Fix type errors * Make tests pass again * Add tests * Fix es5 compatibility * Add header with minimap * Fix sizing issue due to column resizer handle * Fix issues with sizing, search functionality, duplicate react, tests * Refactor TraceView component, fix tests * Fix type errors * Add tests for hooks Co-authored-by: David Kaltschmidt <david.kaltschmidt@gmail.com>
2020-04-08 10:16:22 -05:00
<main className={cx(styles.logsMain)} style={{ width }}>
<ErrorBoundaryAlert>
{showStartPage && StartPage && (
<div className={'grafana-info-box grafana-info-box--max-lg'}>
<StartPage
onClickExample={this.onClickExample}
datasource={datasourceInstance}
exploreId={exploreId}
/>
</div>
)}
{!showStartPage && (
<>
{showMetrics && (
<ExploreGraphPanel
2020-08-28 11:59:56 -05:00
ariaLabel={selectors.pages.Explore.General.graph}
series={graphResult}
width={width}
loading={loading}
absoluteRange={absoluteRange}
isStacked={false}
showPanel={true}
timeZone={timeZone}
onUpdateTimeRange={this.onUpdateTimeRange}
showBars={false}
showLines={true}
/>
)}
{showTable && (
<TableContainer
2020-08-28 11:59:56 -05:00
ariaLabel={selectors.pages.Explore.General.table}
width={width}
exploreId={exploreId}
onCellFilterAdded={
this.props.datasourceInstance?.modifyQuery ? this.onCellFilterAdded : undefined
}
/>
)}
{showLogs && (
<LogsContainer
width={width}
exploreId={exploreId}
syncedTimes={syncedTimes}
onClickFilterLabel={this.onClickFilterLabel}
onClickFilterOutLabel={this.onClickFilterOutLabel}
onStartScanning={this.onStartScanning}
onStopScanning={this.onStopScanning}
/>
)}
{/* TODO:unification */}
{showTrace &&
// We expect only one trace at the moment to be in the dataframe
// If there is not data (like 404) we show a separate error so no need to show anything here
queryResponse.series[0] && (
<TraceView trace={queryResponse.series[0].fields[0].values.get(0) as any} />
)}
</>
)}
{showRichHistory && (
<RichHistoryContainer
width={width}
exploreId={exploreId}
onClose={this.toggleShowRichHistory}
/>
)}
{showQueryInspector && (
<ExploreQueryInspector
exploreId={exploreId}
width={width}
onClose={this.toggleShowQueryInspector}
/>
)}
</ErrorBoundaryAlert>
</main>
);
}}
</AutoSizer>
</div>
)}
2018-04-26 04:58:42 -05:00
</div>
);
}
}
const ensureQueriesMemoized = memoizeOne(ensureQueries);
const getTimeRangeFromUrlMemoized = memoizeOne(getTimeRangeFromUrl);
function mapStateToProps(state: StoreState, { exploreId }: ExploreProps): Partial<ExploreProps> {
2019-01-11 11:26:56 -06:00
const explore = state.explore;
const { split, syncedTimes } = explore;
2019-01-11 11:26:56 -06:00
const item: ExploreItemState = explore[exploreId];
const timeZone = getTimeZone(state.user);
2019-01-10 07:24:31 -06:00
const {
datasourceInstance,
datasourceMissing,
2019-01-12 16:22:28 -06:00
initialized,
2019-02-04 06:41:29 -06:00
queryKeys,
urlState,
update,
isLive,
graphResult,
logsResult,
showLogs,
showMetrics,
showTable,
showTrace,
loading,
absoluteRange,
queryResponse,
2019-01-11 11:26:56 -06:00
} = item;
const { datasource, queries, range: urlRange, originPanelId } = (urlState || {}) as ExploreUrlState;
const initialDatasource = datasource || store.get(lastUsedDatasourceKeyForOrgId(state.user.orgId));
const initialQueries: DataQuery[] = ensureQueriesMemoized(queries);
const initialRange = urlRange
? getTimeRangeFromUrlMemoized(urlRange, timeZone)
: getTimeRange(timeZone, DEFAULT_RANGE);
2019-01-10 07:24:31 -06:00
return {
datasourceInstance,
datasourceMissing,
2019-01-12 16:22:28 -06:00
initialized,
2019-01-11 11:26:56 -06:00
split,
2019-02-04 06:41:29 -06:00
queryKeys,
update,
initialDatasource,
initialQueries,
initialRange,
isLive,
graphResult: graphResult ?? undefined,
logsResult: logsResult ?? undefined,
loading,
absoluteRange,
queryResponse,
originPanelId,
syncedTimes,
timeZone,
showLogs,
showMetrics,
showTable,
showTrace,
2019-01-10 07:24:31 -06:00
};
}
const mapDispatchToProps: Partial<ExploreProps> = {
2019-01-11 11:26:56 -06:00
changeSize,
2019-01-10 07:24:31 -06:00
initializeExplore,
modifyQueries,
refreshExplore,
2019-01-10 07:24:31 -06:00
scanStart,
scanStopAction,
setQueries,
updateTimeRange,
addQueryRow,
2019-01-10 07:24:31 -06:00
};
export default compose(
hot(module),
connect(mapStateToProps, mapDispatchToProps),
withTheme
)(Explore) as React.ComponentType<{ exploreId: ExploreId }>;