summaryrefslogtreecommitdiff
path: root/meetingtools/apps/stats/tasks.py
blob: 0a26d664da1d959f2ec4641cdf5eef978e9d2996 (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
import logging
from celery.schedules import crontab
from celery.task import periodic_task, task
from meetingtools.apps.room.models import Room
from meetingtools.ac import ac_api_client
from meetingtools.apps.cluster.models import ACCluster
from datetime import datetime,timedelta
from meetingtools.apps.stats.models import UserMeetingTransaction

__author__ = 'leifj'


def import_acc_sessions(acc,since=0,room_last_visited=False):
    with ac_api_client(acc) as api:
        p = {'sort': 'asc','sort1': 'date-created','filter-type': 'meeting'}

        begin = None
        if since > 0:
            begin = datetime.now()-timedelta(seconds=since)
            begin = begin.replace(microsecond=0)

        if begin is not None:
            p['filter-gte-date-created'] = begin.isoformat()

        r = api.request('report-bulk-consolidated-transactions',p,True)
        nr = 0
        ne = 0
        for tx in r.et.findall(".//row"):
            try:
                tx = UserMeetingTransaction.create(acc,tx)
                if tx:
                    if room_last_visited:
                        rooms = Room.objects.filter(sco=tx.sco)
                        if len(rooms) == 1:
                            room = rooms[0]
                            if room.lastvisited is None or room.lastvisited < tx.date_closed:
                                room.lastvisited = tx.date_created
                                room.save()
                    nr += 1
            except Exception,ex:
                logging.error(ex)
                ne += 1
        logging.info("%s: Imported %d transactions with %d errors" % (acc,nr,ne))

@task
def import_sessions(since,room_last_visited=False):
    for acc in ACCluster.objects.all():
        import_acc_sessions(acc,since,room_last_visited)

@periodic_task(run_every=crontab(hour="*", minute="*/5", day_of_week="*"))
def _hourly_import():
    import_sessions(600,True)


#@periodic_task(run_every=crontab(hour="5", minute="0", day_of_week="*"))
def timed_full_import():
    years = [2009, 2010, 2011, 2012, 2013, 2014]
    for acc in ACCluster.objects.all():
        for year in years:
            begin = datetime(year=year, month=1, day=1)
            end = datetime(year=year, month=12, day=31)
            with ac_api_client(acc) as api:
                p = {'sort': 'asc', 'sort1': 'date-created', 'filter-type': 'meeting',
                     'filter-gte-date-created': begin.isoformat(), 'filter-lte-date-created': end.isoformat()}
                r = api.request('report-bulk-consolidated-transactions', p, True)
                nr = 0
                ne = 0
                for tx in r.et.findall(".//row"):
                    try:
                        tx = UserMeetingTransaction.create(acc, tx)
                        if tx:
                            rooms = Room.objects.filter(sco=tx.sco)
                            if len(rooms) == 1:
                                room = rooms[0]
                                if room.lastvisited is None or room.lastvisited < tx.date_closed:
                                    room.lastvisited = tx.date_created
                                    room.save()
                            nr += 1
                    except Exception, ex:
                        logging.error(ex)
                        ne += 1
                logging.info("%s: Imported %d transactions with %d errors" % (acc, nr, ne))