Loki: Query builder, hook up instant toggle and improve inline switch (#44704)

This commit is contained in:
Torkel Ödegaard 2022-02-01 13:56:11 +01:00 committed by GitHub
parent 3873c5e7b5
commit fb680b3575
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 21 deletions

View File

@ -1,12 +1,14 @@
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import { GrafanaTheme2, LoadingState } from '@grafana/data'; import { GrafanaTheme2, LoadingState } from '@grafana/data';
import { EditorHeader, FlexItem, InlineSelect, Space, Stack } from '@grafana/experimental'; import { EditorHeader, FlexItem, InlineSelect, Space } from '@grafana/experimental';
import { Button, Switch, useStyles2 } from '@grafana/ui'; import { Button, useStyles2 } from '@grafana/ui';
import { QueryEditorModeToggle } from 'app/plugins/datasource/prometheus/querybuilder/shared/QueryEditorModeToggle'; import { QueryEditorModeToggle } from 'app/plugins/datasource/prometheus/querybuilder/shared/QueryEditorModeToggle';
import { QueryHeaderSwitch } from 'app/plugins/datasource/prometheus/querybuilder/shared/QueryHeaderSwitch';
import { QueryEditorMode } from 'app/plugins/datasource/prometheus/querybuilder/shared/types'; import { QueryEditorMode } from 'app/plugins/datasource/prometheus/querybuilder/shared/types';
import React, { useCallback, useState } from 'react'; import React, { SyntheticEvent, useCallback, useState } from 'react';
import { LokiQueryEditor } from '../../components/LokiQueryEditor'; import { LokiQueryEditor } from '../../components/LokiQueryEditor';
import { LokiQueryEditorProps } from '../../components/types'; import { LokiQueryEditorProps } from '../../components/types';
import { LokiQueryType } from '../../types';
import { lokiQueryModeller } from '../LokiQueryModeller'; import { lokiQueryModeller } from '../LokiQueryModeller';
import { getDefaultEmptyQuery, LokiVisualQuery } from '../types'; import { getDefaultEmptyQuery, LokiVisualQuery } from '../types';
import { LokiQueryBuilder } from './LokiQueryBuilder'; import { LokiQueryBuilder } from './LokiQueryBuilder';
@ -35,6 +37,11 @@ export const LokiQueryEditorSelector = React.memo<LokiQueryEditorProps>((props)
}); });
}; };
const onInstantChange = (event: SyntheticEvent<HTMLInputElement>) => {
onChange({ ...query, queryType: event.currentTarget.checked ? LokiQueryType.Instant : LokiQueryType.Range });
onRunQuery();
};
// If no expr (ie new query) then default to builder // If no expr (ie new query) then default to builder
const editorMode = query.editorMode ?? (query.expr ? QueryEditorMode.Code : QueryEditorMode.Builder); const editorMode = query.editorMode ?? (query.expr ? QueryEditorMode.Code : QueryEditorMode.Builder);
@ -53,14 +60,11 @@ export const LokiQueryEditorSelector = React.memo<LokiQueryEditorProps>((props)
> >
Run query Run query
</Button> </Button>
<Stack gap={1}> <QueryHeaderSwitch
<label className={styles.switchLabel}>Instant</label> label="Instant"
<Switch /> value={query.queryType === LokiQueryType.Instant}
</Stack> onChange={onInstantChange}
<Stack gap={1}> />
<label className={styles.switchLabel}>Exemplars</label>
<Switch />
</Stack>
<InlineSelect <InlineSelect
value={null} value={null}
placeholder="Query patterns" placeholder="Query patterns"

View File

@ -1,12 +1,13 @@
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import { CoreApp, GrafanaTheme2, LoadingState } from '@grafana/data'; import { CoreApp, GrafanaTheme2, LoadingState } from '@grafana/data';
import { EditorHeader, FlexItem, InlineSelect, Space, Stack } from '@grafana/experimental'; import { EditorHeader, FlexItem, InlineSelect, Space } from '@grafana/experimental';
import { Button, Switch, useStyles2 } from '@grafana/ui'; import { Button, useStyles2 } from '@grafana/ui';
import React, { SyntheticEvent, useCallback, useState } from 'react'; import React, { SyntheticEvent, useCallback, useState } from 'react';
import { PromQueryEditor } from '../../components/PromQueryEditor'; import { PromQueryEditor } from '../../components/PromQueryEditor';
import { PromQueryEditorProps } from '../../components/types'; import { PromQueryEditorProps } from '../../components/types';
import { promQueryModeller } from '../PromQueryModeller'; import { promQueryModeller } from '../PromQueryModeller';
import { QueryEditorModeToggle } from '../shared/QueryEditorModeToggle'; import { QueryEditorModeToggle } from '../shared/QueryEditorModeToggle';
import { QueryHeaderSwitch } from '../shared/QueryHeaderSwitch';
import { QueryEditorMode } from '../shared/types'; import { QueryEditorMode } from '../shared/types';
import { getDefaultEmptyQuery, PromVisualQuery } from '../types'; import { getDefaultEmptyQuery, PromVisualQuery } from '../types';
import { PromQueryBuilder } from './PromQueryBuilder'; import { PromQueryBuilder } from './PromQueryBuilder';
@ -66,15 +67,9 @@ export const PromQueryEditorSelector = React.memo<PromQueryEditorProps>((props)
> >
Run query Run query
</Button> </Button>
<Stack gap={1}> <QueryHeaderSwitch label="Instant" value={query.instant} onChange={onInstantChange} />
<label className={styles.switchLabel}>Instant</label>
<Switch value={query.instant} onChange={onInstantChange} />
</Stack>
{showExemplarSwitch && ( {showExemplarSwitch && (
<Stack gap={1}> <QueryHeaderSwitch label="Exemplars" value={query.exemplar} onChange={onExemplarChange} />
<label className={styles.switchLabel}>Exemplars</label>
<Switch value={query.exemplar} onChange={onExemplarChange} />
</Stack>
)} )}
{editorMode === QueryEditorMode.Builder && ( {editorMode === QueryEditorMode.Builder && (
<> <>

View File

@ -0,0 +1,38 @@
import { css } from '@emotion/css';
import { GrafanaTheme2 } from '@grafana/data';
import { Stack } from '@grafana/experimental';
import { Switch, useStyles2 } from '@grafana/ui';
import { uniqueId } from 'lodash';
import React, { HTMLProps, useRef } from 'react';
export interface Props extends Omit<HTMLProps<HTMLInputElement>, 'value' | 'ref'> {
value?: boolean;
label: string;
}
export function QueryHeaderSwitch({ label, ...inputProps }: Props) {
const switchIdRef = useRef(uniqueId(`switch-${label}`));
const styles = useStyles2(getStyles);
return (
<Stack gap={1}>
<label htmlFor={switchIdRef.current} className={styles.switchLabel}>
{label}
</label>
<Switch {...inputProps} id={switchIdRef.current} />
</Stack>
);
}
const getStyles = (theme: GrafanaTheme2) => {
return {
switchLabel: css({
color: theme.colors.text.secondary,
cursor: 'pointer',
fontSize: theme.typography.bodySmall.fontSize,
'&:hover': {
color: theme.colors.text.primary,
},
}),
};
};