Skip to content

Commit

Permalink
[PAGOPA-1305] invalidare i bundle settando la data di fine validita p…
Browse files Browse the repository at this point in the history
…er psp (#111)

* [PAGOPA-1215] logging

* Bump to version 2.8.0-1-PAGOPA-1215-afm-calculator-creare-dashboard-elk [skip ci]

* [PAGOPA-1215] replicas

* [PAGOPA-1215] replicas

* [PAGOPA-1215] log

* Bump to version 2.8.0-2-PAGOPA-1215-afm-calculator-creare-dashboard-elk [skip ci]

* [PAGOPA-1215] response

* [PAGOPA-1215] commit

* Bump to version 2.8.0-3-PAGOPA-1215-afm-calculator-creare-dashboard-elk [skip ci]

* [PAGOPA-1305] fix

* Bump to version 2.8.0-4-PAGOPA-1305-invalidare-i-bundle-settando-la-data-di-fine-validita-per-psp [skip ci]

* Bump to version 2.8.0-5-PAGOPA-1305-invalidare-i-bundle-settando-la-data-di-fine-validita-per-psp [skip ci]

* [PAGOPA-1305] format

---------

Co-authored-by: pagopa-github-bot <[email protected]>
  • Loading branch information
jacopocarlini and pagopa-github-bot authored Oct 26, 2023
1 parent 5f60adf commit 284937d
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 130 deletions.
242 changes: 123 additions & 119 deletions src/main/java/it/gov/pagopa/afm/calculator/config/LoggingAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,124 +30,128 @@
@Slf4j
public class LoggingAspect {

public static final String START_TIME = "startTime";
public static final String METHOD = "method";
public static final String STATUS = "status";
public static final String CODE = "httpCode";
public static final String RESPONSE_TIME = "responseTime";

@Value("${info.application.artifactId}")
private String artifactId;

@Value("${info.application.version}")
private String version;

@Value("${info.properties.environment}")
private String environment;

@Autowired HttpServletRequest httRequest;

@Autowired HttpServletResponse httpResponse;

@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
public void restController() {
// all rest controllers
}

@Pointcut("@within(org.springframework.stereotype.Repository)")
public void repository() {
// all repository methods
}

@Pointcut("@within(org.springframework.stereotype.Service)")
public void service() {
// all service methods
}

/** Log essential info of application during the startup. */
@PostConstruct
public void logStartup() {
log.info("-> Starting {} version {} - environment {}", artifactId, version, environment);
}

/**
* If DEBUG log-level is enabled prints the env variables and the application properties.
*
* @param event Context of application
*/
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
final Environment env = event.getApplicationContext().getEnvironment();
log.debug("Active profiles: {}", Arrays.toString(env.getActiveProfiles()));
final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
StreamSupport.stream(sources.spliterator(), false)
.filter(EnumerablePropertySource.class::isInstance)
.map(ps -> ((EnumerablePropertySource<?>) ps).getPropertyNames())
.flatMap(Arrays::stream)
.distinct()
.filter(
prop ->
!(prop.toLowerCase().contains("credentials")
|| prop.toLowerCase().contains("password")
|| prop.toLowerCase().contains("pass")
|| prop.toLowerCase().contains("pwd")
|| prop.toLowerCase().contains("key")
|| prop.toLowerCase().contains("secret")))
.forEach(prop -> log.debug("{}: {}", prop, env.getProperty(prop)));
}

@Around(value = "restController()")
public Object logApiInvocation(ProceedingJoinPoint joinPoint) throws Throwable {
MDC.put(METHOD, joinPoint.getSignature().getName());
MDC.put(START_TIME, String.valueOf(System.currentTimeMillis()));
log.info("{} {}", httRequest.getMethod(), httRequest.getRequestURI());
log.info(
"Invoking API operation {} - args: {}",
joinPoint.getSignature().getName(),
joinPoint.getArgs());

Object result = joinPoint.proceed();

MDC.put(STATUS, "OK");
MDC.put(CODE, String.valueOf(httpResponse.getStatus()));
MDC.put(RESPONSE_TIME, getExecutionTime());
log.info(
"Successful API operation {} - result: {}", joinPoint.getSignature().getName(), result);
MDC.remove(STATUS);
MDC.remove(CODE);
MDC.remove(RESPONSE_TIME);
MDC.remove(START_TIME);
return result;
}

@AfterReturning(value = "execution(* *..exception.ErrorHandler.*(..))", returning = "result")
public void trowingApiInvocation(JoinPoint joinPoint, ResponseEntity<?> result) {
MDC.put(STATUS, "KO");
MDC.put(CODE, String.valueOf(result.getStatusCodeValue()));
MDC.put(RESPONSE_TIME, getExecutionTime());
log.info("Failed API operation {} - error: {}", MDC.get(METHOD), result);
MDC.remove(STATUS);
MDC.remove(CODE);
MDC.remove(RESPONSE_TIME);
MDC.remove(START_TIME);
}

@Around(value = "repository() || service()")
public Object logTrace(ProceedingJoinPoint joinPoint) throws Throwable {
log.debug(
"Call method {} - args: {}", joinPoint.getSignature().toShortString(), joinPoint.getArgs());
Object result = joinPoint.proceed();
log.debug("Return method {} - result: {}", joinPoint.getSignature().toShortString(), result);
return result;
}

private static String getExecutionTime() {
String startTime = MDC.get(START_TIME);
if (startTime != null) {
long endTime = System.currentTimeMillis();
long executionTime = endTime - Long.parseLong(startTime);
return String.valueOf(executionTime);
public static final String START_TIME = "startTime";
public static final String METHOD = "method";
public static final String STATUS = "status";
public static final String CODE = "httpCode";
public static final String RESPONSE_TIME = "responseTime";

@Value("${info.application.artifactId}")
private String artifactId;

@Value("${info.application.version}")
private String version;

@Value("${info.properties.environment}")
private String environment;

@Autowired
HttpServletRequest httRequest;

@Autowired
HttpServletResponse httpResponse;

@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
public void restController() {
// all rest controllers
}

@Pointcut("@within(org.springframework.stereotype.Repository)")
public void repository() {
// all repository methods
}

@Pointcut("@within(org.springframework.stereotype.Service)")
public void service() {
// all service methods
}

/**
* Log essential info of application during the startup.
*/
@PostConstruct
public void logStartup() {
log.info("-> Starting {} version {} - environment {}", artifactId, version, environment);
}

/**
* If DEBUG log-level is enabled prints the env variables and the application properties.
*
* @param event Context of application
*/
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
final Environment env = event.getApplicationContext().getEnvironment();
log.debug("Active profiles: {}", Arrays.toString(env.getActiveProfiles()));
final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
StreamSupport.stream(sources.spliterator(), false)
.filter(EnumerablePropertySource.class::isInstance)
.map(ps -> ((EnumerablePropertySource<?>) ps).getPropertyNames())
.flatMap(Arrays::stream)
.distinct()
.filter(
prop ->
!(prop.toLowerCase().contains("credentials")
|| prop.toLowerCase().contains("password")
|| prop.toLowerCase().contains("pass")
|| prop.toLowerCase().contains("pwd")
|| prop.toLowerCase().contains("key")
|| prop.toLowerCase().contains("secret")))
.forEach(prop -> log.debug("{}: {}", prop, env.getProperty(prop)));
}

@Around(value = "restController()")
public Object logApiInvocation(ProceedingJoinPoint joinPoint) throws Throwable {
MDC.put(METHOD, joinPoint.getSignature().getName());
MDC.put(START_TIME, String.valueOf(System.currentTimeMillis()));
log.info("{} {}", httRequest.getMethod(), httRequest.getRequestURI());
log.info(
"Invoking API operation {} - args: {}",
joinPoint.getSignature().getName(),
joinPoint.getArgs());

Object result = joinPoint.proceed();

MDC.put(STATUS, "OK");
MDC.put(CODE, String.valueOf(httpResponse.getStatus()));
MDC.put(RESPONSE_TIME, getExecutionTime());
log.info(
"Successful API operation {} - result: {}", joinPoint.getSignature().getName(), result);
MDC.remove(STATUS);
MDC.remove(CODE);
MDC.remove(RESPONSE_TIME);
MDC.remove(START_TIME);
return result;
}

@AfterReturning(value = "execution(* *..exception.ErrorHandler.*(..))", returning = "result")
public void trowingApiInvocation(JoinPoint joinPoint, ResponseEntity<?> result) {
MDC.put(STATUS, "KO");
MDC.put(CODE, String.valueOf(result.getStatusCodeValue()));
MDC.put(RESPONSE_TIME, getExecutionTime());
log.info("Failed API operation {} - error: {}", MDC.get(METHOD), result);
MDC.remove(STATUS);
MDC.remove(CODE);
MDC.remove(RESPONSE_TIME);
MDC.remove(START_TIME);
}

@Around(value = "repository() || service()")
public Object logTrace(ProceedingJoinPoint joinPoint) throws Throwable {
log.debug(
"Call method {} - args: {}", joinPoint.getSignature().toShortString(), joinPoint.getArgs());
Object result = joinPoint.proceed();
log.debug("Return method {} - result: {}", joinPoint.getSignature().toShortString(), result);
return result;
}

private static String getExecutionTime() {
String startTime = MDC.get(START_TIME);
if(startTime != null) {
long endTime = System.currentTimeMillis();
long executionTime = endTime - Long.parseLong(startTime);
return String.valueOf(executionTime);
}
return "1";
}
return "1";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package it.gov.pagopa.afm.calculator.service;

import static it.gov.pagopa.afm.calculator.service.UtilityComponent.inTransferList;
import static it.gov.pagopa.afm.calculator.service.UtilityComponent.isGlobal;

import it.gov.pagopa.afm.calculator.entity.CiBundle;
import it.gov.pagopa.afm.calculator.entity.IssuerRangeEntity;
import it.gov.pagopa.afm.calculator.entity.ValidBundle;
Expand All @@ -13,12 +10,6 @@
import it.gov.pagopa.afm.calculator.model.calculator.BundleOption;
import it.gov.pagopa.afm.calculator.model.calculator.Transfer;
import it.gov.pagopa.afm.calculator.repository.CosmosRepository;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.validation.Valid;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -27,6 +18,16 @@
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static it.gov.pagopa.afm.calculator.service.UtilityComponent.inTransferList;
import static it.gov.pagopa.afm.calculator.service.UtilityComponent.isGlobal;

@Service
@Setter
public class CalculatorService {
Expand Down Expand Up @@ -113,6 +114,8 @@ private List<Transfer> calculateTaxPayerFee(
// sort according onus and taxpayer fee
Collections.sort(transfers);

sortByFeePerPsp(transfers);

return transfers.stream().limit(limit).collect(Collectors.toList());
}

Expand Down Expand Up @@ -167,7 +170,9 @@ private void analyzeTransferList(
for (CiBundle cibundle : ciBundles) {
if (cibundle.getAttributes() != null && !cibundle.getAttributes().isEmpty()) {
transfers.addAll(
cibundle.getAttributes().parallelStream()
cibundle
.getAttributes()
.parallelStream()
.filter(
attribute ->
(attribute.getTransferCategory() != null
Expand All @@ -180,7 +185,9 @@ private void analyzeTransferList(
createTransfer(bundle.getPaymentAmount(), 0, bundle, null, paymentOption))
.collect(Collectors.toList()));
transfers.addAll(
cibundle.getAttributes().parallelStream()
cibundle
.getAttributes()
.parallelStream()
.filter(
attribute ->
(attribute.getTransferCategory() == null
Expand Down Expand Up @@ -273,4 +280,21 @@ && isOnusBundle(bundle)) {
private boolean isBelowThreshold(long paymentAmount) {
return paymentAmount < Long.parseLong(StringUtils.trim(amountThreshold));
}

/**
* sort by bundles' fee grouped by PSP
*
* @param transfers list of transfers to sort
*/
private static void sortByFeePerPsp(List<Transfer> transfers) {
transfers.sort(
(t1, t2) -> {
int primarySort = t1.getIdPsp().compareTo(t2.getIdPsp());
if (primarySort == 0) {
// if two bundles are of the same PSP we'll sort by fees
return t1.getTaxPayerFee().compareTo(t2.getTaxPayerFee());
}
return 0; // fixed to 0 because we don't want to sort by PSP name.
});
}
}

0 comments on commit 284937d

Please sign in to comment.