Skip to content

Commit

Permalink
Merge branch 'main' into case_s3
Browse files Browse the repository at this point in the history
  • Loading branch information
jonenst committed Sep 17, 2024
2 parents bed9105 + 1b6db24 commit e98c4fb
Show file tree
Hide file tree
Showing 19 changed files with 583 additions and 114 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

<mockito-inline.version>3.11.1</mockito-inline.version>
<log4j2-mock-version>0.0.2</log4j2-mock-version>
<powsybl-ws-dependencies.version>2.11.0</powsybl-ws-dependencies.version>
<powsybl-ws-dependencies.version>2.12.0</powsybl-ws-dependencies.version>
<testcontainers.version>1.18.3</testcontainers.version>
</properties>

Expand Down
83 changes: 83 additions & 0 deletions src/main/java/com/powsybl/caseserver/SupervisionController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.caseserver;

import com.powsybl.caseserver.elasticsearch.CaseInfosService;
import com.powsybl.caseserver.server.CaseService;
import com.powsybl.caseserver.services.SupervisionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
* @author Jamal KHEYYAD <jamal.kheyyad at rte-international.com>
*/
@RestController
@RequestMapping(value = "/" + CaseConstants.API_VERSION + "/supervision")
@Tag(name = "case-server - Supervision")
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, CaseService caseService, ClientConfiguration elasticsearchClientConfiguration, CaseInfosService caseInfosService) {
this.supervisionService = supervisionService;
this.caseService = caseService;
this.elasticsearchClientConfiguration = elasticsearchClientConfiguration;
this.caseInfosService = caseInfosService;
}

@GetMapping(value = "/elasticsearch-host")
@Operation(summary = "get the elasticsearch address")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "the elasticsearch address")})
public ResponseEntity<String> getElasticsearchHost() {
String host = elasticsearchClientConfiguration.getEndpoints().get(0).getHostName()
+ ":"
+ elasticsearchClientConfiguration.getEndpoints().get(0).getPort();
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(host);
}

@GetMapping(value = "/cases/index-name")
@Operation(summary = "get the indexed cases index name")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Indexed directory cases index name")})
public ResponseEntity<String> getIndexedCasesFIndexName() {
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(caseInfosService.getDirectoryCasesIndexName());
}

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

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

