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 { isTruthy } from '@grafana/data';
import { BackendSrvRequest, getBackendSrv } from '@grafana/runtime'; import { BackendSrvRequest, getBackendSrv } from '@grafana/runtime';
import { DescendantCount, FolderDTO } from 'app/types'; import { DescendantCount, DescendantCountDTO, FolderDTO } from 'app/types';
import { DashboardTreeSelection } from '../types'; import { DashboardTreeSelection } from '../types';
@ -41,30 +41,30 @@ export const browseDashboardsAPI = createApi({
getAffectedItems: builder.query<DescendantCount, DashboardTreeSelection>({ getAffectedItems: builder.query<DescendantCount, DashboardTreeSelection>({
queryFn: async (selectedItems) => { queryFn: async (selectedItems) => {
const folderUIDs = Object.keys(selectedItems.folder).filter((uid) => selectedItems.folder[uid]); const folderUIDs = Object.keys(selectedItems.folder).filter((uid) => selectedItems.folder[uid]);
const promises = folderUIDs.map((folderUID) => { 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 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 const totalCounts = {
// acc.libraryPanel += val.libraryPanel; folder: Object.values(selectedItems.folder).filter(isTruthy).length,
// acc.alertRule += val.alertRule; dashboard: Object.values(selectedItems.dashboard).filter(isTruthy).length,
return acc; libraryPanel: 0,
}, alertRule: 0,
{ };
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; canViewFolderPermissions: boolean;
} }
export interface DescendantCountDTO {
folder: number;
dashboard: number;
libraryPanel: number;
alertrule?: number;
}
export interface DescendantCount { export interface DescendantCount {
folder: number; folder: number;
dashboard: number; dashboard: number;