generated from pagopa/template-java-spring-microservice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6805dac
commit a01b52d
Showing
8 changed files
with
198 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import requests | ||
import time | ||
from datetime import datetime, timedelta | ||
import os | ||
|
||
|
||
SUBSCRIPTION_KEY = os.getenv("OCP_APIM_SUBSCRIPTION_KEY") | ||
PM_INGESTION_URL = os.getenv("PM_INGESTION_URL") | ||
|
||
if not SUBSCRIPTION_KEY: | ||
raise EnvironmentError("La variabile di ambiente 'OCP_APIM_SUBSCRIPTION_KEY' non è configurata.") | ||
|
||
if not PM_INGESTION_URL: | ||
raise EnvironmentError("La variabile di ambiente 'PM_INGESTION_URL' non è configurata.") | ||
|
||
# Configurazioni | ||
BASE_URL = f"{PM_INGESTION_URL}/extraction/data" | ||
PM_EXTRACTION_TYPES = ["CARD", "BPAY", "PAYPAL"] | ||
HEADERS = { | ||
"accept": "*/*", | ||
"Content-Type": "application/json", | ||
"Ocp-Apim-Subscription-Key": SUBSCRIPTION_KEY, # Ottieni la subkey dall'ambiente | ||
} | ||
INTERVAL_SECONDS = 5 * 60 # Configura qui l'intervallo in secondi (5 minuti = 300 secondi) | ||
current_date = datetime(2023, 3, 31) # Data di partenza | ||
end_date = datetime(2018, 1, 1) # Data finale | ||
|
||
def post_requests(): | ||
"""Esegue le chiamate POST per ogni tipo di estrazione.""" | ||
creation_date = current_date.strftime("%Y-%m-%d") | ||
for pm_type in PM_EXTRACTION_TYPES: | ||
payload = { | ||
"taxCodes": [], | ||
"creationDateFrom": creation_date, | ||
"creationDateTo": creation_date, | ||
} | ||
url = f"{BASE_URL}?pmExtractionType={pm_type}" | ||
try: | ||
response = requests.post(url, headers=HEADERS, json=payload) | ||
print(f"POST to {url} with payload {payload}: {response.status_code} - {response.text}") | ||
except Exception as e: | ||
print(f"Errore durante la richiesta POST per {pm_type}: {e}") | ||
|
||
# Loop principale | ||
print(f"Avvio dello script. Data iniziale: {current_date.strftime('%Y-%m-%d')}") | ||
|
||
while current_date >= end_date: | ||
post_requests() | ||
current_date -= timedelta(days=1) # Riduci la data di 1 giorno | ||
print(f"Data successiva: {current_date.strftime('%Y-%m-%d')}") | ||
time.sleep(INTERVAL_SECONDS) # Attendi l'intervallo configurato | ||
|
||
print("Script terminato correttamente.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/it/gov/pagopa/bizpmingestion/model/ExtractionResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package it.gov.pagopa.bizpmingestion.model; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public class ExtractionResponse { | ||
private Integer element; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/it/gov/pagopa/bizpmingestion/service/impl/AyncService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package it.gov.pagopa.bizpmingestion.service.impl; | ||
|
||
import it.gov.pagopa.bizpmingestion.entity.cosmos.execution.BizEventsPMIngestionExecution; | ||
import it.gov.pagopa.bizpmingestion.entity.pm.PPTransaction; | ||
import it.gov.pagopa.bizpmingestion.enumeration.PaymentMethodType; | ||
import it.gov.pagopa.bizpmingestion.model.pm.PMEvent; | ||
import it.gov.pagopa.bizpmingestion.model.pm.PMEventPaymentDetail; | ||
import it.gov.pagopa.bizpmingestion.model.pm.PMEventToViewResult; | ||
import it.gov.pagopa.bizpmingestion.repository.*; | ||
import it.gov.pagopa.bizpmingestion.service.IPMEventToViewService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.*; | ||
|
||
@EnableAsync | ||
@Service | ||
@Slf4j | ||
public class AyncService { | ||
|
||
private static final String LOG_BASE_HEADER_INFO = "[ClassMethod: %s] - [MethodParamsToLog: %s]"; | ||
|
||
private final ModelMapper modelMapper; | ||
private final BizEventsViewGeneralRepository bizEventsViewGeneralRepository; | ||
private final BizEventsViewCartRepository bizEventsViewCartRepository; | ||
private final BizEventsViewUserRepository bizEventsViewUserRepository; | ||
private final PMIngestionExecutionRepository pmIngestionExecutionRepository; | ||
private final IPMEventToViewService pmEventToViewService; | ||
|
||
@Autowired | ||
public AyncService(ModelMapper modelMapper, PPTransactionRepository ppTransactionRepository, | ||
BizEventsViewGeneralRepository bizEventsViewGeneralRepository, BizEventsViewCartRepository bizEventsViewCartRepository, | ||
BizEventsViewUserRepository bizEventsViewUserRepository, PMIngestionExecutionRepository pmIngestionExecutionRepository, | ||
IPMEventToViewService pmEventToViewService) { | ||
this.modelMapper = modelMapper; | ||
this.bizEventsViewGeneralRepository = bizEventsViewGeneralRepository; | ||
this.bizEventsViewCartRepository = bizEventsViewCartRepository; | ||
this.bizEventsViewUserRepository = bizEventsViewUserRepository; | ||
this.pmIngestionExecutionRepository = pmIngestionExecutionRepository; | ||
this.pmEventToViewService = pmEventToViewService; | ||
} | ||
|
||
@Async | ||
public void processDataAsync(List<PPTransaction> ppTrList, PaymentMethodType paymentMethodType, BizEventsPMIngestionExecution pmIngestionExec) { | ||
|
||
try { | ||
|
||
List<Long> skippedId = Collections.synchronizedList(new ArrayList<>()); | ||
pmIngestionExec.setStatus("DONE"); | ||
|
||
var pmEventList = ppTrList.stream() | ||
.map(ppTransaction -> modelMapper.map(ppTransaction, PMEvent.class)) | ||
.toList(); | ||
|
||
int importedEventsCounter = pmEventList.parallelStream() | ||
.map(pmEvent -> { | ||
try { | ||
|
||
PMEventPaymentDetail pmEventPaymentDetail = Optional.ofNullable(pmEvent.getPaymentDetailList()) | ||
.orElse(Collections.emptyList()) | ||
.stream() | ||
.max(Comparator.comparing(PMEventPaymentDetail::getImporto)) | ||
.orElseThrow(); | ||
|
||
PMEventToViewResult result = pmEventToViewService.mapPMEventToView(pmEvent, pmEventPaymentDetail, paymentMethodType); | ||
if (result != null) { | ||
bizEventsViewGeneralRepository.save(result.getGeneralView()); | ||
bizEventsViewCartRepository.save(result.getCartView()); | ||
bizEventsViewUserRepository.saveAll(result.getUserViewList()); | ||
return 1; | ||
} | ||
return 0; | ||
} catch (Exception e) { | ||
pmIngestionExec.setStatus("DONE WITH SKIP"); | ||
skippedId.add(pmEvent.getPkTransactionId()); | ||
|
||
log.error(String.format(LOG_BASE_HEADER_INFO, "processDataAsync", "[processId=" + pmIngestionExec.getId() + "] - Error importing PM event with id=" + pmEvent.getPkTransactionId() | ||
+ " (err desc = " + e.getMessage() + ")"), e); | ||
return 0; | ||
} | ||
}) | ||
.reduce(Integer::sum) | ||
.orElse(-1); | ||
|
||
pmIngestionExec.setNumRecordIngested(importedEventsCounter); | ||
pmIngestionExec.setSkippedID(skippedId); | ||
|
||
|
||
} catch (Exception e) { | ||
pmIngestionExec.setStatus("FAILED"); | ||
|
||
log.error(String.format(LOG_BASE_HEADER_INFO, "processDataAsync", "[processId=" + pmIngestionExec.getId() + "] - Error during asynchronous processing: " + e.getMessage())); | ||
} finally { | ||
pmIngestionExec.setEndTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ROOT).format(LocalDateTime.now())); | ||
pmIngestionExecutionRepository.save(pmIngestionExec); | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters