Skip to content

Commit

Permalink
handle the get count filters ids (#54)
Browse files Browse the repository at this point in the history
add and handle the sensitivity analysis factors count
Signed-off-by: Ghazwa REHILI [email protected]
  • Loading branch information
ghazwarhili authored Dec 14, 2023
1 parent 2cb0e40 commit 697d341
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.web.bind.annotation.*;

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

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
Expand Down Expand Up @@ -97,12 +98,23 @@ public ResponseEntity<UUID> runAndSave(@Parameter(description = "Network UUID")
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(resultUuid);
}

@PostMapping(value = "/networks/{networkUuid}/factors-count", produces = APPLICATION_JSON_VALUE)
@Operation(summary = "Get factors count")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The sensitivity analysis factors count"),
@ApiResponse(responseCode = "404", description = "Filters or contingencies has not been found")})
public ResponseEntity<Long> getFactorsCount(@Parameter(description = "Network UUID") @PathVariable("networkUuid") UUID networkUuid,
@Parameter(description = "Variant Id") @RequestParam(name = "variantId", required = false) String variantId,
@Parameter(description = "Is Injections Set") @RequestParam(name = "isInjectionsSet", required = false) Boolean isInjectionsSet,
@RequestBody Map<String, List<UUID>> ids) {
return ResponseEntity.ok().body(workerService.getFactorsCount(ids, networkUuid, variantId, isInjectionsSet));
}

