func (m *Monitor) VerifyUnitsBasedChannels() error {
	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 offer.unit_type = 'units'
                 AND acc.in_use
               GROUP BY channels.id, offer.setup_price,
                     offer.unit_price, offer.max_unit
              HAVING offer.setup_price + coalesce(sum(ses.units_used), 0) * offer.unit_price >= channels.total_deposit
                  OR COALESCE(SUM(ses.units_used), 0) >= offer.max_unit;`

	return m.processEachChannel(query, m.terminateService)
}
func (m *Monitor) VerifyUnitsBasedChannels() error {
	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 offer.unit_type = 'units'
                 AND acc.in_use
               GROUP BY channels.id, offer.setup_price,
                     offer.unit_price, offer.max_unit
              HAVING offer.setup_price + coalesce(sum(ses.units_used), 0) * offer.unit_price >= channels.total_deposit
                  OR COALESCE(SUM(ses.units_used), 0) >= offer.max_unit;`

	return m.processEachChannel(query, m.terminateService)
}
/* TODO: uncomment when timebased billing will be implemented
// VerifySecondsBasedChannels checks all active seconds based channels
// for not using more units, than provided by quota and not exceeding
// over total deposit.
func (m *Monitor) VerifySecondsBasedChannels() error {
	// Selects all channels, which
	// 1. used tokens >= deposit tokens
	// 2. total consumed seconds >= max offer units (seconds in this case)
	// Only checks channels, which corresponding offers are using seconds
	// as billing basis.
	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 offer.unit_type = 'seconds'
                 AND acc.in_use
               GROUP BY channels.id, offer.setup_price,
                     offer.unit_price, offer.max_unit
              HAVING offer.setup_price + COALESCE(SUM(ses.seconds_consumed), 0) * offer.unit_price >= channels.total_deposit
                  OR COALESCE(SUM(ses.seconds_consumed), 0) >= offer.max_unit;`

	return m.processEachChannel(query, m.terminateService)
}
*/
// VerifyUnitsBasedChannels checks all active units based channels
// for not using more units, than provided by quota
// and not exceeding over total deposit.