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 { 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 @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()) { if (accountingProperties.getEnabled()) {
AccountingEntryCreatedIntegrationEvent event = new AccountingEntryCreatedIntegrationEvent(); AccountingEntryCreatedIntegrationEvent event = new AccountingEntryCreatedIntegrationEvent();
event.setTimeStamp(Instant.now()); event.setTimeStamp(Instant.now());
@ -49,8 +49,7 @@ public class AccountingEntryCreatedIntegrationEventHandlerImpl implements Accoun
event.setType(valueType); event.setType(valueType);
event.setResource(tenantCode); event.setResource(tenantCode);
event.setUserId(subjectId); event.setUserId(subjectId);
if (valueType != null && valueType.equals(AccountingValueType.Reset)) event.setValue(0.0); event.setValue((double) value);
else event.setValue(1.0);
event.setTenant(tenantId); event.setTenant(tenantId);
this.handle(event); this.handle(event);

View File

@ -10,7 +10,7 @@ public interface AccountingService {
Integer getCurrentMetricValue(UsageLimitTargetMetric metric, DefinitionEntity definition) throws InvalidApplicationException; 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; 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())); throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{this.accountingProperties.getSources().getFirst().getRepositoryId(), AccountingClient.class.getSimpleName()}, LocaleContextHolder.getLocale()));
AccountingInfoLookup lookup = new AccountingInfoLookup(); AccountingInfoLookup lookup = new AccountingInfoLookup();
if (definition.getHasPeriodicity()) { if (definition != null && definition.getHasPeriodicity()) {
if (definition.getPeriodicityRange().equals(UsageLimitPeriodicityRange.Monthly)) lookup.setDateRangeType(AccountingDataRangeType.ThisMonth); if (definition.getPeriodicityRange().equals(UsageLimitPeriodicityRange.Monthly)) lookup.setDateRangeType(AccountingDataRangeType.ThisMonth);
else lookup.setDateRangeType(AccountingDataRangeType.ThisYear); else lookup.setDateRangeType(AccountingDataRangeType.ThisYear);
} else { } else {
@ -197,11 +197,11 @@ public class AccountingServiceImpl implements AccountingService {
return null; 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) { if (this.isEnabled) {
String subjectId = this.getSubjectId(); 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) { if (this.isEnabled) {
String subjectId = this.getSubjectId(); 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) { if (this.isEnabled) {
String subjectId = this.getSubjectId(); 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.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.opencdmp.commons.enums.UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT;
@Service @Service
public class MaintenanceServiceImpl implements MaintenanceService { public class MaintenanceServiceImpl implements MaintenanceService {
@ -220,13 +218,13 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try { try {
this.tenantEntityManager.disableTenantFilters(); 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<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)); List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (PlanEntity item : items) { if (!items.isEmpty()) {
String subjectId = this.findUserCredential(userCredentials, item.getCreatorId()); this.calculateReset(UsageLimitTargetMetric.PLAN_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode());
String tenantCode = this.findTenantCode(tenants, item.getTenantId()); for (TenantEntity tenant : tenants) {
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PLAN_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); this.calculateReset(UsageLimitTargetMetric.PLAN_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode());
}
} }
} finally { } finally {
this.tenantEntityManager.reloadTenantFilters(); 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<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)); List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (PlanBlueprintEntity item : items) { if (!items.isEmpty()) {
String subjectId = accountingProperties.getSubjectId(); this.calculateReset(UsageLimitTargetMetric.BLUEPRINT_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode());
String tenantCode = this.findTenantCode(tenants, item.getTenantId()); for (TenantEntity tenant : tenants) {
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.BLUEPRINT_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); this.calculateReset(UsageLimitTargetMetric.BLUEPRINT_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode());
}
} }
} finally { } finally {
this.tenantEntityManager.reloadTenantFilters(); this.tenantEntityManager.reloadTenantFilters();
@ -261,13 +260,13 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try { try {
this.tenantEntityManager.disableTenantFilters(); 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<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)); List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (DescriptionEntity item : items) { if (!items.isEmpty()) {
String subjectId = this.findUserCredential(userCredentials, item.getCreatedById()); this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode());
String tenantCode = this.findTenantCode(tenants, item.getTenantId()); for (TenantEntity tenant : tenants) {
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode());
}
} }
} finally { } finally {
this.tenantEntityManager.reloadTenantFilters(); 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<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)); List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (DescriptionTemplateEntity item : items) { if (!items.isEmpty()) {
String subjectId = accountingProperties.getSubjectId(); this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode());
String tenantCode = this.findTenantCode(tenants, item.getTenantId()); for (TenantEntity tenant : tenants) {
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode());
}
} }
} finally { } finally {
this.tenantEntityManager.reloadTenantFilters(); 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<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)); List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (DescriptionTemplateTypeEntity item : items) { if (!items.isEmpty()) {
String subjectId = accountingProperties.getSubjectId(); this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode());
String tenantCode = this.findTenantCode(tenants, item.getTenantId()); for (TenantEntity tenant : tenants) {
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); this.calculateReset(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode());
}
} }
} finally { } finally {
this.tenantEntityManager.reloadTenantFilters(); this.tenantEntityManager.reloadTenantFilters();
@ -323,12 +324,12 @@ public class MaintenanceServiceImpl implements MaintenanceService {
this.tenantEntityManager.disableTenantFilters(); 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<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)); 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) { if (!items.isEmpty()) {
String subjectId = accountingProperties.getSubjectId(); this.calculateReset(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode());
String tenantCode = this.findTenantCode(tenants, item.getTenantId()); for (TenantEntity tenant : tenants) {
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); this.calculateReset(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode());
}
} }
} finally { } finally {
this.tenantEntityManager.reloadTenantFilters(); 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<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)); List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (ReferenceTypeEntity item : items) { if (!items.isEmpty()) {
String subjectId = accountingProperties.getSubjectId(); this.calculateReset(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode());
String tenantCode = this.findTenantCode(tenants, item.getTenantId()); for (TenantEntity tenant : tenants) {
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); this.calculateReset(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode());
}
} }
} finally { } finally {
this.tenantEntityManager.reloadTenantFilters(); this.tenantEntityManager.reloadTenantFilters();
@ -363,36 +365,31 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try { try {
this.tenantEntityManager.disableTenantFilters(); 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<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)); List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (TenantUserEntity item : items) { if (!items.isEmpty()) {
String subjectId = this.findUserCredential(userCredentials, item.getUserId()); this.calculateReset(UsageLimitTargetMetric.USER_COUNT, items.stream().filter(x -> x.getTenantId() == null).count(), null ,this.tenantScope.getDefaultTenantCode());
String tenantCode = this.findTenantCode(tenants, item.getTenantId()); for (TenantEntity tenant : tenants) {
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.USER_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode); this.calculateReset(UsageLimitTargetMetric.USER_COUNT, items.stream().filter(x -> x.getTenantId() == tenant.getId()).count(), tenant.getId(), tenant.getCode());
}
} }
} finally { } finally {
this.tenantEntityManager.reloadTenantFilters(); this.tenantEntityManager.reloadTenantFilters();
} }
} }
private String findTenantCode(List<TenantEntity> tenants, UUID tenantId) { private void calculateReset(UsageLimitTargetMetric usageLimitTargetMetric, long itemsCount, UUID tenantId ,String tenantCode) throws InvalidApplicationException {
if (tenants != null && !tenants.isEmpty() && tenantId != null){ try {
TenantEntity tenant = tenants.stream().filter(x -> x.getId().equals(tenantId)).findFirst().orElse(null); this.tenantScope.setTempTenant(this.entityManager, tenantId, tenantCode);
if (tenant != null) return tenant.getCode(); Integer currentValue = this.accountingService.getCurrentMetricValue(usageLimitTargetMetric, null);
else return this.tenantScope.getDefaultTenantCode(); if (currentValue > itemsCount) {
} else { this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, currentValue - (int) itemsCount);
return this.tenantScope.getDefaultTenantCode(); }
} if (currentValue < itemsCount) {
} this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, (int) itemsCount - currentValue);
}
private String findUserCredential(List<UserCredentialEntity> userCredentials, UUID userId) { } finally {
if (userCredentials != null) { this.tenantEntityManager.reloadTenantFilters();
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();
} }
} }
} }