Skip to content

Commit 00d942a

Browse files
committed
feat: add endpoints necessary for automized processing
1 parent 358a3af commit 00d942a

File tree

8 files changed

+133
-15
lines changed

8 files changed

+133
-15
lines changed

src/main/java/fr/insee/genesis/controller/rest/responses/RawResponseController.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public ResponseEntity<String> saveRawResponsesFromRawResponseDto(
191191
}
192192

193193
//PROCESS
194-
@Operation(summary = "Process raw data of a campaign")
194+
@Operation(summary = "Process raw data for a list of interrogations")
195195
@PostMapping(path = "/raw-responses/process")
196196
@PreAuthorize("hasRole('SCHEDULER')")
197197
public ResponseEntity<String> processRawResponses(
@@ -202,9 +202,8 @@ public ResponseEntity<String> processRawResponses(
202202
@RequestParam("collectionInstrumentId") String collectionInstrumentId,
203203
@RequestBody List<String> interrogationIdList
204204
) {
205-
log.info("Try to process raw responses for questionnaireId {} and {} interrogationIds", collectionInstrumentId, interrogationIdList.size());
205+
log.info("Try to process raw responses for collectionInstrumentId {} and {} interrogationIds", collectionInstrumentId, interrogationIdList.size());
206206
List<GenesisError> errors = new ArrayList<>();
207-
208207
try {
209208
DataProcessResult result = rawResponseApiPort.processRawResponses(collectionInstrumentId, interrogationIdList, errors);
210209
return result.formattedDataCount() == 0 ?
@@ -215,6 +214,29 @@ public ResponseEntity<String> processRawResponses(
215214
return ResponseEntity.status(e.getStatus()).body(e.getMessage());
216215
}
217216
}
217+
218+
@Operation(summary = "Process raw data for all data of an collection instrument")
219+
@PostMapping(path = "/raw-responses/{collectionInstrumentId}/process")
220+
@PreAuthorize("hasRole('SCHEDULER')")
221+
public ResponseEntity<String> processRawResponsesByCollectionInstrumentId(
222+
@Parameter(
223+
description = "Id of the collection instrument (old questionnaireId)",
224+
example = "ENQTEST2025X00"
225+
)
226+
@PathVariable("collectionInstrumentId") String collectionInstrumentId
227+
) {
228+
log.info("Try to process raw responses for collectionInstrumentId {}", collectionInstrumentId);
229+
try {
230+
DataProcessResult result = rawResponseApiPort.processRawResponses(collectionInstrumentId);
231+
return result.formattedDataCount() == 0 ?
232+
ResponseEntity.ok("%d document(s) processed".formatted(result.dataCount()))
233+
: ResponseEntity.ok("%d document(s) processed, including %d FORMATTED after data verification"
234+
.formatted(result.dataCount(), result.formattedDataCount()));
235+
} catch (GenesisException e) {
236+
return ResponseEntity.status(e.getStatus()).body(e.getMessage());
237+
}
238+
}
239+
218240
@Operation(summary = "Get the list of collection instruments containing unprocessed interrogations")
219241
@GetMapping(path = "/raw-responses/unprocessed/collection-intrument-ids")
220242
@PreAuthorize("hasRole('SCHEDULER')")
@@ -269,7 +291,7 @@ public ResponseEntity<String> processJsonRawData(
269291
}
270292

271293
@Operation(summary = "Process raw data of a questionnaire")
272-
@PostMapping(path = "/{collectionInstrumentId}/process")
294+
@PostMapping(path = "/responses/raw/lunatic-json/{collectionInstrumentId}/process")
273295
@PreAuthorize("hasRole('SCHEDULER')")
274296
public ResponseEntity<String> processJsonRawData(
275297
@PathVariable String collectionInstrumentId

src/main/java/fr/insee/genesis/domain/ports/api/RawResponseApiPort.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ public interface RawResponseApiPort {
1414

1515
List<RawResponse> getRawResponses(String collectionInstrumentId, Mode mode, List<String> interrogationIdList);
1616
DataProcessResult processRawResponses(String collectionInstrumentId, List<String> interrogationIdList, List<GenesisError> errors) throws GenesisException;
17+
DataProcessResult processRawResponses(String collectionInstrumentId) throws GenesisException;
1718
List<SurveyUnitModel> convertRawResponse(List<RawResponse> rawResponses, VariablesMap variablesMap);
1819
List<String> getUnprocessedCollectionInstrumentIds();
1920
void updateProcessDates(List<SurveyUnitModel> surveyUnitModels);
21+
2022
}

src/main/java/fr/insee/genesis/domain/ports/spi/RawResponsePersistencePort.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ public interface RawResponsePersistencePort {
1111
List<RawResponse> findRawResponses(String collectionInstrumentId, Mode mode, List<String> interrogationIdList);
1212
void updateProcessDates(String collectionInstrumentId, Set<String> interrogationIds);
1313
List<String> getUnprocessedCollectionIds();
14-
}
14+
Set<String> findUnprocessedInterrogationIdsByCollectionInstrumentId(String collectionInstrumentId);
15+
}

src/main/java/fr/insee/genesis/domain/service/rawdata/RawResponseService.java

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,7 @@ public DataProcessResult processRawResponses(String collectionInstrumentId, List
7979
List<Mode> modesList = controllerUtils.getModesList(collectionInstrumentId, null);
8080
for (Mode mode : modesList) {
8181
//Load and save metadata into database, throw exception if none
82-
VariablesMap variablesMap = metadataService.loadAndSaveIfNotExists(collectionInstrumentId, collectionInstrumentId, mode, fileUtils,
83-
errors).getVariables();
84-
if (variablesMap == null) {
85-
throw new GenesisException(400,
86-
"Error during metadata parsing for mode %s :%n%s"
87-
.formatted(mode, errors.getLast().getMessage())
88-
);
89-
}
82+
VariablesMap variablesMap = getVariablesMap(collectionInstrumentId,mode,errors);
9083
int totalBatchs = Math.ceilDiv(interrogationIdList.size() , config.getRawDataProcessingBatchSize());
9184
int batchNumber = 1;
9285
List<String> interrogationIdListForMode = new ArrayList<>(interrogationIdList);
@@ -127,7 +120,84 @@ public DataProcessResult processRawResponses(String collectionInstrumentId, List
127120
batchNumber++;
128121
}
129122
}
130-
return new DataProcessResult(dataCount, formattedDataCount, List.of());
123+
return new DataProcessResult(dataCount, formattedDataCount, errors);
124+
}
125+
126+
@Override
127+
public DataProcessResult processRawResponses(String collectionInstrumentId) throws GenesisException {
128+
int dataCount=0;
129+
int formattedDataCount=0;
130+
DataProcessingContextModel dataProcessingContext =
131+
dataProcessingContextService.getContextByCollectionInstrumentId(collectionInstrumentId);
132+
List<GenesisError> errors = new ArrayList<>();
133+
134+
List<Mode> modesList = controllerUtils.getModesList(collectionInstrumentId, null);
135+
for (Mode mode : modesList) {
136+
//Load and save metadata into database, throw exception if none
137+
VariablesMap variablesMap = getVariablesMap(collectionInstrumentId,mode,errors);
138+
Set<String> interrogationIds =
139+
rawResponsePersistencePort.findUnprocessedInterrogationIdsByCollectionInstrumentId(collectionInstrumentId);
140+
141+
int totalBatchs = Math.ceilDiv(interrogationIds.size() , config.getRawDataProcessingBatchSize());
142+
int batchNumber = 1;
143+
List<String> interrogationIdListForMode = new ArrayList<>(interrogationIds);
144+
while(!interrogationIdListForMode.isEmpty()){
145+
log.info("Processing raw data batch {}/{}", batchNumber, totalBatchs);
146+
int maxIndex = Math.min(interrogationIdListForMode.size(), config.getRawDataProcessingBatchSize());
147+
148+
List<SurveyUnitModel> surveyUnitModels = getConvertedSurveyUnits(
149+
collectionInstrumentId,
150+
mode,
151+
interrogationIdListForMode,
152+
maxIndex,
153+
variablesMap);
154+
155+
//Save converted data
156+
surveyUnitQualityService.verifySurveyUnits(surveyUnitModels, variablesMap);
157+
surveyUnitService.saveSurveyUnits(surveyUnitModels);
158+
159+
//Update process dates
160+
updateProcessDates(surveyUnitModels);
161+
162+
//Increment data count
163+
dataCount += surveyUnitModels.size();
164+
formattedDataCount += surveyUnitModels.stream()
165+
.filter(surveyUnitModel -> surveyUnitModel.getState().equals(DataState.FORMATTED))
166+
.toList()
167+
.size();
168+
169+
//Send processed ids grouped by questionnaire (if review activated)
170+
if(dataProcessingContext != null && dataProcessingContext.isWithReview()) {
171+
sendProcessedIdsToQualityTool(surveyUnitModels);
172+
}
173+
174+
//Remove processed ids from list
175+
interrogationIdListForMode = interrogationIdListForMode.subList(maxIndex, interrogationIdListForMode.size());
176+
batchNumber++;
177+
}
178+
}
179+
return new DataProcessResult(dataCount, formattedDataCount, errors);
180+
}
181+
182+
private List<SurveyUnitModel> getConvertedSurveyUnits(String collectionInstrumentId, Mode mode, List<String> interrogationIdListForMode, int maxIndex, VariablesMap variablesMap) {
183+
List<String> interrogationIdToProcess = interrogationIdListForMode.subList(0, maxIndex);
184+
List<RawResponse> rawResponses = getRawResponses(collectionInstrumentId, mode, interrogationIdToProcess);
185+
return convertRawResponse(
186+
rawResponses,
187+
variablesMap
188+
);
189+
}
190+
191+
private VariablesMap getVariablesMap(String collectionInstrumentId, Mode mode, List<GenesisError> errors) throws GenesisException {
192+
VariablesMap variablesMap = metadataService.loadAndSaveIfNotExists(collectionInstrumentId, collectionInstrumentId, mode, fileUtils,
193+
errors).getVariables();
194+
if (variablesMap == null) {
195+
throw new GenesisException(400,
196+
"Error during metadata parsing for mode %s :%n%s"
197+
.formatted(mode, errors.getLast().getMessage())
198+
);
199+
}
200+
return variablesMap;
131201
}
132202

133203
@Override
@@ -179,7 +249,6 @@ public List<SurveyUnitModel> convertRawResponse(List<RawResponse> rawResponses,
179249
}
180250
}
181251
return surveyUnitModels;
182-
//return List.of();
183252
}
184253