@GetMapping(value = "/cases/indexation-count")
@Operation(summary = "get indexed cases count")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Indexed cases count")})
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 @@ -9,7 +9,9 @@
import co.elastic.clients.elasticsearch._types.query_dsl.QueryStringQuery;
import com.google.common.collect.Lists;
import com.powsybl.caseserver.dto.CaseInfos;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -42,6 +44,10 @@ public class CaseInfosService {
@Autowired
private ElasticsearchOperations operations;

@Value(ESConfig.CASE_INFOS_INDEX_NAME)
@Getter
private String directoryCasesIndexName;

public CaseInfos addCaseInfos(@NonNull final CaseInfos ci) {
caseInfosRepository.save(ci);
return ci;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
@EnableElasticsearchRepositories
public class ESConfig extends ElasticsearchConfiguration {

public static final String CASE_INFOS_INDEX_NAME = "#{@environment.getProperty('powsybl-ws.elasticsearch.index.prefix')}cases";

@Value("#{'${spring.data.elasticsearch.embedded:false}' ? 'localhost' : '${spring.data.elasticsearch.host}'}")
private String esHost;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ public class CaseMetadataEntity {

@Column(name = "expirationDate", columnDefinition = "timestamptz")
private Instant expirationDate;

@Column(name = "indexed", columnDefinition = "boolean default false", nullable = false)
private boolean indexed = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

/**
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
*/
@Repository
public interface CaseMetadataRepository extends JpaRepository<CaseMetadataEntity, UUID> {

List<CaseMetadataEntity> findAllByIndexedTrue();
}
53 changes: 22 additions & 31 deletions src/main/java/com/powsybl/caseserver/server/CaseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

import com.powsybl.caseserver.CaseConstants;
import com.powsybl.caseserver.dto.CaseInfos;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -38,8 +34,11 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import static com.powsybl.caseserver.CaseException.createDirectoryNotFound;

Expand Down Expand Up @@ -134,8 +133,8 @@ public ResponseEntity<byte[]> exportCase(
var headers = new HttpHeaders();
headers.setContentDisposition(
ContentDisposition.builder("attachment")
.filename(networkInfos.networkName())
.build()
.filename(networkInfos.networkName())
.build()
);
return ResponseEntity.ok()
.headers(headers)
Expand All @@ -152,28 +151,14 @@ public ResponseEntity<Boolean> exists(@PathVariable("caseUuid") UUID caseUuid) {

}

@GetMapping(value = "/cases/metadata")
@Operation(summary = "Get cases Metadata")
public ResponseEntity<List<CaseInfos>> getMetadata(@RequestParam("ids") List<UUID> ids) {
LOGGER.debug("get Cases metadata");
return ResponseEntity.ok().body(caseService.getMetadata(ids));
}

@GetMapping(value = "/cases/search")
@Operation(summary = "Search cases by metadata")
public ResponseEntity<List<CaseInfos>> searchCases(@RequestParam(value = "q") String query) {
LOGGER.debug("search cases request received");
List<CaseInfos> cases = caseService.searchCases(query);
return ResponseEntity.ok().body(cases);
}

@PostMapping(value = "/cases", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "import a case")
@SuppressWarnings("javasecurity:S5145")
public ResponseEntity<UUID> importCase(@RequestParam("file") MultipartFile file,
@RequestParam(value = "withExpiration", required = false, defaultValue = "false") boolean withExpiration) {
@RequestParam(value = "withExpiration", required = false, defaultValue = "false") boolean withExpiration,
@RequestParam(value = "withIndexation", required = false, defaultValue = "false") boolean withIndexation) {
LOGGER.debug("importCase request received with file = {}", file.getName());
UUID caseUuid = caseService.importCase(file, withExpiration);
UUID caseUuid = caseService.importCase(file, withExpiration, withIndexation);
return ResponseEntity.ok().body(caseUuid);
}

Expand All @@ -190,14 +175,6 @@ public ResponseEntity<UUID> duplicateCase(
return ResponseEntity.ok().body(newCaseUuid);
}

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

@PutMapping(value = "/cases/{caseUuid}/disableExpiration")
@Operation(summary = "disable the case expiration")
@ApiResponses(value = {
Expand Down Expand Up @@ -228,4 +205,18 @@ public ResponseEntity<Void> deleteCases() {
return ResponseEntity.ok().build();
}

@GetMapping(value = "/cases/search")
@Operation(summary = "Search cases by metadata")
public ResponseEntity<List<CaseInfos>> searchCases(@RequestParam(value = "q") String query) {
LOGGER.debug("search cases request received");
List<CaseInfos> cases = caseService.searchCases(query);
return ResponseEntity.ok().body(cases);
}

@GetMapping(value = "/cases/metadata")
@Operation(summary = "Get cases Metadata")
public ResponseEntity<List<CaseInfos>> getMetadata(@RequestParam("ids") List<UUID> ids) {
LOGGER.debug("get Cases metadata");
return ResponseEntity.ok().body(caseService.getMetadata(ids));
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/powsybl/caseserver/server/CaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ default CaseInfos createInfos(String fileBaseName, UUID caseUuid, String format)
return CaseInfos.builder().name(fileBaseName).uuid(caseUuid).format(format).build();
}

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

default Importer getImporterOrThrowsException(Path caseFile, ComputationManager computationManager) {
Expand Down Expand Up @@ -114,6 +114,8 @@ default Optional<ExportCaseInfos> exportCase(UUID caseUuid, String format, Strin
}
}

List<CaseInfos> getCasesToReindex();

List<CaseInfos> getCases();

boolean caseExists(UUID caseUuid);
Expand All @@ -128,7 +130,7 @@ default Optional<ExportCaseInfos> exportCase(UUID caseUuid, String format, Strin

Optional<byte[]> getCaseBytes(UUID caseUuid);

UUID importCase(MultipartFile file, boolean withExpiration);
UUID importCase(MultipartFile file, boolean withExpiration, boolean withIndexation);

UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration);

Expand All @@ -140,8 +142,6 @@ default Optional<ExportCaseInfos> exportCase(UUID caseUuid, String format, Strin

List<CaseInfos> searchCases(String query);

void reindexAllCases();

List<CaseInfos> getMetadata(List<UUID> ids);

void setComputationManager(ComputationManager mock);
Expand Down
Loading

0 comments on commit e98c4fb

Please sign in to comment.