All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.arc.runtime.devui.ArcJsonRPCService Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
package io.quarkus.arc.runtime.devui;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;

import io.quarkus.arc.runtime.devconsole.Invocation;
import io.quarkus.arc.runtime.devconsole.InvocationsMonitor;
import io.quarkus.arc.runtime.devmode.EventInfo;
import io.quarkus.arc.runtime.devmode.EventsMonitor;
import io.quarkus.arc.runtime.devmode.InvocationInfo;
import io.smallrye.common.annotation.NonBlocking;
import io.smallrye.mutiny.Multi;

public class ArcJsonRPCService {

    @Inject
    Instance eventsMonitor;

    @Inject
    Instance invocationsMonitor;

    public Multi streamEvents() {
        return eventsMonitor.isResolvable() ? eventsMonitor.get().streamEvents() : Multi.createFrom().empty();
    }

    public Multi streamSkipContextEvents() {
        return eventsMonitor.isResolvable() ? eventsMonitor.get().streamSkipContextEvents() : Multi.createFrom().empty();
    }

    @NonBlocking
    public List getLastEvents() {
        if (eventsMonitor.isResolvable()) {
            return eventsMonitor.get().getLastEvents();
        }
        return List.of();
    }

    @NonBlocking
    public List clearLastEvents() {
        if (eventsMonitor.isResolvable()) {
            eventsMonitor.get().clear();
            return getLastEvents();
        }
        return List.of();
    }

    @NonBlocking
    public List toggleSkipContextEvents() {
        if (eventsMonitor.isResolvable()) {
            eventsMonitor.get().toggleSkipContextEvents();
            return getLastEvents();
        }
        return List.of();
    }

    @NonBlocking
    public List getLastInvocations() {
        if (invocationsMonitor.isResolvable()) {
            List lastInvocations = invocationsMonitor.get().getLastInvocations();
            return toInvocationInfos(lastInvocations);
        }
        return List.of();
    }

    @NonBlocking
    public List clearLastInvocations() {
        if (invocationsMonitor.isResolvable()) {
            invocationsMonitor.get().clear();
            return getLastInvocations();
        }
        return List.of();
    }

    private List toInvocationInfos(List invocations) {
        List infos = new ArrayList<>();
        for (Invocation invocation : invocations) {
            infos.add(toInvocationInfo(invocation));
        }
        return infos;
    }

    private InvocationInfo toInvocationInfo(Invocation invocation) {
        InvocationInfo info = new InvocationInfo();
        info.setStartTime(
                timeString(LocalDateTime.ofInstant(Instant.ofEpochMilli(invocation.getStart()), ZoneId.systemDefault())));
        info.setMethodName(invocation.getDeclaringClassName() + "#" + invocation.getMethod().getName());
        info.setDuration(invocation.getDurationMillis());
        info.setKind(invocation.getKind().toString());
        info.setChildren(toInvocationInfos(invocation.getChildren()));
        info.setQuarkusBean(invocation.isQuarkusBean());
        return info;
    }

    private String timeString(LocalDateTime time) {
        String timestamp = time.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME).replace("T", " ");
        int lastIndexOfDot = timestamp.lastIndexOf(".");
        return lastIndexOfDot > 0 ? timestamp.substring(0, lastIndexOfDot) : timestamp;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy