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

pro.fessional.wings.tiny.grow.track.TinyTrackHelper Maven / Gradle / Ivy

The newest version!
package pro.fessional.wings.tiny.grow.track;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.util.function.SingletonSupplier;
import pro.fessional.mirana.func.Lam;
import pro.fessional.mirana.pain.ThrowableUtil;
import pro.fessional.mirana.time.ThreadNow;
import pro.fessional.wings.silencer.spring.help.ApplicationContextHelper;

import java.lang.reflect.Method;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author trydofor
 * @since 2024-07-25
 */
@Slf4j
public class TinyTrackHelper {

    private static final SingletonSupplier TrackService = ApplicationContextHelper.getSingletonSupplier(TinyTrackService.class);

    public static  R track(@NotNull Lam.Ref key, @NotNull Function fun) {
        return track(key.method, fun);
    }

    public static  R track(@NotNull Method key, @NotNull Function fun) {
        TinyTrackService service = TrackService.obtain();
        TinyTracking tracking = service.begin(key);
        return track(service, tracking, fun);
    }

    public static  R track(@NotNull Enum key, @NotNull Function fun) {
        TinyTrackService service = TrackService.obtain();
        TinyTracking tracking = service.begin(key);
        return track(service, tracking, fun);
    }

    public static  R track(@NotNull String key, @NotNull Function fun) {
        TinyTrackService service = TrackService.obtain();
        TinyTracking tracking = service.begin(key);
        return track(service, tracking, fun);
    }

    public static  R track(@NotNull String key, @NotNull String ref, @NotNull Function fun) {
        TinyTrackService service = TrackService.obtain();
        TinyTracking tracking = service.begin(key, ref);
        return track(service, tracking, fun);
    }

    private static  R track(@NotNull TinyTrackService service, @NotNull TinyTracking tracking, @NotNull Function fun) {
        try {
            R out = fun.apply(tracking);
            if (tracking.getOut() == null) {
                tracking.setOut(out);
            }
            return out;
        }
        catch (Throwable e) {
            if (tracking.getErr() == null) {
                tracking.setErr(e);
            }
            throw ThrowableUtil.runtime(e);
        }
        finally {
            tracking.setElapse(ThreadNow.millis() - tracking.getBegin());
            service.track(tracking, true);
        }
    }

    public static TrackWrapper track(@NotNull Lam.Ref key) {
        return track(key.method);
    }

    public static TrackWrapper track(@NotNull Method key) {
        TinyTrackService service = TrackService.obtain();
        TinyTracking tracking = service.begin(key);
        return new TrackWrapper(tracking, service);
    }

    public static TrackWrapper track(@NotNull Enum key) {
        TinyTrackService service = TrackService.obtain();
        TinyTracking tracking = service.begin(key);
        return new TrackWrapper(tracking, service);
    }

    public static TrackWrapper track(@NotNull String key) {
        TinyTrackService service = TrackService.obtain();
        TinyTracking tracking = service.begin(key);
        return new TrackWrapper(tracking, service);
    }

    public static TrackWrapper track(@NotNull String key, @NotNull String ref) {
        TinyTrackService service = TrackService.obtain();
        TinyTracking tracking = service.begin(key, ref);
        return new TrackWrapper(tracking, service);
    }

    /**
     * 
     * try(tryTrack) {
     *   // biz logic
     * };
     * 
*/ @RequiredArgsConstructor @Getter public static class TrackWrapper implements AutoCloseable { @NotNull @Delegate(types = TinyTracking.class) private final TinyTracking tracking; @NotNull private final TinyTrackService service; /** * set properties without exception thrown */ public void safeSet(@NotNull Consumer fun) { try { fun.accept(tracking); } catch (Throwable e) { log.warn("safeSet get error", e); } } /** * set properties and out without exception thrown */ public R safeOut(@NotNull Function fun) { R out = null; try { out = fun.apply(tracking); tracking.setOut(out); } catch (Throwable e) { log.warn("safeOut get error", e); } return out; } @Override public void close() { tracking.setElapse(ThreadNow.millis() - tracking.getBegin()); service.track(tracking, true); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy