package com.lanyuanxiaoyao.service.monitor.metric;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author lanyuanxiaoyao
 * @date 2024-03-05
 */
public abstract class Metrics {
    private final AtomicBoolean running = new AtomicBoolean(false);
    private final AtomicLong finished = new AtomicLong(0);
    private final AtomicLong total = new AtomicLong(0);

    public abstract String name();

    public abstract void update();

    public boolean running() {
        return running.get();
    }

    public double progress() {
        if (total.get() == 0) {
            return 0;
        } else {
            return finished.get() * 1.0 / total.get();
        }
    }

    protected void start() {
        running.set(true);
    }

    protected void stop() {
        running.set(false);
    }

    protected void setTotal(Long total) {
        this.total.set(total);
    }

    protected void setTotal(Integer total) {
        this.total.set(total);
    }

    protected void finished() {
        finished.incrementAndGet();
    }

    protected void reset() {
        stop();
        setTotal(0);
        finished.set(0);
    }
}
