Files
grafana/public/app/features/alerting/unified/components/rule-editor/QueryWrapper.tsx
Marcus Andersson c9e28044f1 Alerting: view to display alert rule and its underlying data. (#35546)
* add page and basic things

* quick annotations

* added so we can run queries on the view rule page.

* wip.

* merge

* cleaned up the combined rule hook.

* readd queries

* fixing so you can run queries.

* renamed variable.

* fix rerenders and visualizing

* minor fixes.

* work in progress.

* wip

* a working version that can be tested.

* changing check if we have data.

* removed unused styling.

* removed unused dep.

* removed another dep.

* Update public/app/features/alerting/unified/hooks/useCombinedRule.ts

Co-authored-by: Domas <domas.lapinskas@grafana.com>

* Update public/app/features/alerting/unified/hooks/useCombinedRule.ts

Co-authored-by: Domas <domas.lapinskas@grafana.com>

* refactored and changed UI according to figma.

* resseting menu item.

* removing unused external link.

* refactor according to feedback.

* changed so we always fetch the rule.

* fixing so datasource only is displayed once. Also changed so we only navigate to alert list when rule has been deleted.

* removed unused dep.

* Will display query as json if we can't find data source.

* changed to a function instead of the React.FC.

* refactoring of id generation and added support to generate ids for native prometheus alerts without ruler.

* set max width on page content

* added page where you can easily link to a rule in grafana.

* listing rules with same name.

* fixing error cases.

* updates after pr feedback

* more pr feedback

* use 1h-now as timerange

* remove unused import

* start on test

* add test for cloud case

* add ruleview render test

* add render tests for grafana and cloud alerts

* add mock for backendsrv

* add rendering test for the find route

* check if cards are rendered

Co-authored-by: Peter Holmberg <peter.hlmbrg@gmail.com>
Co-authored-by: Domas <domas.lapinskas@grafana.com>
2021-07-01 12:02:41 +02:00

96 lines
3.0 KiB
TypeScript

import React, { FC, ReactNode, useState } from 'react';
import { css } from '@emotion/css';
import { cloneDeep } from 'lodash';
import {
DataQuery,
DataSourceInstanceSettings,
GrafanaTheme2,
PanelData,
RelativeTimeRange,
getDefaultRelativeTimeRange,
} from '@grafana/data';
import { useStyles2, RelativeTimeRangePicker } from '@grafana/ui';
import { QueryEditorRow } from 'app/features/query/components/QueryEditorRow';
import { VizWrapper } from './VizWrapper';
import { isExpressionQuery } from 'app/features/expressions/guards';
import { TABLE, TIMESERIES } from '../../utils/constants';
import { AlertQuery } from 'app/types/unified-alerting-dto';
import { SupportedPanelPlugins } from '../PanelPluginsButtonGroup';
interface Props {
data: PanelData;
query: AlertQuery;
queries: AlertQuery[];
dsSettings: DataSourceInstanceSettings;
onChangeDataSource: (settings: DataSourceInstanceSettings, index: number) => void;
onChangeQuery: (query: DataQuery, index: number) => void;
onChangeTimeRange?: (timeRange: RelativeTimeRange, index: number) => void;
onRemoveQuery: (query: DataQuery) => void;
onDuplicateQuery: (query: AlertQuery) => void;
onRunQueries: () => void;
index: number;
}
export const QueryWrapper: FC<Props> = ({
data,
dsSettings,
index,
onChangeDataSource,
onChangeQuery,
onChangeTimeRange,
onRunQueries,
onRemoveQuery,
onDuplicateQuery,
query,
queries,
}) => {
const styles = useStyles2(getStyles);
const isExpression = isExpressionQuery(query.model);
const [pluginId, changePluginId] = useState<SupportedPanelPlugins>(isExpression ? TABLE : TIMESERIES);
const renderTimePicker = (query: AlertQuery, index: number): ReactNode => {
if (isExpressionQuery(query.model) || !onChangeTimeRange) {
return null;
}
return (
<RelativeTimeRangePicker
timeRange={query.relativeTimeRange ?? getDefaultRelativeTimeRange()}
onChange={(range) => onChangeTimeRange(range, index)}
/>
);
};
return (
<div className={styles.wrapper}>
<QueryEditorRow
dataSource={dsSettings}
onChangeDataSource={!isExpression ? (settings) => onChangeDataSource(settings, index) : undefined}
id={query.refId}
index={index}
key={query.refId}
data={data}
query={cloneDeep(query.model)}
onChange={(query) => onChangeQuery(query, index)}
onRemoveQuery={onRemoveQuery}
onAddQuery={onDuplicateQuery}
onRunQuery={onRunQueries}
queries={queries}
renderHeaderExtras={() => renderTimePicker(query, index)}
visualization={data ? <VizWrapper data={data} changePanel={changePluginId} currentPanel={pluginId} /> : null}
hideDisableQuery={true}
/>
</div>
);
};
const getStyles = (theme: GrafanaTheme2) => ({
wrapper: css`
label: AlertingQueryWrapper;
margin-bottom: ${theme.spacing(1)};
border: 1px solid ${theme.colors.border.medium};
border-radius: ${theme.shape.borderRadius(1)};
padding-bottom: ${theme.spacing(1)};
`,
});