Ensure lists are initialized for sleep pattern graph days

Fixed #293
This commit is contained in:
Christopher C. Wells 2021-08-27 20:06:18 -07:00
parent 81fad7d30e
commit ce805f501b
1 changed files with 32 additions and 12 deletions

View File

@ -26,27 +26,23 @@ def sleep_pattern(instances):
start_time = timezone.localtime(instance.start) start_time = timezone.localtime(instance.start)
end_time = timezone.localtime(instance.end) end_time = timezone.localtime(instance.end)
start_date = start_time.date().isoformat() start_date = start_time.date().isoformat()
end_date = end_time.date().isoformat()
duration = instance.duration duration = instance.duration
# Ensure that lists are initialized for the start and end date (as they
# may be different dates).
if start_date not in times: if start_date not in times:
times[start_date] = [] times[start_date] = []
if start_date not in labels: if start_date not in labels:
labels[start_date] = [] labels[start_date] = []
if end_date not in times:
times[end_date] = []
if end_date not in labels:
labels[end_date] = []
# Check if the previous entry crossed midnight (see below). # Check if the previous entry crossed midnight (see below).
if adjustment: if adjustment:
# Fake (0) entry to keep the color switching logic working. _add_adjustment(adjustment, times, labels)
times[adjustment['column']].append(0)
labels[adjustment['column']].append(0)
# Real adjustment entry.
times[adjustment['column']].append(
adjustment['duration'].seconds/60)
labels[adjustment['column']].append(_format_label(
adjustment['duration'],
adjustment['start_time'],
adjustment['end_time']))
last_end_time = timezone.localtime(adjustment['end_time']) last_end_time = timezone.localtime(adjustment['end_time'])
adjustment = None adjustment = None
@ -91,6 +87,10 @@ def sleep_pattern(instances):
last_end_time = end_time last_end_time = end_time
# Handle any left over adjustment (if the last entry crossed midnight).
if adjustment:
_add_adjustment(adjustment, times, labels)
dates = list(times.keys()) dates = list(times.keys())
traces = [] traces = []
color = 'rgba(255, 255, 255, 0)' color = 'rgba(255, 255, 255, 0)'
@ -159,6 +159,26 @@ def sleep_pattern(instances):
return utils.split_graph_output(output) return utils.split_graph_output(output)
def _add_adjustment(adjustment, times, labels):
"""
Adds "adjustment" data for entries that cross midnight.
:param adjustment: Column, start time, end time, and duration of entry.
:param times: Graph times data.
:param labels: Graph labels data.
"""
# Fake (0) entry to keep the color switching logic working.
times[adjustment['column']].append(0)
labels[adjustment['column']].append(0)
# Real adjustment entry.
times[adjustment['column']].append(
adjustment['duration'].seconds / 60)
labels[adjustment['column']].append(_format_label(
adjustment['duration'],
adjustment['start_time'],
adjustment['end_time']))
def _format_label(duration, start_time, end_time): def _format_label(duration, start_time, end_time):
""" """
Formats a time block label. Formats a time block label.