add accounting enabled/disabled
This commit is contained in:
parent
37a0fb18a6
commit
a30bd2e194
|
@ -40,18 +40,20 @@ public class AccountingEntryCreatedIntegrationEventHandlerImpl implements Accoun
|
|||
|
||||
@Override
|
||||
public void handleAccountingEntry(String metric, AccountingValueType valueType, String subjectId, UUID tenantId, String tenantCode) throws InvalidApplicationException {
|
||||
AccountingEntryCreatedIntegrationEvent event = new AccountingEntryCreatedIntegrationEvent();
|
||||
event.setTimeStamp(Instant.now());
|
||||
event.setServiceId(accountingProperties.getServiceId());
|
||||
event.setAction(metric);
|
||||
event.setMeasure(AccountingMeasureType.Unit);
|
||||
event.setType(valueType);
|
||||
event.setResource(tenantCode);
|
||||
event.setUserId(subjectId);
|
||||
event.setValue(1.0);
|
||||
event.setTenant(tenantId);
|
||||
if (accountingProperties.getEnabled()) {
|
||||
AccountingEntryCreatedIntegrationEvent event = new AccountingEntryCreatedIntegrationEvent();
|
||||
event.setTimeStamp(Instant.now());
|
||||
event.setServiceId(accountingProperties.getServiceId());
|
||||
event.setAction(metric);
|
||||
event.setMeasure(AccountingMeasureType.Unit);
|
||||
event.setType(valueType);
|
||||
event.setResource(tenantCode);
|
||||
event.setUserId(subjectId);
|
||||
event.setValue(1.0);
|
||||
event.setTenant(tenantId);
|
||||
|
||||
this.handle(event);
|
||||
this.handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import java.util.List;
|
|||
|
||||
@ConfigurationProperties(prefix = "accounting")
|
||||
public class AccountingProperties {
|
||||
private Boolean enabled;
|
||||
private String serviceId;
|
||||
|
||||
private String action;
|
||||
|
@ -19,6 +20,14 @@ public class AccountingProperties {
|
|||
return serviceId;
|
||||
}
|
||||
|
||||
public Boolean getEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(Boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public void setServiceId(String serviceId) {
|
||||
this.serviceId = serviceId;
|
||||
}
|
||||
|
|
|
@ -60,6 +60,8 @@ public class AccountingServiceImpl implements AccountingService {
|
|||
private final AccountingProperties accountingProperties;
|
||||
private final ValidatorFactory validatorFactory;
|
||||
|
||||
private final Boolean isEnabled;
|
||||
|
||||
@Autowired
|
||||
public AccountingServiceImpl(
|
||||
QueryFactory queryFactory, AccountingEntryCreatedIntegrationEventHandler accountingEntryCreatedIntegrationEventHandler, UserScope userScope, TenantScope tenantScope, MessageSource messageSource, ConventionService conventionService, TokenExchangeCacheService tokenExchangeCacheService, AccountingProperties accountingProperties, ValidatorFactory validatorFactory) {
|
||||
|
@ -73,6 +75,7 @@ public class AccountingServiceImpl implements AccountingService {
|
|||
this.accountingProperties = accountingProperties;
|
||||
this.validatorFactory = validatorFactory;
|
||||
this.clients = new HashMap<>();
|
||||
this.isEnabled = this.accountingProperties.getEnabled();
|
||||
}
|
||||
|
||||
private AccountingClient getAccountingClient(String repositoryId) throws InvalidApplicationException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
|
||||
|
@ -120,50 +123,54 @@ public class AccountingServiceImpl implements AccountingService {
|
|||
}
|
||||
|
||||
public Integer getCurrentMetricValue(UsageLimitTargetMetric metric) {
|
||||
AccountingClient accountingClient = null;
|
||||
try {
|
||||
accountingClient = this.getAccountingClient(this.accountingProperties.getSources().getFirst().getRepositoryId());
|
||||
} catch (InvalidApplicationException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvalidAlgorithmParameterException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (NoSuchPaddingException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (BadPaddingException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new RuntimeException(e);
|
||||
if (this.isEnabled) {
|
||||
AccountingClient accountingClient = null;
|
||||
try {
|
||||
accountingClient = this.getAccountingClient(this.accountingProperties.getSources().getFirst().getRepositoryId());
|
||||
} catch (InvalidApplicationException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvalidAlgorithmParameterException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (NoSuchPaddingException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (BadPaddingException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (accountingClient == null)
|
||||
throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{this.accountingProperties.getSources().getFirst().getRepositoryId(), AccountingClient.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
||||
|
||||
AccountingInfoLookup lookup = new AccountingInfoLookup();
|
||||
lookup.setTo(Instant.now());
|
||||
lookup.setDateRangeType(AccountingDataRangeType.ThisYear);
|
||||
lookup.setMeasure(AccountingMeasureType.Unit);
|
||||
lookup.setAggregateTypes(new ArrayList<>());
|
||||
lookup.getAggregateTypes().add(AccountingAggregateType.Sum);
|
||||
|
||||
lookup.setGroupingFields(Set.of(
|
||||
AccountingEntryCreatedIntegrationEvent._serviceId,
|
||||
AccountingEntryCreatedIntegrationEvent._action,
|
||||
AccountingEntryCreatedIntegrationEvent._resource,
|
||||
AccountingEntryCreatedIntegrationEvent._userId
|
||||
));
|
||||
|
||||
this.validatorFactory.validator(AccountingInfoLookup.AccountingInfoLookupValidator.class).validateForce(lookup);
|
||||
|
||||
AccountingAggregateResultItem accountingAggregateResultItem = null;
|
||||
try {
|
||||
accountingAggregateResultItem = accountingClient.calculate(lookup);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return accountingAggregateResultItem.getSum().intValue();
|
||||
}
|
||||
if (accountingClient == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{this.accountingProperties.getSources().getFirst().getRepositoryId(), AccountingClient.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
||||
|
||||
AccountingInfoLookup lookup = new AccountingInfoLookup();
|
||||
lookup.setTo(Instant.now());
|
||||
lookup.setDateRangeType(AccountingDataRangeType.ThisYear);
|
||||
lookup.setMeasure(AccountingMeasureType.Unit);
|
||||
lookup.setAggregateTypes(new ArrayList<>());
|
||||
lookup.getAggregateTypes().add(AccountingAggregateType.Sum);
|
||||
|
||||
lookup.setGroupingFields(Set.of(
|
||||
AccountingEntryCreatedIntegrationEvent._serviceId,
|
||||
AccountingEntryCreatedIntegrationEvent._action,
|
||||
AccountingEntryCreatedIntegrationEvent._resource,
|
||||
AccountingEntryCreatedIntegrationEvent._userId
|
||||
));
|
||||
|
||||
this.validatorFactory.validator(AccountingInfoLookup.AccountingInfoLookupValidator.class).validateForce(lookup);
|
||||
|
||||
AccountingAggregateResultItem accountingAggregateResultItem = null;
|
||||
try {
|
||||
accountingAggregateResultItem = accountingClient.calculate(lookup);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return accountingAggregateResultItem.getSum().intValue();
|
||||
return null;
|
||||
}
|
||||
|
||||
public void set(String metric) {
|
||||
|
@ -171,23 +178,27 @@ public class AccountingServiceImpl implements AccountingService {
|
|||
}
|
||||
|
||||
public void increase(String metric) throws InvalidApplicationException {
|
||||
String subjectId;
|
||||
if (this.isEnabled) {
|
||||
String subjectId;
|
||||
|
||||
UserCredentialEntity userCredential = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(this.userScope.getUserId()).first();
|
||||
if (userCredential != null) subjectId = userCredential.getExternalId();
|
||||
else subjectId = this.userScope.getUserId().toString();
|
||||
UserCredentialEntity userCredential = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(this.userScope.getUserId()).first();
|
||||
if (userCredential != null) subjectId = userCredential.getExternalId();
|
||||
else subjectId = this.userScope.getUserId().toString();
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
public void decrease(String metric) throws InvalidApplicationException {
|
||||
String subjectId;
|
||||
if (this.isEnabled) {
|
||||
String subjectId;
|
||||
|
||||
UserCredentialEntity userCredential = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(this.userScope.getUserId()).first();
|
||||
if (userCredential != null) subjectId = userCredential.getExternalId();
|
||||
else subjectId = this.userScope.getUserId().toString();
|
||||
UserCredentialEntity userCredential = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(this.userScope.getUserId()).first();
|
||||
if (userCredential != null) subjectId = userCredential.getExternalId();
|
||||
else subjectId = this.userScope.getUserId().toString();
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ import org.opencdmp.model.builder.UsageLimitBuilder;
|
|||
import org.opencdmp.model.deleter.UsageLimitDeleter;
|
||||
import org.opencdmp.model.persist.UsageLimitPersist;
|
||||
import org.opencdmp.query.UsageLimitQuery;
|
||||
import org.opencdmp.service.accounting.AccountingProperties;
|
||||
import org.opencdmp.service.accounting.AccountingService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -66,6 +67,8 @@ public class UsageLimitServiceImpl implements UsageLimitService {
|
|||
|
||||
private final UsageLimitProperties usageLimitProperties;
|
||||
|
||||
private final AccountingProperties accountingProperties;
|
||||
|
||||
|
||||
@Autowired
|
||||
public UsageLimitServiceImpl(
|
||||
|
@ -76,7 +79,7 @@ public class UsageLimitServiceImpl implements UsageLimitService {
|
|||
ConventionService conventionService,
|
||||
ErrorThesaurusProperties errors,
|
||||
MessageSource messageSource,
|
||||
QueryFactory queryFactory, TenantEntityManager tenantEntityManager, AccountingService accountingService, UsageLimitProperties usageLimitProperties) {
|
||||
QueryFactory queryFactory, TenantEntityManager tenantEntityManager, AccountingService accountingService, UsageLimitProperties usageLimitProperties, AccountingProperties accountingProperties) {
|
||||
this.entityManager = entityManager;
|
||||
this.authorizationService = authorizationService;
|
||||
this.deleterFactory = deleterFactory;
|
||||
|
@ -88,6 +91,7 @@ public class UsageLimitServiceImpl implements UsageLimitService {
|
|||
this.tenantEntityManager = tenantEntityManager;
|
||||
this.accountingService = accountingService;
|
||||
this.usageLimitProperties = usageLimitProperties;
|
||||
this.accountingProperties = accountingProperties;
|
||||
}
|
||||
|
||||
public UsageLimit persist(UsageLimitPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException {
|
||||
|
@ -147,7 +151,7 @@ public class UsageLimitServiceImpl implements UsageLimitService {
|
|||
|
||||
public void checkIncrease(UsageLimitTargetMetric metric) throws InvalidApplicationException {
|
||||
|
||||
if (usageLimitProperties.getEnabled() == null || !usageLimitProperties.getEnabled()) return;
|
||||
if (!usageLimitProperties.getEnabled() || !accountingProperties.getEnabled()) return;
|
||||
|
||||
if (metric == null) throw new MyApplicationException("Target Metric not defined");
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
accounting:
|
||||
enabled: ${ACCOUNTING_ENABLED}
|
||||
serviceId: ${SERVICE_ID}
|
||||
action: ${ACCOUNTING_ACTION}
|
||||
subjectId: unknown
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
usage-limits:
|
||||
enabled: false
|
||||
enabled: ${USAGE_LIMIT_ENABLED}
|
||||
|
|
Loading…
Reference in New Issue