accounting calculate fixes

This commit is contained in:
amentis 2024-07-23 13:49:33 +03:00
parent d0b97066af
commit 8bb909f513
8 changed files with 115 additions and 13 deletions

View File

@ -1,4 +1,4 @@
package org.opencdmp.model; package org.opencdmp.model.accounting;
public class AccountingAggregateResultItem { public class AccountingAggregateResultItem {

View File

@ -0,0 +1,26 @@
package org.opencdmp.model.accounting;
import java.util.List;
public class AccountingAggregateResults {
private List<AccountingAggregateResultItem> items;
private long count;
public List<AccountingAggregateResultItem> getItems() {
return items;
}
public void setItems(List<AccountingAggregateResultItem> items) {
this.items = items;
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
}

View File

@ -21,6 +21,18 @@ import java.util.List;
public class AccountingInfoLookup { public class AccountingInfoLookup {
private List<String> serviceCodes;
public static final String _serviceCodes = "serviceCodes";
private List<String> userCodes;
public static final String _userCodes = "userCodes";
private List<String> resourceCodes;
public static final String _resourceCodes = "resourceCodes";
private List<String> actionCodes;
public static final String _actionCodes = "actionCodes";
private Instant from; private Instant from;
public static final String _from = "from"; public static final String _from = "from";
@ -42,6 +54,41 @@ public class AccountingInfoLookup {
private FieldSet groupingFields; private FieldSet groupingFields;
public static final String _groupingFields = "groupingFields"; public static final String _groupingFields = "groupingFields";
private FieldSet project;
public static final String _project = "project";
public List<String> getServiceCodes() {
return serviceCodes;
}
public void setServiceCodes(List<String> serviceCodes) {
this.serviceCodes = serviceCodes;
}
public List<String> getUserCodes() {
return userCodes;
}
public void setUserCodes(List<String> userCodes) {
this.userCodes = userCodes;
}
public List<String> getResourceCodes() {
return resourceCodes;
}
public void setResourceCodes(List<String> resourceCodes) {
this.resourceCodes = resourceCodes;
}
public List<String> getActionCodes() {
return actionCodes;
}
public void setActionCodes(List<String> actionCodes) {
this.actionCodes = actionCodes;
}
public Instant getFrom() { public Instant getFrom() {
return this.from; return this.from;
} }
@ -98,6 +145,14 @@ public class AccountingInfoLookup {
this.groupingFields = groupingFields; this.groupingFields = groupingFields;
} }
public FieldSet getProject() {
return project;
}
public void setProject(FieldSet project) {
this.project = project;
}
@Component(AccountingInfoLookup.AccountingInfoLookupValidator.ValidatorName) @Component(AccountingInfoLookup.AccountingInfoLookupValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class AccountingInfoLookupValidator extends BaseValidator<AccountingInfoLookup> { public static class AccountingInfoLookupValidator extends BaseValidator<AccountingInfoLookup> {

View File

@ -1,10 +1,10 @@
package org.opencdmp.service.accounting; package org.opencdmp.service.accounting;
import org.opencdmp.model.AccountingAggregateResultItem; import org.opencdmp.model.accounting.AccountingAggregateResults;
import org.opencdmp.query.lookup.accounting.AccountingInfoLookup; import org.opencdmp.query.lookup.accounting.AccountingInfoLookup;
public interface AccountingClient { public interface AccountingClient {
AccountingAggregateResultItem calculate(AccountingInfoLookup lookup) throws Exception; AccountingAggregateResults calculate(AccountingInfoLookup lookup) throws Exception;
} }

View File

@ -2,7 +2,7 @@ package org.opencdmp.service.accounting;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry; import gr.cite.tools.logging.MapLogEntry;
import org.opencdmp.model.AccountingAggregateResultItem; import org.opencdmp.model.accounting.AccountingAggregateResults;
import org.opencdmp.query.lookup.accounting.AccountingInfoLookup; import org.opencdmp.query.lookup.accounting.AccountingInfoLookup;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
@ -18,9 +18,9 @@ public class AccountingClientImpl implements AccountingClient {
} }
@Override @Override
public AccountingAggregateResultItem calculate(AccountingInfoLookup lookup) throws Exception { public AccountingAggregateResults calculate(AccountingInfoLookup lookup) throws Exception {
logger.debug(new MapLogEntry("calculate").And("lookup", lookup)); logger.debug(new MapLogEntry("calculate").And("lookup", lookup));
return this.accountingClient.post().uri("/accounting/calculate", uriBuilder -> uriBuilder.build()).bodyValue(lookup).exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(AccountingAggregateResultItem.class)).block(); return this.accountingClient.post().uri("/accounting/calculate", uriBuilder -> uriBuilder.build()).bodyValue(lookup).exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(AccountingAggregateResults.class)).block();
} }
} }

View File

@ -14,6 +14,8 @@ public class AccountingProperties {
private String subjectId; private String subjectId;
private List<String> projectFields;
private List<AccountingSourceEntity> sources; private List<AccountingSourceEntity> sources;
public String getServiceId() { public String getServiceId() {
@ -48,6 +50,14 @@ public class AccountingProperties {
this.subjectId = subjectId; this.subjectId = subjectId;
} }
public List<String> getProjectFields() {
return projectFields;
}
public void setProjectFields(List<String> projectFields) {
this.projectFields = projectFields;
}
public List<AccountingSourceEntity> getSources() { public List<AccountingSourceEntity> getSources() {
return sources; return sources;
} }

View File

@ -23,7 +23,7 @@ import org.opencdmp.convention.ConventionService;
import org.opencdmp.data.UserCredentialEntity; import org.opencdmp.data.UserCredentialEntity;
import org.opencdmp.integrationevent.outbox.accountingentrycreated.AccountingEntryCreatedIntegrationEvent; import org.opencdmp.integrationevent.outbox.accountingentrycreated.AccountingEntryCreatedIntegrationEvent;
import org.opencdmp.integrationevent.outbox.accountingentrycreated.AccountingEntryCreatedIntegrationEventHandler; import org.opencdmp.integrationevent.outbox.accountingentrycreated.AccountingEntryCreatedIntegrationEventHandler;
import org.opencdmp.model.AccountingAggregateResultItem; import org.opencdmp.model.accounting.AccountingAggregateResults;
import org.opencdmp.query.UserCredentialQuery; import org.opencdmp.query.UserCredentialQuery;
import org.opencdmp.query.lookup.accounting.AccountingInfoLookup; import org.opencdmp.query.lookup.accounting.AccountingInfoLookup;
import org.opencdmp.query.lookup.accounting.FieldSet; import org.opencdmp.query.lookup.accounting.FieldSet;
@ -157,8 +157,14 @@ public class AccountingServiceImpl implements AccountingService {
lookup.setTo(Instant.now()); lookup.setTo(Instant.now());
lookup.setDateRangeType(AccountingDataRangeType.ThisYear); lookup.setDateRangeType(AccountingDataRangeType.ThisYear);
lookup.setMeasure(AccountingMeasureType.Unit); lookup.setMeasure(AccountingMeasureType.Unit);
lookup.setAggregateTypes(new ArrayList<>()); lookup.setServiceCodes(List.of(this.accountingProperties.getServiceId()));
lookup.getAggregateTypes().add(AccountingAggregateType.Sum); lookup.setAggregateTypes(List.of(AccountingAggregateType.Sum));
lookup.setActionCodes(List.of(metric.getValue()));
try {
lookup.setResourceCodes(List.of(this.tenantScope.getTenantCode() != null ? this.tenantScope.getTenantCode() : this.tenantScope.getDefaultTenantCode()));
} catch (InvalidApplicationException e) {
throw new RuntimeException(e);
}
lookup.setGroupingFields(new FieldSet(List.of( lookup.setGroupingFields(new FieldSet(List.of(
AccountingEntryCreatedIntegrationEvent._serviceId, AccountingEntryCreatedIntegrationEvent._serviceId,
@ -167,16 +173,19 @@ public class AccountingServiceImpl implements AccountingService {
AccountingEntryCreatedIntegrationEvent._userId AccountingEntryCreatedIntegrationEvent._userId
))); )));
// this.validatorFactory.validator(AccountingInfoLookup.AccountingInfoLookupValidator.class).validateForce(lookup); lookup.setProject(new FieldSet(this.accountingProperties.getProjectFields()));
AccountingAggregateResultItem accountingAggregateResultItem = null; this.validatorFactory.validator(AccountingInfoLookup.AccountingInfoLookupValidator.class).validateForce(lookup);
AccountingAggregateResults accountingAggregateResultItem = null;
try { try {
accountingAggregateResultItem = accountingClient.calculate(lookup); accountingAggregateResultItem = accountingClient.calculate(lookup);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
if (accountingAggregateResultItem != null && !this.conventionService.isListNullOrEmpty(accountingAggregateResultItem.getItems())) {
return accountingAggregateResultItem.getSum().intValue(); return accountingAggregateResultItem.getItems().getFirst().getSum().intValue();
} else return 0;
} }
return null; return null;
} }

View File

@ -3,3 +3,5 @@ accounting:
serviceId: ${SERVICE_ID} serviceId: ${SERVICE_ID}
action: ${ACCOUNTING_ACTION} action: ${ACCOUNTING_ACTION}
subjectId: unknown subjectId: unknown
projectFields:
- sum