From 89eb5bc9639cf75bdac783ca5acfa5300f223db3 Mon Sep 17 00:00:00 2001 From: "CITE\\amentis" Date: Thu, 25 Jul 2024 15:59:56 +0300 Subject: [PATCH] maintenance changes --- ...ngEntryCreatedIntegrationEventHandler.java | 2 +- ...tryCreatedIntegrationEventHandlerImpl.java | 5 +- .../service/accounting/AccountingService.java | 2 +- .../accounting/AccountingServiceImpl.java | 10 +- .../maintenance/MaintenanceServiceImpl.java | 107 +++++++++--------- 5 files changed, 61 insertions(+), 65 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/integrationevent/outbox/accountingentrycreated/AccountingEntryCreatedIntegrationEventHandler.java b/backend/core/src/main/java/org/opencdmp/integrationevent/outbox/accountingentrycreated/AccountingEntryCreatedIntegrationEventHandler.java index e875d1f4d..8d1e14699 100644 --- a/backend/core/src/main/java/org/opencdmp/integrationevent/outbox/accountingentrycreated/AccountingEntryCreatedIntegrationEventHandler.java +++ b/backend/core/src/main/java/org/opencdmp/integrationevent/outbox/accountingentrycreated/AccountingEntryCreatedIntegrationEventHandler.java @@ -7,5 +7,5 @@ import java.util.UUID; public interface AccountingEntryCreatedIntegrationEventHandler { - void handleAccountingEntry(String metric, AccountingValueType valueType, String subjectId, UUID tenantId, String tenantCode) throws InvalidApplicationException; + void handleAccountingEntry(String metric, AccountingValueType valueType, String subjectId, UUID tenantId, String tenantCode, Integer value) throws InvalidApplicationException; } diff --git a/backend/core/src/main/java/org/opencdmp/integrationevent/outbox/accountingentrycreated/AccountingEntryCreatedIntegrationEventHandlerImpl.java b/backend/core/src/main/java/org/opencdmp/integrationevent/outbox/accountingentrycreated/AccountingEntryCreatedIntegrationEventHandlerImpl.java index 688b61fea..cb238e005 100644 --- a/backend/core/src/main/java/org/opencdmp/integrationevent/outbox/accountingentrycreated/AccountingEntryCreatedIntegrationEventHandlerImpl.java +++ b/backend/core/src/main/java/org/opencdmp/integrationevent/outbox/accountingentrycreated/AccountingEntryCreatedIntegrationEventHandlerImpl.java @@ -39,7 +39,7 @@ public class AccountingEntryCreatedIntegrationEventHandlerImpl implements Accoun } @Override - public void handleAccountingEntry(String metric, AccountingValueType valueType, String subjectId, UUID tenantId, String tenantCode) throws InvalidApplicationException { + public void handleAccountingEntry(String metric, AccountingValueType valueType, String subjectId, UUID tenantId, String tenantCode, Integer value) throws InvalidApplicationException { if (accountingProperties.getEnabled()) { AccountingEntryCreatedIntegrationEvent event = new AccountingEntryCreatedIntegrationEvent(); event.setTimeStamp(Instant.now()); @@ -49,8 +49,7 @@ public class AccountingEntryCreatedIntegrationEventHandlerImpl implements Accoun event.setType(valueType); event.setResource(tenantCode); event.setUserId(subjectId); - if (valueType != null && valueType.equals(AccountingValueType.Reset)) event.setValue(0.0); - else event.setValue(1.0); + event.setValue((double) value); event.setTenant(tenantId); this.handle(event); diff --git a/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java b/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java index f3bda76ce..0c1a5659d 100644 --- a/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java +++ b/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java @@ -10,7 +10,7 @@ public interface AccountingService { Integer getCurrentMetricValue(UsageLimitTargetMetric metric, DefinitionEntity definition) throws InvalidApplicationException; - void set(String metric, UUID tenantId, String tenantCode) throws InvalidApplicationException; + void set(String metric, UUID tenantId, String tenantCode, Integer value) throws InvalidApplicationException; void increase(String metric) throws InvalidApplicationException; diff --git a/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingServiceImpl.java index a7757808d..0d7c1f423 100644 --- a/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingServiceImpl.java @@ -153,7 +153,7 @@ public class AccountingServiceImpl implements AccountingService { throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{this.accountingProperties.getSources().getFirst().getRepositoryId(), AccountingClient.class.getSimpleName()}, LocaleContextHolder.getLocale())); AccountingInfoLookup lookup = new AccountingInfoLookup(); - if (definition.getHasPeriodicity()) { + if (definition != null && definition.getHasPeriodicity()) { if (definition.getPeriodicityRange().equals(UsageLimitPeriodicityRange.Monthly)) lookup.setDateRangeType(AccountingDataRangeType.ThisMonth); else lookup.setDateRangeType(AccountingDataRangeType.ThisYear); } else { @@ -197,11 +197,11 @@ public class AccountingServiceImpl implements AccountingService { return null; } - public void set(String metric, UUID tenantId, String tenantCode) throws InvalidApplicationException{ + public void set(String metric, UUID tenantId, String tenantCode, Integer value) throws InvalidApplicationException{ if (this.isEnabled) { String subjectId = this.getSubjectId(); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(metric, AccountingValueType.Reset, subjectId, tenantId, tenantCode); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(metric, AccountingValueType.Reset, subjectId, tenantId, tenantCode, value); } } @@ -209,7 +209,7 @@ public class AccountingServiceImpl implements AccountingService { if (this.isEnabled) { String subjectId = this.getSubjectId(); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(metric, AccountingValueType.Plus, subjectId, this.tenantScope.getTenant(), this.tenantScope.getTenantCode() != null ? this.tenantScope.getTenantCode() : this.tenantScope.getDefaultTenantCode()); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(metric, AccountingValueType.Plus, subjectId, this.tenantScope.getTenant(), this.tenantScope.getTenantCode() != null ? this.tenantScope.getTenantCode() : this.tenantScope.getDefaultTenantCode(), 1); } } @@ -217,7 +217,7 @@ public class AccountingServiceImpl implements AccountingService { if (this.isEnabled) { String subjectId = this.getSubjectId(); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(metric, AccountingValueType.Minus, subjectId, this.tenantScope.getTenant(), this.tenantScope.getTenantCode() != null ? this.tenantScope.getTenantCode() : this.tenantScope.getDefaultTenantCode()); + this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(metric, AccountingValueType.Minus, subjectId, this.tenantScope.getTenant(), this.tenantScope.getTenantCode() != null ? this.tenantScope.getTenantCode() : this.tenantScope.getDefaultTenantCode(), 1); } } diff --git a/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java index 6859d312e..0626fa10a 100644 --- a/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java @@ -39,8 +39,6 @@ import java.util.List; import java.util.UUID; import java.util.stream.Collectors; -import static org.opencdmp.commons.enums.UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT; - @Service public class MaintenanceServiceImpl implements MaintenanceService { @@ -220,13 +218,13 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); List items = this.queryFactory.query(PlanQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(Plan._id).ensure(Plan._isActive).ensure(Plan._creator).ensure(Plan._belongsToCurrentTenant)); - List userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getCreatorId()).distinct().collect(Collectors.toList())).collect(); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); - for (PlanEntity item : items) { - String subjectId = this.findUserCredential(userCredentials, item.getCreatorId()); - String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PLAN_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); + if (!items.isEmpty()) { + this.calculateReset(UsageLimitTargetMetric.PLAN_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode()); + for (TenantEntity tenant : tenants) { + this.calculateReset(UsageLimitTargetMetric.PLAN_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode()); + } } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -243,10 +241,11 @@ public class MaintenanceServiceImpl implements MaintenanceService { List items = this.queryFactory.query(PlanBlueprintQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(PlanBlueprint._id).ensure(PlanBlueprint._isActive).ensure(PlanBlueprint._belongsToCurrentTenant)); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); - for (PlanBlueprintEntity item : items) { - String subjectId = accountingProperties.getSubjectId(); - String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.BLUEPRINT_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); + if (!items.isEmpty()) { + this.calculateReset(UsageLimitTargetMetric.BLUEPRINT_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode()); + for (TenantEntity tenant : tenants) { + this.calculateReset(UsageLimitTargetMetric.BLUEPRINT_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode()); + } } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -261,13 +260,13 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); List items = this.queryFactory.query(DescriptionQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(Description._id).ensure(Description._isActive).ensure(Description._createdBy).ensure(Description._belongsToCurrentTenant)); - List userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getCreatedById()).distinct().collect(Collectors.toList())).collect(); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); - for (DescriptionEntity item : items) { - String subjectId = this.findUserCredential(userCredentials, item.getCreatedById()); - String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); + if (!items.isEmpty()) { + this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode()); + for (TenantEntity tenant : tenants) { + this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode()); + } } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -284,10 +283,11 @@ public class MaintenanceServiceImpl implements MaintenanceService { List items = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(DescriptionTemplate._id).ensure(DescriptionTemplate._isActive).ensure(DescriptionTemplate._belongsToCurrentTenant)); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); - for (DescriptionTemplateEntity item : items) { - String subjectId = accountingProperties.getSubjectId(); - String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); + if (!items.isEmpty()) { + this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode()); + for (TenantEntity tenant : tenants) { + this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode()); + } } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -304,10 +304,11 @@ public class MaintenanceServiceImpl implements MaintenanceService { List items = this.queryFactory.query(DescriptionTemplateTypeQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(DescriptionTemplateType._id).ensure(DescriptionTemplateType._isActive).ensure(DescriptionTemplateType._belongsToCurrentTenant)); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); - for (DescriptionTemplateTypeEntity item : items) { - String subjectId = accountingProperties.getSubjectId(); - String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); + if (!items.isEmpty()) { + this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode()); + for (TenantEntity tenant : tenants) { + this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode()); + } } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -323,12 +324,12 @@ public class MaintenanceServiceImpl implements MaintenanceService { this.tenantEntityManager.disableTenantFilters(); List items = this.queryFactory.query(PrefillingSourceQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(PrefillingSource._id).ensure(PrefillingSource._isActive).ensure(PrefillingSource._belongsToCurrentTenant)); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); - this.accountingService.set(PREFILLING_SOURCES_COUNT.getValue(), null, this.tenantScope.getDefaultTenantCode()); - for (PrefillingSourceEntity item : items) { - String subjectId = accountingProperties.getSubjectId(); - String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); + if (!items.isEmpty()) { + this.calculateReset(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode()); + for (TenantEntity tenant : tenants) { + this.calculateReset(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode()); + } } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -345,10 +346,11 @@ public class MaintenanceServiceImpl implements MaintenanceService { List items = this.queryFactory.query(ReferenceTypeQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(ReferenceType._id).ensure(ReferenceType._isActive).ensure(ReferenceType._belongsToCurrentTenant)); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); - for (ReferenceTypeEntity item : items) { - String subjectId = accountingProperties.getSubjectId(); - String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); + if (!items.isEmpty()) { + this.calculateReset(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode()); + for (TenantEntity tenant : tenants) { + this.calculateReset(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode()); + } } } finally { this.tenantEntityManager.reloadTenantFilters(); @@ -363,36 +365,31 @@ public class MaintenanceServiceImpl implements MaintenanceService { try { this.tenantEntityManager.disableTenantFilters(); List items = this.queryFactory.query(TenantUserQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(TenantUserEntity._id).ensure(TenantUserEntity._userId).ensure(TenantUserEntity._isActive).ensure(TenantUserEntity._tenantId)); - List userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getUserId()).distinct().collect(Collectors.toList())).collect(); List tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); - for (TenantUserEntity item : items) { - String subjectId = this.findUserCredential(userCredentials, item.getUserId()); - String tenantCode = this.findTenantCode(tenants, item.getTenantId()); - this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.USER_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); + if (!items.isEmpty()) { + this.calculateReset(UsageLimitTargetMetric.USER_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode()); + for (TenantEntity tenant : tenants) { + this.calculateReset(UsageLimitTargetMetric.USER_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode()); + } } } finally { this.tenantEntityManager.reloadTenantFilters(); } } - private String findTenantCode(List tenants, UUID tenantId) { - if (tenants != null && !tenants.isEmpty() && tenantId != null){ - TenantEntity tenant = tenants.stream().filter(x -> x.getId().equals(tenantId)).findFirst().orElse(null); - if (tenant != null) return tenant.getCode(); - else return this.tenantScope.getDefaultTenantCode(); - } else { - return this.tenantScope.getDefaultTenantCode(); - } - } - - private String findUserCredential(List userCredentials, UUID userId) { - if (userCredentials != null) { - UserCredentialEntity userCredential = userCredentials.stream().filter(x -> x.getUserId().equals(userId)).findFirst().orElse(null); - if (userCredential != null) return userCredential.getExternalId(); - else return userId.toString(); - } else { - return userId.toString(); + private void calculateReset(UsageLimitTargetMetric usageLimitTargetMetric, long itemsCount, UUID tenantId ,String tenantCode) throws InvalidApplicationException { + try { + this.tenantScope.setTempTenant(this.entityManager, tenantId, tenantCode); + Integer currentValue = this.accountingService.getCurrentMetricValue(usageLimitTargetMetric, null); + if (currentValue > itemsCount) { + this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, currentValue - (int) itemsCount); + } + if (currentValue < itemsCount) { + this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, (int) itemsCount - currentValue); + } + } finally { + this.tenantEntityManager.reloadTenantFilters(); } } }