Skip to content

Commit

Permalink
handle complexity endpoint for filter (#81)
Browse files Browse the repository at this point in the history
return the size of identifiables for a list of filters
Signed-off-by: Ghazwa REHILI <[email protected]>
  • Loading branch information
ghazwarhili authored Dec 14, 2023
1 parent 6ddd2ab commit d108519
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/gridsuite/filter/server/FilterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.persistence.EntityNotFoundException;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
Expand Down Expand Up @@ -146,6 +147,18 @@ public ResponseEntity<List<IdentifiableAttributes>> exportFilter(@PathVariable("
.orElse(ResponseEntity.notFound().build());
}

@PostMapping(value = "/filters/identifiables-count", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Calculate the total of identifiables for a list of filters")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Identifiables count")})
public ResponseEntity<Map<String, List<Long>>> getIdentifiablesCount(@RequestBody Map<String, List<UUID>> ids,
@RequestParam(value = "networkUuid") UUID networkUuid,
@RequestParam(value = "variantId", required = false) String variantId) {
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(service.getIdentifiablesCount(ids, networkUuid, variantId));

}

@GetMapping(value = "/filters/export", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Export list of filters to JSON format")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The filters on JSON format")})
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/gridsuite/filter/server/FilterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,13 @@ public Optional<List<IdentifiableAttributes>> exportFilter(UUID id, UUID network
return getFilter(id).map(filter -> getIdentifiableAttributes(filter, networkUuid, variantId));
}

public Map<String, List<Long>> getIdentifiablesCount(Map<String, List<UUID>> ids, UUID networkUuid, String variantId) {
Objects.requireNonNull(ids);
return ids.entrySet().stream()
.map(entry -> Map.entry(entry.getKey(), getFilters(entry.getValue()).stream().map(f -> (long) getIdentifiableAttributes(f, networkUuid, variantId).size()).toList()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

public List<FilterEquipments> exportFilters(List<UUID> ids, UUID networkUuid, String variantId) {

// we stream on the ids so that we can keep the same order of ids sent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,76 @@ public void testExportFilters() throws Exception {

}

@Test
public void testGetIdentifiablesCount() throws Exception {
UUID filterId1 = UUID.fromString("c88c9510-15b4-468d-89c3-c1c6277966c3");
UUID filterId2 = UUID.fromString("1110b06b-9b81-4d31-ac21-450628cd34ff");
UUID filterId3 = UUID.fromString("f631034b-ba7c-4bb8-9d61-258a871e9265");
UUID filterId4 = UUID.fromString("77614d91-c168-4f89-8fb9-77a23729e88e");

LineFilter lineFilter1 = LineFilter.builder().equipmentID("NHV1_NHV2_1").substationName1("P1").substationName2("P2")
.countries1(new TreeSet<>(Set.of("FR"))).countries2(new TreeSet<>(Set.of("FR")))
.nominalVoltage1(new NumericalFilter(RangeType.RANGE, 360., 400.)).nominalVoltage2(new NumericalFilter(RangeType.RANGE, 356.25, 393.75)).build();
CriteriaFilter lineCriteriaFilter1 = new CriteriaFilter(
filterId1,
new Date(),
lineFilter1
);
insertFilter(filterId1, lineCriteriaFilter1);
checkFormFilter(filterId1, lineCriteriaFilter1);

LineFilter lineFilter2 = LineFilter.builder().equipmentID("NHV1_NHV2_2").substationName1("P1").substationName2("P2")
.countries1(new TreeSet<>(Set.of("FR"))).countries2(new TreeSet<>(Set.of("FR")))
.nominalVoltage1(new NumericalFilter(RangeType.RANGE, 360., 400.)).nominalVoltage2(new NumericalFilter(RangeType.RANGE, 356.25, 393.75)).build();
CriteriaFilter lineCriteriaFilter2 = new CriteriaFilter(
filterId2,
new Date(),
lineFilter2
);
insertFilter(filterId2, lineCriteriaFilter2);
checkFormFilter(filterId2, lineCriteriaFilter2);

Date modificationDate = new Date();
CriteriaFilter hvdcLineFilter = new CriteriaFilter(
filterId3,
modificationDate,
HvdcLineFilter.builder().equipmentID("NHV1_NHV2_3").equipmentName("equipmentName_3")
.substationName1("substationName1").substationName2("substationName2")
.countries1(new TreeSet<>(Set.of("FR", "BE"))).countries2(new TreeSet<>(Set.of("FR", "IT")))
.freeProperties2(Map.of("region", List.of("north")))
.nominalVoltage(new NumericalFilter(RangeType.RANGE, 380., 420.))
.build()
);
insertFilter(filterId3, hvdcLineFilter);
checkFormFilter(filterId3, hvdcLineFilter);

CriteriaFilter substationFilter = new CriteriaFilter(
filterId4,
new Date(),
new SubstationFilter("NHV1_NHV2_4", "equipmentName_4", new TreeSet<>(Set.of("FR", "BE")), null)
);

MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("networkUuid", NETWORK_UUID.toString());
params.add("variantId", VARIANT_ID_1);
final var json = TestUtils.resourceToString("/json/identifiables.json");
Map<String, List<Integer>> filtersComplexityCount = objectMapper.readValue(
mvc.perform(post("/" + FilterApi.API_VERSION + "/filters/identifiables-count")
.params(params)
.content(json)
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(),
new TypeReference<>() {
});
assertEquals(1, filtersComplexityCount.get("0").size());
assertEquals(1, filtersComplexityCount.get("1").size());
assertEquals(1, filtersComplexityCount.get("2").size());
assertEquals(0, filtersComplexityCount.get("3").size());

assertEquals(4, filtersComplexityCount.size());
}

private void checkFilterEquipments(List<FilterEquipments> filterEquipments1, List<FilterEquipments> filterEquipments2) {
assertEquals(CollectionUtils.isEmpty(filterEquipments1), CollectionUtils.isEmpty(filterEquipments2));
assertEquals(filterEquipments1.size(), filterEquipments2.size());
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/org/gridsuite/filter/server/utils/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2022, 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 org.gridsuite.filter.server.utils;

import com.google.common.io.ByteStreams;
import org.junit.platform.commons.util.StringUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
* @author Rehili Ghazoua <[email protected]>
*/
public final class TestUtils {

private static final long TIMEOUT = 100;

private TestUtils() {
}

public static String resourceToString(String resource) throws IOException {
String content = new String(ByteStreams.toByteArray(TestUtils.class.getResourceAsStream(resource)), StandardCharsets.UTF_8);
return StringUtils.replaceWhitespaceCharacters(content, "");
}
}
14 changes: 14 additions & 0 deletions src/test/resources/json/identifiables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"0": [
"c88c9510-15b4-468d-89c3-c1c6277966c3"
],
"1": [
"1110b06b-9b81-4d31-ac21-450628cd34ff"
],
"2": [
"f631034b-ba7c-4bb8-9d61-258a871e9265"
],
"3": [
"77614d91-c168-4f89-8fb9-77a23729e88e"
]
}

0 comments on commit d108519

Please sign in to comment.