Skip to content

Commit

Permalink
add delete index
Browse files Browse the repository at this point in the history
  • Loading branch information
jamal-khey committed Jul 31, 2024
1 parent 36bfb9e commit d0e0ed7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
33 changes: 15 additions & 18 deletions src/main/java/com/powsybl/caseserver/SupervisionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

/**
* @author Jamal KHEYYAD <jamal.kheyyad at rte-international.com>
Expand All @@ -41,14 +38,6 @@ public SupervisionController(SupervisionService supervisionService, ClientConfig
this.caseInfosService = caseInfosService;
}

@PostMapping(value = "/cases/reindex-all")
@Operation(summary = "reindex all cases")
public ResponseEntity<Void> reindexAllCases() {
LOGGER.debug("reindex all cases request received");
supervisionService.reindexAllCases();
return ResponseEntity.ok().build();
}

@GetMapping(value = "/elasticsearch-host")
@Operation(summary = "get the elasticsearch address")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "the elasticsearch address")})
Expand All @@ -66,11 +55,19 @@ public ResponseEntity<String> getIndexedDirectoryElementsIndexName() {
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(caseInfosService.getDirectoryElementsIndexName());
}

// @GetMapping(value = "/elements/indexation-count")
// @Operation(summary = "get indexed directory elements count")
// @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Indexed directory elements count")})
// public ResponseEntity<String> getIndexedDirectoryElementsCount() {
// return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(Long.toString(caseInfosService.getIndexedDirectoryElementsCount()));
// }
@PostMapping(value = "/cases/reindex")
@Operation(summary = "reindex all cases")
public ResponseEntity<Void> reindexAllCases() {
LOGGER.debug("reindex all cases request received");
supervisionService.reindexAllCases();
return ResponseEntity.ok().build();
}

@DeleteMapping(value = "/elements/indexation")
@Operation(summary = "delete indexed elements")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "all indexed elements have been deleted")})
public ResponseEntity<String> deleteIndexedDirectoryElements() {
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(Long.toString(supervisionService.deleteIndexedDirectoryElements()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@

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;

/**
* @author Jamal KHEYYAD <jamal.kheyyad at rte-international.com>
*/
@Service
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) {
public SupervisionService(CaseInfosService caseInfosService, CaseService caseService, CaseInfosRepository caseInfosRepository) {
this.caseInfosService = caseInfosService;
this.caseService = caseService;
this.caseInfosRepository = caseInfosRepository;
}

public void reindexAllCases() {
Expand All @@ -35,4 +44,17 @@ public void reindexAllCases() {
caseInfosService.recreateAllCaseInfos(data);
}

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

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

private long getIndexedDirectoryElementsCount() {
return caseInfosRepository.count();
}
}

0 comments on commit d0e0ed7

Please sign in to comment.