refacto(sleep-reports): #283 Rename some variables and add constants for magic strings

This commit is contained in:
codisart 2021-10-30 15:20:41 +02:00 committed by Christopher Charbonneau Wells
parent c33ace2bf9
commit c76f7d7d2d
1 changed files with 14 additions and 11 deletions

View File

@ -11,23 +11,26 @@ from core.utils import duration_string
from reports import utils from reports import utils
ASLEEP_COLOR = 'rgb(35, 110, 150)'
AWAKE_COLOR = 'rgba(255, 255, 255, 0)'
def sleep_pattern(instances):
def sleep_pattern(sleeps):
""" """
Create a graph showing blocked out periods of sleep during each day. Create a graph showing blocked out periods of sleep during each day.
:param instances: 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 = {} times = {}
labels = {} labels = {}
last_end_time = None last_end_time = None
adjustment = None adjustment = None
for instance in instances: for sleep in sleeps:
start_time = timezone.localtime(instance.start) start_time = timezone.localtime(sleep.start)
end_time = timezone.localtime(instance.end) end_time = timezone.localtime(sleep.end)
start_date = start_time.date().isoformat() start_date = start_time.date().isoformat()
end_date = end_time.date().isoformat() end_date = end_time.date().isoformat()
duration = instance.duration duration = sleep.duration
# 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).
@ -50,7 +53,7 @@ def sleep_pattern(instances):
if end_time.date() != start_time.date(): if end_time.date() != start_time.date():
adj_start_time = end_time.replace(hour=0, minute=0, second=0) adj_start_time = end_time.replace(hour=0, minute=0, second=0)
adjustment = { adjustment = {
'column': end_time.date().isoformat(), 'column': end_date,
'start_time': adj_start_time, 'start_time': adj_start_time,
'end_time': end_time, 'end_time': end_time,
'duration': end_time - adj_start_time 'duration': end_time - adj_start_time
@ -98,7 +101,7 @@ def sleep_pattern(instances):
dates.append('{} 12:00:00'.format(time)) dates.append('{} 12:00:00'.format(time))
traces = [] traces = []
color = 'rgba(255, 255, 255, 0)' color = AWAKE_COLOR
# Set iterator and determine maximum iteration for dates. # Set iterator and determine maximum iteration for dates.
i = 0 i = 0
@ -127,10 +130,10 @@ def sleep_pattern(instances):
marker={'color': color}, marker={'color': color},
showlegend=False, showlegend=False,
)) ))
if color == 'rgba(255, 255, 255, 0)': if color == AWAKE_COLOR:
color = 'rgb(35, 110, 150)' color = ASLEEP_COLOR
else: else:
color = 'rgba(255, 255, 255, 0)' color = AWAKE_COLOR
layout_args = utils.default_graph_layout_options() layout_args = utils.default_graph_layout_options()
layout_args['margin']['b'] = 100 layout_args['margin']['b'] = 100