small fix

This commit is contained in:
CITE\amentis 2024-09-09 13:35:42 +03:00
parent aeefa6cfc4
commit 2bc28461a4
4 changed files with 20 additions and 14 deletions

View File

@ -8,7 +8,7 @@ import java.util.UUID;
public interface AccountingService {
Integer getCurrentMetricValue(UsageLimitTargetMetric metric, DefinitionEntity definition) throws InvalidApplicationException;
Integer getCurrentMetricValue(UsageLimitTargetMetric metric, DefinitionEntity definition, boolean userIdEnabled) throws InvalidApplicationException;
void set(String metric, UUID tenantId, String tenantCode, Integer value) throws InvalidApplicationException;

View File

@ -129,7 +129,7 @@ public class AccountingServiceImpl implements AccountingService {
});
}
public Integer getCurrentMetricValue(UsageLimitTargetMetric metric, DefinitionEntity definition) throws InvalidApplicationException {
public Integer getCurrentMetricValue(UsageLimitTargetMetric metric, DefinitionEntity definition, boolean userIdEnabled) throws InvalidApplicationException {
if (this.isEnabled) {
AccountingClient accountingClient = null;
try {
@ -173,12 +173,14 @@ public class AccountingServiceImpl implements AccountingService {
throw new RuntimeException(e);
}
lookup.setGroupingFields(new FieldSet(List.of(
AccountingEntryCreatedIntegrationEvent._serviceId,
AccountingEntryCreatedIntegrationEvent._action,
AccountingEntryCreatedIntegrationEvent._resource,
AccountingEntryCreatedIntegrationEvent._userId
)));
List<String> fields = new ArrayList<>();
fields.add(AccountingEntryCreatedIntegrationEvent._serviceId);
fields.add(AccountingEntryCreatedIntegrationEvent._action);
fields.add(AccountingEntryCreatedIntegrationEvent._resource);
if (userIdEnabled) fields.add(AccountingEntryCreatedIntegrationEvent._userId);
lookup.setGroupingFields(new FieldSet(fields));
lookup.setProject(new FieldSet(this.accountingProperties.getProjectFields()));

View File

@ -381,11 +381,15 @@ public class MaintenanceServiceImpl implements MaintenanceService {
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) {
Integer currentValue = this.accountingService.getCurrentMetricValue(usageLimitTargetMetric, null, false);
if (currentValue == 0) this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, (int) itemsCount);
else if (currentValue < itemsCount) {
if (currentValue < 0) {
this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, Math.abs(currentValue - (int) itemsCount));
} else {
this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, currentValue - (int) itemsCount);
}
if (currentValue < itemsCount) {
} else if (currentValue > itemsCount) {
this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, (int) itemsCount - currentValue);
}
} finally {

View File

@ -184,7 +184,7 @@ public class UsageLimitServiceImpl implements UsageLimitService {
DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(DefinitionEntity.class, usageLimitEntity.getDefinition());
if (definition == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{usageLimitEntity.getId(), DefinitionEntity.class.getSimpleName()}, LocaleContextHolder.getLocale()));
Integer currentValue = this.accountingService.getCurrentMetricValue(metric, definition);
Integer currentValue = this.accountingService.getCurrentMetricValue(metric, definition, true);
if (currentValue >= usageLimitEntity.getValue()) throw new MyValidationException(this.errors.getUsageLimitException().getCode(), usageLimitEntity.getLabel());
}
} catch (InvalidApplicationException e) {