summaryrefslogtreecommitdiff
path: root/src/meetingtools/apps/stats/views.py
blob: 38f21d1a6ad84930d7de107e6d552c297f15d8a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'''
Created on Jan 16, 2012

@author: leifj
'''
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden, HttpResponseBadRequest
from meetingtools.ac import ac_api_client
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")
    return date

def _iso2ts(iso):
    return mktime(iso8601.parse_date(iso).timetuple())*1000

def _date_ts(date):
    (y,m,d) = date.split("-")
    return int(mktime((int(y),int(m),int(d),12,0,0,0,0,-1)))*1000 # high noon

@login_required
def user(request,username=None):
    if username == None:
        username = request.user.username
    (local,domain) = username.split('@')
    return respond_to(request,{'text/html': 'apps/stats/user.html'},{'domain': domain,'username': username})

@login_required
def domain(request,domain):
    (l,d) = request.user.username.split('@')
    if d != 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!")
    
    if username == None:
        username = request.user.username
    
    api = ac_api_client(request)
    p = {'sort1-type': 'asc','sort2-type': 'asc','sort1': 'date-created','sort2': 'date-closed','filter-type': 'meeting','filter-login':username}
    
    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
    rc = {}
    for row in r.et.xpath("//row"):
        rc[row.get('sco-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()), 'minutes': int(t_ms/60000)},request)

@login_required
def domain_minutes_api(request,domain):
    api = ac_api_client(request)
    p = {'sort': 'asc','sort1': 'date-created','filter-type': 'meeting'}
    
    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
    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")
        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]), 'users': len(uc.keys()), 'minutes': int(t_ms/60000)},request)