Prefetches Collaborators
[laas.git] / src / booking / stats.py
1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from booking.models import Booking
10 import datetime
11 import pytz
12
13
14 class StatisticsManager(object):
15
16     @staticmethod
17     def getContinuousBookingTimeSeries(span=28):
18         """
19         Will return a dictionary of names and 2-D array of x and y data points.
20         e.g. {"plot1": [["x1", "x2", "x3"],["y1", "y2", "y3]]}
21         x values will be dates in string
22         every change (booking start / end) will be reflected,
23         instead of one data point per day
24         y values are the integer number of bookings/users active at
25         some point in the given date span is the number of days to plot.
26         The last x value will always be the current time
27         """
28         data = []
29         x = []
30         y = []
31         users = []
32         now = datetime.datetime.now(pytz.utc)
33         delta = datetime.timedelta(days=span)
34         end = now - delta
35         bookings = Booking.objects.filter(start__lte=now, end__gte=end).prefetch_related("collaborators")
36         for booking in bookings:  # collect data from each booking
37             user_list = [u.pk for u in booking.collaborators.all()]
38             user_list.append(booking.owner.pk)
39             data.append((booking.start, 1, user_list))
40             data.append((booking.end, -1, user_list))
41
42         # sort based on time
43         data.sort(key=lambda i: i[0])
44
45         # collect data
46         count = 0
47         active_users = {}
48         for datum in data:
49             x.append(str(datum[0]))  # time
50             count += datum[1]  # booking count
51             y.append(count)
52             for pk in datum[2]:  # maintain count of each user's active bookings
53                 active_users[pk] = active_users.setdefault(pk, 0) + datum[1]
54                 if active_users[pk] == 0:
55                     del active_users[pk]
56             users.append(len([x for x in active_users.values() if x > 0]))
57
58         return {"booking": [x, y], "user": [x, users]}