Commit 5feef220 by Andrej Ocenas Committed by GitHub

CloudWatch/Logs: Make stats hint show consistently (#24392)

parent 75de6165
...@@ -29,6 +29,7 @@ export interface QueryFieldProps { ...@@ -29,6 +29,7 @@ export interface QueryFieldProps {
onRunQuery?: () => void; onRunQuery?: () => void;
onBlur?: () => void; onBlur?: () => void;
onChange?: (value: string) => void; onChange?: (value: string) => void;
onRichValueChange?: (value: Value) => void;
onClick?: (event: Event, editor: CoreEditor, next: () => any) => any; onClick?: (event: Event, editor: CoreEditor, next: () => any) => any;
onTypeahead?: (typeahead: TypeaheadInput) => Promise<TypeaheadOutput>; onTypeahead?: (typeahead: TypeaheadInput) => Promise<TypeaheadOutput>;
onWillApplySuggestion?: (suggestion: string, state: SuggestionsState) => string; onWillApplySuggestion?: (suggestion: string, state: SuggestionsState) => string;
...@@ -121,6 +122,9 @@ export class QueryField extends React.PureComponent<QueryFieldProps, QueryFieldS ...@@ -121,6 +122,9 @@ export class QueryField extends React.PureComponent<QueryFieldProps, QueryFieldS
onChange = (value: Value, runQuery?: boolean) => { onChange = (value: Value, runQuery?: boolean) => {
const documentChanged = value.document !== this.state.value.document; const documentChanged = value.document !== this.state.value.document;
const prevValue = this.state.value; const prevValue = this.state.value;
if (this.props.onRichValueChange) {
this.props.onRichValueChange(value);
}
// Update local state with new value and optionally change value upstream. // Update local state with new value and optionally change value upstream.
this.setState({ value }, () => { this.setState({ value }, () => {
......
// Libraries // Libraries
import React, { ReactNode } from 'react'; import React, { ReactNode } from 'react';
import intersection from 'lodash/intersection'; import intersection from 'lodash/intersection';
import debounce from 'lodash/debounce';
import { import {
QueryField, QueryField,
...@@ -12,10 +13,11 @@ import { ...@@ -12,10 +13,11 @@ import {
Select, Select,
MultiSelect, MultiSelect,
} from '@grafana/ui'; } from '@grafana/ui';
import Plain from 'slate-plain-serializer';
// Utils & Services // Utils & Services
// dom also includes Element polyfills // dom also includes Element polyfills
import { Plugin, Node, Editor } from 'slate'; import { Plugin, Node, Editor, Value } from 'slate';
import syntax from '../syntax'; import syntax from '../syntax';
// Types // Types
...@@ -140,33 +142,11 @@ export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogs ...@@ -140,33 +142,11 @@ export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogs
}); });
}; };
onChangeQuery = (value: string, override?: boolean) => { onChangeQuery = (value: string) => {
// Send text change to parent // Send text change to parent
const { query, onChange, onRunQuery, datasource, exploreMode } = this.props; const { query, onChange } = this.props;
const { selectedLogGroups, selectedRegion } = this.state; const { selectedLogGroups, selectedRegion } = this.state;
// TEMP: Remove when logs/metrics unification is complete
if (datasource.languageProvider && exploreMode === ExploreMode.Logs) {
const cloudwatchLanguageProvider = datasource.languageProvider as CloudWatchLanguageProvider;
const queryUsesStatsCommand = cloudwatchLanguageProvider.isStatsQuery(query.expression);
if (queryUsesStatsCommand) {
this.setState({
hint: {
message: 'You are trying to run a stats query in Logs mode. ',
fix: {
label: 'Switch to Metrics mode.',
action: this.switchToMetrics,
},
},
});
} else {
this.setState({
hint: undefined,
});
}
}
if (onChange) { if (onChange) {
const nextQuery = { const nextQuery = {
...query, ...query,
...@@ -175,10 +155,6 @@ export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogs ...@@ -175,10 +155,6 @@ export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogs
region: selectedRegion.value ?? 'default', region: selectedRegion.value ?? 'default',
}; };
onChange(nextQuery); onChange(nextQuery);
if (override && onRunQuery) {
onRunQuery();
}
} }
}; };
...@@ -240,12 +216,10 @@ export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogs ...@@ -240,12 +216,10 @@ export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogs
const { history, absoluteRange } = this.props; const { history, absoluteRange } = this.props;
const { prefix, text, value, wrapperClasses, labelKey, editor } = typeahead; const { prefix, text, value, wrapperClasses, labelKey, editor } = typeahead;
const result = await cloudwatchLanguageProvider.provideCompletionItems( return await cloudwatchLanguageProvider.provideCompletionItems(
{ text, value, prefix, wrapperClasses, labelKey, editor }, { text, value, prefix, wrapperClasses, labelKey, editor },
{ history, absoluteRange, logGroupNames: selectedLogGroups.map(logGroup => logGroup.value!) } { history, absoluteRange, logGroupNames: selectedLogGroups.map(logGroup => logGroup.value!) }
); );
return result;
}; };
switchToMetrics = () => { switchToMetrics = () => {
...@@ -282,6 +256,34 @@ export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogs ...@@ -282,6 +256,34 @@ export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogs
}); });
}; };
/**
* Check if query is stats query in logs mode and shows a hint to switch to metrics mode. Needs to be done
* on update of the rich Value because standard onChange is not called on load for example.
*/
checkForStatsQuery = debounce((value: Value) => {
const { datasource } = this.props;
// TEMP: Remove when logs/metrics unification is complete
if (datasource.languageProvider && this.props.exploreMode === ExploreMode.Logs) {
const cloudwatchLanguageProvider = datasource.languageProvider as CloudWatchLanguageProvider;
const queryUsesStatsCommand = cloudwatchLanguageProvider.isStatsQuery(Plain.serialize(value));
if (queryUsesStatsCommand) {
this.setState({
hint: {
message: 'You are trying to run a stats query in Logs mode. ',
fix: {
label: 'Switch to Metrics mode.',
action: this.switchToMetrics,
},
},
});
} else {
this.setState({
hint: undefined,
});
}
}
}, 250);
render() { render() {
const { ExtraFieldElement, data, query, syntaxLoaded, datasource } = this.props; const { ExtraFieldElement, data, query, syntaxLoaded, datasource } = this.props;
const { const {
...@@ -359,6 +361,7 @@ export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogs ...@@ -359,6 +361,7 @@ export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogs
portalOrigin="cloudwatch" portalOrigin="cloudwatch"
syntaxLoaded={syntaxLoaded} syntaxLoaded={syntaxLoaded}
disabled={loadingLogGroups || selectedLogGroups.length === 0} disabled={loadingLogGroups || selectedLogGroups.length === 0}
onRichValueChange={this.checkForStatsQuery}
/> />
</div> </div>
{ExtraFieldElement} {ExtraFieldElement}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment