aboutsummaryrefslogtreecommitdiffstats
path: root/src/dashboard/admin_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/dashboard/admin_utils.py')
-rw-r--r--src/dashboard/admin_utils.py98
1 files changed, 95 insertions, 3 deletions
diff --git a/src/dashboard/admin_utils.py b/src/dashboard/admin_utils.py
index 045caeb..821ca03 100644
--- a/src/dashboard/admin_utils.py
+++ b/src/dashboard/admin_utils.py
@@ -30,6 +30,7 @@ import json
import sys
import inspect
import pydoc
+import csv
from django.contrib.auth.models import User
@@ -43,9 +44,7 @@ from resource_inventory.pdf_templater import PDFTemplater
from booking.quick_deployer import update_template
-from datetime import timedelta
-
-from django.utils import timezone
+from datetime import timedelta, date, datetime, timezone
from booking.models import Booking
from notifier.manager import NotificationHandler
@@ -225,6 +224,99 @@ def get_info(host_labid, lab_username):
return info
+class CumulativeData:
+ use_days = 0
+ count_bookings = 0
+ count_extensions = 0
+
+ def __init__(self, file_writer):
+ self.file_writer = file_writer
+
+ def account(self, booking, usage_days):
+ self.count_bookings += 1
+ self.count_extensions += booking.ext_count
+ self.use_days += usage_days
+
+ def write_cumulative(self):
+ self.file_writer.writerow([])
+ self.file_writer.writerow([])
+ self.file_writer.writerow(['Lab Use Days', 'Count of Bookings', 'Total Extensions Used'])
+ self.file_writer.writerow([self.use_days, self.count_bookings, (self.count_bookings * 2) - self.count_extensions])
+
+
+def get_years_booking_data(start_year=None, end_year=None):
+ """
+ Outputs yearly booking information from the past 'start_year' years (default: current year)
+ until the last day of the end year (default current year) as a csv file.
+ """
+ if start_year is None and end_year is None:
+ start = datetime.combine(date(datetime.now().year, 1, 1), datetime.min.time()).replace(tzinfo=timezone.utc)
+ end = datetime.combine(date(start.year + 1, 1, 1), datetime.min.time()).replace(tzinfo=timezone.utc)
+ elif end_year is None:
+ start = datetime.combine(date(start_year, 1, 1), datetime.min.time()).replace(tzinfo=timezone.utc)
+ end = datetime.combine(date(datetime.now().year, 1, 1), datetime.min.time()).replace(tzinfo=timezone.utc)
+ else:
+ start = datetime.combine(date(start_year, 1, 1), datetime.min.time()).replace(tzinfo=timezone.utc)
+ end = datetime.combine(date(end_year + 1, 1, 1), datetime.min.time()).replace(tzinfo=timezone.utc)
+
+ if (start.year == end.year - 1):
+ file_name = "yearly_booking_data_" + str(start.year) + ".csv"
+ else:
+ file_name = "yearly_booking_data_" + str(start.year) + "-" + str(end.year - 1) + ".csv"
+
+ with open(file_name, "w", newline="") as file:
+ file_writer = csv.writer(file)
+ cumulative_data = CumulativeData(file_writer)
+ file_writer.writerow(
+ [
+ 'ID',
+ 'Project',
+ 'Purpose',
+ 'User',
+ 'Collaborators',
+ 'Extensions Left',
+ 'Usage Days',
+ 'Start',
+ 'End'
+ ]
+ )
+
+ for booking in Booking.objects.filter(start__gte=start, start__lte=end):
+ filtered = False
+ booking_filter = [279]
+ user_filter = ["ParkerBerberian", "ssmith", "ahassick", "sbergeron", "jhodgdon", "rhodgdon", "aburch", "jspewock"]
+ user = booking.owner.username if booking.owner.username is not None else "None"
+
+ for b in booking_filter:
+ if b == booking.id:
+ filtered = True
+
+ for u in user_filter:
+ if u == user:
+ filtered = True
+ # trims time delta to the the specified year(s) if between years
+ usage_days = ((end if booking.end > end else booking.end) - (start if booking.start < start else booking.start)).days
+ collaborators = []
+
+ for c in booking.collaborators.all():
+ collaborators.append(c.username)
+
+ if (not filtered):
+ cumulative_data.account(booking, usage_days)
+ file_writer.writerow([
+ str(booking.id),
+ str(booking.project),
+ str(booking.purpose),
+ str(booking.owner.username),
+ ','.join(collaborators),
+ str(booking.ext_count),
+ str(usage_days),
+ str(booking.start),
+ str(booking.end)
+ ])
+ cumulative_data.write_cumulative()
+
+
def map_cntt_interfaces(labid: str):
"""
Use this during cntt migrations, call it with a host labid and it will change profiles for this host