Browse Source

Add logging for notification services

pull/7511/head
ViacheslavKlimov 4 years ago
parent
commit
a545ace4ec
  1. 14
      application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationSubscriptionService.java
  2. 12
      application/src/main/java/org/thingsboard/server/service/ws/notification/DefaultNotificationCommandsHandler.java
  3. 2
      application/src/main/java/org/thingsboard/server/service/ws/notification/cmd/UnreadNotificationsCountUpdate.java
  4. 3
      application/src/main/java/org/thingsboard/server/service/ws/notification/cmd/UnreadNotificationsUpdate.java
  5. 4
      dao/src/main/resources/sql/schema-entities.sql

14
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<String> coreServices = new HashSet<>(partitionService.getAllServiceIds(ServiceType.TB_CORE));

12
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<Notification> 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);
}

2
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;

3
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<Notification> notifications;

4
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,

Loading…
Cancel
Save