Subversion Repositories SmartDukaan

Rev

Blame | 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 balance signals that used to be written to
 * /var/log/services/nagios-Monitoring.properties for an NRPE check.
 *
 * The Nagios transport is gone — there is no Nagios server, and the NRPE
 * daemons have been removed from every host — but the signals themselves are
 * worth watching, so they are exposed on /actuator/prometheus instead.
 *
 * Values are pushed in by {@link NagiosMonitorTasks}; -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);
    }
}