Skip to content

Commit 8642a3a

Browse files
Merge branch 'main' into move_parameters
2 parents 22c947c + 2c9804b commit 8642a3a

37 files changed

+961
-593
lines changed

pom.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@
141141
<groupId>org.springdoc</groupId>
142142
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
143143
</dependency>
144+
<dependency>
145+
<groupId>org.springframework.boot</groupId>
146+
<artifactId>spring-boot-starter-actuator</artifactId>
147+
</dependency>
148+
149+
<!-- Runtime dependencies -->
144150
<dependency>
145151
<groupId>com.powsybl</groupId>
146152
<artifactId>powsybl-config-classic</artifactId>
@@ -171,10 +177,6 @@
171177
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
172178
<scope>runtime</scope>
173179
</dependency>
174-
<dependency>
175-
<groupId>org.springframework.boot</groupId>
176-
<artifactId>spring-boot-starter-actuator</artifactId>
177-
</dependency>
178180

179181
<!-- Test dependencies -->
180182
<dependency>

src/main/java/org/gridsuite/shortcircuit/server/ShortCircuitApplication.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* @author Etienne Homer <etienne.homer at rte-france.com>
1515
*/
1616
@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
17-
@SpringBootApplication(scanBasePackageClasses = {ShortCircuitApplication.class, NetworkStoreService.class})
17+
@SpringBootApplication(scanBasePackageClasses = { ShortCircuitApplication.class, NetworkStoreService.class })
1818
public class ShortCircuitApplication {
19-
2019
public static void main(String[] args) {
2120
SpringApplication.run(ShortCircuitApplication.class, args);
2221
}

src/main/java/org/gridsuite/shortcircuit/server/ShortCircuitController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.util.UUID;
2828

2929
import static com.powsybl.shortcircuit.Fault.FaultType;
30-
import static org.gridsuite.shortcircuit.server.service.NotificationService.HEADER_USER_ID;
30+
import static org.gridsuite.shortcircuit.server.computation.service.NotificationService.HEADER_USER_ID;
3131
import static org.springframework.http.MediaType.*;
3232

3333
/**
@@ -136,15 +136,15 @@ public ResponseEntity<Void> deleteResults() {
136136
@Operation(summary = "Get the short circuit analysis status from the database")
137137
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The short circuit analysis status")})
138138
public ResponseEntity<String> getStatus(@Parameter(description = "Result UUID") @PathVariable("resultUuid") UUID resultUuid) {
139-
String result = shortCircuitService.getStatus(resultUuid);
140-
return ResponseEntity.ok().body(result);
139+
ShortCircuitAnalysisStatus result = shortCircuitService.getStatus(resultUuid);
140+
return ResponseEntity.ok().body(result != null ? result.name() : null);
141141
}
142142

143143
@PutMapping(value = "/results/invalidate-status", produces = APPLICATION_JSON_VALUE)
144144
@Operation(summary = "Invalidate the short circuit analysis status from the database")
145145
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The short circuit analysis status has been invalidated")})
146146
public ResponseEntity<Void> invalidateStatus(@Parameter(description = "Result uuids") @RequestParam(name = "resultUuid") List<UUID> resultUuids) {
147-
shortCircuitService.setStatus(resultUuids, ShortCircuitAnalysisStatus.NOT_DONE.name());
147+
shortCircuitService.setStatus(resultUuids, ShortCircuitAnalysisStatus.NOT_DONE);
148148
return ResponseEntity.ok().build();
149149
}
150150

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.shortcircuit.server.computation.dto;
8+
9+
import io.swagger.v3.oas.annotations.media.Schema;
10+
import lombok.Builder;
11+
12+
import java.util.UUID;
13+
14+
/**
15+
* @author Florent MILLOT <florent.millot at rte-france.com>
16+
*/
17+
@Builder
18+
@Schema(description = "Report infos")
19+
public record ReportInfos(
20+
UUID reportUuid,
21+
String reporterId,
22+
String computationType
23+
) {
24+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.shortcircuit.server.computation.service;
8+
9+
import io.micrometer.core.instrument.Counter;
10+
import io.micrometer.core.instrument.MeterRegistry;
11+
import io.micrometer.observation.Observation;
12+
import io.micrometer.observation.ObservationRegistry;
13+
import lombok.NonNull;
14+
15+
/**
16+
* @author Mathieu Deharbe <mathieu.deharbe at rte-france.com>
17+
* @param <R> powsybl Result class specific to the computation
18+
* @param <P> powsybl and gridsuite parameters specifics to the computation
19+
*/
20+
public abstract class AbstractComputationObserver<R, P> {
21+
protected static final String OBSERVATION_PREFIX = "app.computation.";
22+
protected static final String PROVIDER_TAG_NAME = "provider";
23+
protected static final String TYPE_TAG_NAME = "type";
24+
protected static final String STATUS_TAG_NAME = "status";
25+
protected static final String COMPUTATION_COUNTER_NAME = OBSERVATION_PREFIX + "count";
26+
protected final ObservationRegistry observationRegistry;
27+
protected final MeterRegistry meterRegistry;
28+
29+
protected AbstractComputationObserver(@NonNull ObservationRegistry observationRegistry, @NonNull MeterRegistry meterRegistry) {
30+
this.observationRegistry = observationRegistry;
31+
this.meterRegistry = meterRegistry;
32+
}
33+
34+
protected abstract String getComputationType();
35+
36+
protected Observation createObservation(String name, AbstractComputationRunContext<P> runContext) {
37+
Observation observation = Observation.createNotStarted(OBSERVATION_PREFIX + name, observationRegistry)
38+
.lowCardinalityKeyValue(TYPE_TAG_NAME, getComputationType());
39+
if (runContext.getProvider() != null) {
40+
observation.lowCardinalityKeyValue(PROVIDER_TAG_NAME, runContext.getProvider());
41+
}
42+
return observation;
43+
}
44+
45+
public <E extends Throwable> void observe(String name, AbstractComputationRunContext<P> runContext, Observation.CheckedRunnable<E> callable) throws E {
46+
createObservation(name, runContext).observeChecked(callable);
47+
}
48+
49+
public <T, E extends Throwable> T observe(String name, AbstractComputationRunContext<P> runContext, Observation.CheckedCallable<T, E> callable) throws E {
50+
return createObservation(name, runContext).observeChecked(callable);
51+
}
52+
53+
public <T extends R, E extends Throwable> T observeRun(
54+
String name, AbstractComputationRunContext<P> runContext, Observation.CheckedCallable<T, E> callable) throws E {
55+
T result = createObservation(name, runContext).observeChecked(callable);
56+
incrementCount(runContext, result);
57+
return result;
58+
}
59+
60+
private void incrementCount(AbstractComputationRunContext<P> runContext, R result) {
61+
Counter.Builder builder =
62+
Counter.builder(COMPUTATION_COUNTER_NAME);
63+
if (runContext.getProvider() != null) {
64+
builder.tag(PROVIDER_TAG_NAME, runContext.getProvider());
65+
}
66+
builder.tag(TYPE_TAG_NAME, getComputationType())
67+
.tag(STATUS_TAG_NAME, getResultStatus(result))
68+
.register(meterRegistry)
69+
.increment();
70+
}
71+
72+
protected abstract String getResultStatus(R res);
73+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.shortcircuit.server.computation.service;
8+
9+
import java.util.List;
10+
import java.util.UUID;
11+
12+
/**
13+
* @author Mathieu Deharbe <mathieu.deharbe at rte-france.com>
14+
* @param <S> status specific to the computation
15+
*/
16+
public abstract class AbstractComputationResultService<S> {
17+
18+
public abstract void insertStatus(List<UUID> resultUuids, S status);
19+
20+
public abstract void delete(UUID resultUuid);
21+
22+
public abstract void deleteAll();
23+
24+
public abstract S findStatus(UUID resultUuid);
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.shortcircuit.server.computation.service;
8+
9+
import com.powsybl.commons.report.ReportNode;
10+
import lombok.AllArgsConstructor;
11+
import lombok.Getter;
12+
import lombok.Setter;
13+
import org.gridsuite.shortcircuit.server.computation.dto.ReportInfos;
14+
15+
import java.util.UUID;
16+
17+
/**
18+
* @author Mathieu Deharbe <mathieu.deharbe at rte-france.com>
19+
* @param <P> parameters structure specific to the computation
20+
*/
21+
@Getter
22+
@AllArgsConstructor
23+
public abstract class AbstractComputationRunContext<P> {
24+
private final UUID networkUuid;
25+
private final String variantId;
26+
private final String receiver;
27+
private final ReportInfos reportInfos;
28+
private final String userId;
29+
@Setter protected String provider;
30+
@Setter protected P parameters;
31+
@Setter ReportNode reportNode;
32+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.shortcircuit.server.computation.service;
8+
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import lombok.Getter;
11+
import org.springframework.util.CollectionUtils;
12+
13+
import java.util.List;
14+
import java.util.Objects;
15+
import java.util.UUID;
16+
17+
/**
18+
* @author Mathieu Deharbe <mathieu.deharbe at rte-france.com>
19+
* @param <R> run context specific to a computation, including parameters
20+
* @param <T> run service specific to a computation
21+
* @param <S> enum status specific to a computation
22+
*/
23+
public abstract class AbstractComputationService<R extends AbstractComputationRunContext<?>, T extends AbstractComputationResultService<S>, S> {
24+
25+
protected ObjectMapper objectMapper;
26+
protected NotificationService notificationService;
27+
@Getter
28+
protected String defaultProvider;
29+
30+
protected UuidGeneratorService uuidGeneratorService;
31+
protected T resultService;
32+
33+
protected AbstractComputationService(NotificationService notificationService,
34+
T resultService,
35+
ObjectMapper objectMapper,
36+
UuidGeneratorService uuidGeneratorService,
37+
String defaultProvider) {
38+
this.notificationService = Objects.requireNonNull(notificationService);
39+
this.objectMapper = objectMapper;
40+
this.uuidGeneratorService = Objects.requireNonNull(uuidGeneratorService);
41+
this.defaultProvider = defaultProvider;
42+
this.resultService = Objects.requireNonNull(resultService);
43+
}
44+
45+
public void stop(UUID resultUuid, String receiver) {
46+
notificationService.sendCancelMessage(new CancelContext(resultUuid, receiver).toMessage());
47+
}
48+
49+
public abstract List<String> getProviders();
50+
51+
public abstract UUID runAndSaveResult(R runContext);
52+
53+
public void deleteResult(UUID resultUuid) {
54+
resultService.delete(resultUuid);
55+
}
56+
57+
public void deleteResults(List<UUID> resultUuids) {
58+
if (!CollectionUtils.isEmpty(resultUuids)) {
59+
resultUuids.forEach(resultService::delete);
60+
} else {
61+
deleteResults();
62+
}
63+
}
64+
65+
public void deleteResults() {
66+
resultService.deleteAll();
67+
}
68+
69+
public void setStatus(List<UUID> resultUuids, S status) {
70+
resultService.insertStatus(resultUuids, status);
71+
}
72+
73+
public S getStatus(UUID resultUuid) {
74+
return resultService.findStatus(resultUuid);
75+
}
76+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.shortcircuit.server.computation.service;
8+
9+
import com.fasterxml.jackson.core.JsonProcessingException;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import lombok.Getter;
12+
import org.springframework.messaging.Message;
13+
import org.springframework.messaging.support.MessageBuilder;
14+
15+
import java.io.UncheckedIOException;
16+
import java.util.Map;
17+
import java.util.Objects;
18+
import java.util.UUID;
19+
20+
import static org.gridsuite.shortcircuit.server.computation.service.NotificationService.*;
21+
22+
/**
23+
* @author Mathieu Deharbe <mathieu.deharbe at rte-france.com>
24+
* @param <R> run context specific to a computation, including parameters
25+
*/
26+
@Getter
27+
public abstract class AbstractResultContext<R extends AbstractComputationRunContext<?>> {
28+
29+
protected static final String RESULT_UUID_HEADER = "resultUuid";
30+
31+
protected static final String NETWORK_UUID_HEADER = "networkUuid";
32+
33+
protected static final String REPORT_UUID_HEADER = "reportUuid";
34+
35+
public static final String VARIANT_ID_HEADER = "variantId";
36+
37+
public static final String REPORTER_ID_HEADER = "reporterId";
38+
39+
public static final String REPORT_TYPE_HEADER = "reportType";
40+
41+
protected static final String MESSAGE_ROOT_NAME = "parameters";
42+
43+
protected final UUID resultUuid;
44+
45+
protected final R runContext;
46+
47+
protected AbstractResultContext(UUID resultUuid, R runContext) {
48+
this.resultUuid = Objects.requireNonNull(resultUuid);
49+
this.runContext = Objects.requireNonNull(runContext);
50+
}
51+
52+
public Message<String> toMessage(ObjectMapper objectMapper) {
53+
String parametersJson;
54+
try {
55+
parametersJson = objectMapper.writeValueAsString(runContext.getParameters());
56+
} catch (JsonProcessingException e) {
57+
throw new UncheckedIOException(e);
58+
}
59+
return MessageBuilder.withPayload(parametersJson)
60+
.setHeader(RESULT_UUID_HEADER, resultUuid.toString())
61+
.setHeader(NETWORK_UUID_HEADER, runContext.getNetworkUuid().toString())
62+
.setHeader(VARIANT_ID_HEADER, runContext.getVariantId())
63+
.setHeader(HEADER_RECEIVER, runContext.getReceiver())
64+
.setHeader(HEADER_PROVIDER, runContext.getProvider())
65+
.setHeader(HEADER_USER_ID, runContext.getUserId())
66+
.setHeader(REPORT_UUID_HEADER, runContext.getReportInfos().reportUuid() != null ? runContext.getReportInfos().reportUuid().toString() : null)
67+
.setHeader(REPORTER_ID_HEADER, runContext.getReportInfos().reporterId())
68+
.setHeader(REPORT_TYPE_HEADER, runContext.getReportInfos().computationType())
69+
.copyHeaders(getSpecificMsgHeaders())
70+
.build();
71+
}
72+
73+
protected Map<String, String> getSpecificMsgHeaders() {
74+
return Map.of();
75+
}
76+
}

0 commit comments

Comments
 (0)