2017-08-25 16:19:06 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-08-25 18:17:15 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2017-08-25 16:19:06 +00:00
|
|
|
from django.db.models import Count, Case, When
|
|
|
|
from django.db.models.functions import TruncDate
|
2017-08-25 21:32:46 +00:00
|
|
|
from django.utils import timezone
|
2017-08-25 16:19:06 +00:00
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
import plotly.offline as plotly
|
|
|
|
import plotly.graph_objs as go
|
|
|
|
|
2017-09-16 21:17:09 +00:00
|
|
|
from core.models import DiaperChange, Feeding, Sleep, TummyTime
|
2017-09-25 15:39:08 +00:00
|
|
|
from core.utils import duration_string, duration_parts
|
2017-08-25 16:19:06 +00:00
|
|
|
|
2017-10-19 17:19:52 +00:00
|
|
|
from . import utils
|
2017-08-25 16:19:06 +00:00
|
|
|
|
|
|
|
|
2017-09-25 17:14:14 +00:00
|
|
|
def diaperchange_lifetimes(child):
|
|
|
|
"""Create a graph showing how long diapers last (time between changes)."""
|
|
|
|
changes = DiaperChange.objects.filter(child=child).order_by('time')
|
|
|
|
|
|
|
|
durations = []
|
|
|
|
last_change = changes.first()
|
|
|
|
for change in changes[1:]:
|
|
|
|
duration = change.time - last_change.time
|
|
|
|
if duration.seconds > 0:
|
|
|
|
durations.append(duration)
|
|
|
|
last_change = change
|
|
|
|
|
|
|
|
trace = go.Box(
|
|
|
|
y=[round(d.seconds/3600, 2) for d in durations],
|
|
|
|
name='Changes',
|
|
|
|
jitter=0.3,
|
|
|
|
pointpos=-1.8,
|
|
|
|
boxpoints='all'
|
|
|
|
)
|
|
|
|
|
2017-10-19 17:19:52 +00:00
|
|
|
layout_args = utils.default_graph_layout_options()
|
2017-09-25 17:14:14 +00:00
|
|
|
layout_args['title'] = '<b>Diaper Lifetimes</b><br>{}'.format(child)
|
|
|
|
layout_args['yaxis']['title'] = 'Time between changes (hours)'
|
|
|
|
layout_args['yaxis']['zeroline'] = False
|
|
|
|
layout_args['yaxis']['dtick'] = 1
|
|
|
|
|
|
|
|
fig = go.Figure({
|
|
|
|
'data': [trace],
|
|
|
|
'layout': go.Layout(**layout_args)
|
|
|
|
})
|
|
|
|
output = plotly.plot(fig, output_type='div', include_plotlyjs=False)
|
2017-10-19 17:19:52 +00:00
|
|
|
return utils.split_graph_output(output)
|
2017-09-25 17:14:14 +00:00
|
|
|
|
|
|
|
|
2017-08-25 16:19:06 +00:00
|
|
|
def diaperchange_types(child):
|
2017-09-16 21:17:09 +00:00
|
|
|
"""Create a graph showing types of totals for diaper changes."""
|
2017-08-25 16:19:06 +00:00
|
|
|
changes = DiaperChange.objects.filter(child=child) \
|
|
|
|
.annotate(date=TruncDate('time')) \
|
|
|
|
.values('date') \
|
|
|
|
.annotate(wet_count=Count(Case(When(wet=True, then=1)))) \
|
|
|
|
.annotate(solid_count=Count(Case(When(solid=True, then=1)))) \
|
|
|
|
.annotate(total=Count('id')) \
|
|
|
|
.order_by('-date')
|
|
|
|
|
|
|
|
solid_trace = go.Scatter(
|
|
|
|
mode='markers',
|
2017-09-25 16:18:02 +00:00
|
|
|
name='Solid',
|
2017-08-25 16:19:06 +00:00
|
|
|
x=list(changes.values_list('date', flat=True)),
|
|
|
|
y=list(changes.values_list('solid_count', flat=True)),
|
|
|
|
)
|
|
|
|
wet_trace = go.Scatter(
|
|
|
|
mode='markers',
|
2017-09-25 16:18:02 +00:00
|
|
|
name='Wet',
|
2017-08-25 16:19:06 +00:00
|
|
|
x=list(changes.values_list('date', flat=True)),
|
|
|
|
y=list(changes.values_list('wet_count', flat=True))
|
|
|
|
)
|
|
|
|
total_trace = go.Scatter(
|
2017-09-25 16:18:02 +00:00
|
|
|
name='Total',
|
2017-08-25 16:19:06 +00:00
|
|
|
x=list(changes.values_list('date', flat=True)),
|
|
|
|
y=list(changes.values_list('total', flat=True))
|
|
|
|
)
|
|
|
|
|
2017-10-19 17:19:52 +00:00
|
|
|
layout_args = utils.default_graph_layout_options()
|
2017-08-25 16:19:06 +00:00
|
|
|
layout_args['barmode'] = 'stack'
|
2017-09-13 15:20:28 +00:00
|
|
|
layout_args['title'] = '<b>Diaper Change Types</b><br>{}'.format(child)
|
2017-08-25 16:19:06 +00:00
|
|
|
layout_args['xaxis']['title'] = 'Date'
|
2017-10-19 17:19:52 +00:00
|
|
|
layout_args['xaxis']['rangeselector'] = utils.rangeselector_date()
|
2017-08-25 16:19:06 +00:00
|
|
|
layout_args['yaxis']['title'] = 'Number of changes'
|
|
|
|
|
|
|
|
fig = go.Figure({
|
|
|
|
'data': [solid_trace, wet_trace, total_trace],
|
|
|
|
'layout': go.Layout(**layout_args)
|
|
|
|
})
|
|
|
|
output = plotly.plot(fig, output_type='div', include_plotlyjs=False)
|
2017-10-19 17:19:52 +00:00
|
|
|
return utils.split_graph_output(output)
|
2017-08-25 16:19:06 +00:00
|
|
|
|
|
|
|
|
2017-09-13 15:53:32 +00:00
|
|
|
def sleep_totals(child):
|
2017-09-10 21:26:18 +00:00
|
|
|
"""Create a graph showing total time sleeping for each day."""
|
|
|
|
instances = Sleep.objects.filter(child=child).order_by('start')
|
|
|
|
|
|
|
|
totals = {}
|
|
|
|
for instance in instances:
|
2017-09-13 15:53:32 +00:00
|
|
|
start = timezone.localtime(instance.start)
|
|
|
|
end = timezone.localtime(instance.end)
|
|
|
|
if start.date() not in totals.keys():
|
|
|
|
totals[start.date()] = timezone.timedelta(seconds=0)
|
|
|
|
if end.date() not in totals.keys():
|
|
|
|
totals[end.date()] = timezone.timedelta(seconds=0)
|
|
|
|
|
|
|
|
# Account for dates crossing midnight.
|
|
|
|
if start.date() != end.date():
|
|
|
|
totals[start.date()] += end.replace(
|
2017-10-01 14:00:42 +00:00
|
|
|
year=start.year, month=start.month, day=start.day,
|
|
|
|
hour=23, minute=59, second=59) - start
|
2017-09-13 15:53:32 +00:00
|
|
|
totals[end.date()] += end - start.replace(
|
2017-10-01 14:00:42 +00:00
|
|
|
year=end.year, month=end.month, day=end.day, hour=0, minute=0,
|
|
|
|
second=0)
|
2017-09-13 15:53:32 +00:00
|
|
|
else:
|
|
|
|
totals[start.date()] += instance.duration
|
|
|
|
|
|
|
|
trace = go.Bar(
|
|
|
|
name='Total sleep',
|
|
|
|
x=list(totals.keys()),
|
|
|
|
y=[td.seconds/3600 for td in totals.values()],
|
|
|
|
hoverinfo='text',
|
2017-09-13 18:10:11 +00:00
|
|
|
textposition='outside',
|
2017-09-25 15:39:08 +00:00
|
|
|
text=[_duration_string_short(td) for td in totals.values()]
|
2017-09-13 15:53:32 +00:00
|
|
|
)
|
2017-09-10 21:26:18 +00:00
|
|
|
|
2017-10-19 17:19:52 +00:00
|
|
|
layout_args = utils.default_graph_layout_options()
|
2017-09-13 15:53:32 +00:00
|
|
|
layout_args['barmode'] = 'stack'
|
|
|
|
layout_args['title'] = '<b>Sleep Totals</b><br>{}'.format(child)
|
|
|
|
layout_args['xaxis']['title'] = 'Date'
|
2017-10-19 17:19:52 +00:00
|
|
|
layout_args['xaxis']['rangeselector'] = utils.rangeselector_date()
|
2017-09-13 15:53:32 +00:00
|
|
|
layout_args['yaxis']['title'] = 'Hours of sleep'
|
2017-09-10 21:26:18 +00:00
|
|
|
|
2017-09-13 15:53:32 +00:00
|
|
|
fig = go.Figure({
|
|
|
|
'data': [trace],
|
|
|
|
'layout': go.Layout(**layout_args)
|
|
|
|
})
|
|
|
|
output = plotly.plot(fig, output_type='div', include_plotlyjs=False)
|
2017-10-19 17:19:52 +00:00
|
|
|
return utils.split_graph_output(output)
|
2017-09-10 21:26:18 +00:00
|
|
|
|
|
|
|
|
2017-09-25 15:39:08 +00:00
|
|
|
def _duration_string_short(duration):
|
|
|
|
"""Format a "short" duration string without seconds precision. This is
|
|
|
|
intended to fit better in smaller spaces on a graph."""
|
|
|
|
h, m, s = duration_parts(duration)
|
|
|
|
return '{}h{}m'.format(h, m)
|
|
|
|
|
|
|
|
|
2017-09-13 15:25:12 +00:00
|
|
|
def sleep_pattern(child):
|
2017-09-09 15:20:37 +00:00
|
|
|
"""Create a graph showing blocked out periods of sleep during each day."""
|
2017-09-15 12:11:41 +00:00
|
|
|
# TODO: Simplify this using the bar charts "base" property.
|
2017-08-25 16:19:06 +00:00
|
|
|
instances = Sleep.objects.filter(child=child).order_by('start')
|
2017-08-25 18:17:15 +00:00
|
|
|
y_df = pd.DataFrame()
|
|
|
|
text_df = pd.DataFrame()
|
2017-08-25 16:19:06 +00:00
|
|
|
last_end_time = None
|
2017-08-25 21:32:46 +00:00
|
|
|
adjustment = None
|
2017-08-25 16:19:06 +00:00
|
|
|
df_index = 0
|
|
|
|
for instance in instances:
|
|
|
|
start_time = timezone.localtime(instance.start)
|
|
|
|
end_time = timezone.localtime(instance.end)
|
|
|
|
start_date = start_time.date().isoformat()
|
2017-08-25 21:32:46 +00:00
|
|
|
duration = instance.duration
|
|
|
|
|
|
|
|
# Check if the previous entry crossed midnight (see below).
|
|
|
|
if adjustment:
|
|
|
|
# Fake (0) entry to keep the color switching logic working.
|
|
|
|
df_index = _add_sleep_entry(
|
|
|
|
y_df, text_df, 0, adjustment['column'], 0)
|
|
|
|
# Real adjustment entry.
|
|
|
|
df_index = _add_sleep_entry(
|
|
|
|
y_df,
|
|
|
|
text_df,
|
|
|
|
df_index,
|
|
|
|
adjustment['column'],
|
2017-09-09 15:20:37 +00:00
|
|
|
adjustment['duration'].seconds/60,
|
2017-08-25 21:32:46 +00:00
|
|
|
'Asleep {} ({} to {})'.format(
|
2017-09-09 15:20:37 +00:00
|
|
|
duration_string(adjustment['duration']),
|
2017-08-25 21:32:46 +00:00
|
|
|
adjustment['start_time'].strftime('%I:%M %p'),
|
|
|
|
adjustment['end_time'].strftime('%I:%M %p')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
last_end_time = timezone.localtime(adjustment['end_time'])
|
|
|
|
adjustment = None
|
|
|
|
|
|
|
|
# If the dates do not match, set up an adjustment for the next day.
|
|
|
|
if end_time.date() != start_time.date():
|
|
|
|
adj_start_time = end_time.replace(hour=0, minute=0, second=0)
|
|
|
|
adjustment = {
|
|
|
|
'column': end_time.date().isoformat(),
|
|
|
|
'start_time': adj_start_time,
|
|
|
|
'end_time': end_time,
|
2017-09-09 15:20:37 +00:00
|
|
|
'duration': end_time - adj_start_time
|
2017-08-25 21:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Adjust end_time for the current entry.
|
|
|
|
end_time = end_time.replace(
|
2017-10-01 14:00:42 +00:00
|
|
|
year=start_time.year, month=start_time.month,
|
2017-08-25 21:32:46 +00:00
|
|
|
day=start_time.day, hour=23, minute=59, second=0)
|
|
|
|
duration = end_time - start_time
|
|
|
|
|
2017-08-25 18:17:15 +00:00
|
|
|
if start_date not in y_df:
|
2017-08-25 16:19:06 +00:00
|
|
|
last_end_time = start_time.replace(hour=0, minute=0, second=0)
|
2017-08-25 18:17:15 +00:00
|
|
|
|
|
|
|
# Awake time.
|
2017-08-25 21:32:46 +00:00
|
|
|
df_index = _add_sleep_entry(
|
|
|
|
y_df,
|
|
|
|
text_df,
|
2017-08-25 18:17:15 +00:00
|
|
|
df_index,
|
|
|
|
start_date,
|
2017-08-25 21:32:46 +00:00
|
|
|
(start_time - last_end_time).seconds/60
|
2017-08-25 18:17:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Asleep time.
|
2017-08-25 21:32:46 +00:00
|
|
|
df_index = _add_sleep_entry(
|
|
|
|
y_df,
|
|
|
|
text_df,
|
2017-08-25 18:17:15 +00:00
|
|
|
df_index,
|
|
|
|
start_date,
|
2017-08-25 21:32:46 +00:00
|
|
|
duration.seconds/60,
|
|
|
|
'Asleep {} ({} to {})'.format(
|
|
|
|
duration_string(duration),
|
2017-08-25 18:17:15 +00:00
|
|
|
start_time.strftime('%I:%M %p'),
|
2017-08-25 21:32:46 +00:00
|
|
|
end_time.strftime('%I:%M %p')
|
2017-08-25 18:17:15 +00:00
|
|
|
)
|
|
|
|
)
|
2017-08-25 21:32:46 +00:00
|
|
|
|
2017-08-25 16:19:06 +00:00
|
|
|
last_end_time = end_time
|
|
|
|
|
2017-08-25 18:17:15 +00:00
|
|
|
dates = list(y_df)
|
2017-08-25 16:19:06 +00:00
|
|
|
traces = []
|
|
|
|
color = 'rgba(255, 255, 255, 0)'
|
2017-08-25 18:17:15 +00:00
|
|
|
for index, row in y_df.iterrows():
|
2017-08-25 16:19:06 +00:00
|
|
|
traces.append(go.Bar(
|
|
|
|
x=dates,
|
|
|
|
y=row,
|
2017-08-25 18:17:15 +00:00
|
|
|
text=text_df.ix[index],
|
|
|
|
hoverinfo='text',
|
2017-08-25 16:19:06 +00:00
|
|
|
marker={'color': color},
|
|
|
|
showlegend=False,
|
|
|
|
))
|
|
|
|
if color == 'rgba(255, 255, 255, 0)':
|
|
|
|
color = 'rgb(0, 0, 255)'
|
|
|
|
else:
|
|
|
|
color = 'rgba(255, 255, 255, 0)'
|
|
|
|
|
2017-10-19 17:19:52 +00:00
|
|
|
layout_args = utils.default_graph_layout_options()
|
2017-08-25 18:17:15 +00:00
|
|
|
layout_args['margin']['b'] = 100
|
2017-08-25 16:19:06 +00:00
|
|
|
|
|
|
|
layout_args['barmode'] = 'stack'
|
|
|
|
layout_args['hovermode'] = 'closest'
|
2017-09-13 15:25:12 +00:00
|
|
|
layout_args['title'] = '<b>Sleep Pattern</b><br>{}'.format(child)
|
2017-08-25 18:17:15 +00:00
|
|
|
layout_args['height'] = 600
|
2017-08-25 16:19:06 +00:00
|
|
|
|
|
|
|
layout_args['xaxis']['title'] = 'Date'
|
2017-08-25 18:17:15 +00:00
|
|
|
layout_args['xaxis']['tickangle'] = -65
|
2017-10-19 17:19:52 +00:00
|
|
|
layout_args['xaxis']['rangeselector'] = utils.rangeselector_date()
|
2017-08-25 18:17:15 +00:00
|
|
|
|
|
|
|
start = timezone.localtime().strptime('12:00 AM', '%I:%M %p')
|
|
|
|
ticks = OrderedDict()
|
|
|
|
ticks[0] = start.strftime('%I:%M %p')
|
|
|
|
for i in range(30, 60*24, 30):
|
|
|
|
ticks[i] = (start + timezone.timedelta(minutes=i)).strftime('%I:%M %p')
|
2017-08-25 16:19:06 +00:00
|
|
|
|
|
|
|
layout_args['yaxis']['title'] = 'Time of day'
|
|
|
|
layout_args['yaxis']['rangemode'] = 'tozero'
|
|
|
|
layout_args['yaxis']['tickmode'] = 'array'
|
2017-08-25 18:17:15 +00:00
|
|
|
layout_args['yaxis']['tickvals'] = list(ticks.keys())
|
|
|
|
layout_args['yaxis']['ticktext'] = list(ticks.values())
|
|
|
|
layout_args['yaxis']['tickfont'] = {'size': 10}
|
2017-08-25 16:19:06 +00:00
|
|
|
|
|
|
|
fig = go.Figure({
|
|
|
|
'data': traces,
|
|
|
|
'layout': go.Layout(**layout_args)
|
|
|
|
})
|
|
|
|
output = plotly.plot(fig, output_type='div', include_plotlyjs=False)
|
2017-10-19 17:19:52 +00:00
|
|
|
return utils.split_graph_output(output)
|
2017-08-25 21:32:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _add_sleep_entry(y_df, text_df, index, column, duration, text=''):
|
|
|
|
"""Create a duration and text description entry in a DataFrame and return
|
|
|
|
the next index on success."""
|
|
|
|
if column not in y_df:
|
|
|
|
y_df.assign(**{column: 0 in range(0, len(y_df.index))})
|
|
|
|
text_df.assign(**{column: 0 in range(0, len(text_df.index))})
|
|
|
|
index = 0
|
|
|
|
|
|
|
|
y_df.set_value(index, column, duration)
|
|
|
|
text_df.set_value(index, column, text)
|
|
|
|
return index + 1
|
2017-09-16 21:17:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def timeline(child, date):
|
|
|
|
"""Create a time-sorted dictionary for all events for a child."""
|
|
|
|
min_date = date
|
|
|
|
max_date = date.replace(hour=23, minute=59, second=59)
|
|
|
|
events = []
|
2017-09-20 15:52:30 +00:00
|
|
|
|
2017-09-16 21:17:09 +00:00
|
|
|
instances = DiaperChange.objects.filter(child=child).filter(
|
|
|
|
time__range=(min_date, max_date)).order_by('-time')
|
|
|
|
for instance in instances:
|
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.time),
|
|
|
|
'event': '{} had a diaper change.'.format(child.first_name),
|
2017-09-20 15:52:30 +00:00
|
|
|
'model_name': instance.model_name,
|
2017-09-16 21:17:09 +00:00
|
|
|
})
|
2017-09-20 15:52:30 +00:00
|
|
|
|
2017-09-16 21:17:09 +00:00
|
|
|
instances = Feeding.objects.filter(child=child).filter(
|
|
|
|
start__range=(min_date, max_date)).order_by('-start')
|
|
|
|
for instance in instances:
|
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.start),
|
|
|
|
'event': '{} started feeding.'.format(instance.child.first_name),
|
2017-09-20 15:52:30 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'start'
|
2017-09-16 21:17:09 +00:00
|
|
|
})
|
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.end),
|
|
|
|
'event': '{} finished feeding.'.format(instance.child.first_name),
|
2017-09-20 15:52:30 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'end'
|
2017-09-16 21:17:09 +00:00
|
|
|
})
|
2017-09-20 15:52:30 +00:00
|
|
|
|
2017-09-16 21:17:09 +00:00
|
|
|
instances = Sleep.objects.filter(child=child).filter(
|
|
|
|
start__range=(min_date, max_date)).order_by('-start')
|
|
|
|
for instance in instances:
|
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.start),
|
|
|
|
'event': '{} fell asleep.'.format(instance.child.first_name),
|
2017-09-20 15:52:30 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'start'
|
2017-09-16 21:17:09 +00:00
|
|
|
})
|
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.end),
|
|
|
|
'event': '{} woke up.'.format(instance.child.first_name),
|
2017-09-20 15:52:30 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'end'
|
2017-09-16 21:17:09 +00:00
|
|
|
})
|
2017-09-20 15:52:30 +00:00
|
|
|
|
2017-09-16 21:17:09 +00:00
|
|
|
instances = TummyTime.objects.filter(child=child).filter(
|
|
|
|
start__range=(min_date, max_date)).order_by('-start')
|
|
|
|
for instance in instances:
|
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.start),
|
|
|
|
'event': '{} started tummy time!'.format(
|
|
|
|
instance.child.first_name),
|
2017-09-20 15:52:30 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'start'
|
2017-09-16 21:17:09 +00:00
|
|
|
})
|
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.end),
|
|
|
|
'event': '{} finished tummy time.'.format(
|
|
|
|
instance.child.first_name),
|
2017-09-20 15:52:30 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'end'
|
2017-09-16 21:17:09 +00:00
|
|
|
})
|
2017-09-20 15:52:30 +00:00
|
|
|
|
2017-09-16 21:17:09 +00:00
|
|
|
events.sort(key=lambda x: x['time'], reverse=True)
|
2017-09-20 15:52:30 +00:00
|
|
|
|
2017-09-16 21:17:09 +00:00
|
|
|
return events
|