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 { 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; 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) { if (this.isEnabled) {
AccountingClient accountingClient = null; AccountingClient accountingClient = null;
try { try {
@ -173,12 +173,14 @@ public class AccountingServiceImpl implements AccountingService {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
lookup.setGroupingFields(new FieldSet(List.of( List<String> fields = new ArrayList<>();
AccountingEntryCreatedIntegrationEvent._serviceId, fields.add(AccountingEntryCreatedIntegrationEvent._serviceId);
AccountingEntryCreatedIntegrationEvent._action, fields.add(AccountingEntryCreatedIntegrationEvent._action);
AccountingEntryCreatedIntegrationEvent._resource, fields.add(AccountingEntryCreatedIntegrationEvent._resource);
AccountingEntryCreatedIntegrationEvent._userId
))); if (userIdEnabled) fields.add(AccountingEntryCreatedIntegrationEvent._userId);
lookup.setGroupingFields(new FieldSet(fields));
lookup.setProject(new FieldSet(this.accountingProperties.getProjectFields())); 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 { private void calculateReset(UsageLimitTargetMetric usageLimitTargetMetric, long itemsCount, UUID tenantId ,String tenantCode) throws InvalidApplicationException {
try { try {
this.tenantScope.setTempTenant(this.entityManager, tenantId, tenantCode); this.tenantScope.setTempTenant(this.entityManager, tenantId, tenantCode);
Integer currentValue = this.accountingService.getCurrentMetricValue(usageLimitTargetMetric, null); Integer currentValue = this.accountingService.getCurrentMetricValue(usageLimitTargetMetric, null, false);
if (currentValue > itemsCount) { 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); 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); this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, (int) itemsCount - currentValue);
} }
} finally { } finally {

View File

@ -184,7 +184,7 @@ public class UsageLimitServiceImpl implements UsageLimitService {
DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(DefinitionEntity.class, usageLimitEntity.getDefinition()); 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())); 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()); if (currentValue >= usageLimitEntity.getValue()) throw new MyValidationException(this.errors.getUsageLimitException().getCode(), usageLimitEntity.getLabel());
} }
} catch (InvalidApplicationException e) { } catch (InvalidApplicationException e) {