Skip to content

merge check indexation status and reindex all in one endpoint #448

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions src/main/java/org/gridsuite/study/server/StudyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,6 @@ public ResponseEntity<BasicStudyInfos> recreateStudyNetwork(@PathVariable("study
return ResponseEntity.ok().build();
}

@GetMapping(value = "/studies/{studyUuid}/indexation/status")
@Operation(summary = "check study indexation")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "The study indexation status"),
@ApiResponse(responseCode = "204", description = "The study indexation status doesn't exist"),
@ApiResponse(responseCode = "404", description = "The study or network doesn't exist")})
public ResponseEntity<String> checkStudyIndexationStatus(@PathVariable("studyUuid") UUID studyUuid) {
String result = studyService.getStudyIndexationStatus(studyUuid).name();
return result != null ? ResponseEntity.ok().body(result) :
ResponseEntity.noContent().build();
}

@PostMapping(value = "/studies/{studyUuid}/tree/subtrees", params = {"subtreeToCutParentNodeUuid", "referenceNodeUuid"})
@Operation(summary = "cut and paste a subtree")
@ApiResponses(value = {
Expand Down Expand Up @@ -1317,11 +1305,13 @@ public ResponseEntity<String> getDefaultDynamicSimulationProvider() {
return ResponseEntity.ok().body(studyService.getDefaultDynamicSimulationProvider());
}

@PostMapping(value = "/studies/{studyUuid}/reindex-all")
@Operation(summary = "reindex the study")
@ApiResponse(responseCode = "200", description = "Study reindexed")
public ResponseEntity<Void> reindexStudy(@Parameter(description = "study uuid") @PathVariable("studyUuid") UUID studyUuid) {
studyService.reindexStudy(studyUuid);
@PostMapping(value = "/studies/{studyUuid}/reindex-if-needed")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be better to keep the endpoint reindex-all to force reindexation ?
/studies/{studyUuid}/reindex-all?if-needed=true/false

@Operation(summary = "reindex the study if needed")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Study reindexed"),
@ApiResponse(responseCode = "404", description = "The study or network doesn't exist")})
public ResponseEntity<Void> reindexStudyIfNeeded(@Parameter(description = "study uuid") @PathVariable("studyUuid") UUID studyUuid) {
studyService.reindexStudyIfNeeded(studyUuid);
return ResponseEntity.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,13 @@ public void reindexStudy(UUID studyUuid) {
}

@Transactional
public void reindexStudyIfNeeded(UUID studyUuid) {
StudyIndexationStatus status = getStudyIndexationStatus(studyUuid);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make private getStudyIndexationStatus

if (status == StudyIndexationStatus.NOT_INDEXED) {
reindexStudy(studyUuid);
}
}

public StudyIndexationStatus getStudyIndexationStatus(UUID studyUuid) {
StudyEntity study = studyRepository.findById(studyUuid).orElseThrow(() -> new StudyException(STUDY_NOT_FOUND));
if (study.getIndexationStatus() == StudyIndexationStatus.INDEXED
Expand Down
55 changes: 17 additions & 38 deletions src/test/java/org/gridsuite/study/server/StudyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2194,63 +2194,49 @@ private void checkElementUpdatedMessageSent(UUID elementUuid, String userId) {

@Test
public void reindexStudyTest() throws Exception {
mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", UUID.randomUUID()))
// if study doesn't exist
mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", UUID.randomUUID()))
.andExpect(status().isNotFound());

mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", UUID.randomUUID()))
.andExpectAll(status().isNotFound());

UUID notExistingNetworkStudyUuid = createStudy("userId", NOT_EXISTING_NETWORK_CASE_UUID);

mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", notExistingNetworkStudyUuid))
// if study exists but not the network
mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", notExistingNetworkStudyUuid))
.andExpect(status().isInternalServerError());
Message<byte[]> indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination);
Message<byte[]> indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination);

var requests = TestUtils.getRequestsWithBodyDone(1, server);
assertTrue(requests.stream().anyMatch(r -> r.getPath().contains("/v1/networks/" + NOT_EXISTING_NETWORK_UUID + "/reindex-all")));

mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", notExistingNetworkStudyUuid))
.andExpectAll(status().isInternalServerError());

requests = TestUtils.getRequestsWithBodyDone(1, server);
assertEquals(1, requests.stream().filter(r -> r.getPath().contains("/v1/networks/" + NOT_EXISTING_NETWORK_UUID + "/indexed-equipments")).count());

UUID study1Uuid = createStudy("userId", CASE_UUID);

mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", study1Uuid))
.andExpectAll(status().isOk(),
content().string("NOT_INDEXED"));
indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination);

mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", study1Uuid))
mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", study1Uuid))
.andExpect(status().isOk());

// first the indexationStatus is updated to NOT_INDEXED when checking status
indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination);
// Then it follow the reinxation process
indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination);
Message<byte[]> indexationStatusMessageDone = output.receive(TIMEOUT, studyUpdateDestination);
assertEquals(study1Uuid, indexationStatusMessageDone.getHeaders().get(NotificationService.HEADER_STUDY_UUID));
assertEquals(NotificationService.UPDATE_TYPE_INDEXATION_STATUS, indexationStatusMessageDone.getHeaders().get(HEADER_UPDATE_TYPE));

mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", study1Uuid))
.andExpectAll(status().isOk(),
content().string("INDEXED"));

requests = TestUtils.getRequestsWithBodyDone(4, server);
requests = TestUtils.getRequestsWithBodyDone(3, server);
assertTrue(requests.stream().anyMatch(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/reindex-all")));
assertEquals(2, requests.stream().filter(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/indexed-equipments")).count());
assertEquals(1, requests.stream().filter(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/indexed-equipments")).count());
assertEquals(1, requests.stream().filter(r -> r.getPath().matches("/v1/reports/.*")).count());

Message<byte[]> buildStatusMessage = output.receive(TIMEOUT, studyUpdateDestination);
assertEquals(study1Uuid, buildStatusMessage.getHeaders().get(NotificationService.HEADER_STUDY_UUID));
assertEquals(NotificationService.NODE_BUILD_STATUS_UPDATED, buildStatusMessage.getHeaders().get(HEADER_UPDATE_TYPE));

mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", study1Uuid))
.andExpect(status().is5xxServerError());
indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination);
indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination);
// Finally we just check the status and it's ok, we don't need a reindexation for this study
mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", study1Uuid))
.andExpect(status().isOk());

requests = TestUtils.getRequestsWithBodyDone(1, server);
assertTrue(requests.stream().anyMatch(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/reindex-all")));
assertEquals(1, requests.stream().filter(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/indexed-equipments")).count());
}

@Test
Expand Down Expand Up @@ -2313,18 +2299,15 @@ public void testSupervision() throws Exception {
MvcResult mvcResult;
UUID studyUuid = createStudy("userId", CASE_UUID);

mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", studyUuid))
mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", studyUuid))
.andExpect(status().isOk());

Message<byte[]> indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination);
Message<byte[]> indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination);
Message<byte[]> indexationStatusMessageDone = output.receive(TIMEOUT, studyUpdateDestination);
assertEquals(studyUuid, indexationStatusMessageDone.getHeaders().get(NotificationService.HEADER_STUDY_UUID));
assertEquals(NotificationService.UPDATE_TYPE_INDEXATION_STATUS, indexationStatusMessageDone.getHeaders().get(HEADER_UPDATE_TYPE));

mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", studyUuid))
.andExpectAll(status().isOk(),
content().string("INDEXED"));

Message<byte[]> buildStatusMessage = output.receive(TIMEOUT, studyUpdateDestination);
assertEquals(studyUuid, buildStatusMessage.getHeaders().get(NotificationService.HEADER_STUDY_UUID));
assertEquals(NotificationService.NODE_BUILD_STATUS_UPDATED, buildStatusMessage.getHeaders().get(HEADER_UPDATE_TYPE));
Expand Down Expand Up @@ -2369,11 +2352,7 @@ public void testSupervision() throws Exception {

assertEquals(20, Long.parseLong(mvcResult.getResponse().getContentAsString()));

Message<byte[]> indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination);

mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", studyUuid))
.andExpectAll(status().isOk(),
content().string("NOT_INDEXED"));
indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination);

var requests = TestUtils.getRequestsWithBodyDone(3, server);
assertTrue(requests.stream().anyMatch(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/reindex-all")));
Expand Down