185254
@Override

src/main/java/fr/insee/genesis/infrastructure/adapter/RawResponseMongoAdapter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.stereotype.Service;
1717

1818
import java.time.LocalDateTime;
19+
import java.util.HashSet;
1920
import java.util.List;
2021
import java.util.Set;
2122

@@ -51,4 +52,10 @@ public void updateProcessDates(String collectionInstrumentId, Set<String> interr
5152
public List<String> getUnprocessedCollectionIds() {
5253
return repository.findDistinctCollectionInstrumentIdByProcessDateIsNull();
5354
}
55+
56+
@Override
57+
public Set<String> findUnprocessedInterrogationIdsByCollectionInstrumentId(String collectionInstrumentId) {
58+
// We remove duplicate ids
59+
return new HashSet<>(repository.findInterrogationIdByCollectionInstrumentId(collectionInstrumentId));
60+
}
5461
}

src/main/java/fr/insee/genesis/infrastructure/repository/RawResponseRepository.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ public interface RawResponseRepository extends MongoRepository<RawResponseDocume
1919
"{ $project: { _id: 0, collectionInstrumentId: '$_id' } }"
2020
})
2121
List<String> findDistinctCollectionInstrumentIdByProcessDateIsNull();
22+
23+
@Aggregation(pipeline = {
24+
"{ $match: { collectionInstrumentId: ?0 } }",
25+
"{ $project: { _id: 0, interrogationId: '$interrogationId' } }"
26+
})
27+
List<String> findInterrogationIdByCollectionInstrumentId(String collectionInstrumentId);
28+
2229
}

src/test/java/cucumber/functional_tests/RawDataDefinitions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public DataProcessResult processRawResponses(String questionnaireId, List<String
115115
return null;
116116
}
117117

118+
@Override
119+
public DataProcessResult processRawResponses(String collectionInstrumentId) throws GenesisException {
120+
return null;
121+
}
122+
118123
@Override
119124
public List<SurveyUnitModel> convertRawResponse(List<RawResponse> rawResponses, VariablesMap variablesMap) {
120125
return List.of();

src/test/java/fr/insee/genesis/controller/rest/responses/RawResponseControllerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public DataProcessResult processRawResponses(String questionnaireId, List<String
8888
return null;
8989
}
9090

91+
@Override
92+
public DataProcessResult processRawResponses(String collectionInstrumentId) throws GenesisException {
93+
return null;
94+
}
95+
9196
@Override
9297
public List<SurveyUnitModel> convertRawResponse(List<RawResponse> rawResponses, VariablesMap variablesMap) {
9398
return List.of();

0 commit comments

Comments
 (0)