From 47773eae78862b8346c5f328e0fa6b6cd71f9564 Mon Sep 17 00:00:00 2001 From: Leif Johansson Date: Tue, 17 Jan 2012 12:19:44 +0100 Subject: per-room usage statistics too --- src/meetingtools/apps/stats/views.py | 86 +++++++++++++++++++++++++++++++++++- src/meetingtools/urls.py | 6 ++- src/templates/apps/room/list.html | 2 +- src/templates/apps/stats/domain.html | 4 +- src/templates/apps/stats/room.html | 32 ++++++++++++++ src/templates/apps/stats/user.html | 2 +- 6 files changed, 125 insertions(+), 7 deletions(-) create mode 100644 src/templates/apps/stats/room.html diff --git a/src/meetingtools/apps/stats/views.py b/src/meetingtools/apps/stats/views.py index 5090c1f..8cdee25 100644 --- a/src/meetingtools/apps/stats/views.py +++ b/src/meetingtools/apps/stats/views.py @@ -10,6 +10,8 @@ from iso8601 import iso8601 from time import mktime from meetingtools.multiresponse import json_response, respond_to from meetingtools.apps.stats.forms import StatCaledarForm +from django.shortcuts import get_object_or_404 +from meetingtools.apps.room.models import Room def _iso2datesimple(iso): (date,rest) = iso.split("T") @@ -31,11 +33,19 @@ def user(request): def domain(request,domain): (l,d) = request.user.username.split('@') if d != domain: - return HttpResponseForbidden("You can only look at stats for your own domain!") + return HttpResponseForbidden("You can only look at statistics for your own domain!") return respond_to(request,{'text/html': 'apps/stats/domain.html'},{'domain': domain}) @login_required +def room(request,rid): + room = get_object_or_404(Room,pk=rid) + if not room.creator == request.user: + return HttpResponseForbidden("You can only look at statistics for your own rooms!") + + return respond_to(request,{'text/html': 'apps/stats/room.html'},{'room': room}) + +@login_required def user_minutes_api(request,username=None): if username and username != request.user.username: return HttpResponseForbidden("You can't spy on others!") @@ -131,12 +141,84 @@ def domain_minutes_api(request,domain): curdate = None t_ms = 0 rc = {} + uc = {} for row in r.et.xpath("//row"): login = row.findtext("login") if not login.endswith("@%s" % domain): continue rc[row.get('sco-id')] = True + uc[row.get('principal-id')] = True + date_created_str = row.findtext("date-created") + ts_created = _iso2ts(date_created_str) + date_closed_str = row.findtext("date-closed") + ts_closed = _iso2ts(date_closed_str) + + d1 = _iso2datesimple(date_created_str) + if d_created == None: + d_created = d1 + + d2 = _iso2datesimple(date_closed_str) + if d_closed == None: + d_closed = d2 + + if curdate == None: + curdate = d1 + + if curdate != d1: + series.append([_date_ts(curdate),int(ms/60000)]) + ms = 0 + curdate = d1 + + if d1 == d2: #same date + diff = (ts_closed - ts_created) + ms = ms + diff + t_ms = t_ms + diff + else: # meeting spanned midnight + ts_date_ts = _date_ts(d2) + ms = ms + (ts_date_ts - ts_created) + series.append([_date_ts(d1),int(ms/60000)]) + t_ms = t_ms + ms + curdate = d2 + ms = (ts_closed - ts_date_ts) + + if curdate != None and ms > 0: + series.append([_date_ts(curdate),int(ms/60000)]) + + return json_response({'data': sorted(series,key=lambda x: x[0]), 'rooms': len(rc.keys()), 'users': len(uc.keys()), 'minutes': int(t_ms/60000)},request) + + +@login_required +def room_minutes_api(request,rid): + room = get_object_or_404(Room,pk=rid) + if not room.creator == request.user: + return HttpResponseForbidden("You can only look at statistics for your own rooms!") + + api = ac_api_client(request) + p = {'sort': 'asc','sort1': 'date-created','filter-type': 'meeting','filter-sco-id': room.sco_id} + + form = StatCaledarForm(request.GET) + if not form.is_valid(): + return HttpResponseBadRequest() + + begin = form.cleaned_data['begin'] + end = form.cleaned_data['end'] + + if begin != None: + p['filter-gte-date-created'] = begin + if end != None: + p['filter-lt-date-created'] = end + r = api.request('report-bulk-consolidated-transactions',p) + + series = [] + d_created = None + d_closed = None + ms = 0 + curdate = None + t_ms = 0 + uc = {} + for row in r.et.xpath("//row"): + uc[row.get('principal-id')] = True date_created_str = row.findtext("date-created") ts_created = _iso2ts(date_created_str) date_closed_str = row.findtext("date-closed") @@ -173,4 +255,4 @@ def domain_minutes_api(request,domain): if curdate != None and ms > 0: series.append([_date_ts(curdate),int(ms/60000)]) - return json_response({'data': sorted(series,key=lambda x: x[0]), 'rooms': len(rc.keys()), 'minutes': int(t_ms/60000)},request) \ No newline at end of file + return json_response({'data': sorted(series,key=lambda x: x[0]), 'users': len(uc.keys()), 'minutes': int(t_ms/60000)},request) \ No newline at end of file diff --git a/src/meetingtools/urls.py b/src/meetingtools/urls.py index 13382d8..d1a6ef2 100644 --- a/src/meetingtools/urls.py +++ b/src/meetingtools/urls.py @@ -42,9 +42,11 @@ urlpatterns = patterns('', # Uncomment the admin/doc line below to enable admin documentation: # (r'^admin/doc/', include('django.contrib.admindocs.urls')), (r'^api/stats/user/(.*)$','meetingtools.apps.stats.views.user_minutes_api'), - (r'^api/stats/domain/(.*)$','meetingtools.apps.stats.views.domain_minutes_api'), + (r'^api/stats/domain/(.+)$','meetingtools.apps.stats.views.domain_minutes_api'), + (r'^api/stats/room/(\d+)$','meetingtools.apps.stats.views.room_minutes_api'), (r'^stats$','meetingtools.apps.stats.views.user'), - (r'^stats/(.+)$','meetingtools.apps.stats.views.domain'), + (r'^stats/domain/(.+)$','meetingtools.apps.stats.views.domain'), + (r'^stats/room/(\d+)$','meetingtools.apps.stats.views.room'), # Uncomment the next line to enable the admin: (r'^admin/', include(admin.site.urls)) ) diff --git a/src/templates/apps/room/list.html b/src/templates/apps/room/list.html index c314f4b..5de2221 100644 --- a/src/templates/apps/room/list.html +++ b/src/templates/apps/room/list.html @@ -39,7 +39,7 @@ {% if r.self_cleaning %}
  • » Room will be reset when empty.
  • {%else%}
  • » Room state is preserved between sessions.
  • {% endif %} {% if r.allow_host %}
  • » First participant can elect to become host.
  • {% endif %} {% if r.lastvisited %}
  • » Last visited {{r.lastvisited|datehumanize}}
  • {%endif%} -
  • » Hosted on {{r.acc.name}}
  • +
  • » Hosted on {{r.acc.name}} (usage statistics)