[v11.1.x] Alerting: Skip fetching alerts for unsaved dashboards (#90074)

* Alerting: Skip fetching alerts for unsaved dashboards (#90061)

skip fetching alerts for dashboards with null uid

(cherry picked from commit dd4e254900)

---------

Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
This commit is contained in:
grafana-delivery-bot[bot]
2024-07-04 17:36:12 +02:00
committed by GitHub
co-authored by Gilles De Mey
parent 2835a1c9c7
commit b1751659c7
3 changed files with 28 additions and 5 deletions
@@ -1,7 +1,7 @@
import { render } from '@testing-library/react';
import React from 'react';
import { TestProvider } from 'test/helpers/TestProvider';
import { byTestId } from 'testing-library-selector';
import { byTestId, byText } from 'testing-library-selector';
import { DataSourceApi } from '@grafana/data';
import { PromOptions, PrometheusDatasource } from '@grafana/prometheus';
@@ -184,6 +184,7 @@ const panel = new PanelModel({
const ui = {
row: byTestId('row'),
createButton: byTestId<HTMLAnchorElement>('create-alert-rule-button'),
notSavedYet: byText('Dashboard not saved'),
};
const server = setupMswServer();
@@ -282,6 +283,25 @@ describe('PanelAlertTabContent', () => {
});
});
it('should not make requests for unsaved dashboard', async () => {
const unsavedDashboard = {
...dashboard,
uid: null,
} as DashboardModel;
renderAlertTabContent(
unsavedDashboard,
new PanelModel({
...panel,
datasource: undefined,
maxDataPoints: 100,
interval: '10s',
})
);
expect(await ui.notSavedYet.find()).toBeInTheDocument();
});
it('Will take into account datasource minInterval', async () => {
(getDatasourceSrv() as unknown as MockDataSourceSrv).datasources[dataSources.prometheus.uid].interval = '7m';
@@ -468,7 +468,7 @@ function hashQuery(query: string) {
This hook returns combined Grafana rules. Optionally, it can filter rules by dashboard UID and panel ID.
*/
export function useCombinedRules(
dashboardUID?: string,
dashboardUID?: string | null,
panelId?: number,
poll?: boolean
): {
@@ -483,10 +483,12 @@ export function useCombinedRules(
} = alertRuleApi.endpoints.prometheusRuleNamespaces.useQuery(
{
ruleSourceName: GRAFANA_RULES_SOURCE_NAME,
dashboardUid: dashboardUID,
dashboardUid: dashboardUID ?? undefined,
panelId,
},
{
// "null" means the dashboard isn't saved yet, as opposed to "undefined" which means we don't want to filter by dashboard UID
skip: dashboardUID === null,
pollingInterval: poll ? RULE_LIST_POLL_INTERVAL_MS : undefined,
}
);
@@ -498,10 +500,11 @@ export function useCombinedRules(
} = alertRuleApi.endpoints.rulerRules.useQuery(
{
rulerConfig: grafanaRulerConfig,
filter: { dashboardUID, panelId },
filter: { dashboardUID: dashboardUID ?? undefined, panelId },
},
{
pollingInterval: poll ? RULE_LIST_POLL_INTERVAL_MS : undefined,
skip: dashboardUID === null,
}
);
@@ -3,7 +3,7 @@ import { CombinedRule } from 'app/types/unified-alerting';
import { useCombinedRules } from './useCombinedRuleNamespaces';
interface Options {
dashboardUID: string;
dashboardUID: string | null;
panelId: number;
poll?: boolean;