Skip to content

Commit

Permalink
feat(observability): add observations to loadflow run
Browse files Browse the repository at this point in the history
Signed-off-by: Joris Mancini <joris.mancini_externe@rte-france.com>
  • Loading branch information
TheMaskedTurtle committed Dec 1, 2023
1 parent 927b81a commit 404fa5c
Showing 1 changed file with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.powsybl.security.LimitViolation;
import com.powsybl.security.LimitViolationType;
import com.powsybl.security.Security;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.lang3.StringUtils;
import org.gridsuite.loadflow.server.dto.LimitViolationInfos;
import org.gridsuite.loadflow.server.repositories.LoadFlowResultRepository;
Expand All @@ -34,7 +36,6 @@

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
Expand Down Expand Up @@ -62,13 +63,16 @@ public class LoadFlowWorkerService {

private LoadFlowResultRepository resultRepository;

private final ObservationRegistry observationRegistry;

public LoadFlowWorkerService(NetworkStoreService networkStoreService, NotificationService notificationService, ReportService reportService,
LoadFlowResultRepository resultRepository, ObjectMapper objectMapper) {
LoadFlowResultRepository resultRepository, ObjectMapper objectMapper, ObservationRegistry observationRegistry) {
this.networkStoreService = networkStoreService;
this.notificationService = notificationService;
this.reportService = reportService;
this.resultRepository = resultRepository;
this.objectMapper = objectMapper;
this.observationRegistry = observationRegistry;
}

private Map<UUID, CompletableFuture<LoadFlowResult>> futures = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -244,20 +248,28 @@ public Consumer<Message<String>> consumeRun() {
LoadFlowResultContext resultContext = LoadFlowResultContext.fromMessage(message, objectMapper);
try {
runRequests.add(resultContext.getResultUuid());
AtomicReference<Long> startTime = new AtomicReference<>();

startTime.set(System.nanoTime());
Network network = getNetwork(resultContext.getRunContext().getNetworkUuid(), resultContext.getRunContext().getVariantId());

LoadFlowResult result = run(network, resultContext.getRunContext(), resultContext.getResultUuid());
long nanoTime = System.nanoTime();
LOGGER.info("Just run in {}s", TimeUnit.NANOSECONDS.toSeconds(nanoTime - startTime.getAndSet(nanoTime)));

List<LimitViolationInfos> limitViolationInfos = getLimitViolations(network, resultContext.getRunContext());
List<LimitViolationInfos> limitViolationsWithCalculatedOverload = calculateOverloadLimitViolations(limitViolationInfos, network);
resultRepository.insert(resultContext.getResultUuid(), result, LoadFlowService.computeLoadFlowStatus(result), limitViolationsWithCalculatedOverload);
long finalNanoTime = System.nanoTime();
LOGGER.info("Stored in {}s", TimeUnit.NANOSECONDS.toSeconds(finalNanoTime - startTime.getAndSet(finalNanoTime)));
Network network = Observation.createNotStarted("loadflow.load.network", observationRegistry)
.lowCardinalityKeyValue("userId", resultContext.getRunContext().getUserId())
.lowCardinalityKeyValue("loadflow-provider", resultContext.getRunContext().getProvider())
.contextualName("loadflow-load-network")
.observeChecked(() -> getNetwork(resultContext.getRunContext().getNetworkUuid(), resultContext.getRunContext().getVariantId()));

LoadFlowResult result = Observation.createNotStarted("loadflow.run", observationRegistry)
.lowCardinalityKeyValue("userId", resultContext.getRunContext().getUserId())
.lowCardinalityKeyValue("loadflow-provider", resultContext.getRunContext().getProvider())
.contextualName("loadflow-run")
.observeChecked(() -> run(network, resultContext.getRunContext(), resultContext.getResultUuid()));

Observation.createNotStarted("loadflow.save", observationRegistry)
.lowCardinalityKeyValue("userId", resultContext.getRunContext().getUserId())
.lowCardinalityKeyValue("loadflow-provider", resultContext.getRunContext().getProvider())
.contextualName("loadflow-save")
.observe(() -> {
List<LimitViolationInfos> limitViolationInfos = getLimitViolations(network, resultContext.getRunContext());
List<LimitViolationInfos> limitViolationsWithCalculatedOverload = calculateOverloadLimitViolations(limitViolationInfos, network);
resultRepository.insert(resultContext.getResultUuid(), result, LoadFlowService.computeLoadFlowStatus(result), limitViolationsWithCalculatedOverload);
});

if (result != null) { // result available
notificationService.sendResultMessage(resultContext.getResultUuid(), resultContext.getRunContext().getReceiver());
Expand Down

0 comments on commit 404fa5c

Please sign in to comment.