package com.atlassian.bitbucket.mesh.metrics;

import com.atlassian.bitbucket.mesh.git.cache.PackCacheService;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;

import javax.annotation.Nonnull;

import static com.atlassian.bitbucket.mesh.metrics.MetricsConstants.PREFIX_PACK_CACHE;
import static java.util.Objects.requireNonNull;

/**
 * Configures {@link PackCacheService pack cache} related metrics.
 */
public class PackCacheMetrics implements MeterBinder {

    private final PackCacheService packCacheService;

    public PackCacheMetrics(@Nonnull PackCacheService packCacheService) {
        this.packCacheService = requireNonNull(packCacheService, "packCacheService");
    }

    @Override
    public void bindTo(@Nonnull MeterRegistry registry) {
        String prefix = PREFIX_PACK_CACHE;
        Gauge.builder(prefix + "hits", () -> packCacheService.getStatistics().getHits())
                .description("The total number of cache hits since the application started")
                .register(registry);
        Gauge.builder(prefix + "misses", () -> packCacheService.getStatistics().getMisses())
                .description("The total number of cache misses since the application started")
                .register(registry);
        Gauge.builder(prefix + "size", () -> packCacheService.getStatistics().getSize())
                .description("The total size on disk of all pack caches in bytes")
                .register(registry);
    }
}
