mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerts: Dedupe alerts so that we do not fill the screen with the same alert messsage (#30935)
This commit is contained in:
@@ -116,4 +116,46 @@ describe('notify', () => {
|
||||
|
||||
expect(result).toEqual(expectedResult);
|
||||
});
|
||||
|
||||
it('Dedupe identical alerts', () => {
|
||||
const initialState = {
|
||||
appNotifications: [
|
||||
{
|
||||
id: 'id1',
|
||||
severity: AppNotificationSeverity.Success,
|
||||
icon: 'success',
|
||||
title: 'test',
|
||||
text: 'test alert',
|
||||
timeout: AppNotificationTimeout.Success,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = appNotificationsReducer(
|
||||
initialState,
|
||||
notifyApp({
|
||||
id: 'id2',
|
||||
severity: AppNotificationSeverity.Success,
|
||||
icon: 'success',
|
||||
title: 'test',
|
||||
text: 'test alert',
|
||||
timeout: AppNotificationTimeout.Success,
|
||||
})
|
||||
);
|
||||
|
||||
const expectedResult = {
|
||||
appNotifications: [
|
||||
{
|
||||
id: 'id1',
|
||||
severity: AppNotificationSeverity.Success,
|
||||
icon: 'success',
|
||||
title: 'test',
|
||||
text: 'test alert',
|
||||
timeout: AppNotificationTimeout.Success,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
expect(result).toEqual(expectedResult);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,10 +15,23 @@ const appNotificationsSlice = createSlice({
|
||||
name: 'appNotifications',
|
||||
initialState,
|
||||
reducers: {
|
||||
notifyApp: (state, action: PayloadAction<AppNotification>): AppNotificationsState => ({
|
||||
...state,
|
||||
appNotifications: state.appNotifications.concat([action.payload]),
|
||||
}),
|
||||
notifyApp: (state, action: PayloadAction<AppNotification>) => {
|
||||
const newAlert = action.payload;
|
||||
|
||||
for (const existingAlert of state.appNotifications) {
|
||||
if (
|
||||
newAlert.icon === existingAlert.icon &&
|
||||
newAlert.severity === existingAlert.severity &&
|
||||
newAlert.text === existingAlert.text &&
|
||||
newAlert.title === existingAlert.title &&
|
||||
newAlert.component === existingAlert.component
|
||||
) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
state.appNotifications.push(newAlert);
|
||||
},
|
||||
clearAppNotification: (state, action: PayloadAction<string>): AppNotificationsState => ({
|
||||
...state,
|
||||
appNotifications: state.appNotifications.filter((appNotification) => appNotification.id !== action.payload),
|
||||
|
||||
Reference in New Issue
Block a user