Skip to content
Snippets Groups Projects
Commit cf20d100 authored by Mario Raciti's avatar Mario Raciti
Browse files

TMSS-610: Update get_usage_mode by gathering durations per semester, not per week anymore

parent d5c2c7b9
No related branches found
No related tags found
3 merge requests!634WIP: COBALT commissioning delta,!492Resolve TMSS-610,!481Draft: SW-971 SW-973 SW-975: Various fixes to build LOFAR correctly.
......@@ -198,50 +198,31 @@ def _get_projects_summary(request: Request, cycle: models.Cycle) -> {}:
return projects_summary
def _get_usage_mode(cycle: models.Cycle) -> []:
def _get_usage_mode(cycle: models.Cycle) -> {}:
"""
Help function to retrieve ILT and local usage mode info.
"""
result = []
# Get all the reservations related to the projects in the cycle
reservations = models.Reservation.objects.filter(project__cycles=cycle.pk)
# # Get all the reservations in stand-alone mode
# reservations_sa_mode = models.Reservation.objects.filter(specifications_doc__activity__type='stand-alone mode')
# Get the first Monday before the cycle.start
start, stop = cycle.start - timedelta(days=cycle.start.weekday()), cycle.stop
# Iterate through weeks and sum durations per week
overflow, week_duration = 0, 0
step, d = timedelta(days=7), start
while d < stop:
# Durations entirely contained in the current week
durations_in_week = reservations.filter(start_time__range=(d, d + step), stop_time__lt=(d + step)) \
.aggregate(duration=Sum(F('stop_time') - F('start_time')))['duration'] or 0
# Update week_duration and reset overflow
week_duration += overflow + durations_in_week
overflow = 0
# Reservations with overflow durations between two or more weeks
# TODO: This only covers an overflow of a week (two weeks coverage).
# We need to consider also more weeks (>= 3 weeks coverage). Maybe making the "overflow" var modular.
overflow_reservations = reservations.filter(start_time__range=(d, d + step), stop_time__gte=(d + step))
for r in overflow_reservations:
# Split the duration and store overflow to be added to the next week
stop_time = r.stop_time - timedelta(days=r.stop_time.weekday())
week_duration += (stop_time - r.start_time).total_seconds()
overflow += timedelta(days=r.stop_time.weekday()).total_seconds()
# Store the duration for the current week
result.append({'week': d.date().isoformat(), 'duration': week_duration if week_duration > 0 else None})
d += step
# TODO: Filter stand-alone mode not for project, sum for each project, mixed project|no project, ILT mode per week.
# result['total_duration'] += r.duration
# result['local_duration'] += r.duration if r.specifications_doc['activity']['type'] == 'stand-alone mode' else 0
# result['ILT_duration'] = result['total_duration'] - result['local_duration']
result = {'all modes': {}, 'stand-alone mode': {}, 'ILT mode': {}}
# Get all the reservations in the cycle semester
reservations = models.Reservation.objects.filter(start_time__gte=cycle.start, stop_time__lte=cycle.stop)
result['all modes']['total'] = reservations.aggregate(duration=Sum(F('stop_time') - F('start_time')))['duration'] or 0
reservations_projects = reservations.filter(project__project_category='regular', project__cycles=cycle.pk) # Production projects
result['all modes']['observing'] = reservations_projects.aggregate(duration=Sum(F('stop_time') - F('start_time')))['duration'] or 0
reservations_mixed = reservations.filter(project__cycles=cycle.pk).exclude(project__project_category='regular') # Non-production projects
result['all modes']['idle/test'] = reservations_mixed.aggregate(duration=Sum(F('stop_time') - F('start_time')))['duration'] or 0
# Get all the reservations in stand-alone mode
reservations_sa = reservations.filter(specifications_doc__activity__type='stand-alone mode')
result['stand-alone mode']['total'] = reservations_sa.aggregate(duration=Sum(F('stop_time') - F('start_time')))['duration'] or 0
result['stand-alone mode']['no project'] = reservations_sa.filter(project=None).aggregate(duration=Sum(F('stop_time') - F('start_time')))['duration'] or 0
result['stand-alone mode']['project'] = (reservations_sa & reservations_projects).aggregate(duration=Sum(F('stop_time') - F('start_time')))['duration'] or 0
result['stand-alone mode']['mixed/no project'] = (reservations_sa & reservations_mixed).aggregate(duration=Sum(F('stop_time') - F('start_time')))['duration'] or 0
# Get ILT durations
result['ILT mode']['total'] = result['all modes']['total'] - result['stand-alone mode']['total']
result['ILT mode']['observing'] = result['all modes']['observing'] - result['stand-alone mode']['project']
result['ILT mode']['idle/test'] = result['all modes']['idle/test'] - result['stand-alone mode']['mixed/no project']
return result
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment