add logger to gacksbilling, add auto_renew to nightly
smbaker [Thu, 27 Sep 2012 01:23:48 +0000 (18:23 -0700)]
apps/gacks/API.py
apps/gacks/gacksbilling.py

index e05671b..b8d6b7a 100644 (file)
@@ -81,7 +81,7 @@ class RemoteApi(AuthenticatedApi):
         self.init_invoices()
         self.init_policies()
         self.init_acls()
-        self.billing = GacksBilling(self.accounts, self.invoices, self.policies)
+        self.billing = GacksBilling(self.accounts, self.invoices, self.policies, self.resources)
         self.picker = GacksNodePicker(resources=self.resources)
         self.node_status = GacksNodeStatus()
         self.currentUpdateDicts = []
index ec94954..12ed434 100644 (file)
@@ -1,15 +1,29 @@
+import logging
+import time
 import gacksaccount
 import gacksinvoice
 import gackspolicy
-from gacksinvoice import GacksInvoiceManager, STATE_PENDING, STATE_AGGREGATED
+from gacksinvoice import GacksInvoiceManager, STATE_PENDING, STATE_AGGREGATED, KIND_CYCLE_CHARGE, KIND_SLOT_CHARGE, KIND_CYCLE_AGGREGATE
+
+glo_logger_name = "gacksapi"
+
+def set_logger_name(x):
+    global glo_logger_name
+    glo_logger_name = x
 
 class GacksBilling:
-   def __init__(self, accounts, invoices, policies):
+   def __init__(self, accounts, invoices, policies, resources):
        self.accounts = accounts
        self.invoices = invoices
        self.policies = policies
+       self.resources = resources
+
+   def mylogger(self):
+       return logging.getLogger(glo_logger_name)
 
    def bill_monthly(self):
+       self.mylogger().info("doing bill_monthly")
+
        accounts = self.accounts.get_accounts()
        for acct in accounts:
             policy = self.policies.get_policy(acct.level, None)
@@ -18,7 +32,29 @@ class GacksBilling:
 
             self.invoices.bill_monthly(acct, policy)
 
+   def auto_renew(self):
+       self.mylogger().info("doing auto-renew")
+
+       time_threshold = time.time() - 48*60*60*10
+       inv = self.invoices.get_invoice_prime({"account_id": "_GRP_",
+                                              "start_date": time_threshold,
+                                              "kind_id": [KIND_CYCLE_CHARGE, KIND_SLOT_CHARGE, KIND_CYCLE_AGGREGATE]},
+                                             lookup_objects=True)
+
+       handlers = []
+       for resource in self.resources.resources:
+           handler = resource.get_handler_obj()
+           if handler is not None:
+               handlers.append(handler)
+
+       for charge in inv.charges:
+           account_name = charge.account_name
+           for handler in handlers:
+               handler.renewSlice(account_name, 30)
+
    def do_nightly(self):
+        self.mylogger().info("doing do_nightly")
+
         # do monthly service fees
         self.bill_monthly()
 
@@ -28,4 +64,7 @@ class GacksBilling:
         # apply pending charges to nightly invoices
         self.invoices.apply_invoices()
 
+        # auto-renew slices
+        self.auto_renew()
+