accounting for reservations now done by gackscollector
smbaker [Tue, 16 Oct 2012 05:48:45 +0000 (22:48 -0700)]
apps/gacks/gacksbackend.py
apps/gacks/gackscollector.py

index 62d2849..f1490be 100644 (file)
@@ -204,7 +204,8 @@ class GacksBackend:
                # resource["consumers"] is a dict of dicts: consumerHRN -> {"id", "consumer", "qty", "records"}
                self.handlers[kind].applyReservations(resource["consumers"],freezable_accounts)
 
-           self.do_accounting(tNow, resourceDefinition, resource["consumers"])
+           # we'll rely on gackswatcher/gackscollector to post the reservation charges
+           #self.do_accounting(tNow, resourceDefinition, resource["consumers"])
 
 def test():
     print "TEST"
index bcd17c7..168fe64 100644 (file)
@@ -163,26 +163,41 @@ class ChargeProcessor(threading.Thread):
                              (account, host, kind, date, amount))
 
     def process_charge(self, account, host, charge):
-        # figure out a way to identify unique charges
+        # XXX figure out a way to identify unique charges
 
         # If there were cores reserved by a portion of this internal (resv_charge>0) then
         # subtract the portion of the time that the reservation was in place from the
         # amount of best effort consumed.
-        amount = charge['amount'] - charge['resv_charge']
+
+        resv_amount = charge['resv_charge']
+        amount = charge['amount'] - resv_amount
+
         if amount < charge['amount']:
-            self.log.writeTS("Charge for %s %s at %d of %0.2f mitigated by resv_charge of %0.2f" % \
+            self.log.writeTS("Cycle charge for %s %s at %d of %0.2f mitigated by resv_charge of %0.2f" % \
                               (account, host, charge['date'], charge['amount'], charge['resv_charge']))
 
-        # the resv_charge may have made this charge zero, or even negative.
-        if amount <= 0:
+        # the resv_charge may have made the cycle charge zero, or even negative.
+        if (amount <= 0) and (resv_amount <= 0):
+            # there's nothing to see here. go away.
             return
 
         account = self.account_manager.get_account(account, 'user', create_if_not_exist=True)
-        policy = self.policy_manager.get_policy(account.level, "cycles")
-        if (policy == None):
-            self.log.writeTS("ChargeProcessor: account %s cannot find policy %s resource %s\n" % (account,account.level,"cycles"))
-            return
-        self._process_charge(account.name, host, charge["kind"], charge["date"], float(amount) * float(policy.cost) / 100.0, float(amount))
+
+        # best-effort billing
+        if amount > 0:
+            policy = self.policy_manager.get_policy(account.level, "cycles")
+            if (policy == None):
+                self.log.writeTS("ChargeProcessor: account %s cannot find policy %s resource %s\n" % (account,account.level,"cycles"))
+            else:
+                self._process_charge(account.name, host, charge["kind"], charge["date"], float(amount) * float(policy.cost) / 100.0, float(amount))
+
+        # reservation billing
+        if resv_amount > 0:
+            policy = self.policy_manager.get_policy(account.level, "plc.vicci.cores")
+            if (policy == None):
+                self.log.writeTS("ChargeProcessor: account %s cannot find policy %s resource %s\n" % (account,account.level,"plc.vicci.cores"))
+            else:
+                self._process_charge(account.name, host, 1, charge["date"], float(resv_amount) * float(policy.cost) / 100.0, float(resv_amount))
 
     def process_charges(self, host):
         charge_files = self.charge_file_manager.get_charge_files(host)