fix(通知): 聚合去重键从小时桶改自然日,刷新不再把已读点亮回未读
后台「通知」被刷屏的两个乘数:一条告警一天最多重发 24 次(小时桶换键), 以及刷新时 set read_at=null 把用户已读过的红点重新点亮。现在 task_failed / service_down / maixiang_anomaly 五类键统一按「用户×模块×自然日」聚合, pushOrRefresh 只更新文案不动 read_at;桶的日期取事件自身的 updatedAt, 跨零点轮询不会把昨天的失败挪进今天的桶。
This commit is contained in:
+4
-4
@@ -162,7 +162,7 @@ class MaixiangAnomalyScannerTest {
|
||||
content.capture(), key.capture(), isNull());
|
||||
assertThat(content.getValue()).contains("1 条任务失败");
|
||||
assertThat(content.getValue()).contains("未注册的任务类型: price-track-run");
|
||||
assertThat(key.getValue()).isEqualTo("maixiang_fail:2026091512");
|
||||
assertThat(key.getValue()).isEqualTo("maixiang_fail:20260915");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -238,7 +238,7 @@ class MaixiangAnomalyScannerTest {
|
||||
ArgumentCaptor<String> content = ArgumentCaptor.forClass(String.class);
|
||||
verify(dispatch).pushToAdmins(eq(audience), eq(NotificationService.SCENE_MAIXIANG_ANOMALY),
|
||||
eq(NotificationService.LEVEL_WARNING), eq("麦象异常:麦象队列积压"),
|
||||
content.capture(), eq("maixiang_queue_backlog:2026091512"), isNull());
|
||||
content.capture(), eq("maixiang_queue_backlog:20260915"), isNull());
|
||||
assertThat(content.getValue()).contains("L1 待处理 210 / 处理中 0").contains("堆积且无人在消费");
|
||||
}
|
||||
|
||||
@@ -263,7 +263,7 @@ class MaixiangAnomalyScannerTest {
|
||||
newScanner().checkQueueBacklog(audience, NOW);
|
||||
|
||||
verify(dispatch).pushToAdmins(any(), anyString(), anyString(), anyString(), anyString(),
|
||||
eq("maixiang_queue_backlog:2026091512"), any());
|
||||
eq("maixiang_queue_backlog:20260915"), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -292,7 +292,7 @@ class MaixiangAnomalyScannerTest {
|
||||
ArgumentCaptor<String> content = ArgumentCaptor.forClass(String.class);
|
||||
verify(dispatch).pushToAdmins(eq(audience), eq(NotificationService.SCENE_MAIXIANG_ANOMALY),
|
||||
eq(NotificationService.LEVEL_WARNING), eq("麦象异常:采集机掉线"),
|
||||
content.capture(), eq("maixiang_silent:2026091512"), isNull());
|
||||
content.capture(), eq("maixiang_silent:20260915"), isNull());
|
||||
assertThat(content.getValue())
|
||||
.contains("静默 11 分钟")
|
||||
.contains("跟价 静默 11 分钟")
|
||||
|
||||
+40
-6
@@ -15,6 +15,7 @@ import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -64,13 +65,13 @@ class NotificationDispatchServiceTest {
|
||||
|
||||
NotificationDispatchService.AdminAudience audience = service.prepareAdminAudience();
|
||||
int pushed = service.pushToAdmins(audience, NotificationService.SCENE_TASK_FAILED,
|
||||
NotificationService.LEVEL_WARNING, "标题", "内容", "task_failed_admin:20:PRICE_TRACK:2026091310", 20L);
|
||||
NotificationService.LEVEL_WARNING, "标题", "内容", "task_failed_admin:20:PRICE_TRACK:20260913", 20L);
|
||||
|
||||
// 主管乙不可见用户 20 → 只有超管收到
|
||||
assertThat(pushed).isEqualTo(1);
|
||||
org.mockito.Mockito.verify(notificationService).pushOrRefresh(eq(1L), eq(NotificationService.AUDIENCE_ADMIN),
|
||||
eq(NotificationService.SCENE_TASK_FAILED), eq(NotificationService.LEVEL_WARNING),
|
||||
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:2026091310:1"));
|
||||
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:20260913:1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -89,21 +90,54 @@ class NotificationDispatchServiceTest {
|
||||
|
||||
int pushed = service.pushToAdmins(NotificationService.SCENE_TASK_FAILED,
|
||||
NotificationService.LEVEL_WARNING, "标题", "内容",
|
||||
"task_failed_admin:20:PRICE_TRACK:2026091310", 20L);
|
||||
"task_failed_admin:20:PRICE_TRACK:20260913", 20L);
|
||||
|
||||
// 超管全量 + 本组主管各落一条,各带自己的 dedupe 后缀(已读状态互不影响)
|
||||
assertThat(pushed).isEqualTo(2);
|
||||
verify(notificationService).pushOrRefresh(eq(1L), eq(NotificationService.AUDIENCE_ADMIN),
|
||||
eq(NotificationService.SCENE_TASK_FAILED), eq(NotificationService.LEVEL_WARNING),
|
||||
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:2026091310:1"));
|
||||
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:20260913:1"));
|
||||
verify(notificationService).pushOrRefresh(eq(2L), eq(NotificationService.AUDIENCE_ADMIN),
|
||||
eq(NotificationService.SCENE_TASK_FAILED), eq(NotificationService.LEVEL_WARNING),
|
||||
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:2026091310:2"));
|
||||
eq("标题"), eq("内容"), eq("task_failed_admin:20:PRICE_TRACK:20260913:2"));
|
||||
// 非本组主管一条都不落
|
||||
verify(notificationService, never()).pushOrRefresh(eq(3L), anyString(), anyString(),
|
||||
anyString(), anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并告警的权限口径:能看到组内任一号的管理员收 1 条,一个都看不到的收不到 —— 每人最多一条。
|
||||
* 逐个主体调用 pushToAdmins 会让同一管理员为 N 个账号收到 N 条同内容通知。
|
||||
*/
|
||||
@Test
|
||||
void pushToAdminsAnyOfDeliversAtMostOnePerAdminSeeingAnySubject() {
|
||||
AdminUserEntity superAdmin = admin(1L, "超管甲");
|
||||
AdminUserEntity inScope = admin(2L, "主管乙");
|
||||
AdminUserEntity outOfScope = admin(3L, "主管丙");
|
||||
when(adminUserMapper.selectList(any())).thenReturn(List.of(superAdmin, inScope, outOfScope));
|
||||
when(adminAuthSupport.currentRole(superAdmin)).thenReturn("super_admin");
|
||||
when(adminAuthSupport.currentRole(inScope)).thenReturn("admin");
|
||||
when(adminAuthSupport.currentRole(outOfScope)).thenReturn("admin");
|
||||
when(userDataScopeSupport.resolveVisibleUserIds(2L)).thenReturn(List.of(2L, 20L));
|
||||
when(userDataScopeSupport.resolveVisibleUserIds(3L)).thenReturn(List.of(3L, 30L));
|
||||
when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
|
||||
.thenReturn(true);
|
||||
|
||||
int pushed = service.pushToAdminsAnyOf(service.prepareAdminAudience(),
|
||||
NotificationService.SCENE_SECRET_BALANCE, NotificationService.LEVEL_ERROR,
|
||||
"标题", "内容", "secret_balance_admin:group:hash:20261002", List.of(11L, 12L, 20L));
|
||||
|
||||
// 3 个受影响账号 × 2 个可见管理员 = 只落 2 条;非本组主管一条都不落
|
||||
assertThat(pushed).isEqualTo(2);
|
||||
verify(notificationService, times(2)).pushOrRefresh(anyLong(), anyString(), anyString(),
|
||||
anyString(), anyString(), anyString(), anyString());
|
||||
verify(notificationService).pushOrRefresh(eq(2L), eq(NotificationService.AUDIENCE_ADMIN),
|
||||
eq(NotificationService.SCENE_SECRET_BALANCE), eq(NotificationService.LEVEL_ERROR),
|
||||
eq("标题"), eq("内容"), eq("secret_balance_admin:group:hash:20261002:2"));
|
||||
verify(notificationService, never()).pushOrRefresh(eq(3L), anyString(), anyString(),
|
||||
anyString(), anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalEventReachesEveryAdminByDesign() {
|
||||
AdminUserEntity superAdmin = admin(1L, "超管甲");
|
||||
@@ -120,7 +154,7 @@ class NotificationDispatchServiceTest {
|
||||
// 服务探测失败这类全局事件(subjectUserId=null)按设计推给所有管理员,不受分组限制
|
||||
int pushed = service.pushToAdmins(NotificationService.SCENE_SERVICE_DOWN,
|
||||
NotificationService.LEVEL_ERROR, "品牌检测服务不可用", "探测失败",
|
||||
"service_down:brand-service:2026091310", null);
|
||||
"service_down:brand-service:20260913", null);
|
||||
|
||||
assertThat(pushed).isEqualTo(3);
|
||||
}
|
||||
|
||||
+83
-1
@@ -117,7 +117,89 @@ class NotificationScanSchedulerTest {
|
||||
anyString(), anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 同一天同模块的失败必须共用一条通知:按「扫描时刻的小时」分桶时,同一个失败任务
|
||||
* 只要还落在 60 分钟回看窗口里,跨过整点就会被塞进新桶,于是每小时重发一条
|
||||
* (2026-10-02 取证:task_failed 管理员侧因此 24 倍放大)。
|
||||
*/
|
||||
@Test
|
||||
void failedTasksOfSameDayShareOneDedupeKeyAcrossHours() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(
|
||||
fileTask(101L, "T-101", "PRICE_TRACK", 7L, "浏览器启动失败", LocalDateTime.of(2026, 10, 1, 9, 12)),
|
||||
fileTask(102L, "T-102", "PRICE_TRACK", 7L, "cookie 失效", LocalDateTime.of(2026, 10, 1, 18, 36))));
|
||||
when(brandCrawlTaskMapper.selectList(any())).thenReturn(List.of());
|
||||
stubAdminAudience();
|
||||
|
||||
newScheduler().scanFailedTasks();
|
||||
|
||||
ArgumentCaptor<String> userKey = ArgumentCaptor.forClass(String.class);
|
||||
verify(notificationService, times(1)).pushOrRefresh(eq(7L), eq(NotificationService.AUDIENCE_USER),
|
||||
eq(NotificationService.SCENE_TASK_FAILED), eq(NotificationService.LEVEL_WARNING),
|
||||
anyString(), anyString(), userKey.capture());
|
||||
assertThat(userKey.getValue()).isEqualTo("task_failed:7:PRICE_TRACK:20261001");
|
||||
|
||||
ArgumentCaptor<String> adminKey = ArgumentCaptor.forClass(String.class);
|
||||
verify(dispatch, times(1)).pushToAdmins(any(NotificationDispatchService.AdminAudience.class),
|
||||
eq(NotificationService.SCENE_TASK_FAILED), eq(NotificationService.LEVEL_WARNING),
|
||||
anyString(), anyString(), adminKey.capture(), eq(7L));
|
||||
assertThat(adminKey.getValue()).isEqualTo("task_failed_admin:7:PRICE_TRACK:20261001");
|
||||
}
|
||||
|
||||
/** 桶按「任务自身所属自然日」而非扫描时钟:跨零点轮询时,昨晚的失败仍归昨晚那条通知。 */
|
||||
@Test
|
||||
void failedTasksOnDifferentDaysGetSeparateDedupeKeys() {
|
||||
when(fileTaskMapper.selectList(any())).thenReturn(List.of(
|
||||
fileTask(101L, "T-101", "PRICE_TRACK", 7L, "浏览器启动失败", LocalDateTime.of(2026, 10, 1, 23, 50)),
|
||||
fileTask(102L, "T-102", "PRICE_TRACK", 7L, "cookie 失效", LocalDateTime.of(2026, 10, 2, 0, 10))));
|
||||
when(brandCrawlTaskMapper.selectList(any())).thenReturn(List.of());
|
||||
stubAdminAudience();
|
||||
|
||||
newScheduler().scanFailedTasks();
|
||||
|
||||
ArgumentCaptor<String> userKey = ArgumentCaptor.forClass(String.class);
|
||||
verify(notificationService, times(2)).pushOrRefresh(anyLong(), anyString(), anyString(), anyString(),
|
||||
anyString(), anyString(), userKey.capture());
|
||||
assertThat(userKey.getAllValues()).containsExactlyInAnyOrder(
|
||||
"task_failed:7:PRICE_TRACK:20261001", "task_failed:7:PRICE_TRACK:20261002");
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务探测故障:去重键按天,同一天同服务只提醒一次(小时桶时一个持续 503 的上游
|
||||
* 每小时给每位管理员各塞一条)。取精确 8 位日期,多一位小时都不行。
|
||||
*/
|
||||
@Test
|
||||
void serviceDownAlertIsDedupedPerCalendarDay() {
|
||||
properties.setBrandServiceUrl("http://127.0.0.1:1");
|
||||
properties.setPriceTrackApiUrl("");
|
||||
properties.setJikipProbeEnabled(false);
|
||||
when(dispatch.pushToAdmins(anyString(), anyString(), anyString(), anyString(), anyString(), any()))
|
||||
.thenReturn(1);
|
||||
|
||||
newScheduler().probeServices();
|
||||
|
||||
ArgumentCaptor<String> keyBase = ArgumentCaptor.forClass(String.class);
|
||||
verify(dispatch).pushToAdmins(eq(NotificationService.SCENE_SERVICE_DOWN),
|
||||
eq(NotificationService.LEVEL_ERROR), eq("品牌检测服务不可用"), anyString(),
|
||||
keyBase.capture(), org.mockito.ArgumentMatchers.<Long>isNull());
|
||||
assertThat(keyBase.getValue()).matches("service_down:brand-service:\\d{8}");
|
||||
}
|
||||
|
||||
private void stubAdminAudience() {
|
||||
when(dispatch.prepareAdminAudience()).thenReturn(
|
||||
new NotificationDispatchService.AdminAudience(List.of(), Set.of(), Map.of()));
|
||||
when(dispatch.displayNameOf(anyLong())).thenReturn("张三");
|
||||
when(notificationService.pushOrRefresh(anyLong(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
|
||||
.thenReturn(true);
|
||||
when(dispatch.pushToAdmins(any(NotificationDispatchService.AdminAudience.class), anyString(), anyString(),
|
||||
anyString(), anyString(), anyString(), any())).thenReturn(1);
|
||||
}
|
||||
|
||||
private FileTaskEntity fileTask(Long id, String taskNo, String moduleType, Long userId, String errorMessage) {
|
||||
return fileTask(id, taskNo, moduleType, userId, errorMessage, LocalDateTime.of(2026, 10, 1, 18, 36));
|
||||
}
|
||||
|
||||
private FileTaskEntity fileTask(Long id, String taskNo, String moduleType, Long userId, String errorMessage,
|
||||
LocalDateTime updatedAt) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(id);
|
||||
task.setTaskNo(taskNo);
|
||||
@@ -125,7 +207,7 @@ class NotificationScanSchedulerTest {
|
||||
task.setUserId(userId);
|
||||
task.setErrorMessage(errorMessage);
|
||||
task.setStatus("FAILED");
|
||||
task.setUpdatedAt(LocalDateTime.now());
|
||||
task.setUpdatedAt(updatedAt);
|
||||
return task;
|
||||
}
|
||||
}
|
||||
|
||||
+16
-5
@@ -84,23 +84,34 @@ class NotificationServiceTest {
|
||||
verify(mapper, never()).insert(any(UserNotificationEntity.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 聚合通知按自然日只有一条,一整天会被反复刷新:内容有变化时只改文案,
|
||||
* 绝不能把 read_at 置空——否则管理员刚读完红点又亮,一天点亮几十次
|
||||
* (2026-10-02 后台超管 582 条未读的主放大器之一)。
|
||||
*/
|
||||
@Test
|
||||
void pushOrRefreshUpdatesContentAndResetsUnread() {
|
||||
@SuppressWarnings("unchecked")
|
||||
void pushOrRefreshUpdatesContentButKeepsReadState() {
|
||||
UserNotificationEntity existing = new UserNotificationEntity();
|
||||
existing.setId(9L);
|
||||
existing.setTitle("跟价任务失败");
|
||||
existing.setContent("最近 60 分钟内有 2 个跟价任务失败");
|
||||
existing.setLevel("warning");
|
||||
existing.setReadAt(LocalDateTime.now());
|
||||
existing.setReadAt(LocalDateTime.of(2026, 9, 13, 10, 0));
|
||||
when(mapper.selectOne(any())).thenReturn(existing);
|
||||
|
||||
boolean refreshed = service.pushOrRefresh(7L, NotificationService.AUDIENCE_ADMIN,
|
||||
NotificationService.SCENE_TASK_FAILED, NotificationService.LEVEL_WARNING,
|
||||
"跟价任务失败", "最近 60 分钟内有 5 个跟价任务失败", "task_failed:7:PRICE_TRACK:2026091310");
|
||||
"跟价任务失败", "最近 60 分钟内有 5 个跟价任务失败", "task_failed:7:PRICE_TRACK:20260913");
|
||||
|
||||
assertThat(refreshed).isTrue();
|
||||
verify(mapper, never()).insert(any(UserNotificationEntity.class));
|
||||
verify(mapper, times(1)).update(any(), any());
|
||||
ArgumentCaptor<LambdaUpdateWrapper<UserNotificationEntity>> captor =
|
||||
ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
|
||||
verify(mapper, times(1)).update(any(), captor.capture());
|
||||
String sqlSet = captor.getValue().getSqlSet();
|
||||
assertThat(sqlSet).contains("title").contains("content").contains("level");
|
||||
assertThat(sqlSet).doesNotContain("read_at");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -113,7 +124,7 @@ class NotificationServiceTest {
|
||||
|
||||
boolean refreshed = service.pushOrRefresh(7L, NotificationService.AUDIENCE_USER,
|
||||
NotificationService.SCENE_TASK_FAILED, NotificationService.LEVEL_WARNING,
|
||||
"跟价任务失败", "最近 60 分钟内有 2 个跟价任务失败", "task_failed:7:PRICE_TRACK:2026091310");
|
||||
"跟价任务失败", "最近 60 分钟内有 2 个跟价任务失败", "task_failed:7:PRICE_TRACK:20260913");
|
||||
|
||||
assertThat(refreshed).isFalse();
|
||||
verify(mapper, never()).update(any(), any());
|
||||
|
||||
Reference in New Issue
Block a user