agent/bill/monitor.go
func (m *Monitor) VerifyBillingLags() error {
	// Checking billing lags.
	// All channels, that are not suspended and are not terminated,
	// but are suffering from the billing lags - must be suspended.
	query := `
              SELECT channels.id :: text
		FROM channels
                     LEFT JOIN sessions ses
                     ON channels.id = ses.channel

                     LEFT JOIN offerings offer
                     ON channels.offering = offer.id

                     INNER JOIN accounts acc
                     ON channels.agent = acc.eth_addr
               WHERE channels.service_status IN ('pending', 'active')
                     AND channels.channel_status NOT IN ('pending')
                     AND acc.in_use
               GROUP BY channels.id, offer.billing_interval,
                     offer.setup_price, offer.unit_price,
                     offer.max_billing_unit_lag
              HAVING COALESCE(SUM(ses.units_used), 0) /
	      offer.billing_interval - (channels.receipt_balance - offer.setup_price ) /
	      offer.unit_price > offer.max_billing_unit_lag;`

	return m.processEachChannel(query, m.suspendService)
}
func (m *Monitor) VerifyBillingLags() error {
	// Checking billing lags.
	// All channels, that are not suspended and are not terminated,
	// but are suffering from the billing lags - must be suspended.
	query := `
              SELECT channels.id :: text
		FROM channels
                     LEFT JOIN sessions ses
                     ON channels.id = ses.channel

                     LEFT JOIN offerings offer
                     ON channels.offering = offer.id

                     INNER JOIN accounts acc
                     ON channels.agent = acc.eth_addr
               WHERE channels.service_status IN ('pending', 'active')
                     AND channels.channel_status NOT IN ('pending')
                     AND acc.in_use
               GROUP BY channels.id, offer.billing_interval,
                     offer.setup_price, offer.unit_price,
                     offer.max_billing_unit_lag
              HAVING COALESCE(SUM(ses.units_used), 0) /
	      offer.billing_interval - (channels.receipt_balance - offer.setup_price ) /
	      offer.unit_price > offer.max_billing_unit_lag;`

	return m.processEachChannel(query, m.suspendService)
}
// VerifyBillingLags checks all active channels for billing lags,
// and schedules suspending of those, who are suffering from billing lags.