Files
grafana/public/app/features/alerting/unified/components/rules/RuleDetailsDataSources.tsx

75 lines
2.1 KiB
TypeScript
Raw Normal View History

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
import { css } from '@emotion/css';
import { GrafanaTheme2 } from '@grafana/data';
import { getDataSourceSrv } from '@grafana/runtime';
import { useStyles2 } from '@grafana/ui';
import { ExpressionDatasourceUID } from 'app/features/expressions/ExpressionDatasource';
import { CombinedRule, RulesSource } from 'app/types/unified-alerting';
import React, { useMemo } from 'react';
import { isCloudRulesSource } from '../../utils/datasource';
import { isGrafanaRulerRule } from '../../utils/rules';
import { DetailsField } from '../DetailsField';
type Props = {
rule: CombinedRule;
rulesSource: RulesSource;
};
export function RuleDetailsDataSources(props: Props): JSX.Element | null {
const { rulesSource, rule } = props;
const styles = useStyles2(getStyles);
const dataSources: Array<{ name: string; icon?: string }> = useMemo(() => {
if (isCloudRulesSource(rulesSource)) {
return [{ name: rulesSource.name, icon: rulesSource.meta.info.logos.small }];
}
if (isGrafanaRulerRule(rule.rulerRule)) {
const { data } = rule.rulerRule.grafana_alert;
const unique = data.reduce((dataSources, query) => {
const ds = getDataSourceSrv().getInstanceSettings(query.datasourceUid);
if (!ds || ds.uid === ExpressionDatasourceUID) {
return dataSources;
}
dataSources[ds.name] = { name: ds.name, icon: ds.meta.info.logos.small };
return dataSources;
}, {} as Record<string, { name: string; icon?: string }>);
return Object.values(unique);
}
return [];
}, [rule, rulesSource]);
if (dataSources.length === 0) {
return null;
}
return (
<DetailsField label="Data source">
{dataSources.map(({ name, icon }, index) => (
<div key={name}>
{icon && (
<>
<img alt={`${name} datasource logo`} className={styles.dataSourceIcon} src={icon} />{' '}
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
</>
)}
{name}
</div>
))}
</DetailsField>
);
}
function getStyles(theme: GrafanaTheme2) {
const size = theme.spacing(2);
return {
dataSourceIcon: css`
width: ${size};
height: ${size};
`,
};
}