From a545ace4ecc4bbb98af41670c40af9911b96f628 Mon Sep 17 00:00:00 2001 From: ViacheslavKlimov Date: Sun, 6 Nov 2022 18:51:49 +0200 Subject: [PATCH] Add logging for notification services --- .../DefaultNotificationSubscriptionService.java | 14 +++++++++++--- .../DefaultNotificationCommandsHandler.java | 12 +++++++++++- .../cmd/UnreadNotificationsCountUpdate.java | 2 ++ .../cmd/UnreadNotificationsUpdate.java | 3 ++- dao/src/main/resources/sql/schema-entities.sql | 4 ++++ 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationSubscriptionService.java b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationSubscriptionService.java index 8709f76179..74fb400fb2 100644 --- a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationSubscriptionService.java +++ b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationSubscriptionService.java @@ -74,6 +74,7 @@ public class DefaultNotificationSubscriptionService extends AbstractSubscription @Override public NotificationRequest processNotificationRequest(TenantId tenantId, NotificationRequest notificationRequest) { + log.info("Processing notification request (tenant id: {}, notification target id: {})", tenantId, notificationRequest.getTargetId()); notificationRequest.setTenantId(tenantId); if (notificationRequest.getAdditionalConfig() != null) { NotificationRequestConfig config = notificationRequest.getAdditionalConfig(); @@ -93,6 +94,7 @@ public class DefaultNotificationSubscriptionService extends AbstractSubscription return notificationTargetService.findRecipientsForNotificationTarget(tenantId, notificationRequest.getTargetId(), pageLink); }, 100, recipients -> { dbCallbackExecutorService.submit(() -> { + log.debug("Sending notifications for request {} to recipients batch", savedNotificationRequest.getId()); for (User recipient : recipients) { try { Notification notification = createNotification(recipient, savedNotificationRequest); @@ -111,6 +113,7 @@ public class DefaultNotificationSubscriptionService extends AbstractSubscription public void markNotificationAsRead(TenantId tenantId, UserId recipientId, NotificationId notificationId) { boolean updated = notificationService.markNotificationAsRead(tenantId, recipientId, notificationId); if (updated) { + log.debug("Marking notification {} as read (recipient id: {}, tenant id: {})", notificationId, recipientId, tenantId); Notification notification = notificationService.findNotificationById(tenantId, notificationId); onNotificationUpdate(tenantId, recipientId, notification, false); } @@ -118,6 +121,7 @@ public class DefaultNotificationSubscriptionService extends AbstractSubscription @Override public void deleteNotificationRequest(TenantId tenantId, NotificationRequestId notificationRequestId) { + log.debug("Deleting notification request {}", notificationRequestId); notificationService.deleteNotificationRequestById(tenantId, notificationRequestId); onNotificationRequestUpdate(tenantId, NotificationRequestUpdate.builder() .notificationRequestId(notificationRequestId) @@ -127,6 +131,7 @@ public class DefaultNotificationSubscriptionService extends AbstractSubscription @Override public void updateNotificationRequest(TenantId tenantId, NotificationRequest notificationRequest) { + log.debug("Updating notification request {}", notificationRequest.getId()); notificationService.saveNotificationRequest(tenantId, notificationRequest); notificationService.updateNotificationsInfosByRequestId(tenantId, notificationRequest.getId(), notificationRequest.getNotificationInfo()); onNotificationRequestUpdate(tenantId, NotificationRequestUpdate.builder() @@ -137,6 +142,7 @@ public class DefaultNotificationSubscriptionService extends AbstractSubscription } private Notification createNotification(User recipient, NotificationRequest notificationRequest) { + log.trace("Creating notification for recipient {} (notification request id: {})", recipient.getId(), notificationRequest.getId()); Notification notification = Notification.builder() .requestId(notificationRequest.getId()) .recipientId(recipient.getId()) @@ -159,20 +165,22 @@ public class DefaultNotificationSubscriptionService extends AbstractSubscription } private void onNotificationUpdate(TenantId tenantId, UserId recipientId, Notification notification, boolean isNew) { - NotificationUpdate notificationUpdate = NotificationUpdate.builder() + NotificationUpdate update = NotificationUpdate.builder() .notification(notification) .isNew(isNew) .build(); + log.trace("Submitting notification update for recipient {}: {}", recipientId, update); wsCallBackExecutor.submit(() -> { forwardToSubscriptionManagerService(tenantId, recipientId, subscriptionManagerService -> { - subscriptionManagerService.onNotificationUpdate(tenantId, recipientId, notificationUpdate, TbCallback.EMPTY); + subscriptionManagerService.onNotificationUpdate(tenantId, recipientId, update, TbCallback.EMPTY); }, () -> { - return TbSubscriptionUtils.notificationUpdateToProto(tenantId, recipientId, notificationUpdate); + return TbSubscriptionUtils.notificationUpdateToProto(tenantId, recipientId, update); }); }); } private void onNotificationRequestUpdate(TenantId tenantId, NotificationRequestUpdate update) { + log.trace("Submitting notification request update: {}", update); wsCallBackExecutor.submit(() -> { TransportProtos.ToCoreMsg notificationRequestDeletedProto = TbSubscriptionUtils.notificationRequestUpdateToProto(tenantId, update); Set coreServices = new HashSet<>(partitionService.getAllServiceIds(ServiceType.TB_CORE)); diff --git a/application/src/main/java/org/thingsboard/server/service/ws/notification/DefaultNotificationCommandsHandler.java b/application/src/main/java/org/thingsboard/server/service/ws/notification/DefaultNotificationCommandsHandler.java index 6e4ff69a88..6af03caeb5 100644 --- a/application/src/main/java/org/thingsboard/server/service/ws/notification/DefaultNotificationCommandsHandler.java +++ b/application/src/main/java/org/thingsboard/server/service/ws/notification/DefaultNotificationCommandsHandler.java @@ -16,6 +16,7 @@ package org.thingsboard.server.service.ws.notification; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; @@ -53,6 +54,7 @@ import java.util.stream.Collectors; @Service @TbCoreComponent @RequiredArgsConstructor +@Slf4j public class DefaultNotificationCommandsHandler implements NotificationCommandsHandler { private final NotificationService notificationService; @@ -64,6 +66,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH @Override public void handleUnreadNotificationsSubCmd(WebSocketSessionRef sessionRef, NotificationsSubCmd cmd) { + log.debug("[{}] Handling unread notifications subscription cmd (cmdId: {})", sessionRef.getSessionId(), cmd.getCmdId()); SecurityUser user = sessionRef.getSecurityCtx(); NotificationsSubscription subscription = NotificationsSubscription.builder() .serviceId(serviceInfoProvider.getServiceId()) @@ -82,6 +85,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH @Override public void handleUnreadNotificationsCountSubCmd(WebSocketSessionRef sessionRef, NotificationsCountSubCmd cmd) { + log.debug("[{}] Handling unread notifications count subscription cmd (cmdId: {})", sessionRef.getSessionId(), cmd.getCmdId()); SecurityUser user = sessionRef.getSecurityCtx(); NotificationsCountSubscription subscription = NotificationsCountSubscription.builder() .serviceId(serviceInfoProvider.getServiceId()) @@ -98,6 +102,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH } private void fetchUnreadNotifications(NotificationsSubscription subscription) { + log.trace("[{}, subId: {}] Fetching unread notifications from DB", subscription.getSessionId(), subscription.getSubscriptionId()); PageData notifications = notificationService.findLatestUnreadNotificationsByUserId(subscription.getTenantId(), (UserId) subscription.getEntityId(), subscription.getLimit()); subscription.getUnreadNotifications().clear(); @@ -106,6 +111,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH } private void fetchUnreadNotificationsCount(NotificationsCountSubscription subscription) { + log.trace("[{}, subId: {}] Fetching unread notifications count from DB", subscription.getSessionId(), subscription.getSubscriptionId()); int unreadCount = notificationService.countUnreadNotificationsByUserId(subscription.getTenantId(), (UserId) subscription.getEntityId()); subscription.getUnreadCounter().set(unreadCount); } @@ -121,6 +127,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH } private void handleNotificationUpdate(NotificationsSubscription subscription, NotificationUpdate update) { + log.trace("[{}, subId: {}] Handling notification update: {}", subscription.getSessionId(), subscription.getSubscriptionId(), update); Notification notification = update.getNotification(); if (notification.getStatus() == NotificationStatus.READ) { fetchUnreadNotifications(subscription); @@ -139,6 +146,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH } private void handleNotificationRequestUpdate(NotificationsSubscription subscription, NotificationRequestUpdate update) { + log.trace("[{}, subId: {}] Handling notification request update: {}", subscription.getSessionId(), subscription.getSubscriptionId(), update); NotificationRequestId notificationRequestId = update.getNotificationRequestId(); if (update.isDeleted()) { if (subscription.getUnreadNotifications().values().stream() @@ -165,10 +173,10 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH } else if (subscriptionUpdate.getNotificationRequestUpdate() != null) { handleNotificationRequestUpdate(subscription, subscriptionUpdate.getNotificationRequestUpdate()); } - sendUpdate(subscription.getSessionId(), subscription.createUpdate()); } private void handleNotificationUpdate(NotificationsCountSubscription subscription, NotificationUpdate update) { + log.trace("[{}, subId: {}] Handling notification update for count sub: {}", subscription.getSessionId(), subscription.getSubscriptionId(), update); Notification notification = update.getNotification(); if (update.isNew()) { subscription.getUnreadCounter().incrementAndGet(); @@ -180,6 +188,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH } private void handleNotificationRequestUpdate(NotificationsCountSubscription subscription, NotificationRequestUpdate update) { + log.trace("[{}, subId: {}] Handling notification request update for count sub: {}", subscription.getSessionId(), subscription.getSubscriptionId(), update); if (update.isDeleted()) { fetchUnreadNotificationsCount(subscription); sendUpdate(subscription.getSessionId(), subscription.createUpdate()); @@ -188,6 +197,7 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH private void sendUpdate(String sessionId, CmdUpdate update) { + log.trace("[{}] Sending WS update: {}", sessionId, update); wsService.sendWsMsg(sessionId, update); } diff --git a/application/src/main/java/org/thingsboard/server/service/ws/notification/cmd/UnreadNotificationsCountUpdate.java b/application/src/main/java/org/thingsboard/server/service/ws/notification/cmd/UnreadNotificationsCountUpdate.java index c839b36e8d..4f99414be7 100644 --- a/application/src/main/java/org/thingsboard/server/service/ws/notification/cmd/UnreadNotificationsCountUpdate.java +++ b/application/src/main/java/org/thingsboard/server/service/ws/notification/cmd/UnreadNotificationsCountUpdate.java @@ -19,10 +19,12 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Builder; import lombok.Getter; +import lombok.ToString; import org.thingsboard.server.service.ws.telemetry.cmd.v2.CmdUpdate; import org.thingsboard.server.service.ws.telemetry.cmd.v2.CmdUpdateType; @Getter +@ToString public class UnreadNotificationsCountUpdate extends CmdUpdate { private final int totalUnreadCount; diff --git a/application/src/main/java/org/thingsboard/server/service/ws/notification/cmd/UnreadNotificationsUpdate.java b/application/src/main/java/org/thingsboard/server/service/ws/notification/cmd/UnreadNotificationsUpdate.java index 0597612569..d6da5f9e7e 100644 --- a/application/src/main/java/org/thingsboard/server/service/ws/notification/cmd/UnreadNotificationsUpdate.java +++ b/application/src/main/java/org/thingsboard/server/service/ws/notification/cmd/UnreadNotificationsUpdate.java @@ -19,14 +19,15 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Builder; import lombok.Getter; +import lombok.ToString; import org.thingsboard.server.common.data.notification.Notification; import org.thingsboard.server.service.ws.telemetry.cmd.v2.CmdUpdate; import org.thingsboard.server.service.ws.telemetry.cmd.v2.CmdUpdateType; import java.util.Collection; -import java.util.List; @Getter +@ToString(exclude = "notifications") public class UnreadNotificationsUpdate extends CmdUpdate { private final Collection notifications; diff --git a/dao/src/main/resources/sql/schema-entities.sql b/dao/src/main/resources/sql/schema-entities.sql index 1018abe42a..1ef732b8b6 100644 --- a/dao/src/main/resources/sql/schema-entities.sql +++ b/dao/src/main/resources/sql/schema-entities.sql @@ -786,6 +786,10 @@ CREATE TABLE IF NOT EXISTS notification_target ( configuration varchar(1000) NOT NULL ); +CREATE TABLE IF NOT EXISTS notification_rule ( + id UUID NOT NULL CONSTRAINT notification_rule_pkey PRIMARY KEY +); + CREATE TABLE IF NOT EXISTS notification_request ( id UUID NOT NULL CONSTRAINT notification_request_pkey PRIMARY KEY, created_time BIGINT NOT NULL,