-
Notifications
You must be signed in to change notification settings - Fork 4
add two reprocess endpoints #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
49a0caf
b9b9980
7c9949e
2f2b47d
3897f7c
8b92f81
28072a9
9f747cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| import io.swagger.v3.oas.annotations.Hidden; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.Parameter; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.Valid; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.domain.Page; | ||
|
|
@@ -150,6 +151,54 @@ public ResponseEntity<String> processRawResponsesByCollectionInstrumentId( | |
| } | ||
| } | ||
|
|
||
| @Operation(summary = "Reprocess raw data for processed data of a collection instrument") | ||
| @PostMapping(path = "/raw-responses/{collectionInstrumentId}/reprocess") | ||
| @PreAuthorize("hasRole('SCHEDULER')") | ||
| public ResponseEntity<String> reProcessRawResponsesByCollectionInstrumentId( | ||
| @Parameter( | ||
| description = "Id of the collection instrument (old questionnaireId)", | ||
| example = "ENQTEST2025X00" | ||
| ) | ||
| @PathVariable("collectionInstrumentId") String collectionInstrumentId, | ||
| @RequestParam(value = "sinceDate", required = false) | ||
| @Parameter(description = "Extract since", | ||
| schema = @Schema(type = "string", format = "date-time", example = "2026-01-01T00:00:00") | ||
| ) | ||
| @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime sinceDate, | ||
| @RequestParam(value = "endDate", required = false) | ||
| @Parameter(description = "Extract until", | ||
| schema = @Schema(type = "string", format = "date-time", example = "2026-02-02T00:00:00") | ||
| ) | ||
| @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate | ||
| ) { | ||
| log.info( | ||
| "Try to reprocess raw responses for collectionInstrumentId {}, sinceDate={}, endDate={}", | ||
| collectionInstrumentId, | ||
| sinceDate, | ||
| endDate | ||
| ); | ||
|
|
||
| try { | ||
| DataProcessResult result = rawResponseApiPort.reprocessRawResponses( | ||
| collectionInstrumentId, | ||
| sinceDate, | ||
| endDate | ||
| ); | ||
|
|
||
| return result.formattedDataCount() == 0 | ||
| ? ResponseEntity.ok(NB_DOCS.formatted(result.dataCount(), collectionInstrumentId)) | ||
| : ResponseEntity.ok( | ||
| NB_DOCS_WITH_FORMATTED.formatted( | ||
| result.dataCount(), | ||
| result.formattedDataCount(), | ||
| collectionInstrumentId | ||
| ) | ||
| ); | ||
| } catch (GenesisException e) { | ||
| return ResponseEntity.status(e.getStatus()).body(e.getMessage()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Est-ce qu'on gèrerait pas plutôt via un ExceptionHandler ? |
||
| } | ||
| } | ||
|
|
||
| @Operation(summary = "Get the list of collection instruments containing unprocessed interrogations") | ||
| @GetMapping(path = "/raw-responses/unprocessed/collection-instrument-ids") | ||
| @PreAuthorize("hasRole('SCHEDULER')") | ||
|
|
@@ -229,6 +278,54 @@ public ResponseEntity<String> processJsonRawData( | |
| } | ||
| } | ||
|
|
||
| @Operation(summary = "Reprocess raw data of a questionnaire (old raw model)") | ||
| @PostMapping(path = "/responses/raw/lunatic-json/{questionnaireId}/reprocess") | ||
| @PreAuthorize("hasRole('SCHEDULER')") | ||
| public ResponseEntity<String> reProcessJsonRawDataByQuestionnaireId( | ||
| @Parameter( | ||
| description = "Id of the questionnaireId", | ||
| example = "ENQTEST2025X00" | ||
| ) | ||
| @PathVariable("questionnaireId") String questionnaireId, | ||
| @RequestParam(value = "sinceDate", required = false) | ||
| @Parameter(description = "Extract since", | ||
| schema = @Schema(type = "string", format = "date-time", example = "2026-01-01T00:00:00") | ||
| ) | ||
| @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime sinceDate, | ||
| @RequestParam(value = "endDate", required = false) | ||
| @Parameter(description = "Extract until", | ||
| schema = @Schema(type = "string", format = "date-time", example = "2026-02-02T00:00:00") | ||
| ) | ||
| @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate | ||
| ) { | ||
| log.info( | ||
| "Try to reprocess raw responses for questionnaireId {}, sinceDate={}, endDate={}", | ||
| questionnaireId, | ||
| sinceDate, | ||
| endDate | ||
| ); | ||
|
|
||
| try { | ||
| DataProcessResult result = lunaticJsonRawDataApiPort.reprocessRawData( | ||
| questionnaireId, | ||
| sinceDate, | ||
| endDate | ||
| ); | ||
|
|
||
| return result.formattedDataCount() == 0 | ||
| ? ResponseEntity.ok(NB_DOCS.formatted(result.dataCount(), questionnaireId)) | ||
| : ResponseEntity.ok( | ||
| NB_DOCS_WITH_FORMATTED.formatted( | ||
| result.dataCount(), | ||
| result.formattedDataCount(), | ||
| questionnaireId | ||
| ) | ||
| ); | ||
| } catch (GenesisException e) { | ||
| return ResponseEntity.status(e.getStatus()).body(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @Operation(summary = "Get processed data ids from last n hours (default 24h)") | ||
| @GetMapping(path = "/responses/raw/lunatic-json/processed/ids") | ||
| @PreAuthorize("hasRole('ADMIN')") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ | |
| import java.math.BigDecimal; | ||
| import java.time.Instant; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
|
|
@@ -97,8 +98,8 @@ | |
| } | ||
|
|
||
| @Override | ||
| public List<LunaticJsonRawDataModel> getRawData(String campaignName, Mode mode, List<String> interrogationIdList) { | ||
| return lunaticJsonRawDataPersistencePort.findRawData(campaignName, mode, interrogationIdList); | ||
| public List<LunaticJsonRawDataModel> getRawData(String questionnaireId, Mode mode, List<String> interrogationIdList) { | ||
| return lunaticJsonRawDataPersistencePort.findRawData(questionnaireId, mode, interrogationIdList); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -113,15 +114,15 @@ | |
|
|
||
| @Override | ||
| @Deprecated(since = "1.13.0") | ||
| public DataProcessResult processRawData(String campaignName, List<String> interrogationIdList, List<GenesisError> errors) throws GenesisException { | ||
| public DataProcessResult processRawData(String questionnaireId, List<String> interrogationIdList, List<GenesisError> errors) throws GenesisException { | ||
|
Check warning on line 117 in src/main/java/fr/insee/genesis/domain/service/rawdata/LunaticJsonRawDataService.java
|
||
| int dataCount=0; | ||
| int formattedDataCount=0; | ||
| DataProcessingContextModel dataProcessingContext = | ||
| dataProcessingContextService.getContextByCollectionInstrumentId(campaignName); | ||
| List<Mode> modesList = controllerUtils.getModesList(campaignName, null); | ||
| dataProcessingContextService.getContextByCollectionInstrumentId(questionnaireId); | ||
| List<Mode> modesList = controllerUtils.getModesList(questionnaireId, null); | ||
| for (Mode mode : modesList) { | ||
| //Load and save metadata into database, throw exception if none | ||
| VariablesMap variablesMap = getVariablesMap(campaignName, mode, errors); | ||
| VariablesMap variablesMap = getVariablesMap(questionnaireId, mode, errors); | ||
| int totalBatchs = Math.ceilDiv(interrogationIdList.size() , config.getRawDataProcessingBatchSize()); | ||
| int batchNumber = 1; | ||
| List<String> interrogationIdListForMode = new ArrayList<>(interrogationIdList); | ||
|
|
@@ -130,7 +131,7 @@ | |
| int maxIndex = Math.min(interrogationIdListForMode.size(), config.getRawDataProcessingBatchSize()); | ||
| List<String> interrogationIdToProcess = interrogationIdListForMode.subList(0, maxIndex); | ||
|
|
||
| List<LunaticJsonRawDataModel> rawData = getRawData(campaignName, mode, interrogationIdToProcess); | ||
| List<LunaticJsonRawDataModel> rawData = getRawData(questionnaireId, mode, interrogationIdToProcess); | ||
|
|
||
| List<SurveyUnitModel> surveyUnitModels = convertRawData( | ||
| rawData, | ||
|
|
@@ -222,6 +223,7 @@ | |
| return new DataProcessResult(dataCount, formattedDataCount, errors); | ||
| } | ||
|
|
||
|
|
||
| private VariablesMap getVariablesMap(String questionnaireId, Mode mode, List<GenesisError> errors) throws GenesisException { | ||
| VariablesMap variablesMap = metadataService.loadAndSaveIfNotExists(questionnaireId, questionnaireId, mode, fileUtils, | ||
| errors).getVariables(); | ||
|
|
@@ -269,6 +271,57 @@ | |
| return processedInterrogationIdsPerQuestionnaire; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public DataProcessResult reprocessRawData( | ||
| String questionnaireId, | ||
| LocalDateTime sinceDate, | ||
| LocalDateTime endDate | ||
| ) throws GenesisException { | ||
|
|
||
| log.info( | ||
| "Reprocessing raw responses for questionnaireId={}, sinceDate={}, endDate={}", | ||
| questionnaireId, | ||
| sinceDate, | ||
| endDate | ||
| ); | ||
|
|
||
| if (sinceDate == null && endDate != null) { | ||
| throw new GenesisException(400, "endDate cannot be provided without sinceDate"); | ||
| } | ||
|
|
||
| if (sinceDate != null && endDate != null && endDate.isBefore(sinceDate)) { | ||
| throw new GenesisException(400, "endDate must be after or equal to sinceDate"); | ||
| } | ||
|
|
||
| Set<String> interrogationIds; | ||
|
|
||
| if (sinceDate == null) { | ||
| interrogationIds = | ||
| lunaticJsonRawDataPersistencePort.findProcessedInterrogationIdsByQuestionnaireId(questionnaireId); | ||
| } else { | ||
| LocalDateTime effectiveEndDate = endDate != null ? endDate : LocalDateTime.now(); | ||
|
|
||
| interrogationIds = | ||
| lunaticJsonRawDataPersistencePort.findProcessedInterrogationIdsByQuestionnaireIdAndRecordDateBetween( | ||
| questionnaireId, | ||
| sinceDate, | ||
| effectiveEndDate | ||
| ); | ||
| } | ||
|
|
||
| if (interrogationIds.isEmpty()) { | ||
| return new DataProcessResult(0, 0, new ArrayList<>()); | ||
| } | ||
|
|
||
| surveyUnitService.deleteByQuestionnaireIdAndInterrogationIds(questionnaireId, interrogationIds); | ||
| lunaticJsonRawDataPersistencePort.resetProcessDates(questionnaireId, interrogationIds); | ||
|
|
||
| return processRawData(questionnaireId, new ArrayList<>(interrogationIds), | ||
| new ArrayList<>()); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public List<SurveyUnitModel> convertRawData(List<LunaticJsonRawDataModel> rawDataList, VariablesMap variablesMap) { | ||
| //Convert to genesis model | ||
|
|
@@ -334,7 +387,7 @@ | |
| private static LocalDateTime getValidationDate(LunaticJsonRawDataModel rawData) { | ||
| try{ | ||
| return rawData.data().get("validationDate") == null ? null : | ||
| LocalDateTime.parse(rawData.data().get("validationDate").toString()); | ||
| LocalDateTime.parse(rawData.data().get("validationDate").toString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| }catch(Exception e){ | ||
| log.warn("Exception when parsing validation date : {}}",e.toString()); | ||
| return null; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ça serait bien de définir des
private static final String ...(constante ou fonction) pour faire plaisir à Sonar sur les string dupliquées