Skip to content

Commit

Permalink
Fix Laundry Snapshot Collection (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuneerroy authored Nov 7, 2023
1 parent de53021 commit 4ae2611
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions backend/laundry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import datetime

from django.core.cache import cache
from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.utils.timezone import make_aware
from requests.exceptions import HTTPError
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
Expand Down Expand Up @@ -64,29 +64,21 @@ def safe_division(a, b):
return round(a / float(b), 3) if b > 0 else 0

def get_snapshot_info(hall_id):
now = timezone.localtime()

# start is beginning of day, end is 27 hours after start
start = make_aware(datetime.datetime(year=now.year, month=now.month, day=now.day))
end = start + datetime.timedelta(hours=27)

# filters for LaundrySnapshots within timeframe
room = get_object_or_404(LaundryRoom, hall_id=hall_id)

snapshots = LaundrySnapshot.objects.filter(room=room, date__gt=start, date__lte=end)
# get start time, which is now without the times
start = timezone.localtime().replace(hour=0, minute=0, second=0, microsecond=0)

# adds all the LaundrySnapshots from the same weekday within the previous 28 days
for week in range(1, 4):
# new_start is beginning of day, new_end is 27 hours after start
filter = Q()
for week in range(4):
new_start = start - datetime.timedelta(weeks=week)
new_end = new_start + datetime.timedelta(hours=27)
filter |= Q(room=room, date__gt=new_start, date__lt=new_end)

new_snapshots = LaundrySnapshot.objects.filter(
room=room, date__gt=new_start, date__lte=new_end
)
snapshots = snapshots.union(new_snapshots)

return (room, snapshots.order_by("-date"))
snapshots = LaundrySnapshot.objects.filter(filter).order_by("-date")
return (room, snapshots)

def compute_usage(hall_id):
try:
Expand All @@ -103,13 +95,8 @@ def compute_usage(hall_id):

for snapshot in snapshots:
date = snapshot.date.astimezone()

if date < min_date:
min_date = date

if date > max_date:
max_date = date

min_date = min(min_date, date)
max_date = max(max_date, date)
hour = date.hour

# accounts for the 3 hours on the next day
Expand All @@ -132,8 +119,12 @@ def compute_usage(hall_id):
"day_of_week": calendar.day_name[timezone.localtime().weekday()],
"start_date": min_date.date(),
"end_date": max_date.date(),
"washer_data": {x: HallUsage.safe_division(data[x][0], data[x][2]) for x in range(27)},
"dryer_data": {x: HallUsage.safe_division(data[x][1], data[x][2]) for x in range(27)},
"washer_data": {
x: HallUsage.safe_division(data[x][0], data[x][2]) for x in range(len(data))
},
"dryer_data": {
x: HallUsage.safe_division(data[x][1], data[x][2]) for x in range(len(data))
},
"total_number_of_washers": room.total_washers,
"total_number_of_dryers": room.total_dryers,
}
Expand Down

0 comments on commit 4ae2611

Please sign in to comment.