mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 01:53:33 -06:00
* 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>
119 lines
3.2 KiB
TypeScript
119 lines
3.2 KiB
TypeScript
import React, { FC, useState } from 'react';
|
|
import { css } from '@emotion/css';
|
|
import AutoSizer from 'react-virtualized-auto-sizer';
|
|
import { DataFrame, DataQuery, GrafanaTheme, PanelData } from '@grafana/data';
|
|
import { Icon, Tab, TabContent, TabsBar, useStyles } from '@grafana/ui';
|
|
import { PreviewQueryTab } from './PreviewQueryTab';
|
|
import { PreviewInstancesTab } from './PreviewInstancesTab';
|
|
import { EmptyState } from './EmptyState';
|
|
|
|
enum Tabs {
|
|
Query = 'query',
|
|
Instances = 'instances',
|
|
}
|
|
|
|
const tabs = [
|
|
{ id: Tabs.Query, text: 'Query result' },
|
|
{ id: Tabs.Instances, text: 'Alerting instances' },
|
|
];
|
|
|
|
interface Props {
|
|
getInstances: () => DataFrame[];
|
|
queries: DataQuery[];
|
|
onTest: () => void;
|
|
}
|
|
|
|
export const AlertingQueryPreview: FC<Props> = ({ getInstances, onTest, queries }) => {
|
|
const [activeTab, setActiveTab] = useState<string>(Tabs.Query);
|
|
const styles = useStyles(getStyles);
|
|
|
|
let data = {} as PanelData;
|
|
|
|
const instances = getInstances();
|
|
|
|
return (
|
|
<div className={styles.wrapper}>
|
|
<TabsBar>
|
|
{tabs.map((tab, index) => {
|
|
return (
|
|
<Tab
|
|
key={`${tab.id}-${index}`}
|
|
label={tab.text}
|
|
onChangeTab={() => setActiveTab(tab.id)}
|
|
active={activeTab === tab.id}
|
|
/>
|
|
);
|
|
})}
|
|
</TabsBar>
|
|
<TabContent className={styles.tabContent}>
|
|
{data &&
|
|
(data.state === 'Error' ? (
|
|
<EmptyState title="There was an error :(">
|
|
<div>{data.error?.data?.error}</div>
|
|
</EmptyState>
|
|
) : (
|
|
<QueriesAndInstances
|
|
instances={instances}
|
|
onTest={onTest}
|
|
data={data}
|
|
activeTab={activeTab}
|
|
queries={queries}
|
|
/>
|
|
))}
|
|
</TabContent>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
interface PreviewProps {
|
|
queries: DataQuery[];
|
|
instances: DataFrame[];
|
|
onTest: () => void;
|
|
data: PanelData;
|
|
activeTab: string;
|
|
}
|
|
|
|
const QueriesAndInstances: FC<PreviewProps> = ({ queries, instances, onTest, data, activeTab }) => {
|
|
if (queries.length === 0) {
|
|
return (
|
|
<EmptyState title="No queries added.">
|
|
<div>Start adding queries to this alert and a visualisation for your queries will appear here.</div>
|
|
<div>
|
|
Learn more about how to create alert definitions <Icon name="external-link-alt" />
|
|
</div>
|
|
</EmptyState>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<AutoSizer style={{ width: '100%', height: '100%' }}>
|
|
{({ width, height }) => {
|
|
switch (activeTab) {
|
|
case Tabs.Instances:
|
|
return <PreviewInstancesTab instances={instances} width={width} height={height} onTest={onTest} />;
|
|
|
|
case Tabs.Query:
|
|
default:
|
|
return <PreviewQueryTab data={data} width={width} height={height} />;
|
|
}
|
|
}}
|
|
</AutoSizer>
|
|
);
|
|
};
|
|
|
|
const getStyles = (theme: GrafanaTheme) => {
|
|
return {
|
|
wrapper: css`
|
|
display: flex;
|
|
flex-direction: column;
|
|
width: 100%;
|
|
height: 100%;
|
|
padding: ${theme.spacing.md} 0 0 ${theme.spacing.md};
|
|
`,
|
|
tabContent: css`
|
|
background: ${theme.colors.panelBg};
|
|
height: 100%;
|
|
`,
|
|
};
|
|
};
|