def do_bill(self, context): '''Do bill once, pay the cost until now.''' now = timeutils.utcnow() total_seconds = (now - self.last_bill).total_seconds() self.balance = self.balance - self.rate * total_seconds self.last_bill = now if self.balance < 0: self._freeze(context, reason="Balance overdraft") self.store(context) event_mod.record(context, self.id, action='charge', seconds=total_seconds)
def do_recharge(self, context, value): '''Do recharge for user.''' if self.rate > 0 and self.status != self.FREEZE: self.do_bill(context) self.balance += value if self.status == self.INIT and self.balance > 0: self.set_status(self.ACTIVE, reason='Recharged') elif self.status == self.FREEZE and self.balance > 0: reason = _("Status change from freeze to active because " "of recharge.") self.set_status(self.ACTIVE, reason=reason) elif self.status == self.WARNING: prior_notify_time = cfg.CONF.bilean_task.prior_notify_time * 3600 rest_usage = prior_notify_time * self.rate if self.balance > rest_usage: reason = _("Status change from warning to active because " "of recharge.") self.set_status(self.ACTIVE, reason=reason) event_mod.record(context, self.id, action='recharge', value=value) self.store(context)