Show alert rule counts when API returns them

This commit is contained in:
joshhunt 2023-05-02 19:44:40 +01:00
parent d2edc9d1d6
commit d71ef0a0c4
No known key found for this signature in database
GPG Key ID: F767869890CBA3F6
2 changed files with 26 additions and 19 deletions

View File

@ -3,7 +3,7 @@ import { lastValueFrom } from 'rxjs';
import { isTruthy } from '@grafana/data';
import { BackendSrvRequest, getBackendSrv } from '@grafana/runtime';
import { DescendantCount, FolderDTO } from 'app/types';
import { DescendantCount, DescendantCountDTO, FolderDTO } from 'app/types';
import { DashboardTreeSelection } from '../types';
@ -41,30 +41,30 @@ export const browseDashboardsAPI = createApi({
getAffectedItems: builder.query<DescendantCount, DashboardTreeSelection>({
queryFn: async (selectedItems) => {
const folderUIDs = Object.keys(selectedItems.folder).filter((uid) => selectedItems.folder[uid]);
const promises = folderUIDs.map((folderUID) => {
return getBackendSrv().get<DescendantCount>(`/api/folders/${folderUID}/counts`);
return getBackendSrv().get<DescendantCountDTO>(`/api/folders/${folderUID}/counts`);
});
const results = await Promise.all(promises);
const aggregatedResults = results.reduce(
(acc, val) => {
acc.folder += val.folder;
acc.dashboard += val.dashboard;
// TODO enable these once the backend correctly returns them
// acc.libraryPanel += val.libraryPanel;
// acc.alertRule += val.alertRule;
return acc;
},
{
const totalCounts = {
folder: Object.values(selectedItems.folder).filter(isTruthy).length,
dashboard: Object.values(selectedItems.dashboard).filter(isTruthy).length,
libraryPanel: 0,
alertRule: 0,
}
);
};
return { data: aggregatedResults };
for (const folderCounts of results) {
totalCounts.folder += folderCounts.folder;
totalCounts.dashboard += folderCounts.dashboard;
totalCounts.alertRule += folderCounts.alertrule ?? 0;
// TODO enable these once the backend correctly returns them
// totalCounts.libraryPanel += folderCounts.libraryPanel;
}
return { data: totalCounts };
},
}),
}),

View File

@ -27,6 +27,13 @@ export interface FolderState {
canViewFolderPermissions: boolean;
}
export interface DescendantCountDTO {
folder: number;
dashboard: number;
libraryPanel: number;
alertrule?: number;
}
export interface DescendantCount {
folder: number;
dashboard: number;