From 5c5d6832364a910bc0bfc325b60e57b31d366cf2 Mon Sep 17 00:00:00 2001 From: ViacheslavKlimov Date: Sun, 8 Jan 2023 13:42:43 +0200 Subject: [PATCH] Add indexes for notification entities; refactoring --- .../main/data/upgrade/3.4.3/schema_update.sql | 11 +-- .../controller/NotificationController.java | 2 +- .../DefaultNotificationCenter.java | 8 +-- .../DefaultNotificationCommandsHandler.java | 1 - .../notification/NotificationApiTest.java | 71 ++++++++++++++++--- .../NotificationRequestService.java | 3 +- .../NotificationRequestStats.java | 7 +- .../DefaultNotificationRequestService.java | 5 +- .../notification/NotificationRequestDao.java | 3 +- .../JpaNotificationRequestDao.java | 5 +- .../NotificationRequestRepository.java | 3 +- .../resources/sql/schema-entities-idx.sql | 13 ++-- .../main/resources/sql/schema-entities.sql | 2 +- 13 files changed, 100 insertions(+), 34 deletions(-) diff --git a/application/src/main/data/upgrade/3.4.3/schema_update.sql b/application/src/main/data/upgrade/3.4.3/schema_update.sql index bf152cadb8..22d4767900 100644 --- a/application/src/main/data/upgrade/3.4.3/schema_update.sql +++ b/application/src/main/data/upgrade/3.4.3/schema_update.sql @@ -32,6 +32,7 @@ CREATE TABLE IF NOT EXISTS notification_template ( notification_subject VARCHAR(255), configuration VARCHAR(10000) NOT NULL ); +CREATE INDEX IF NOT EXISTS idx_notification_template_tenant_id_created_time ON notification_template(tenant_id, created_time DESC); CREATE TABLE IF NOT EXISTS notification_rule ( id UUID NOT NULL CONSTRAINT notification_rule_pkey PRIMARY KEY, @@ -42,12 +43,13 @@ CREATE TABLE IF NOT EXISTS notification_rule ( delivery_methods VARCHAR(255) NOT NULL, configuration VARCHAR(2000) NOT NULL ); +CREATE INDEX IF NOT EXISTS idx_notification_rule_tenant_id_created_time ON notification_rule(tenant_id, created_time DESC); CREATE TABLE IF NOT EXISTS notification_request ( id UUID NOT NULL CONSTRAINT notification_request_pkey PRIMARY KEY, created_time BIGINT NOT NULL, tenant_id UUID NULL CONSTRAINT fk_notification_request_tenant_id REFERENCES tenant(id) ON DELETE CASCADE, - targets VARCHAR(5000) NOT NULL, + targets VARCHAR(10000) NOT NULL, template_id UUID NOT NULL, info VARCHAR(1000), delivery_methods VARCHAR(255), @@ -60,6 +62,8 @@ CREATE TABLE IF NOT EXISTS notification_request ( stats VARCHAR(10000) ); CREATE INDEX IF NOT EXISTS idx_notification_request_tenant_id_originator_type_created_time ON notification_request(tenant_id, originator_type, created_time DESC); +CREATE INDEX IF NOT EXISTS idx_notification_request_rule_id_originator_entity_id ON notification_request(rule_id, originator_entity_id); +CREATE INDEX IF NOT EXISTS idx_notification_request_status ON notification_request(status); CREATE TABLE IF NOT EXISTS notification ( id UUID NOT NULL, @@ -73,9 +77,8 @@ CREATE TABLE IF NOT EXISTS notification ( originator_type VARCHAR(32) NOT NULL, status VARCHAR(32) ) PARTITION BY RANGE (created_time); -CREATE INDEX IF NOT EXISTS idx_notification_id ON notification(id); -CREATE INDEX IF NOT EXISTS idx_notification_recipient_id_created_time ON notification(recipient_id, created_time DESC); -CREATE INDEX IF NOT EXISTS idx_notification_notification_request_id ON notification(request_id); +CREATE INDEX IF NOT EXISTS idx_notification_id_recipient_id ON notification(id, recipient_id); +CREATE INDEX IF NOT EXISTS idx_notification_recipient_id_status_created_time ON notification(recipient_id, status, created_time DESC); ALTER TABLE alarm ADD COLUMN IF NOT EXISTS notification_rule_id UUID; diff --git a/application/src/main/java/org/thingsboard/server/controller/NotificationController.java b/application/src/main/java/org/thingsboard/server/controller/NotificationController.java index de860eca1f..cd4d790d9a 100644 --- a/application/src/main/java/org/thingsboard/server/controller/NotificationController.java +++ b/application/src/main/java/org/thingsboard/server/controller/NotificationController.java @@ -130,7 +130,7 @@ public class NotificationController extends BaseController { @RequestParam(required = false) String sortOrder, @AuthenticationPrincipal SecurityUser user) throws ThingsboardException { PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); - return notificationRequestService.findNotificationRequestsByTenantId(user.getTenantId(), pageLink); + return notificationRequestService.findNotificationRequestsByTenantIdAndOriginatorType(user.getTenantId(), NotificationOriginatorType.ADMIN, pageLink); } @DeleteMapping("/notification/request/{id}") diff --git a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java index 671acd8be4..8abdf1ff0d 100644 --- a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java +++ b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationCenter.java @@ -70,6 +70,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @Service @@ -139,10 +140,9 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple return notificationTargetService.findRecipientsForNotificationTarget(tenantId, ctx.getCustomerId(), targetId, pageLink); }, 200, recipientsBatch -> { for (NotificationDeliveryMethod deliveryMethod : savedNotificationRequest.getDeliveryMethods()) { - NotificationChannel notificationChannel = channels.get(deliveryMethod); - log.debug("Sending {} notifications for request {} to recipients batch", deliveryMethod, savedNotificationRequest.getId()); - List recipients = recipientsBatch.getData(); + log.debug("Sending {} notifications for request {} to recipients batch ({})", deliveryMethod, savedNotificationRequest.getId(), recipients.size()); + NotificationChannel notificationChannel = channels.get(deliveryMethod); for (User recipient : recipients) { ListenableFuture resultFuture = processForRecipient(notificationChannel, recipient, ctx); DonAsynchron.withCallback(resultFuture, result -> { @@ -167,7 +167,7 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple UserId senderId = notificationRequest.getSenderId(); if (senderId != null) { if (stats.getErrors().isEmpty()) { - int sent = stats.getSent().values().stream().mapToInt(Set::size).sum(); + int sent = stats.getSent().values().stream().mapToInt(AtomicInteger::get).sum(); sendBasicNotification(tenantId, senderId, NotificationType.COMPLETED, "Notifications sent", "All notifications were successfully sent (" + sent + ")"); } else { 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 958ee91bd0..57dd925c88 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 @@ -207,7 +207,6 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH Notification notification = update.getNotification(); switch (update.getUpdateType()) { case CREATED: { - System.err.println("NotificationsCountSubscription CREATED"); subscription.getUnreadCounter().incrementAndGet(); sendUpdate(subscription.getSessionId(), subscription.createUpdate()); break; diff --git a/application/src/test/java/org/thingsboard/server/service/notification/NotificationApiTest.java b/application/src/test/java/org/thingsboard/server/service/notification/NotificationApiTest.java index cd9e15e2e7..c91529d232 100644 --- a/application/src/test/java/org/thingsboard/server/service/notification/NotificationApiTest.java +++ b/application/src/test/java/org/thingsboard/server/service/notification/NotificationApiTest.java @@ -15,6 +15,7 @@ */ package org.thingsboard.server.service.notification; +import com.google.common.util.concurrent.ListenableFuture; import lombok.extern.slf4j.Slf4j; import org.assertj.core.data.Offset; import org.java_websocket.client.WebSocketClient; @@ -30,9 +31,15 @@ import org.thingsboard.server.common.data.notification.NotificationInfo; import org.thingsboard.server.common.data.notification.NotificationRequest; import org.thingsboard.server.common.data.notification.NotificationRequestStats; import org.thingsboard.server.common.data.notification.NotificationRequestStatus; +import org.thingsboard.server.common.data.notification.targets.AllUsersNotificationTargetConfig; import org.thingsboard.server.common.data.notification.targets.NotificationTarget; +import org.thingsboard.server.common.data.page.PageData; +import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.security.Authority; +import org.thingsboard.server.dao.notification.NotificationDao; +import org.thingsboard.server.dao.notification.NotificationRequestDao; import org.thingsboard.server.dao.service.DaoSqlTest; +import org.thingsboard.server.service.executors.DbCallbackExecutorService; import org.thingsboard.server.service.ws.notification.cmd.UnreadNotificationsCountUpdate; import org.thingsboard.server.service.ws.notification.cmd.UnreadNotificationsUpdate; @@ -41,6 +48,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; @@ -57,6 +65,12 @@ public class NotificationApiTest extends AbstractNotificationApiTest { @Autowired private NotificationCenter notificationCenter; + @Autowired + private NotificationDao notificationDao; + @Autowired + private NotificationRequestDao notificationRequestDao; + @Autowired + private DbCallbackExecutorService executor; @Before public void beforeEach() throws Exception { @@ -251,7 +265,7 @@ public class NotificationApiTest extends AbstractNotificationApiTest { @Test public void testNotificationUpdatesForALotOfUsers() throws Exception { - int usersCount = 100; // FIXME: sometimes if set e.g. to 150, up to 5 WS sessions don't receive update + int usersCount = 200; // FIXME: sometimes if set e.g. to 150, up to 5 WS sessions don't receive update Map sessions = new HashMap<>(); List targets = new ArrayList<>(); @@ -278,7 +292,7 @@ public class NotificationApiTest extends AbstractNotificationApiTest { sessions.forEach((user, wsClient) -> wsClient.registerWaitForUpdate(2)); NotificationRequest notificationRequest = submitNotificationRequest(targets, "Hello, ${email}", 0, NotificationDeliveryMethod.PUSH, NotificationDeliveryMethod.EMAIL); - await().atMost(20, TimeUnit.SECONDS) + await().atMost(10, TimeUnit.SECONDS) .pollDelay(1, TimeUnit.SECONDS).pollInterval(500, TimeUnit.MILLISECONDS) .until(() -> { long receivedUpdate = sessions.values().stream() @@ -303,10 +317,8 @@ public class NotificationApiTest extends AbstractNotificationApiTest { await().atMost(2, TimeUnit.SECONDS) .until(() -> findNotificationRequest(notificationRequest.getId()).getStats() != null); NotificationRequestStats stats = findNotificationRequest(notificationRequest.getId()).getStats(); - assertThat(stats.getSent().get(NotificationDeliveryMethod.PUSH)) - .containsAll(sessions.keySet().stream().map(User::getEmail).collect(Collectors.toSet())); - assertThat(stats.getSent().get(NotificationDeliveryMethod.EMAIL)) - .containsAll(sessions.keySet().stream().map(User::getEmail).collect(Collectors.toSet())); + assertThat(stats.getSent().get(NotificationDeliveryMethod.PUSH)).hasValue(usersCount); + assertThat(stats.getSent().get(NotificationDeliveryMethod.EMAIL)).hasValue(usersCount); sessions.values().forEach(wsClient -> wsClient.registerWaitForUpdate(2)); deleteNotificationRequest(notificationRequest.getId()); @@ -335,11 +347,54 @@ public class NotificationApiTest extends AbstractNotificationApiTest { .until(() -> findNotificationRequest(notificationRequest.getId()).getStats() != null); NotificationRequestStats stats = findNotificationRequest(notificationRequest.getId()).getStats(); - assertThat(stats.getSent().get(NotificationDeliveryMethod.PUSH)).containsOnly(CUSTOMER_USER_EMAIL); - assertThat(stats.getSent().get(NotificationDeliveryMethod.EMAIL)).containsOnly(CUSTOMER_USER_EMAIL); + assertThat(stats.getSent().get(NotificationDeliveryMethod.PUSH)).hasValue(1); + assertThat(stats.getSent().get(NotificationDeliveryMethod.EMAIL)).hasValue(1); assertThat(stats.getErrors().get(NotificationDeliveryMethod.SMS)).size().isOne(); } + @Test + public void testNotificationsForALotOfUsers() throws Exception { + int usersCount = 7000; + + List users = new ArrayList<>(); + for (int i = 1; i <= usersCount; i++) { + User user = new User(); + user.setTenantId(tenantId); + user.setAuthority(Authority.TENANT_ADMIN); + user.setEmail("test-user-" + i + "@thingsboard.org"); + user = doPost("/api/user", user, User.class); + System.err.println(i); + users.add(user); + } + + NotificationTarget notificationTarget = new NotificationTarget(); + notificationTarget.setTenantId(tenantId); + notificationTarget.setName("All my users"); + AllUsersNotificationTargetConfig config = new AllUsersNotificationTargetConfig(); + notificationTarget.setConfiguration(config); + notificationTarget = saveNotificationTarget(notificationTarget); + NotificationTargetId notificationTargetId = notificationTarget.getId(); + + ListenableFuture request = executor.submit(() -> { + return submitNotificationRequest(notificationTargetId, "Hello, ${email}", 0, NotificationDeliveryMethod.PUSH); + }); + await().atMost(10, TimeUnit.SECONDS).until(request::isDone); + NotificationRequest notificationRequest = request.get(); + + await().atMost(5, TimeUnit.SECONDS) + .pollInterval(200, TimeUnit.MILLISECONDS) + .until(() -> { + PageData sentNotifications = notificationDao.findByRequestId(tenantId, notificationRequest.getId(), new PageLink(1)); + return sentNotifications.getTotalElements() >= usersCount; + }); + + PageData sentNotifications = notificationDao.findByRequestId(tenantId, notificationRequest.getId(), new PageLink(Integer.MAX_VALUE)); + assertThat(sentNotifications.getData()).extracting(Notification::getRecipientId) + .containsAll(users.stream().map(User::getId).collect(Collectors.toSet())); + + NotificationRequestStats stats = findNotificationRequest(notificationRequest.getId()).getStats(); + assertThat(stats.getSent().values().stream().mapToInt(AtomicInteger::get).sum()).isGreaterThanOrEqualTo(usersCount); + } private void checkFullNotificationsUpdate(UnreadNotificationsUpdate notificationsUpdate, String... expectedNotifications) { assertThat(notificationsUpdate.getNotifications()).extracting(Notification::getText).containsOnly(expectedNotifications); diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestService.java index a4e244948d..029040f480 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestService.java @@ -19,6 +19,7 @@ import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.NotificationRequestId; import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.notification.NotificationOriginatorType; import org.thingsboard.server.common.data.notification.NotificationRequest; import org.thingsboard.server.common.data.notification.NotificationRequestStats; import org.thingsboard.server.common.data.notification.NotificationRequestStatus; @@ -33,7 +34,7 @@ public interface NotificationRequestService { NotificationRequest findNotificationRequestById(TenantId tenantId, NotificationRequestId id); - PageData findNotificationRequestsByTenantId(TenantId tenantId, PageLink pageLink); + PageData findNotificationRequestsByTenantIdAndOriginatorType(TenantId tenantId, NotificationOriginatorType originatorType, PageLink pageLink); List findNotificationRequestsIdsByStatusAndRuleId(TenantId tenantId, NotificationRequestStatus requestStatus, NotificationRuleId ruleId); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequestStats.java b/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequestStats.java index 4af9ef2c58..34c9d70ed8 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequestStats.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequestStats.java @@ -26,11 +26,12 @@ import java.util.Collections; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; @Data public class NotificationRequestStats { - private final Map> sent; + private final Map sent; private final Map> errors; @JsonIgnore private final Map> processedRecipients; @@ -42,7 +43,7 @@ public class NotificationRequestStats { } @JsonCreator - public NotificationRequestStats(@JsonProperty("sent") Map> sent, + public NotificationRequestStats(@JsonProperty("sent") Map sent, @JsonProperty("errors") Map> errors) { this.sent = sent; this.errors = errors; @@ -50,7 +51,7 @@ public class NotificationRequestStats { } public void reportSent(NotificationDeliveryMethod deliveryMethod, User recipient) { - sent.computeIfAbsent(deliveryMethod, k -> ConcurrentHashMap.newKeySet()).add(recipient.getEmail()); + sent.computeIfAbsent(deliveryMethod, k -> new AtomicInteger()).incrementAndGet(); processedRecipients.computeIfAbsent(deliveryMethod, k -> ConcurrentHashMap.newKeySet()).add(recipient.getId()); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java index 327f7db754..dc1aecaae5 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java @@ -22,6 +22,7 @@ import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.NotificationRequestId; import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.notification.NotificationOriginatorType; import org.thingsboard.server.common.data.notification.NotificationRequest; import org.thingsboard.server.common.data.notification.NotificationRequestStats; import org.thingsboard.server.common.data.notification.NotificationRequestStatus; @@ -52,8 +53,8 @@ public class DefaultNotificationRequestService implements NotificationRequestSer } @Override - public PageData findNotificationRequestsByTenantId(TenantId tenantId, PageLink pageLink) { - return notificationRequestDao.findByTenantIdAndPageLink(tenantId, pageLink); + public PageData findNotificationRequestsByTenantIdAndOriginatorType(TenantId tenantId, NotificationOriginatorType originatorType, PageLink pageLink) { + return notificationRequestDao.findByTenantIdAndOriginatorTypeAndPageLink(tenantId, originatorType, pageLink); } @Override diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestDao.java b/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestDao.java index a65816d86e..652fcceeeb 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestDao.java @@ -21,6 +21,7 @@ import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.NotificationTargetId; import org.thingsboard.server.common.data.id.NotificationTemplateId; import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.notification.NotificationOriginatorType; import org.thingsboard.server.common.data.notification.NotificationRequest; import org.thingsboard.server.common.data.notification.NotificationRequestStats; import org.thingsboard.server.common.data.notification.NotificationRequestStatus; @@ -32,7 +33,7 @@ import java.util.List; public interface NotificationRequestDao extends Dao { - PageData findByTenantIdAndPageLink(TenantId tenantId, PageLink pageLink); + PageData findByTenantIdAndOriginatorTypeAndPageLink(TenantId tenantId, NotificationOriginatorType originatorType, PageLink pageLink); List findIdsByRuleId(TenantId tenantId, NotificationRequestStatus requestStatus, NotificationRuleId ruleId); diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationRequestDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationRequestDao.java index fe35509043..5d6aa89e34 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationRequestDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationRequestDao.java @@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.NotificationTargetId; import org.thingsboard.server.common.data.id.NotificationTemplateId; import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.notification.NotificationOriginatorType; import org.thingsboard.server.common.data.notification.NotificationRequest; import org.thingsboard.server.common.data.notification.NotificationRequestStats; import org.thingsboard.server.common.data.notification.NotificationRequestStatus; @@ -51,8 +52,8 @@ public class JpaNotificationRequestDao extends JpaAbstractDao findByTenantIdAndPageLink(TenantId tenantId, PageLink pageLink) { - return DaoUtil.toPageData(notificationRequestRepository.findByTenantId(getId(tenantId, true), DaoUtil.toPageable(pageLink))); + public PageData findByTenantIdAndOriginatorTypeAndPageLink(TenantId tenantId, NotificationOriginatorType originatorType, PageLink pageLink) { + return DaoUtil.toPageData(notificationRequestRepository.findByTenantIdAndOriginatorType(getId(tenantId, true), originatorType, DaoUtil.toPageable(pageLink))); } @Override diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRequestRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRequestRepository.java index 77b3ccb158..0b5f0a8b77 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRequestRepository.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRequestRepository.java @@ -25,6 +25,7 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.notification.NotificationOriginatorType; import org.thingsboard.server.common.data.notification.NotificationRequestStatus; import org.thingsboard.server.dao.model.sql.NotificationRequestEntity; @@ -34,7 +35,7 @@ import java.util.UUID; @Repository public interface NotificationRequestRepository extends JpaRepository { - Page findByTenantId(UUID tenantId, Pageable pageable); + Page findByTenantIdAndOriginatorType(UUID tenantId, NotificationOriginatorType originatorType, Pageable pageable); @Query("SELECT r.id FROM NotificationRequestEntity r WHERE r.status = :status AND r.ruleId = :ruleId") List findAllIdsByStatusAndRuleId(@Param("status") NotificationRequestStatus status, diff --git a/dao/src/main/resources/sql/schema-entities-idx.sql b/dao/src/main/resources/sql/schema-entities-idx.sql index ffded2bd39..b3f628a187 100644 --- a/dao/src/main/resources/sql/schema-entities-idx.sql +++ b/dao/src/main/resources/sql/schema-entities-idx.sql @@ -82,10 +82,13 @@ CREATE INDEX IF NOT EXISTS idx_api_usage_state_entity_id ON api_usage_state(enti CREATE INDEX IF NOT EXISTS idx_notification_target_tenant_id_created_time ON notification_target(tenant_id, created_time DESC); +CREATE INDEX IF NOT EXISTS idx_notification_template_tenant_id_created_time ON notification_template(tenant_id, created_time DESC); + +CREATE INDEX IF NOT EXISTS idx_notification_rule_tenant_id_created_time ON notification_rule(tenant_id, created_time DESC); + CREATE INDEX IF NOT EXISTS idx_notification_request_tenant_id_originator_type_created_time ON notification_request(tenant_id, originator_type, created_time DESC); +CREATE INDEX IF NOT EXISTS idx_notification_request_rule_id_originator_entity_id ON notification_request(rule_id, originator_entity_id); +CREATE INDEX IF NOT EXISTS idx_notification_request_status ON notification_request(status); -CREATE INDEX IF NOT EXISTS idx_notification_id ON notification(id); - -CREATE INDEX IF NOT EXISTS idx_notification_recipient_id_created_time ON notification(recipient_id, created_time DESC); - -CREATE INDEX IF NOT EXISTS idx_notification_notification_request_id ON notification(request_id); +CREATE INDEX IF NOT EXISTS idx_notification_id_recipient_id ON notification(id, recipient_id); +CREATE INDEX IF NOT EXISTS idx_notification_recipient_id_status_created_time ON notification(recipient_id, status, created_time DESC); diff --git a/dao/src/main/resources/sql/schema-entities.sql b/dao/src/main/resources/sql/schema-entities.sql index b643e1583e..6f50a9243c 100644 --- a/dao/src/main/resources/sql/schema-entities.sql +++ b/dao/src/main/resources/sql/schema-entities.sql @@ -812,7 +812,7 @@ CREATE TABLE IF NOT EXISTS notification_request ( id UUID NOT NULL CONSTRAINT notification_request_pkey PRIMARY KEY, created_time BIGINT NOT NULL, tenant_id UUID NULL CONSTRAINT fk_notification_request_tenant_id REFERENCES tenant(id) ON DELETE CASCADE, - targets VARCHAR(5000) NOT NULL, + targets VARCHAR(10000) NOT NULL, template_id UUID NOT NULL, info VARCHAR(1000), delivery_methods VARCHAR(255),