Merge pull request #10612 from thingsboard/fix/notification-request-stats

Limit notification request errors stats
This commit is contained in:
Viacheslav Klimov 2024-04-25 13:48:08 +03:00 committed by GitHub
commit 299ecd1cb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 28 additions and 17 deletions

View File

@ -523,18 +523,17 @@ public class NotificationApiTest extends AbstractNotificationApiTest {
@Test
public void testNotificationRequestStats() throws Exception {
wsClient.subscribeForUnreadNotifications(10);
wsClient.waitForReply(true);
wsClient.registerWaitForUpdate();
NotificationTarget notificationTarget = createNotificationTarget(customerUserId);
NotificationRequest notificationRequest = submitNotificationRequest(notificationTarget.getId(), "Test :)", NotificationDeliveryMethod.WEB);
wsClient.waitForUpdate();
await().atMost(2, TimeUnit.SECONDS)
.until(() -> findNotificationRequest(notificationRequest.getId()).isSent());
NotificationRequestStats stats = getStats(notificationRequest.getId());
NotificationRequest notificationRequest = submitNotificationRequest(notificationTarget.getId(), "Test :)", NotificationDeliveryMethod.WEB);
NotificationRequestStats stats = awaitNotificationRequest(notificationRequest.getId());
assertThat(stats.getSent().get(NotificationDeliveryMethod.WEB)).hasValue(1);
doDelete("/api/user/mobile/session").andExpect(status().isOk());
notificationRequest = submitNotificationRequest(notificationTarget.getId(), "Test", NotificationDeliveryMethod.MOBILE_APP);
stats = awaitNotificationRequest(notificationRequest.getId());
assertThat(stats.getErrors().get(NotificationDeliveryMethod.MOBILE_APP)).hasSize(1);
assertThat(stats.getTotalErrors()).hasValue(1);
}
@Test

View File

@ -106,8 +106,11 @@ public class NotificationApiWsClient extends TbTestWebSocketClient {
}
}
} else if (updateType == CmdUpdateType.NOTIFICATIONS_COUNT) {
lastCountUpdate = JacksonUtil.treeToValue(update, UnreadNotificationsCountUpdate.class);
unreadCount = lastCountUpdate.getTotalUnreadCount();
UnreadNotificationsCountUpdate countUpdate = JacksonUtil.treeToValue(update, UnreadNotificationsCountUpdate.class);
if (lastCountUpdate == null || countUpdate.getSequenceNumber() > lastCountUpdate.getSequenceNumber()) {
lastCountUpdate = countUpdate;
unreadCount = lastCountUpdate.getTotalUnreadCount();
}
}
super.onMessage(s);
}

View File

@ -34,7 +34,6 @@ public class NotificationRequestStats {
@JsonIgnore
private final AtomicInteger totalSent;
private final Map<NotificationDeliveryMethod, Map<String, String>> errors;
@JsonIgnore
private final AtomicInteger totalErrors;
private String error;
@JsonIgnore
@ -51,11 +50,19 @@ public class NotificationRequestStats {
@JsonCreator
public NotificationRequestStats(@JsonProperty("sent") Map<NotificationDeliveryMethod, AtomicInteger> sent,
@JsonProperty("errors") Map<NotificationDeliveryMethod, Map<String, String>> errors,
@JsonProperty("totalErrors") Integer totalErrors,
@JsonProperty("error") String error) {
this.sent = sent;
this.totalSent = null;
this.errors = errors;
this.totalErrors = null;
if (totalErrors == null) {
if (errors != null) {
totalErrors = errors.values().stream().mapToInt(Map::size).sum();
} else {
totalErrors = 0;
}
}
this.totalErrors = new AtomicInteger(totalErrors);
this.error = error;
this.processedRecipients = Collections.emptyMap();
}
@ -73,7 +80,10 @@ public class NotificationRequestStats {
if (errorMessage == null) {
errorMessage = error.getClass().getSimpleName();
}
errors.computeIfAbsent(deliveryMethod, k -> new ConcurrentHashMap<>()).put(recipient.getTitle(), errorMessage);
Map<String, String> errors = this.errors.computeIfAbsent(deliveryMethod, k -> new ConcurrentHashMap<>());
if (errors.size() < 100) {
errors.put(recipient.getTitle(), errorMessage);
}
totalErrors.incrementAndGet();
}

View File

@ -165,8 +165,7 @@ export class SentTableConfigResolver implements Resolve<EntityTableConfig<Notifi
if (!stats?.errors) {
return '';
}
let countError = 0;
Object.keys(stats.errors).forEach(method => countError += Object.keys(stats.errors[method]).length);
const countError = stats.totalErrors;
if (countError === 0) {
return '';
}

View File

@ -81,7 +81,7 @@ export interface NotificationRequestPreview {
export interface NotificationRequestStats {
sent: Map<NotificationDeliveryMethod, any>;
errors: { [key in NotificationDeliveryMethod]: {[errorKey in string]: string}};
processedRecipients: Map<NotificationDeliveryMethod, Set<UserId>>;
totalErrors: number;
}
export interface NotificationRequestConfig {