Permalink
Browse files

add bill detail interface: query_report()

  • Loading branch information...
1 parent c0a1044 commit 90d3e780e20981e3ed5ca104e46201ebda5fe1b1 @pengyuwei committed Jun 15, 2012
Showing with 92 additions and 17 deletions.
  1. +28 −4 bin/dough-client
  2. +1 −0 bin/dough-farmer
  3. +53 −12 dough/api.py
  4. +6 −0 dough/billing/api.py
  5. +4 −1 dough/client/dough_client.py
View
@@ -32,10 +32,30 @@ def show_usage():
print "param:"
print "\t -m : query_monthly_report"
print "\t -d : query_report"
- print "./dough-client -d d8462edac32e4d688ae20d67a51d529f 2012-01-01T00:00:00 2012-07-07T00:00:00 24"
+ print "./dough-client -d 1adfb274fee24dbd96ea89b57d110fc5 2012-06-01T00:00:00 2012-07-01T00:00:00 days network resource_name"
def show_result(data):
print data
+
+def show_report(data):
+# print "ID=", data['data']['id']
+ data = data['data']['default']
+ line_total_sum = 0
+ quantity_sum = 0
+ count = 0
+
+ rs = OrderedDict(sorted(data.items(), key=lambda t: t[0]))
+ for k, i in rs.iteritems():
+ count += 1
+ print '-' * 60, count
+ print k
+ for kk, ii in i.iteritems():
+ print "\t", kk, ii
+ if kk == 'line_total':
+ line_total_sum += ii
+ elif kk == 'quantity':
+ quantity_sum += ii
+ print "total[", count, "] = ", line_total_sum
def main():
if len(sys.argv) == 2:
@@ -49,17 +69,21 @@ def main():
data = None
client = DoughClient()
- if len(sys.argv) > 6:
+ if len(sys.argv) > 8:
tenant_id = sys.argv[3]
time_from = sys.argv[4]
time_to = sys.argv[5]
period = sys.argv[6]
+ item_name = sys.argv[7]
+ resource_name = sys.argv[8]
if sys.argv[2] == '-d':
- data = client.query_report(tenant_id, time_from, time_to, period)
+ data = client.query_report(tenant_id, time_from, time_to, period,
+ item_name, resource_name)
else:
pass
- show_result(data)
+ show_report(data)
+ elif len(sys.argv) > 6:
return
elif len(sys.argv) > 5:
tenant_id = sys.argv[3]
View
@@ -29,6 +29,7 @@ if __name__ == '__main__':
context = dough_context.get_admin_context()
while True:
current_time = utils.utcnow()
+ print "-" * 50, str(current_time)
subscriptions = list()
_subscriptions = db.subscription_get_all(context)
for sub in _subscriptions:
View
@@ -214,19 +214,22 @@ def find_timeframe(start_time, end_time, target):
timeframe = find_timeframe(datetime_from,
datetime_to,
purchase['created_at'])
+ monthly_report["id"] = purchase['id']
region_usage = monthly_report.setdefault(region_name, dict())
monthly_usage = region_usage.setdefault(timeframe, dict())
monthly_usage.setdefault(item_name, 0)
monthly_usage[item_name] += line_total
-# monthly_usage["id"] = purchase['id']
return {'data': monthly_report}
-def query_report(context, timestamp_from=None,
- timestamp_to=None, period=None, **kwargs):
+def query_report(context, timestamp_from=None, timestamp_to=None,
+ period=None, item_name=None, resource_name=None, **kwargs):
"""period='days' or 'hours'"""
- print "query_report", timestamp_from, timestamp_to
+ print "query_report", timestamp_from, timestamp_to, item_name, resource_name
# period = int(period)
+
+ if not period in ['days', 'hours', 'months']:
+ return {'data': None}
def find_timeframe(start_time, end_time, target):
target_utc = target.replace(tzinfo=UTC_TIMEZONE)
@@ -243,33 +246,71 @@ def find_timeframe(start_time, end_time, target):
return current_frame.isoformat()
monthly_report = dict()
+ usage_report = dict()
datetime_from = iso8601.parse_date(timestamp_from)
datetime_to = iso8601.parse_date(timestamp_to)
subscriptions = list()
- _subscriptions = db.subscription_get_all_by_project(context,
+ _subscriptions = list()
+
+ __subscriptions = db.subscription_get_all_by_project(context,
context.project_id)
+ if not __subscriptions:
+ return {'data': None}
+# print "context.project_id", context.project_id
+ for subscription in __subscriptions:
+# print subscription['id'], subscription['resource_name'], subscription['product']['item']['name']
+ if subscription['resource_name'] != resource_name:
+ continue
+ elif subscription['product']['item']['name'] != item_name:
+ continue
+ _subscriptions.append(subscription)
+
for subscription in _subscriptions:
subscription_id = subscription['id']
+ resource_uuid = subscription['resource_uuid']
+ resource_name = subscription['resource_name']
+ created_at = subscription['created_at']
+ expires_at = subscription['expires_at']
region_name = subscription['product']['region']['name']
item_name = subscription['product']['item']['name']
- subscriptions.append([subscription_id, region_name, item_name])
- for subscription_id, region_name, item_name in subscriptions:
+ item_type_name = subscription['product']['item_type']['name']
+ order_unit = subscription['product']['order_unit']
+ order_size = subscription['product']['order_size']
+ price = subscription['product']['price']
+ currency = subscription['product']['currency']
+ subscriptions.append([subscription_id, resource_uuid, resource_name,
+ created_at, expires_at,
+ region_name, item_name, item_type_name,
+ order_unit, order_size, price, currency])
+ for (subscription_id, resource_uuid, resource_name, created_at, expires_at,
+ region_name, item_name, item_type_name,
+ order_unit, order_size, price, currency) in subscriptions:
purchases = db.purchase_get_all_by_subscription_and_timeframe(context,
subscription_id,
datetime_from,
datetime_to)
if not purchases:
continue
+ i = 0
for purchase in purchases:
line_total = purchase['line_total']
+ quantity = purchase['quantity']
timeframe = find_timeframe(datetime_from,
datetime_to,
purchase['created_at'])
- print subscription_id, purchase['id'], region_name, "-->", timeframe
+# print timeframe
+ i += 1
+ usage_datum = (resource_uuid, resource_name, item_type_name,
+ order_unit, order_size, price,
+ currency, quantity, line_total,
+ created_at.isoformat(), expires_at.isoformat())
region_usage = monthly_report.setdefault(region_name, dict())
monthly_usage = region_usage.setdefault(timeframe, dict())
- monthly_usage.setdefault(item_name, 0)
- monthly_usage[item_name] += line_total
- monthly_usage["id"] = purchase['id']
+ item = monthly_usage.setdefault(item_name, 0)
+ monthly_usage[item_name] = usage_datum
+ item = monthly_usage.setdefault("quantity", 0)
+ monthly_usage.setdefault("line_total", 0)
+ monthly_usage["quantity"] += quantity
+ monthly_usage["line_total"] += line_total
+ print "total:", i
return {'data': monthly_report}
-
View
@@ -42,6 +42,8 @@ def creating(context, subscription_id, tenant_id, resource_uuid,
quantity = conn.get_usage(resource_uuid,
expires_at - relativedelta(**interval_info),
expires_at, order_size)
+ print "creating", tenant_id, subscription_id, \
+ quantity, order_size, "\033[1;33m", price, "\033[0m"
charge(context, tenant_id, subscription_id, quantity,
order_size, price)
db.subscription_extend(context, subscription_id,
@@ -67,6 +69,8 @@ def deleting(context, subscription_id, tenant_id, resource_uuid,
quantity = conn.get_usage(resource_uuid,
expires_at - relativedelta(**interval_info),
expires_at, order_size)
+ print "deleting", tenant_id, subscription_id, \
+ quantity, order_size, "\033[1;33m", price, "\033[0m"
charge(context, tenant_id, subscription_id, quantity,
order_size, price)
@@ -85,6 +89,8 @@ def verified(context, subscription_id, tenant_id, resource_uuid,
quantity = conn.get_usage(resource_uuid,
expires_at - relativedelta(**interval_info),
expires_at, order_size)
+ print "verified", tenant_id, subscription_id, \
+ quantity, order_size, "\033[1;33m", price, "\033[0m"
charge(context, tenant_id, subscription_id, quantity, order_size, price)
db.subscription_extend(context, subscription_id,
expires_at + relativedelta(**interval_info))
@@ -94,13 +94,16 @@ def query_monthly_report(self, tenant_id, time_from, time_to):
data = self.invoke(request)
return data
- def query_report(self, tenant_id, time_from, time_to, period):
+ def query_report(self, tenant_id, time_from, time_to, period,
+ item_name, resource_name):
request = STANDARD_PROTOCOL
request["method"] = "query_report"
request["args"]["tenant_id"] = tenant_id
request["args"]["timestamp_from"] = time_from
request["args"]["timestamp_to"] = time_to
request["args"]["period"] = period
+ request["args"]["item_name"] = item_name
+ request["args"]["resource_name"] = resource_name
data = self.invoke(request)
return data

0 comments on commit 90d3e78

Please sign in to comment.