@GetMapping(value = "/results/{resultUuid}", produces = APPLICATION_JSON_VALUE)
@Operation(summary = "Get a sensitivity analysis result from the database")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The sensitivity analysis result"),
@ApiResponse(responseCode = "404", description = "Sensitivity analysis result has not been found")})
public ResponseEntity<SensitivityRunQueryResult> getResult(@Parameter(description = "Result UUID")
@PathVariable("resultUuid") UUID resultUuid,
@PathVariable("resultUuid") UUID resultUuid,
@RequestParam(name = "selector", required = false) String selectorJson) {

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

Expand All @@ -30,6 +34,7 @@ public class FilterService {
private static final String DELIMITER = "/";
private final RestTemplate restTemplate = new RestTemplate();
private String filterServerBaseUri;
public static final String NETWORK_UUID = "networkUuid";
private static final String QUERY_PARAM_VARIANT_ID = "variantId";

@Autowired
Expand All @@ -43,7 +48,7 @@ public List<IdentifiableAttributes> getIdentifiablesFromFilter(UUID uuid, UUID n

var uriComponentsBuilder = UriComponentsBuilder
.fromPath(DELIMITER + FILTER_API_VERSION + "/filters/{id}/export")
.queryParam("networkUuid", networkUuid.toString());
.queryParam(NETWORK_UUID, networkUuid.toString());
if (!StringUtils.isBlank(variantId)) {
uriComponentsBuilder.queryParam(QUERY_PARAM_VARIANT_ID, variantId);
}
Expand All @@ -53,4 +58,25 @@ public List<IdentifiableAttributes> getIdentifiablesFromFilter(UUID uuid, UUID n
new ParameterizedTypeReference<List<IdentifiableAttributes>>() {
}).getBody();
}

public Map<String, List<Long>> getIdentifiablesCount(Map<String, List<UUID>> ids, UUID networkUuid, String variantId) {
var uriComponentsBuilder = UriComponentsBuilder
.fromPath(DELIMITER + FILTER_API_VERSION + "/filters/identifiables-count")
.queryParam(NETWORK_UUID, networkUuid);
if (!StringUtils.isBlank(variantId)) {
uriComponentsBuilder.queryParam(QUERY_PARAM_VARIANT_ID, variantId);
}
var path = uriComponentsBuilder
.build()
.toUriString();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<Map<String, List<UUID>>> httpEntity = new HttpEntity<>(ids, headers);

return restTemplate.exchange(filterServerBaseUri + path, HttpMethod.POST, httpEntity,
new ParameterizedTypeReference<Map<String, List<Long>>>() {
}).getBody();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
public class SensitivityAnalysisInputBuilderService {
private static final String EXPECTED_TYPE = "expectedType";
private static final Logger LOGGER = LoggerFactory.getLogger(SensitivityAnalysisInputBuilderService.class);
public static final String INJECTIONS = "injections";
public static final String CONTINGENCIES = "contingencies";
private final ActionsService actionsService;
private final FilterService filterService;

Expand Down Expand Up @@ -113,6 +115,32 @@ private List<IdentifiableAttributes> goGetIdentifiables(EquipmentsContainer filt
}
}

private Integer getContingenciesCount(List<UUID> ids, UUID networkUuid, String variantId) {
return ids.stream()
.mapToInt(uuid -> actionsService.getContingencyList(uuid, networkUuid, variantId).size())
.sum();
}

private Long getFactorsCount(Map<String, List<UUID>> ids, UUID networkUuid, String variantId, Long containersAttributesCount) {
Long contAttributesCountTemp = containersAttributesCount;
if (ids.containsKey(CONTINGENCIES) && !ids.get(CONTINGENCIES).isEmpty()) {
int sumContingencyListSizes = getContingenciesCount(ids.get(CONTINGENCIES), networkUuid, variantId);
sumContingencyListSizes = Math.max(sumContingencyListSizes, 1);
contAttributesCountTemp *= sumContingencyListSizes;
ids.remove(CONTINGENCIES);
}
ids.entrySet().removeIf(entry -> Objects.isNull(entry.getValue()));
Map<String, List<Long>> map = filterService.getIdentifiablesCount(ids, networkUuid, null);
for (List<Long> valueList : map.values()) {
int sensiFactorCount = valueList.stream().mapToInt(Long::intValue).sum();
if (sensiFactorCount != 0) {
contAttributesCountTemp *= sensiFactorCount;
}
}

return contAttributesCountTemp;
}

private Stream<IdentifiableAttributes> getIdentifiablesFromContainer(SensitivityAnalysisRunContext context, EquipmentsContainer filter,
List<IdentifiableType> equipmentsTypesAllowed, Reporter reporter) {

Expand Down Expand Up @@ -430,6 +458,16 @@ private void buildSensitivityNodes(SensitivityAnalysisRunContext context, Networ
});
}

public Long getFactorsCount(Map<String, List<UUID>> ids, UUID networkUuid, String variantId, Boolean isInjectionsSet) {
Long containersAttributesCount = 1L;
if (Boolean.TRUE.equals(isInjectionsSet)) {
containersAttributesCount *= ids.get(INJECTIONS).size();
ids.remove(INJECTIONS);
}
containersAttributesCount = getFactorsCount(ids, networkUuid, variantId, containersAttributesCount);
return containersAttributesCount;
}

public void build(SensitivityAnalysisRunContext context, Network network, Reporter reporter) {
try {
buildSensitivityInjectionsSets(context, network, reporter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,8 @@ public Consumer<Message<String>> consumeRun() {
public Consumer<Message<String>> consumeCancel() {
return message -> cancelSensitivityAnalysisAsync(SensitivityAnalysisCancelContext.fromMessage(message));
}

public Long getFactorsCount(Map<String, List<UUID>> ids, UUID networkUuid, String variantId, Boolean isInjectionsSet) {
return sensitivityAnalysisInputBuilderService.getFactorsCount(ids, networkUuid, variantId, isInjectionsSet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ public class SensitivityAnalysisControllerTest {
private static final UUID MONITORED_VOLTAGE_LEVELS_FILTERS_NODES_UUID = UUID.randomUUID();
private static final UUID EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID = UUID.randomUUID();
private static final UUID CONTINGENCIES_NODES_UUID = UUID.randomUUID();
private static final List<UUID> MONITORED_BRANCHES_FILTERS_UUID = List.of(UUID.randomUUID());
private static final List<UUID> INJECTIONS_FILTERS_UUID = List.of(UUID.randomUUID());
private static final List<UUID> CONTINGENCIES_FILTERS_UUID = List.of(UUID.randomUUID());
public static final String MONITORED_BRANCHS_KEY = "monitoredBranchs";
public static final String INJECTIONS_KEY = "injections";
public static final String CONTINGENCIES_KEY = "contingencies";
private static final Map<String, List<UUID>> IDS = Map.of(MONITORED_BRANCHS_KEY, MONITORED_BRANCHES_FILTERS_UUID, INJECTIONS_KEY, INJECTIONS_FILTERS_UUID, CONTINGENCIES_KEY, CONTINGENCIES_FILTERS_UUID);
private static final Map<String, List<UUID>> IDS_1 = Map.of(MONITORED_BRANCHS_KEY, MONITORED_BRANCHES_FILTERS_UUID, INJECTIONS_KEY, INJECTIONS_FILTERS_UUID);

private static final List<Contingency> CONTINGENCIES = List.of(
new Contingency("l1", new BranchContingency("l1")),
Expand Down Expand Up @@ -402,6 +410,7 @@ public void setUp() {
given(actionsService.getContingencyList(CONTINGENCIES_NODES_UUID, NETWORK_UUID, VARIANT_1_ID)).willReturn(CONTINGENCIES);
given(actionsService.getContingencyList(CONTINGENCIES_NODES_UUID, NETWORK_UUID, VARIANT_3_ID)).willReturn(CONTINGENCIES_VARIANT);
given(actionsService.getContingencyList(CONTINGENCIES_NODES_UUID, NETWORK_UUID, VARIANT_2_ID)).willReturn(CONTINGENCIES);
given(actionsService.getContingencyList(CONTINGENCIES_FILTERS_UUID.get(0), NETWORK_UUID, VARIANT_1_ID)).willReturn(CONTINGENCIES_VARIANT);

// filter service mocking
given(filterService.getIdentifiablesFromFilter(MONITORED_BRANCHES_FILTERS_INJECTIONS_SET_UUID, NETWORK_UUID, VARIANT_1_ID)).willReturn(BRANCHES);
Expand Down Expand Up @@ -465,7 +474,7 @@ public void setUp() {
given(filterService.getIdentifiablesFromFilter(EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID, NETWORK_UUID, VARIANT_2_ID)).willReturn(EQUIPMENTS_IN_VOLTAGE_REGULATION);
given(filterService.getIdentifiablesFromFilter(EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID, NETWORK_UUID, null)).willReturn(EQUIPMENTS_IN_VOLTAGE_REGULATION);
given(filterService.getIdentifiablesFromFilter(EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID, NETWORK_STOP_UUID, VARIANT_2_ID)).willReturn(EQUIPMENTS_IN_VOLTAGE_REGULATION);

given(filterService.getIdentifiablesCount(IDS_1, NETWORK_UUID, null)).willReturn(Map.<String, List<Long>>of(MONITORED_BRANCHS_KEY, List.<Long>of(6L), INJECTIONS_KEY, List.<Long>of(6L)));
// report service mocking
doAnswer(i -> null).when(reportService).sendReport(any(), any());

Expand Down Expand Up @@ -743,6 +752,16 @@ public void testStatus() {
assertEquals(SensitivityAnalysisStatus.NOT_DONE.name(), result.getResponse().getContentAsString());
}

@Test
public void testGetFactorsCount() throws Exception {
MvcResult result = mockMvc.perform(post("/" + VERSION + "/networks/{networkUuid}/factors-count?variantId={variantId}", NETWORK_UUID, VARIANT_1_ID)
.contentType(MediaType.APPLICATION_JSON).content(mapper.writeValueAsString(IDS)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
assertEquals("72", result.getResponse().getContentAsString());
}

@Test
public void stopTest() throws Exception {
mockMvc.perform(post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.gridsuite.sensitivityanalysis.server.dto.IdentifiableAttributes;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -27,6 +28,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
Expand All @@ -50,6 +52,8 @@ public class FilterServiceTest {

private static final UUID LIST_UUID = UUID.randomUUID();

private static final Map<String, List<UUID>> IDENTIFIABLES_UUID = Map.of("0", List.of(LIST_UUID), "1", List.of(LIST_UUID), "2", List.of(LIST_UUID));

private static final UUID VERY_LARGE_LIST_UUID = UUID.randomUUID();

private static final IdentifiableAttributes IDENTIFIABLE = new IdentifiableAttributes("gen1", IdentifiableType.GENERATOR, null);
Expand Down Expand Up @@ -84,6 +88,7 @@ private String initMockWebServer() throws IOException {
String jsonExpected = objectMapper.writeValueAsString(List.of(IDENTIFIABLE));
String veryLargeJsonExpected = objectMapper.writeValueAsString(createVeryLargeList());
String jsonVariantExpected = objectMapper.writeValueAsString(List.of(IDENTIFIABLE_VARIANT));
String jsonIdentifiablesExpected = objectMapper.writeValueAsString(countResultMap());

final Dispatcher dispatcher = new Dispatcher() {
@Override
Expand All @@ -102,6 +107,11 @@ public MockResponse dispatch(RecordedRequest request) {
return new MockResponse().setResponseCode(HttpStatus.OK.value())
.setBody(veryLargeJsonExpected)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (requestPath.equals(String.format("/v1/filters/identifiables-count?networkUuid=%s&variantId=%s", NETWORK_UUID, VARIANT_ID))
|| requestPath.equals(String.format("/v1/filters/identifiables-count?networkUuid=%s", NETWORK_UUID))) {
return new MockResponse().setResponseCode(HttpStatus.OK.value())
.setBody(jsonIdentifiablesExpected)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else {
return new MockResponse().setResponseCode(HttpStatus.NOT_FOUND.value()).setBody("Path not supported: " + request.getPath());
}
Expand All @@ -115,6 +125,11 @@ public MockResponse dispatch(RecordedRequest request) {
return baseHttpUrl.toString().substring(0, baseHttpUrl.toString().length() - 1);
}

@NotNull
private static Map<String, List<Integer>> countResultMap() {
return Map.of("0", List.of(6), "1", List.of(6), "2", List.of(6));
}

private List<IdentifiableAttributes> createVeryLargeList() {
return IntStream.range(0, DATA_BUFFER_LIMIT).mapToObj(i -> new IdentifiableAttributes("l" + i, IdentifiableType.GENERATOR, null)).collect(Collectors.toList());
}
Expand All @@ -137,4 +152,13 @@ public void testVeryLargeList() {
list = filterService.getIdentifiablesFromFilter(VERY_LARGE_LIST_UUID, UUID.fromString(NETWORK_UUID), VARIANT_ID);
assertEquals(objectMapper.writeValueAsString(createVeryLargeList()), objectMapper.writeValueAsString(list));
}

@SneakyThrows
@Test
public void testGetFactorsCount() {
Map<String, List<Long>> list = filterService.getIdentifiablesCount(IDENTIFIABLES_UUID, UUID.fromString(NETWORK_UUID), null);
assertEquals(objectMapper.writeValueAsString(countResultMap()), objectMapper.writeValueAsString(list));
list = filterService.getIdentifiablesCount(IDENTIFIABLES_UUID, UUID.fromString(NETWORK_UUID), VARIANT_ID);
assertEquals(objectMapper.writeValueAsString(countResultMap()), objectMapper.writeValueAsString(list));
}
}

0 comments on commit 697d341

Please sign in to comment.