maintenance changes

This commit is contained in:
CITE\amentis 2024-07-25 15:59:56 +03:00
parent c3e7244d5e
commit 89eb5bc963
5 changed files with 61 additions and 65 deletions

View File

@ -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;
}

View File

@ -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);

View File

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

View File

@ -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);
}
}

View File

@ -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<PlanEntity> 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<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getCreatorId()).distinct().collect(Collectors.toList())).collect();
List<TenantEntity> 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<PlanBlueprintEntity> items = this.queryFactory.query(PlanBlueprintQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(PlanBlueprint._id).ensure(PlanBlueprint._isActive).ensure(PlanBlueprint._belongsToCurrentTenant));
List<TenantEntity> 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<DescriptionEntity> 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<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getCreatedById()).distinct().collect(Collectors.toList())).collect();
List<TenantEntity> 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<DescriptionTemplateEntity> items = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(DescriptionTemplate._id).ensure(DescriptionTemplate._isActive).ensure(DescriptionTemplate._belongsToCurrentTenant));
List<TenantEntity> 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<DescriptionTemplateTypeEntity> items = this.queryFactory.query(DescriptionTemplateTypeQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(DescriptionTemplateType._id).ensure(DescriptionTemplateType._isActive).ensure(DescriptionTemplateType._belongsToCurrentTenant));
List<TenantEntity> 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<PrefillingSourceEntity> items = this.queryFactory.query(PrefillingSourceQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(PrefillingSource._id).ensure(PrefillingSource._isActive).ensure(PrefillingSource._belongsToCurrentTenant));
List<TenantEntity> 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<ReferenceTypeEntity> items = this.queryFactory.query(ReferenceTypeQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(ReferenceType._id).ensure(ReferenceType._isActive).ensure(ReferenceType._belongsToCurrentTenant));
List<TenantEntity> 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<TenantUserEntity> 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<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getUserId()).distinct().collect(Collectors.toList())).collect();
List<TenantEntity> 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<TenantEntity> 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<UserCredentialEntity> 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();
}
}
}