add accounting enabled/disabled
This commit is contained in:
parent
37a0fb18a6
commit
a30bd2e194
|
@ -40,18 +40,20 @@ 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) throws InvalidApplicationException {
|
||||||
AccountingEntryCreatedIntegrationEvent event = new AccountingEntryCreatedIntegrationEvent();
|
if (accountingProperties.getEnabled()) {
|
||||||
event.setTimeStamp(Instant.now());
|
AccountingEntryCreatedIntegrationEvent event = new AccountingEntryCreatedIntegrationEvent();
|
||||||
event.setServiceId(accountingProperties.getServiceId());
|
event.setTimeStamp(Instant.now());
|
||||||
event.setAction(metric);
|
event.setServiceId(accountingProperties.getServiceId());
|
||||||
event.setMeasure(AccountingMeasureType.Unit);
|
event.setAction(metric);
|
||||||
event.setType(valueType);
|
event.setMeasure(AccountingMeasureType.Unit);
|
||||||
event.setResource(tenantCode);
|
event.setType(valueType);
|
||||||
event.setUserId(subjectId);
|
event.setResource(tenantCode);
|
||||||
event.setValue(1.0);
|
event.setUserId(subjectId);
|
||||||
event.setTenant(tenantId);
|
event.setValue(1.0);
|
||||||
|
event.setTenant(tenantId);
|
||||||
|
|
||||||
this.handle(event);
|
this.handle(event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import java.util.List;
|
||||||
|
|
||||||
@ConfigurationProperties(prefix = "accounting")
|
@ConfigurationProperties(prefix = "accounting")
|
||||||
public class AccountingProperties {
|
public class AccountingProperties {
|
||||||
|
private Boolean enabled;
|
||||||
private String serviceId;
|
private String serviceId;
|
||||||
|
|
||||||
private String action;
|
private String action;
|
||||||
|
@ -19,6 +20,14 @@ public class AccountingProperties {
|
||||||
return serviceId;
|
return serviceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean getEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(Boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
public void setServiceId(String serviceId) {
|
public void setServiceId(String serviceId) {
|
||||||
this.serviceId = serviceId;
|
this.serviceId = serviceId;
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,6 +60,8 @@ public class AccountingServiceImpl implements AccountingService {
|
||||||
private final AccountingProperties accountingProperties;
|
private final AccountingProperties accountingProperties;
|
||||||
private final ValidatorFactory validatorFactory;
|
private final ValidatorFactory validatorFactory;
|
||||||
|
|
||||||
|
private final Boolean isEnabled;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public AccountingServiceImpl(
|
public AccountingServiceImpl(
|
||||||
QueryFactory queryFactory, AccountingEntryCreatedIntegrationEventHandler accountingEntryCreatedIntegrationEventHandler, UserScope userScope, TenantScope tenantScope, MessageSource messageSource, ConventionService conventionService, TokenExchangeCacheService tokenExchangeCacheService, AccountingProperties accountingProperties, ValidatorFactory validatorFactory) {
|
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.accountingProperties = accountingProperties;
|
||||||
this.validatorFactory = validatorFactory;
|
this.validatorFactory = validatorFactory;
|
||||||
this.clients = new HashMap<>();
|
this.clients = new HashMap<>();
|
||||||
|
this.isEnabled = this.accountingProperties.getEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
private AccountingClient getAccountingClient(String repositoryId) throws InvalidApplicationException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
|
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) {
|
public Integer getCurrentMetricValue(UsageLimitTargetMetric metric) {
|
||||||
AccountingClient accountingClient = null;
|
if (this.isEnabled) {
|
||||||
try {
|
AccountingClient accountingClient = null;
|
||||||
accountingClient = this.getAccountingClient(this.accountingProperties.getSources().getFirst().getRepositoryId());
|
try {
|
||||||
} catch (InvalidApplicationException e) {
|
accountingClient = this.getAccountingClient(this.accountingProperties.getSources().getFirst().getRepositoryId());
|
||||||
throw new RuntimeException(e);
|
} catch (InvalidApplicationException e) {
|
||||||
} catch (InvalidAlgorithmParameterException e) {
|
throw new RuntimeException(e);
|
||||||
throw new RuntimeException(e);
|
} catch (InvalidAlgorithmParameterException e) {
|
||||||
} catch (NoSuchPaddingException e) {
|
throw new RuntimeException(e);
|
||||||
throw new RuntimeException(e);
|
} catch (NoSuchPaddingException e) {
|
||||||
} catch (IllegalBlockSizeException e) {
|
throw new RuntimeException(e);
|
||||||
throw new RuntimeException(e);
|
} catch (IllegalBlockSizeException e) {
|
||||||
} catch (NoSuchAlgorithmException e) {
|
throw new RuntimeException(e);
|
||||||
throw new RuntimeException(e);
|
} catch (NoSuchAlgorithmException e) {
|
||||||
} catch (BadPaddingException e) {
|
throw new RuntimeException(e);
|
||||||
throw new RuntimeException(e);
|
} catch (BadPaddingException e) {
|
||||||
} catch (InvalidKeyException e) {
|
throw new RuntimeException(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()));
|
return null;
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void set(String metric) {
|
public void set(String metric) {
|
||||||
|
@ -171,23 +178,27 @@ public class AccountingServiceImpl implements AccountingService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void increase(String metric) throws InvalidApplicationException {
|
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();
|
UserCredentialEntity userCredential = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(this.userScope.getUserId()).first();
|
||||||
if (userCredential != null) subjectId = userCredential.getExternalId();
|
if (userCredential != null) subjectId = userCredential.getExternalId();
|
||||||
else subjectId = this.userScope.getUserId().toString();
|
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 {
|
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();
|
UserCredentialEntity userCredential = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(this.userScope.getUserId()).first();
|
||||||
if (userCredential != null) subjectId = userCredential.getExternalId();
|
if (userCredential != null) subjectId = userCredential.getExternalId();
|
||||||
else subjectId = this.userScope.getUserId().toString();
|
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.deleter.UsageLimitDeleter;
|
||||||
import org.opencdmp.model.persist.UsageLimitPersist;
|
import org.opencdmp.model.persist.UsageLimitPersist;
|
||||||
import org.opencdmp.query.UsageLimitQuery;
|
import org.opencdmp.query.UsageLimitQuery;
|
||||||
|
import org.opencdmp.service.accounting.AccountingProperties;
|
||||||
import org.opencdmp.service.accounting.AccountingService;
|
import org.opencdmp.service.accounting.AccountingService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -66,6 +67,8 @@ public class UsageLimitServiceImpl implements UsageLimitService {
|
||||||
|
|
||||||
private final UsageLimitProperties usageLimitProperties;
|
private final UsageLimitProperties usageLimitProperties;
|
||||||
|
|
||||||
|
private final AccountingProperties accountingProperties;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public UsageLimitServiceImpl(
|
public UsageLimitServiceImpl(
|
||||||
|
@ -76,7 +79,7 @@ public class UsageLimitServiceImpl implements UsageLimitService {
|
||||||
ConventionService conventionService,
|
ConventionService conventionService,
|
||||||
ErrorThesaurusProperties errors,
|
ErrorThesaurusProperties errors,
|
||||||
MessageSource messageSource,
|
MessageSource messageSource,
|
||||||
QueryFactory queryFactory, TenantEntityManager tenantEntityManager, AccountingService accountingService, UsageLimitProperties usageLimitProperties) {
|
QueryFactory queryFactory, TenantEntityManager tenantEntityManager, AccountingService accountingService, UsageLimitProperties usageLimitProperties, AccountingProperties accountingProperties) {
|
||||||
this.entityManager = entityManager;
|
this.entityManager = entityManager;
|
||||||
this.authorizationService = authorizationService;
|
this.authorizationService = authorizationService;
|
||||||
this.deleterFactory = deleterFactory;
|
this.deleterFactory = deleterFactory;
|
||||||
|
@ -88,6 +91,7 @@ public class UsageLimitServiceImpl implements UsageLimitService {
|
||||||
this.tenantEntityManager = tenantEntityManager;
|
this.tenantEntityManager = tenantEntityManager;
|
||||||
this.accountingService = accountingService;
|
this.accountingService = accountingService;
|
||||||
this.usageLimitProperties = usageLimitProperties;
|
this.usageLimitProperties = usageLimitProperties;
|
||||||
|
this.accountingProperties = accountingProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UsageLimit persist(UsageLimitPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException {
|
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 {
|
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");
|
if (metric == null) throw new MyApplicationException("Target Metric not defined");
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
accounting:
|
accounting:
|
||||||
|
enabled: ${ACCOUNTING_ENABLED}
|
||||||
serviceId: ${SERVICE_ID}
|
serviceId: ${SERVICE_ID}
|
||||||
action: ${ACCOUNTING_ACTION}
|
action: ${ACCOUNTING_ACTION}
|
||||||
subjectId: unknown
|
subjectId: unknown
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
usage-limits:
|
usage-limits:
|
||||||
enabled: false
|
enabled: ${USAGE_LIMIT_ENABLED}
|
||||||
|
|
Loading…
Reference in New Issue