Subversion Repositories SmartDukaan

Rev

Rev 37395 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.smartdukaan.cron.monitored;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;

/**
 * Publishes the two prepaid balance signals -- SMS credits and the ThinkWalnut
 * recharge wallet -- on /actuator/prometheus, which alloy scrapes off
 * 127.0.0.1:8081 on this host.
 *
 * Values are pushed in by {@link BalanceMonitorTasks}; -1 means "not read yet"
 * so a scrape before the first run is distinguishable from a genuine zero
 * balance.
 */
@Component
public class BalanceGauges {

    private static final int NOT_READ_YET = -1;

    private final AtomicInteger smsBalance = new AtomicInteger(NOT_READ_YET);
    private final AtomicReference<Double> rechargeWalletBalance =
            new AtomicReference<>((double) NOT_READ_YET);

    public BalanceGauges(MeterRegistry meterRegistry) {
        Gauge.builder("sms_balance_remaining", smsBalance, AtomicInteger::get)
                .description("SMS credits remaining with the messaging gateway; -1 = not read yet")
                .register(meterRegistry);

        Gauge.builder("recharge_wallet_balance_rupees", rechargeWalletBalance, AtomicReference::get)
                .description("ThinkWalnut recharge wallet balance in rupees; -1 = not read yet")
                .register(meterRegistry);
    }

    public void setSmsBalance(int count) {
        smsBalance.set(count);
    }

    public void setRechargeWalletBalance(double rupees) {
        rechargeWalletBalance.set(rupees);
    }
}