The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/public/app/features/alerting/unified/components/rules/CloudRules.tsx

187 lines
6.5 KiB

import { css } from '@emotion/css';
import { useMemo } from 'react';
import { useLocation } from 'react-router-dom-v5-compat';
import { GrafanaTheme2, urlUtil } from '@grafana/data';
import { Trans, useTranslate } from '@grafana/i18n';
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
import { config } from '@grafana/runtime';
import { Badge, LinkButton, LoadingPlaceholder, Pagination, Spinner, Stack, Text, useStyles2 } from '@grafana/ui';
import { CombinedRuleNamespace } from 'app/types/unified-alerting';
import { DEFAULT_PER_PAGE_PAGINATION } from '../../../../../core/constants';
import { AlertingAction, useAlertingAbility } from '../../hooks/useAbilities';
import { usePagination } from '../../hooks/usePagination';
import { useUnifiedAlertingSelector } from '../../hooks/useUnifiedAlertingSelector';
import { getPaginationStyles } from '../../styles/pagination';
import { getRulesDataSources, getRulesSourceUid } from '../../utils/datasource';
import { isAsyncRequestStatePending } from '../../utils/redux';
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
import { createRelativeUrl } from '../../utils/url';
import { RulesGroup } from './RulesGroup';
import { useCombinedGroupNamespace } from './useCombinedGroupNamespace';
interface Props {
namespaces: CombinedRuleNamespace[];
expandAll: boolean;
}
export const CloudRules = ({ namespaces, expandAll }: Props) => {
const styles = useStyles2(getStyles);
const promRules = useUnifiedAlertingSelector((state) => state.promRules);
const rulesDataSources = useMemo(getRulesDataSources, []);
const groupsWithNamespaces = useCombinedGroupNamespace(namespaces);
const dataSourcesLoading = useMemo(
() => rulesDataSources.filter((ds) => isAsyncRequestStatePending(promRules[ds.name])),
[promRules, rulesDataSources]
);
const hasSomeResults = rulesDataSources.some((ds) => Boolean(promRules[ds.name]?.result?.length));
const hasDataSourcesConfigured = rulesDataSources.length > 0;
const hasDataSourcesLoading = dataSourcesLoading.length > 0;
const hasNamespaces = namespaces.length > 0;
const { numberOfPages, onPageChange, page, pageItems } = usePagination(
groupsWithNamespaces,
1,
DEFAULT_PER_PAGE_PAGINATION
);
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
const [createRuleSupported, createRuleAllowed] = useAlertingAbility(AlertingAction.CreateAlertRule);
const [viewExternalRuleSupported, viewExternalRuleAllowed] = useAlertingAbility(AlertingAction.ViewExternalAlertRule);
const { t } = useTranslate();
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
const canViewCloudRules = viewExternalRuleSupported && viewExternalRuleAllowed;
const canCreateGrafanaRules = createRuleSupported && createRuleAllowed;
const canMigrateToGMA =
hasDataSourcesConfigured && canCreateGrafanaRules && canViewCloudRules && config.featureToggles.alertingMigrationUI;
return (
<section className={styles.wrapper}>
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
<Stack gap={2} direction="column">
<div className={styles.sectionHeader}>
<div className={styles.headerRow}>
<Text element="h2" variant="h5">
<Trans i18nKey="alerting.list-view.section.dataSourceManaged.title">Data source-managed</Trans>
</Text>
{dataSourcesLoading.length ? (
<LoadingPlaceholder
className={styles.loader}
text={t('alerting.list-view.section.loading-rules', 'Loading rules from {{count}} sources', {
count: dataSourcesLoading.length,
})}
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
/>
) : (
<div />
)}
<Stack gap={1}>
{canMigrateToGMA && hasSomeResults && <MigrateToGMAButton />}
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
<CreateRecordingRuleButton />
</Stack>
</div>
</div>
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
</Stack>
{pageItems.map(({ group, namespace }) => (
<RulesGroup
group={group}
key={`${getRulesSourceUid(namespace.rulesSource)}-${namespace.name}-${group.name}`}
namespace={namespace}
expandAll={expandAll}
viewMode={'grouped'}
/>
))}
{!hasDataSourcesConfigured && (
<p>
<Trans i18nKey="alerting.list-view.no-prom-or-loki-rules">
There are no Prometheus or Loki data sources configured
</Trans>
</p>
)}
{hasDataSourcesConfigured && !hasDataSourcesLoading && !hasNamespaces && (
<p>
<Trans i18nKey="alerting.list-view.no-rules">No rules found.</Trans>
</p>
)}
{!hasSomeResults && hasDataSourcesLoading && <Spinner size="xl" className={styles.spinner} />}
<Pagination
className={styles.pagination}
currentPage={page}
numberOfPages={numberOfPages}
onNavigate={onPageChange}
hideWhenSinglePage
/>
</section>
);
};
const getStyles = (theme: GrafanaTheme2) => ({
loader: css({
marginBottom: 0,
}),
sectionHeader: css({
display: 'flex',
justifyContent: 'space-between',
}),
wrapper: css({
marginBottom: theme.spacing(4),
}),
spinner: css({
textAlign: 'center',
padding: theme.spacing(2),
}),
pagination: getPaginationStyles(theme),
headerRow: css({
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
width: '100%',
marginBottom: theme.spacing(1),
}),
});
export function CreateRecordingRuleButton() {
const [createCloudRuleSupported, createCloudRuleAllowed] = useAlertingAbility(AlertingAction.CreateExternalAlertRule);
const location = useLocation();
const canCreateCloudRules = createCloudRuleSupported && createCloudRuleAllowed;
if (canCreateCloudRules) {
return (
<LinkButton
key="new-recording-rule"
href={urlUtil.renderUrl(`alerting/new/recording`, {
returnTo: location.pathname + location.search,
})}
icon="plus"
variant="secondary"
>
<Trans i18nKey="alerting.list-view.empty.new-ds-managed-recording-rule">
New data source-managed recording rule
</Trans>
</LinkButton>
);
}
return null;
}
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
function MigrateToGMAButton() {
const importUrl = createRelativeUrl('/alerting/import-datasource-managed-rules');
const { t } = useTranslate();
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
return (
<LinkButton variant="secondary" href={importUrl} icon="arrow-up">
<Stack direction="row" gap={1} alignItems="center">
<Trans i18nKey="alerting.rule-list.import-to-gma.text">Import to Grafana-managed rules</Trans>
<Badge
text={t('alerting.rule-list.import-to-gma.new-badge', 'New!')}
aria-label={t('alerting.migrate-to-gmabutton.aria-label-new', 'new')}
Alerting: Migration UI (#102010) * add feature toggle alertingMigrationUI * wip * wip * wip2 * add button to export in alert list and add target folder in the form * update namings * protext migration route with alertingMigrationUI feature toggle * remove unused error style * add namspace and group filter * fix translations and prettier * fix duplication * use the new endpoint * update right roles for the FE route * refactor * notify success * address feedback * fix translations * Simplify migration UI for alert rules to GMA format Extracts migration button component and reuses it in banner for better code organization and consistency. * Fix root folder check in import alert rules UI Replace hardcoded empty string check with lodash isEmpty for folder UID validation * Add collapsible section for import rule options * address feedback * Split useAlertRuleSuggestions, one for labels, and other for namespaces * refactor splited hooks * Alerting: (PR review) Redesign slightly + use combobox (#102614) Redesign slightly + use combobox * udpate naming * remove eslint-disable-next-line @grafana/no-untranslated-strings and add hasDataSourcesConfigured as a condition to render the banner * use button instead of banner * fix routes * use new api * update api * remove query params * update translations * update ff files * skip subpath when redirecting to the alert list view with filters * prettier * disable provenance * prettier * Don't send target forlder uid in the payload as undefined if it's not selected * prettier * add confirmation modal with a preview * remove unnecessary export * Add target datasource UID for recording rules * Use skipToken * Add recording rule datasource and update designs * Use different hook to fetch rulerRules --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2 months ago
color="blue"
icon="rocket"
/>
</Stack>
</LinkButton>
);
}