grafana/public/app/features/alerting/components/PreviewQueryTab.tsx
Peter Holmberg 569fb3f112
Alerting: adding query editor when creating threshold rule. (#33123)
* fix viz

* add datasource picker on query rows in mixed mode

* add timerange, handle add/remove queryrunners

* multiqueryrunner test

* trying things out.

* adding another test to verify running a induvidual query runner will update multirunner.

* cleaned up tests a bit.

* draft version working ok.

* fixing so we base the refId from request targets.

* reenable adding expression

* layout fixes for alerting page

* some cleanup

* cleaning up code that we won't use

* changed so we don't display the time range if params not passed.

* remove unused things in querygroup

* changed button to type button.

* removed timerange from dataQuery and removed multiquery runner.

* minor refactoring.

* renamed callback function to make it more clear what it does.

* renamed droppable area.

* changed so we only display the query editor when selecting threshold.

* removed the refresh picker.

* revert

* wip

* extending with data query.

* timerange fixes

* it is now possible to add grafana queries.

* removed unused type.

* removed expect import.

* added docs.

* moved range converting methods to rangeUtil.

* clean up some typings, remove file

* making sure we don't blow up on component being unmounted.

Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
2021-04-21 13:57:17 +02:00

75 lines
1.9 KiB
TypeScript

import React, { FC, useMemo, useState } from 'react';
import { css } from '@emotion/css';
import { getFrameDisplayName, GrafanaTheme, PanelData, SelectableValue } from '@grafana/data';
import { Button, Select, stylesFactory, Table, useTheme } from '@grafana/ui';
import { EmptyState } from './EmptyState';
interface Props {
data: PanelData;
width: number;
height: number;
}
export const PreviewQueryTab: FC<Props> = ({ data, height, width }) => {
const [currentSeries, setSeries] = useState<number>(0);
const theme = useTheme();
const styles = getStyles(theme, height);
const series = useMemo<Array<SelectableValue<number>>>(() => {
if (data?.series) {
return data.series.map((frame, index) => ({ value: index, label: getFrameDisplayName(frame) }));
}
return [];
}, [data]);
// Select padding
const padding = 16;
if (!data) {
return (
<EmptyState title="Run queries to view data.">
<Button>Run queries</Button>
</EmptyState>
);
}
if (!data.series) {
return null;
}
if (data.series.length > 1) {
return (
<div className={styles.wrapper}>
<div style={{ height: height - theme.spacing.formInputHeight - 16 }}>
<Table
data={data.series[currentSeries]}
height={height - theme.spacing.formInputHeight - padding}
width={width}
/>
</div>
<div className={styles.selectWrapper}>
<Select
onChange={(selectedValue) => setSeries(selectedValue.value!)}
options={series}
value={currentSeries}
/>
</div>
</div>
);
}
return <Table data={data.series[0]} height={height} width={width} />;
};
const getStyles = stylesFactory((theme: GrafanaTheme, height: number) => {
return {
wrapper: css`
label: preview-wrapper;
height: ${height}px;
`,
selectWrapper: css`
padding: ${theme.spacing.md};
`,
};
});