Commit e754bcdc by Andrej Ocenas Committed by GitHub

CloudWatch: Unify look of query mode select between dashboard and explore (#24648)

parent abfa1b5d
import React, { PureComponent } from 'react';
import { ExploreQueryFieldProps } from '@grafana/data';
import { RadioButtonGroup } from '@grafana/ui';
import { CloudWatchQuery } from '../types';
import { CloudWatchDatasource } from '../datasource';
import LogsQueryEditor from './LogsQueryEditor';
import { MetricsQueryEditor } from './MetricsQueryEditor';
import { cx, css } from 'emotion';
export type Props = ExploreQueryFieldProps<CloudWatchDatasource, CloudWatchQuery>;
export class CombinedMetricsEditor extends PureComponent<Props> {
renderMetricsEditor() {
return <MetricsQueryEditor {...this.props} />;
}
renderLogsEditor() {
return <LogsQueryEditor {...this.props} />;
}
render() {
const { query } = this.props;
const apiMode = query.apiMode ?? query.queryMode ?? 'Metrics';
return (
<>
<div
className={cx(
css`
margin-bottom: 4px;
`
)}
>
<RadioButtonGroup
options={[
{ label: 'Metrics API', value: 'Metrics' },
{ label: 'Logs API', value: 'Logs' },
]}
value={apiMode}
onChange={(v: 'Metrics' | 'Logs') => this.props.onChange({ ...query, apiMode: v })}
/>
</div>
{apiMode === 'Metrics' ? this.renderMetricsEditor() : this.renderLogsEditor()}
</>
);
}
}
...@@ -9,35 +9,28 @@ import LogsQueryEditor from './LogsQueryEditor'; ...@@ -9,35 +9,28 @@ import LogsQueryEditor from './LogsQueryEditor';
export type Props = ExploreQueryFieldProps<CloudWatchDatasource, CloudWatchQuery>; export type Props = ExploreQueryFieldProps<CloudWatchDatasource, CloudWatchQuery>;
interface State { const apiModes = {
queryMode: ExploreMode; Metrics: { label: 'CloudWatch Metrics', value: 'Metrics' },
} Logs: { label: 'CloudWatch Logs', value: 'Logs' },
};
export class PanelQueryEditor extends PureComponent<Props, State> {
state: State = { queryMode: (this.props.query.queryMode as ExploreMode) ?? ExploreMode.Metrics };
onQueryModeChange(mode: ExploreMode) {
this.setState({
queryMode: mode,
});
}
export class PanelQueryEditor extends PureComponent<Props> {
render() { render() {
const { queryMode } = this.state; const { query } = this.props;
const apiMode = query.apiMode ?? query.queryMode ?? 'Metrics';
return ( return (
<> <>
<QueryInlineField label="Query Mode"> <QueryInlineField label="Query Mode">
<Segment <Segment
value={queryMode} value={apiModes[apiMode]}
options={[ options={Object.values(apiModes)}
{ label: 'Metrics', value: ExploreMode.Metrics }, onChange={({ value }) =>
{ label: 'Logs', value: ExploreMode.Logs }, this.props.onChange({ ...query, apiMode: (value as 'Metrics' | 'Logs') ?? 'Metrics' })
]} }
onChange={({ value }) => this.onQueryModeChange(value ?? ExploreMode.Metrics)}
/> />
</QueryInlineField> </QueryInlineField>
{queryMode === ExploreMode.Logs ? <LogsQueryEditor {...this.props} /> : <MetricsQueryEditor {...this.props} />} {apiMode === ExploreMode.Logs ? <LogsQueryEditor {...this.props} /> : <MetricsQueryEditor {...this.props} />}
</> </>
); );
} }
......
...@@ -7,7 +7,6 @@ import { CloudWatchJsonData, CloudWatchQuery } from './types'; ...@@ -7,7 +7,6 @@ import { CloudWatchJsonData, CloudWatchQuery } from './types';
import { CloudWatchLogsQueryEditor } from './components/LogsQueryEditor'; import { CloudWatchLogsQueryEditor } from './components/LogsQueryEditor';
import { PanelQueryEditor } from './components/PanelQueryEditor'; import { PanelQueryEditor } from './components/PanelQueryEditor';
import LogsCheatSheet from './components/LogsCheatSheet'; import LogsCheatSheet from './components/LogsCheatSheet';
import { CombinedMetricsEditor } from './components/CombinedMetricsEditor';
export const plugin = new DataSourcePlugin<CloudWatchDatasource, CloudWatchQuery, CloudWatchJsonData>( export const plugin = new DataSourcePlugin<CloudWatchDatasource, CloudWatchQuery, CloudWatchJsonData>(
CloudWatchDatasource CloudWatchDatasource
...@@ -15,6 +14,6 @@ export const plugin = new DataSourcePlugin<CloudWatchDatasource, CloudWatchQuery ...@@ -15,6 +14,6 @@ export const plugin = new DataSourcePlugin<CloudWatchDatasource, CloudWatchQuery
.setExploreStartPage(LogsCheatSheet) .setExploreStartPage(LogsCheatSheet)
.setConfigEditor(ConfigEditor) .setConfigEditor(ConfigEditor)
.setQueryEditor(PanelQueryEditor) .setQueryEditor(PanelQueryEditor)
.setExploreMetricsQueryField(CombinedMetricsEditor) .setExploreMetricsQueryField(PanelQueryEditor)
.setExploreLogsQueryField(CloudWatchLogsQueryEditor) .setExploreLogsQueryField(CloudWatchLogsQueryEditor)
.setAnnotationQueryCtrl(CloudWatchAnnotationsQueryCtrl); .setAnnotationQueryCtrl(CloudWatchAnnotationsQueryCtrl);
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