package com.atlassian.bitbucket.mesh.repair;

import com.atlassian.util.profiling.Ticker;

import javax.annotation.Nonnull;

public interface RepairMetrics {

    /**
     * @return the number of failed repair operations since startup
     */
    long getFailed();

    /**
     * @return the number of currently running repair operations
     */
    long getRunning();

    /**
     * @return the number of successful repair operations since startup
     */
    long getSuccessful();

    /**
     * @return the total number of repair operations that have been started since startup
     */
    long getTotal();

    /**
     * @return a call tracer used to collect metrics for repair calls
     */
    @Nonnull
    CallTracer startTrace();

    interface CallTracer {

        /**
         * Starts a timer for the "compare reflogs" phase of repair
         *
         * @return the ticker, which when {@link Ticker#close() closed} stops the timer
         */
        @Nonnull
        Ticker compareReflogs();

        /**
         * Starts a timer for the "compare refs" phase of repair
         *
         * @return the ticker, which when {@link Ticker#close() closed} stops the timer
         */
        @Nonnull
        Ticker compareRefs();

        /**
         * Starts a timer for the "fetch objects" phase of repair
         *
         * @return the ticker, which when {@link Ticker#close() closed} stops the timer
         */
        @Nonnull
        Ticker fetchObjects();

        /**
         * Callback method that should be called to mark the completion of the repair call
         *
         * @param success {@code true} to indicate that the repair was successful and the replica is now consistent
         */
        void stop(boolean success);
    }
}
