timeline charging hello world
authorroot <gtsouk@cslab.ece.ntua.gr>
Thu, 20 Sep 2012 16:20:24 +0000 (19:20 +0300)
committerroot <gtsouk@cslab.ece.ntua.gr>
Fri, 21 Sep 2012 12:05:20 +0000 (15:05 +0300)
snf-astakos-app/astakos/im/endpoints/quotaholder.py
snf-astakos-app/astakos/im/views.py

index 4cb292d..29fe456 100644 (file)
@@ -162,4 +162,87 @@ def register_resources(resources, client=None):
                                        client=client,
                                        field='service')
     created = (e for e in copy if unicode(e) not in rejected)
-    return send_resource_quantities(created, client)
\ No newline at end of file
+    return send_resource_quantities(created, client)
+
+
+from datetime import datetime
+
+strptime = datetime.strptime
+timefmt = '%Y-%m-%dT%H:%M:%S.%f'
+
+def total_seconds(timedelta_object):
+    return timedelta_object.seconds + timedelta_object.days * 86400
+
+def _usage_units(timeline, details=0):
+    t0 = None
+    uu_total = 0
+    t_total = 0
+    target = None
+    issue_time = None
+
+    for point in timeline:
+        issue_time = point['issue_time']
+        t = strptime(issue_time, timefmt)
+        u = point['target_allocated_through']
+
+        if t0 is None:
+            t0 = t
+            u0 = u
+            target = point['target']
+            continue
+
+        t_diff = int(total_seconds(t - t0) * 1)
+        t_total += t_diff
+        t0 = t
+        uu_cost = u0 * t_diff
+        uu_total += uu_cost
+        u0 = u
+
+        if details:
+            yield  (target,
+                    point['resource'],
+                    point['name'],
+                    issue_time,
+                    uu_cost,
+                    uu_total)
+
+    if not t_total:
+        return
+
+    yield  (target,
+            'total',
+            point['resource'],
+            issue_time,
+            uu_total/t_total,
+            uu_total)
+
+
+def usage_units(timeline, details=0):
+    if details:
+        return list(_usage_units(timeline, details=1))
+    else:
+        return _usage_units(timeline, details=0)[0]
+
+
+def traffic_units(timeline, details=0):
+    pass
+
+
+def timeline_charge(entity, resource, after, before, details, charge_type):
+    key = '1'
+    if charge_type == 'charge_usage':
+        charge_units = usage_units
+    elif charge_type == 'charge_traffic':
+        charge_units = traffic_units
+    else:
+        m = 'charge type %s not supported' % charge_type
+        raise ValueError(m)
+
+    quotaholder = QuotaholderHTTP(QUOTA_HOLDER_URL)
+    timeline = quotaholder.get_timeline(
+                            context         =   {},
+                            after           =   after,
+                            before          =   before,
+                            get_timeline    =   [[entity, resource, key]])
+    return charge_units(timeline, details=details)
+
index fd50920..875827c 100644 (file)
@@ -75,6 +75,7 @@ from astakos.im.functions import (send_feedback, SendMailError,
                                   switch_account_to_shibboleth,
                                   send_admin_notification,
                                   SendNotificationError)
+from astakos.im.endpoints.quotaholder import timeline_charge
 from astakos.im.settings import (
     COOKIE_NAME, COOKIE_DOMAIN, SITENAME, LOGOUT_NEXT,
     LOGGING_LEVEL, PAGINATE_BY)
@@ -1025,31 +1026,22 @@ def clear_billing_data(data):
 @login_required
 def timeline(request):
     data = {'entity':request.user.email}
-    l = []
+    timeline_body = ()
+    timeline_header = ()
+    form = None
     if request.method == 'POST':
         data = request.POST
-        #l =[(u'test/apples', u'2012-09-20T11:39:53.1797', 192172055040L, 25569215632749L),
-        #    (u'test/pears/kate.jpg', u'2012-09-20T11:45:14.1153', 381640896L, 25626036449581L),
-        #    (u'test/pears/kate_beckinsale.jpg', u'2012-09-20T11:45:14.5830', 0L, 25626036449581L),
-        #    (u'test/pears/How To Win Friends And Influence People.pdf', u'2012-09-20T11:45:15.0694', 0L, 25626036449581L),
-        #    (u'test/pears/moms_birthday.jpg', u'2012-09-20T11:45:15.5615', 0L, 25626036449581L),
-        #    (u'test/pears/poodle_strut.mov', u'2012-09-20T11:45:16.0290', 0L, 25626036449581L),
-        #    (u'test/pears/Disturbed - Down With The Sickness.mp3', u'2012-09-20T11:45:16.4950', 0L, 25626036449581L),
-        #    (u'test/pears/army_of_darkness.avi', u'2012-09-20T11:45:16.9844', 0L, 25626036449581L),
-        #    (u'test/pears/the_mad.avi', u'2012-09-20T11:45:17.4516', 0L, 25626036449581L),
-        #    (u'test/apples/photos/animals/dogs/poodle.jpg', u'2012-09-20T11:45:17.9281', 0L, 25626036449581L),
-        #    (u'test/apples/photos/animals/dogs/terrier.jpg', u'2012-09-20T11:45:18.3918', 0L, 25626036449581L),
-        #    (u'test/apples/photos/animals/cats/persian.jpg', u'2012-09-20T11:45:18.8626', 0L, 25626036449581L),
-        #    (u'test/apples/photos/animals/cats/siamese.jpg', u'2012-09-20T11:45:19.3686', 0L, 25626036449581L),
-        #    (u'test/apples/photos/plants/fern.jpg', u'2012-09-20T11:45:19.8464', 0L, 25626036449581L),
-        #    (u'test/apples/photos/plants/rose.jpg', u'2012-09-20T11:45:20.7334', 0L, 25626036449581L)]
-        timeline_header = (data['entity'], data['resource'],
-                           data['start_date'], data['end_date'],
-                           data['details'], data['operation'])
-        timeline_body = [timeline_header] * 2
-    form = TimelineForm(data)
-    if form.is_valid():
-        print '>>>', form.cleaned_data
+        form = TimelineForm(data)
+        if form.is_valid():
+            data = form.cleaned_data
+            timeline_header = ('entity', 'resource',
+                               'event name', 'event date',
+                               'incremental cost', 'total cost')
+            timeline_body = timeline_charge(
+                                    data['entity'],     data['resource'],
+                                    data['start_date'], data['end_date'],
+                                    data['details'],    data['operation'])
+        
     return render_response(template='im/timeline.html',
                            context_instance=get_context(request),
                            form=form,