1 ##############################################################################
2 # Copyright (c) 2020 Parker Berberian, Sawyer Bergeron, Sean Smith and others.
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 ##############################################################################
10 from booking.models import Booking
11 from resource_inventory.models import ResourceQuery, ResourceProfile
12 from datetime import datetime, timedelta
13 from collections import Counter
17 class StatisticsManager(object):
20 def getContinuousBookingTimeSeries(span=28):
22 Calculate Booking usage data points.
24 Gathers all active bookings that fall in interval [(now - span), (now + 1 week)].
25 x data points are every 12 hours
26 y values are the integer number of bookings/users active at time
30 '#6BDAD5', # Turquoise
31 '#E36386', # Pale Violet Red
32 '#F5B335', # Sandy Brown
34 '#BCE194', # Gainsboro
35 '#00CE7C', # Sea Green
50 profiles = {str(profile): [] for profile in ResourceProfile.objects.all()}
52 now = datetime.now(pytz.utc)
53 delta = timedelta(days=span)
55 end = now + timedelta(weeks=1)
57 bookings = Booking.objects.filter(
60 ).prefetch_related("collaborators")
66 books = bookings.filter(
69 ).prefetch_related("collaborators")
72 active_users += booking.collaborators.all().count() + 1
74 x.append(str(start.month) + '-' + str(start.day))
75 y.append(books.count())
77 step_profiles = Counter([
80 for config in book.resource.template.getConfigs()
83 for profile in ResourceProfile.objects.all():
84 profiles[str(profile)].append(step_profiles[str(profile)])
85 users.append(active_users)
87 start += timedelta(hours=12)
89 in_use = len(ResourceQuery.filter(working=True, booked=True))
90 not_in_use = len(ResourceQuery.filter(working=True, booked=False))
91 maintenance = len(ResourceQuery.filter(working=False))
93 projects = [x.project for x in bookings]
94 proj_count = sorted(Counter(projects).items(), key=lambda x: x[1])
96 project_keys = [proj[0] for proj in proj_count[-5:]]
97 project_keys = ['None' if x is None else x for x in project_keys]
98 project_counts = [proj[1] for proj in proj_count[-5:]]
100 resources = {key: [x, value] for key, value in profiles.items()}
103 "resources": resources,
106 "utils": [in_use, not_in_use, maintenance],
107 "projects": [project_keys, project_counts],
108 "colors": anuket_colors if os.environ.get('TEMPLATE_OVERRIDE_DIR') == 'laas' else lfedge_colors