mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Move Visualization to QueryRow (#34658)
* move visualization to queryeditorrow * add prop to hide disable query * move panel plugin state outside the vizwrapper
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import React, { FC, ReactNode } from 'react';
|
import React, { FC, ReactNode, useState } from 'react';
|
||||||
import { css } from '@emotion/css';
|
import { css } from '@emotion/css';
|
||||||
|
import { cloneDeep } from 'lodash';
|
||||||
import {
|
import {
|
||||||
DataQuery,
|
DataQuery,
|
||||||
DataSourceInstanceSettings,
|
DataSourceInstanceSettings,
|
||||||
@@ -9,11 +10,11 @@ import {
|
|||||||
getDefaultRelativeTimeRange,
|
getDefaultRelativeTimeRange,
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
import { useStyles2, RelativeTimeRangePicker } from '@grafana/ui';
|
import { useStyles2, RelativeTimeRangePicker } from '@grafana/ui';
|
||||||
import { QueryEditorRow } from '../../../../query/components/QueryEditorRow';
|
import { QueryEditorRow } from 'app/features/query/components/QueryEditorRow';
|
||||||
import { VizWrapper } from './VizWrapper';
|
import { VizWrapper } from './VizWrapper';
|
||||||
import { isExpressionQuery } from '../../../../expressions/guards';
|
import { isExpressionQuery } from 'app/features/expressions/guards';
|
||||||
|
import { TABLE, TIMESERIES } from '../../utils/constants';
|
||||||
import { GrafanaQuery } from 'app/types/unified-alerting-dto';
|
import { GrafanaQuery } from 'app/types/unified-alerting-dto';
|
||||||
import { cloneDeep } from 'lodash';
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
data: PanelData;
|
data: PanelData;
|
||||||
@@ -29,6 +30,8 @@ interface Props {
|
|||||||
index: number;
|
index: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SupportedPanelPlugins = 'timeseries' | 'table' | 'stat';
|
||||||
|
|
||||||
export const QueryWrapper: FC<Props> = ({
|
export const QueryWrapper: FC<Props> = ({
|
||||||
data,
|
data,
|
||||||
dsSettings,
|
dsSettings,
|
||||||
@@ -44,6 +47,7 @@ export const QueryWrapper: FC<Props> = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const styles = useStyles2(getStyles);
|
const styles = useStyles2(getStyles);
|
||||||
const isExpression = isExpressionQuery(query.model);
|
const isExpression = isExpressionQuery(query.model);
|
||||||
|
const [pluginId, changePluginId] = useState<SupportedPanelPlugins>(isExpression ? TABLE : TIMESERIES);
|
||||||
|
|
||||||
const renderTimePicker = (query: GrafanaQuery, index: number): ReactNode => {
|
const renderTimePicker = (query: GrafanaQuery, index: number): ReactNode => {
|
||||||
if (isExpressionQuery(query.model) || !onChangeTimeRange) {
|
if (isExpressionQuery(query.model) || !onChangeTimeRange) {
|
||||||
@@ -74,14 +78,16 @@ export const QueryWrapper: FC<Props> = ({
|
|||||||
onRunQuery={onRunQueries}
|
onRunQuery={onRunQueries}
|
||||||
queries={queries}
|
queries={queries}
|
||||||
renderHeaderExtras={() => renderTimePicker(query, index)}
|
renderHeaderExtras={() => renderTimePicker(query, index)}
|
||||||
|
visualization={data ? <VizWrapper data={data} changePanel={changePluginId} currentPanel={pluginId} /> : null}
|
||||||
|
hideDisableQuery={true}
|
||||||
/>
|
/>
|
||||||
{data && <VizWrapper data={data} defaultPanel={isExpression ? 'table' : 'timeseries'} />}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getStyles = (theme: GrafanaTheme2) => ({
|
const getStyles = (theme: GrafanaTheme2) => ({
|
||||||
wrapper: css`
|
wrapper: css`
|
||||||
|
label: AlertingQueryWrapper;
|
||||||
margin-bottom: ${theme.spacing(1)};
|
margin-bottom: ${theme.spacing(1)};
|
||||||
border: 1px solid ${theme.colors.border.medium};
|
border: 1px solid ${theme.colors.border.medium};
|
||||||
border-radius: ${theme.shape.borderRadius(1)};
|
border-radius: ${theme.shape.borderRadius(1)};
|
||||||
|
|||||||
@@ -5,22 +5,23 @@ import { GrafanaTheme2, PanelData } from '@grafana/data';
|
|||||||
import { config, PanelRenderer } from '@grafana/runtime';
|
import { config, PanelRenderer } from '@grafana/runtime';
|
||||||
import { RadioButtonGroup, useStyles2 } from '@grafana/ui';
|
import { RadioButtonGroup, useStyles2 } from '@grafana/ui';
|
||||||
import { PanelOptions } from 'app/plugins/panel/table/models.gen';
|
import { PanelOptions } from 'app/plugins/panel/table/models.gen';
|
||||||
|
import { SupportedPanelPlugins } from './QueryWrapper';
|
||||||
import { useVizHeight } from '../../hooks/useVizHeight';
|
import { useVizHeight } from '../../hooks/useVizHeight';
|
||||||
import { STAT, TABLE, TIMESERIES } from '../../utils/constants';
|
import { STAT, TABLE, TIMESERIES } from '../../utils/constants';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
data: PanelData;
|
data: PanelData;
|
||||||
defaultPanel?: 'timeseries' | 'table' | 'stat';
|
currentPanel: SupportedPanelPlugins;
|
||||||
|
changePanel: (panel: SupportedPanelPlugins) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const VizWrapper: FC<Props> = ({ data, defaultPanel }) => {
|
export const VizWrapper: FC<Props> = ({ data, currentPanel, changePanel }) => {
|
||||||
const [pluginId, changePluginId] = useState<string>(defaultPanel ?? TIMESERIES);
|
|
||||||
const [options, setOptions] = useState<PanelOptions>({
|
const [options, setOptions] = useState<PanelOptions>({
|
||||||
frameIndex: 0,
|
frameIndex: 0,
|
||||||
showHeader: true,
|
showHeader: true,
|
||||||
});
|
});
|
||||||
const panels = getSupportedPanels();
|
const panels = getSupportedPanels();
|
||||||
const vizHeight = useVizHeight(data, pluginId, options.frameIndex);
|
const vizHeight = useVizHeight(data, currentPanel, options.frameIndex);
|
||||||
const styles = useStyles2(getStyles);
|
const styles = useStyles2(getStyles);
|
||||||
|
|
||||||
if (!options || !data) {
|
if (!options || !data) {
|
||||||
@@ -30,7 +31,7 @@ export const VizWrapper: FC<Props> = ({ data, defaultPanel }) => {
|
|||||||
return (
|
return (
|
||||||
<div className={styles.wrapper}>
|
<div className={styles.wrapper}>
|
||||||
<div className={styles.buttonGroup}>
|
<div className={styles.buttonGroup}>
|
||||||
<RadioButtonGroup options={panels} value={pluginId} onChange={changePluginId} />
|
<RadioButtonGroup options={panels} value={currentPanel} onChange={changePanel} />
|
||||||
</div>
|
</div>
|
||||||
<div style={{ height: vizHeight, width: '100%' }}>
|
<div style={{ height: vizHeight, width: '100%' }}>
|
||||||
<AutoSizer style={{ width: '100%', height: '100%' }}>
|
<AutoSizer style={{ width: '100%', height: '100%' }}>
|
||||||
@@ -43,7 +44,7 @@ export const VizWrapper: FC<Props> = ({ data, defaultPanel }) => {
|
|||||||
height={height}
|
height={height}
|
||||||
width={width}
|
width={width}
|
||||||
data={data}
|
data={data}
|
||||||
pluginId={pluginId}
|
pluginId={currentPanel}
|
||||||
title="title"
|
title="title"
|
||||||
onOptionsChange={setOptions}
|
onOptionsChange={setOptions}
|
||||||
options={options}
|
options={options}
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ interface Props {
|
|||||||
onRemoveQuery: (query: DataQuery) => void;
|
onRemoveQuery: (query: DataQuery) => void;
|
||||||
onChange: (query: DataQuery) => void;
|
onChange: (query: DataQuery) => void;
|
||||||
onRunQuery: () => void;
|
onRunQuery: () => void;
|
||||||
|
visualization?: ReactNode;
|
||||||
|
hideDisableQuery?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
@@ -271,7 +273,7 @@ export class QueryEditorRow extends PureComponent<Props, State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
renderActions = (props: QueryOperationRowRenderProps) => {
|
renderActions = (props: QueryOperationRowRenderProps) => {
|
||||||
const { query } = this.props;
|
const { query, hideDisableQuery = false } = this.props;
|
||||||
const { hasTextEditMode, datasource } = this.state;
|
const { hasTextEditMode, datasource } = this.state;
|
||||||
const isDisabled = query.hide;
|
const isDisabled = query.hide;
|
||||||
|
|
||||||
@@ -292,11 +294,13 @@ export class QueryEditorRow extends PureComponent<Props, State> {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<QueryOperationAction title="Duplicate query" icon="copy" onClick={this.onCopyQuery} />
|
<QueryOperationAction title="Duplicate query" icon="copy" onClick={this.onCopyQuery} />
|
||||||
<QueryOperationAction
|
{!hideDisableQuery ? (
|
||||||
title="Disable/enable query"
|
<QueryOperationAction
|
||||||
icon={isDisabled ? 'eye-slash' : 'eye'}
|
title="Disable/enable query"
|
||||||
onClick={this.onDisableQuery}
|
icon={isDisabled ? 'eye-slash' : 'eye'}
|
||||||
/>
|
onClick={this.onDisableQuery}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
<QueryOperationAction title="Remove query" icon="trash-alt" onClick={this.onRemoveQuery} />
|
<QueryOperationAction title="Remove query" icon="trash-alt" onClick={this.onRemoveQuery} />
|
||||||
</HorizontalGroup>
|
</HorizontalGroup>
|
||||||
);
|
);
|
||||||
@@ -321,7 +325,7 @@ export class QueryEditorRow extends PureComponent<Props, State> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { query, id, index } = this.props;
|
const { query, id, index, visualization } = this.props;
|
||||||
const { datasource, showingHelp } = this.state;
|
const { datasource, showingHelp } = this.state;
|
||||||
const isDisabled = query.hide;
|
const isDisabled = query.hide;
|
||||||
|
|
||||||
@@ -359,6 +363,7 @@ export class QueryEditorRow extends PureComponent<Props, State> {
|
|||||||
)}
|
)}
|
||||||
{editor}
|
{editor}
|
||||||
</ErrorBoundaryAlert>
|
</ErrorBoundaryAlert>
|
||||||
|
{visualization}
|
||||||
</div>
|
</div>
|
||||||
</QueryOperationRow>
|
</QueryOperationRow>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user