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

org.opensearch.migrations.replay.util.TrackedFutureJsonFormatter Maven / Gradle / Ivy

There is a newer version: 0.2.0.4
Show newest version
package org.opensearch.migrations.replay.util;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Lombok;
import lombok.NonNull;
import lombok.SneakyThrows;

public class TrackedFutureJsonFormatter {

    static ObjectMapper objectMapper = new ObjectMapper();

    private TrackedFutureJsonFormatter() {}

    public static  String format(TrackedFuture tf) {
        return format(tf, x -> null);
    }

    public static  String format(
        TrackedFuture tf,
        @NonNull Function, String> resultFormatter
    ) {
        try {
            return objectMapper.writeValueAsString(makeJson(tf, resultFormatter));
        } catch (Exception e) {
            throw Lombok.sneakyThrow(e);
        }
    }

    public static  List makeJson(
        TrackedFuture tf,
        @NonNull Function, String> resultFormatter
    ) {
        return tf.walkParentsAsStream()
            .map(kvp -> jsonFormatFutureWithDiagnostics(kvp, resultFormatter))
            .collect(Collectors.toList());
    }

    @SneakyThrows
    protected static  Map jsonFormatFutureWithDiagnostics(
        @NonNull TrackedFuture tf,
        @NonNull Function, String> resultFormatter
    ) {
        var diagnosticInfo = tf.diagnosticSupplier.get();
        var isDone = tf.isDone();
        var map = new LinkedHashMap();
        map.put("idHash", System.identityHashCode(tf));
        map.put("label", diagnosticInfo);
        if (isDone) {
            map.put("value", Optional.ofNullable(resultFormatter.apply(tf)).orElse("^"));
        } else {
            var innerResult = Optional.ofNullable(tf.innerComposedPendingCompletableFutureReference)
                .map(r -> (TrackedFuture) r.get())
                .map(df -> makeJson(df, resultFormatter))
                .orElse(null);
            if (innerResult == null) {
                map.put("value", "…");
            } else {
                map.put("pending", innerResult);
            }
        }
        return map;
    }
}