Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
Signed-off-by: jamal-khey <myjamal89@gmail.com>
  • Loading branch information
jamal-khey committed Aug 14, 2024
1 parent cb26803 commit 5524154
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 100 deletions.
20 changes: 4 additions & 16 deletions src/main/java/com/powsybl/caseserver/CaseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ public ResponseEntity<Boolean> exists(@PathVariable("caseUuid") UUID caseUuid) {
@SuppressWarnings("javasecurity:S5145")
public ResponseEntity<UUID> importCase(@RequestParam("file") MultipartFile file,
@RequestParam(value = "withExpiration", required = false, defaultValue = "false") boolean withExpiration,
@RequestParam(value = "indexed", required = false, defaultValue = "false") boolean indexed) {
@RequestParam(value = "withIndexation", required = false, defaultValue = "false") boolean withIndexation) {
LOGGER.debug("importCase request received with file = {}", file.getName());
UUID caseUuid = caseService.importCase(file, withExpiration, indexed);
UUID caseUuid = caseService.importCase(file, withExpiration, withIndexation);
return ResponseEntity.ok().body(caseUuid);
}

Expand All @@ -166,10 +166,9 @@ public ResponseEntity<UUID> importCase(@RequestParam("file") MultipartFile file,
@ApiResponse(responseCode = "500", description = "An error occurred during the case file duplication")})
public ResponseEntity<UUID> duplicateCase(
@RequestParam("duplicateFrom") UUID caseId,
@RequestParam(value = "withExpiration", required = false, defaultValue = "false") boolean withExpiration,
@RequestParam(value = "indexed", required = false, defaultValue = "false") boolean indexed) {
@RequestParam(value = "withExpiration", required = false, defaultValue = "false") boolean withExpiration) {
LOGGER.debug("duplicateCase request received with parameter sourceCaseUuid = {}", caseId);
UUID newCaseUuid = caseService.duplicateCase(caseId, withExpiration, indexed);
UUID newCaseUuid = caseService.duplicateCase(caseId, withExpiration);
return ResponseEntity.ok().body(newCaseUuid);
}

Expand All @@ -184,17 +183,6 @@ public ResponseEntity<Void> disableCaseExpiration(@PathVariable("caseUuid") UUID
return ResponseEntity.ok().build();
}

@PutMapping(value = "/cases/{caseUuid}/indexation")
@Operation(summary = "enable automatic indexation of the case")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "The case index has been changed"),
@ApiResponse(responseCode = "404", description = "Source case not found")})
public ResponseEntity<Void> enableCaseIndexation(@PathVariable("caseUuid") UUID caseUuid, @RequestParam("indexed") boolean indexed) {
LOGGER.debug("enableIndexation request received for caseUuid = {}", caseUuid);
caseService.enableCaseIndexation(caseUuid, indexed);
return ResponseEntity.ok().build();
}

@DeleteMapping(value = "/cases/{caseUuid}")
@Operation(summary = "delete a case")
public ResponseEntity<Void> deleteCase(@PathVariable("caseUuid") UUID caseUuid) {
Expand Down
36 changes: 14 additions & 22 deletions src/main/java/com/powsybl/caseserver/CaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ boolean caseExists(UUID caseName) {
return Files.exists(caseFile) && Files.isRegularFile(caseFile);
}

UUID importCase(MultipartFile mpf, boolean withExpiration, boolean indexed) {
UUID importCase(MultipartFile mpf, boolean withExpiration, boolean withIndexation) {
checkStorageInitialization();

UUID caseUuid = UUID.randomUUID();
Expand Down Expand Up @@ -210,16 +210,16 @@ UUID importCase(MultipartFile mpf, boolean withExpiration, boolean indexed) {
throw e;
}

createCaseMetadataEntity(caseUuid, withExpiration, indexed);
createCaseMetadataEntity(caseUuid, withExpiration, withIndexation);
CaseInfos caseInfos = createInfos(caseFile.getFileName().toString(), caseUuid, importer.getFormat());
if (indexed) {
if (withIndexation) {
caseInfosService.addCaseInfos(caseInfos);
}
sendImportMessage(caseInfos.createMessage());
return caseUuid;
}

UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration, boolean indexed) {
UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration) {
try {
Path existingCaseFile = getCaseFile(sourceCaseUuid);
if (existingCaseFile == null || existingCaseFile.getParent() == null) {
Expand All @@ -235,10 +235,11 @@ UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration, boolean indexed)

CaseInfos existingCaseInfos = caseInfosService.getCaseInfosByUuid(sourceCaseUuid.toString()).orElseThrow();
CaseInfos caseInfos = createInfos(existingCaseInfos.getName(), newCaseUuid, existingCaseInfos.getFormat());
if (indexed) {
caseInfosService.addCaseInfos(caseInfos);
}
createCaseMetadataEntity(newCaseUuid, withExpiration, indexed);
caseInfosService.addCaseInfos(caseInfos);

Optional<CaseMetadataEntity> existingCase = caseMetadataRepository.findById(sourceCaseUuid);
existingCase.ifPresentOrElse(caseMetadataEntity -> createCaseMetadataEntity(newCaseUuid, withExpiration, caseMetadataEntity.isIndexed()),
() -> createCaseMetadataEntity(newCaseUuid, withExpiration, false));

sendImportMessage(caseInfos.createMessage());
return newCaseUuid;
Expand All @@ -248,23 +249,20 @@ UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration, boolean indexed)
}
}

private void createCaseMetadataEntity(UUID newCaseUuid, boolean withExpiration, boolean indexed) {
private void createCaseMetadataEntity(UUID newCaseUuid, boolean withExpiration, boolean withIndexation) {
Instant expirationTime = null;
if (withExpiration) {
expirationTime = Instant.now().plus(1, ChronoUnit.HOURS);
}
caseMetadataRepository.save(new CaseMetadataEntity(newCaseUuid, expirationTime, indexed));
caseMetadataRepository.save(new CaseMetadataEntity(newCaseUuid, expirationTime, withIndexation));
}

public Set<UUID> getCaseToReindex() {
return caseMetadataRepository.findAllByIndexedTrue()
public List<CaseInfos> getCasesToReindex() {
Set<UUID> casesToReindex = caseMetadataRepository.findAllByIndexedTrue()
.stream()
.map(CaseMetadataEntity::getId)
.collect(Collectors.toSet());
}

public List<CaseInfos> getAllCases() {
return getCases(getStorageRootDir());
return getCases(getStorageRootDir()).stream().filter(c -> casesToReindex.contains(c.getUuid())).toList();
}

CaseInfos createInfos(String fileBaseName, UUID caseUuid, String format) {
Expand All @@ -284,12 +282,6 @@ public void disableCaseExpiration(UUID caseUuid) {
caseMetadataEntity.setExpirationDate(null);
}

@Transactional
public void enableCaseIndexation(UUID caseUuid, boolean indexed) {
CaseMetadataEntity caseMetadataEntity = caseMetadataRepository.findById(caseUuid).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "case " + caseUuid + " not found"));
caseMetadataEntity.setIndexed(indexed);
}

Optional<Network> loadNetwork(UUID caseUuid) {
checkStorageInitialization();

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/powsybl/caseserver/SupervisionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public class SupervisionController {
private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionController.class);

private final SupervisionService supervisionService;
private final CaseService caseService;
private final ClientConfiguration elasticsearchClientConfiguration;
private final CaseInfosService caseInfosService;

public SupervisionController(SupervisionService supervisionService, ClientConfiguration elasticsearchClientConfiguration, CaseInfosService caseInfosService) {
public SupervisionController(SupervisionService supervisionService, CaseService caseService, ClientConfiguration elasticsearchClientConfiguration, CaseInfosService caseInfosService) {
this.supervisionService = supervisionService;
this.caseService = caseService;
this.elasticsearchClientConfiguration = elasticsearchClientConfiguration;
this.caseInfosService = caseInfosService;
}
Expand All @@ -59,7 +61,7 @@ public ResponseEntity<String> getIndexedDirectoryElementsIndexName() {
@Operation(summary = "reindex all cases")
public ResponseEntity<Void> reindexAllCases() {
LOGGER.debug("reindex all cases request received");
supervisionService.reindexAllCases();
caseInfosService.recreateAllCaseInfos(caseService.getCasesToReindex());
return ResponseEntity.ok().build();
}

Expand All @@ -73,8 +75,8 @@ public ResponseEntity<String> deleteIndexedDirectoryElements() {
@GetMapping(value = "/cases/indexation-count")
@Operation(summary = "get indexed cases count")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Indexed cases count")})
public ResponseEntity<String> getIndexedDirectoryElementsCount() {
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(Long.toString(supervisionService.getIndexedCaseElementsCount()));
public ResponseEntity<String> getIndexedCasesCount() {
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(Long.toString(supervisionService.getIndexedCasesCount()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@
*/
@Repository
public interface CaseMetadataRepository extends JpaRepository<CaseMetadataEntity, UUID> {

@Override
List<CaseMetadataEntity> findAllById(Iterable<UUID> uuids);

List<CaseMetadataEntity> findAllByIndexedTrue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@
*/
package com.powsybl.caseserver.services;

import com.powsybl.caseserver.CaseService;
import com.powsybl.caseserver.dto.CaseInfos;
import com.powsybl.caseserver.elasticsearch.CaseInfosRepository;
import com.powsybl.caseserver.elasticsearch.CaseInfosService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -27,34 +21,23 @@
public class SupervisionService {
private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionService.class);

private final CaseInfosService caseInfosService;
private final CaseService caseService;
private final CaseInfosRepository caseInfosRepository;

public SupervisionService(CaseInfosService caseInfosService, CaseService caseService, CaseInfosRepository caseInfosRepository) {
this.caseInfosService = caseInfosService;
this.caseService = caseService;
public SupervisionService(CaseInfosRepository caseInfosRepository) {
this.caseInfosRepository = caseInfosRepository;
}

public void reindexAllCases() {
List<CaseInfos> allCases = caseService.getAllCases();
Set<UUID> casesToIndex = caseService.getCaseToReindex();
List<CaseInfos> data = allCases.stream().filter(c -> casesToIndex.contains(c.getUuid())).toList();
caseInfosService.recreateAllCaseInfos(data);
}

public long deleteIndexedDirectoryElements() {
AtomicReference<Long> startTime = new AtomicReference<>();
startTime.set(System.nanoTime());

long nbIndexesToDelete = getIndexedCaseElementsCount();
long nbIndexesToDelete = getIndexedCasesCount();
caseInfosRepository.deleteAll();
LOGGER.trace("Indexed directory elements deletion : {} seconds", TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime.get()));
return nbIndexesToDelete;
}

public long getIndexedCaseElementsCount() {
public long getIndexedCasesCount() {
return caseInfosRepository.count();
}
}
30 changes: 7 additions & 23 deletions src/test/java/com/powsybl/caseserver/CaseControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,35 +474,19 @@ public void test() throws Exception {
assertTrue(response.contains("\"format\":\"XIIDM\""));
}

@Test
public void testChangeIndexation() throws Exception {
createStorageDir();

// import a case
UUID caseUuid = importCase(TEST_CASE, false);
assertNotNull(outputDestination.receive(1000, caseImportDestination));
assertTrue(caseMetadataRepository.findAllById(List.of(caseUuid)).get(0).isIndexed());

// disable indexation
mvc.perform(put("/v1/cases/{caseUuid}/indexation", caseUuid).param("indexed", "false"))
.andExpect(status().isOk());

assertFalse(caseMetadataRepository.findAllById(List.of(caseUuid)).get(0).isIndexed());
}

private UUID importCase(String testCase, Boolean withExpiration) throws Exception {
String importedCase;
if (withExpiration) {
importedCase = mvc.perform(multipart("/v1/cases")
.file(createMockMultipartFile(testCase))
.param("withExpiration", withExpiration.toString())
.param("indexed", "true"))
.param("withIndexation", "true"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
} else {
importedCase = mvc.perform(multipart("/v1/cases")
.file(createMockMultipartFile(testCase))
.param("indexed", "true"))
.param("withIndexation", "true"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
}
Expand Down Expand Up @@ -546,7 +530,7 @@ public void searchCaseTest() throws Exception {
// import IIDM test case
String aCase = mvc.perform(multipart("/v1/cases")
.file(createMockMultipartFile("testCase.xiidm"))
.param("indexed", "true"))
.param("withIndexation", "true"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();

Expand All @@ -563,7 +547,7 @@ public void searchCaseTest() throws Exception {
// import CGMES french file
aCase = mvc.perform(multipart("/v1/cases")
.file(createMockMultipartFile("20200424T1330Z_2D_RTEFRANCE_001.zip"))
.param("indexed", "true"))
.param("withIndexation", "true"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();

Expand All @@ -580,7 +564,7 @@ public void searchCaseTest() throws Exception {
// import UCTE french file
aCase = mvc.perform(multipart("/v1/cases")
.file(createMockMultipartFile("20200103_0915_FO5_FR0.UCT"))
.param("indexed", "true"))
.param("withIndexation", "true"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();

Expand All @@ -597,7 +581,7 @@ public void searchCaseTest() throws Exception {
// import UCTE german file
aCase = mvc.perform(multipart("/v1/cases")
.file(createMockMultipartFile("20200103_0915_SN5_D80.UCT"))
.param("indexed", "true"))
.param("withIndexation", "true"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();

Expand All @@ -614,7 +598,7 @@ public void searchCaseTest() throws Exception {
// import UCTE swiss file
aCase = mvc.perform(multipart("/v1/cases")
.file(createMockMultipartFile("20200103_0915_135_CH2.UCT"))
.param("indexed", "true"))
.param("withIndexation", "true"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();

Expand Down
Loading

0 comments on commit 5524154

Please sign in to comment.