refacto(sleep-reports): #283 Replace times and labels lists by a single days list.

This commit is contained in:
codisart 2021-10-30 15:29:21 +02:00 committed by Christopher Charbonneau Wells
parent 04d191fe7c
commit bb8466b51b
1 changed files with 32 additions and 36 deletions

View File

@ -21,8 +21,7 @@ def sleep_pattern(sleeps):
:param sleeps: a QuerySet of Sleep instances. :param sleeps: a QuerySet of Sleep instances.
:returns: a tuple of the the graph's html and javascript. :returns: a tuple of the the graph's html and javascript.
""" """
times = {} days = {}
labels = {}
last_end_time = None last_end_time = None
adjustment = None adjustment = None
for sleep in sleeps: for sleep in sleeps:
@ -34,18 +33,14 @@ def sleep_pattern(sleeps):
# Ensure that lists are initialized for the start and end date (as they # Ensure that lists are initialized for the start and end date (as they
# may be different dates). # may be different dates).
if start_date not in times: if start_date not in days:
times[start_date] = [] days[start_date] = []
if start_date not in labels: if end_date not in days:
labels[start_date] = [] days[end_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:
_add_adjustment(adjustment, times, labels) _add_adjustment(adjustment, days)
last_end_time = timezone.localtime(adjustment['end_time']) last_end_time = timezone.localtime(adjustment['end_time'])
adjustment = None adjustment = None
@ -69,13 +64,16 @@ def sleep_pattern(sleeps):
last_end_time = start_time.replace(hour=0, minute=0, second=0) last_end_time = start_time.replace(hour=0, minute=0, second=0)
# Awake time. # Awake time.
times[start_date].append((start_time - last_end_time).seconds/60) days[start_date].append({
labels[start_date].append(None) 'time': (start_time - last_end_time).seconds / 60,
'label': None
})
# Asleep time. # Asleep time.
times[start_date].append(duration.seconds/60) days[start_date].append({
labels[start_date].append(_format_label(duration, start_time, 'time': duration.seconds / 60,
end_time)) 'label': _format_label(duration, start_time, end_time)}
)
# Update the previous entry duration if an offset change occurred. # Update the previous entry duration if an offset change occurred.
# This can happen when an entry crosses a daylight savings time change. # This can happen when an entry crosses a daylight savings time change.
@ -84,20 +82,21 @@ def sleep_pattern(sleeps):
duration -= timezone.timedelta(seconds=diff.seconds) duration -= timezone.timedelta(seconds=diff.seconds)
yesterday = (end_time - timezone.timedelta(days=1)) yesterday = (end_time - timezone.timedelta(days=1))
yesterday = yesterday.date().isoformat() yesterday = yesterday.date().isoformat()
times[yesterday][len(times[yesterday]) - 1] = duration.seconds/60 days[yesterday][len(days[yesterday]) - 1] = {
labels[yesterday][len(times[yesterday]) - 1] = _format_label( 'time': duration.seconds / 60,
duration, start_time, end_time) 'label': _format_label(duration, start_time, end_time)
}
last_end_time = end_time last_end_time = end_time
# Handle any left over adjustment (if the last entry crossed midnight). # Handle any left over adjustment (if the last entry crossed midnight).
if adjustment: if adjustment:
_add_adjustment(adjustment, times, labels) _add_adjustment(adjustment, days)
# Create dates for x-axis using a 12:00:00 time to ensure correct # Create dates for x-axis using a 12:00:00 time to ensure correct
# positioning of bars (covering entire day). # positioning of bars (covering entire day).
dates = [] dates = []
for time in list(times.keys()): for time in list(days.keys()):
dates.append('{} 12:00:00'.format(time)) dates.append('{} 12:00:00'.format(time))
traces = [] traces = []
@ -106,15 +105,15 @@ def sleep_pattern(sleeps):
# Set iterator and determine maximum iteration for dates. # Set iterator and determine maximum iteration for dates.
i = 0 i = 0
max_i = 0 max_i = 0
for date_times in times.values(): for date_times in days.values():
max_i = max(len(date_times), max_i) max_i = max(len(date_times), max_i)
while i < max_i: while i < max_i:
y = {} y = {}
text = {} text = {}
for date in times.keys(): for date in days.keys():
try: try:
y[date] = times[date][i] y[date] = days[date][i].time
text[date] = labels[date][i] text[date] = days[date][i].label
except IndexError: except IndexError:
y[date] = None y[date] = None
text[date] = None text[date] = None
@ -172,24 +171,21 @@ def sleep_pattern(sleeps):
return utils.split_graph_output(output) return utils.split_graph_output(output)
def _add_adjustment(adjustment, times, labels): def _add_adjustment(adjustment, days):
""" """
Adds "adjustment" data for entries that cross midnight. Adds "adjustment" data for entries that cross midnight.
:param adjustment: Column, start time, end time, and duration of entry. :param adjustment: Column, start time, end time, and duration of entry.
:param times: Graph times data. :param blocks: List of days
:param labels: Graph labels data.
""" """
column = adjustment.pop('column')
# Fake (0) entry to keep the color switching logic working. # Fake (0) entry to keep the color switching logic working.
times[adjustment['column']].append(0) days[column].append({'time': 0, 'label': 0})
labels[adjustment['column']].append(0)
# Real adjustment entry. # Real adjustment entry.
times[adjustment['column']].append( days[column].append({
adjustment['duration'].seconds / 60) 'time': adjustment['duration'].seconds / 60,
labels[adjustment['column']].append(_format_label( 'label': _format_label(**adjustment)
adjustment['duration'], })
adjustment['start_time'],
adjustment['end_time']))
def _format_label(duration, start_time, end_time): def _format_label(duration, start_time, end_time):