mirror of https://github.com/snachodog/mybuddy.git
Add tummy time duration report (#316)
This commit is contained in:
parent
fe7e16800a
commit
a5ec2d85c8
|
@ -27,6 +27,7 @@
|
||||||
<a class="dropdown-item" href="{% url 'reports:report-feeding-duration-child' object.slug %}">{% trans "Feeding Durations (Average)" %}</a>
|
<a class="dropdown-item" href="{% url 'reports:report-feeding-duration-child' object.slug %}">{% trans "Feeding Durations (Average)" %}</a>
|
||||||
<a class="dropdown-item" href="{% url 'reports:report-sleep-pattern-child' object.slug %}">{% trans "Sleep Pattern" %}</a>
|
<a class="dropdown-item" href="{% url 'reports:report-sleep-pattern-child' object.slug %}">{% trans "Sleep Pattern" %}</a>
|
||||||
<a class="dropdown-item" href="{% url 'reports:report-sleep-totals-child' object.slug %}">{% trans "Sleep Totals" %}</a>
|
<a class="dropdown-item" href="{% url 'reports:report-sleep-totals-child' object.slug %}">{% trans "Sleep Totals" %}</a>
|
||||||
|
<a class="dropdown-item" href="{% url 'reports:report-tummytime-duration-child' object.slug %}">{% trans "Tummy Time Durations (Sum)" %}</a>
|
||||||
<a class="dropdown-item" href="{% url 'reports:report-weight-weight-child' object.slug %}">{% trans "Weight" %}</a>
|
<a class="dropdown-item" href="{% url 'reports:report-weight-weight-child' object.slug %}">{% trans "Weight" %}</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -5,4 +5,5 @@ from .feeding_amounts import feeding_amounts # NOQA
|
||||||
from .feeding_duration import feeding_duration # NOQA
|
from .feeding_duration import feeding_duration # NOQA
|
||||||
from .sleep_pattern import sleep_pattern # NOQA
|
from .sleep_pattern import sleep_pattern # NOQA
|
||||||
from .sleep_totals import sleep_totals # NOQA
|
from .sleep_totals import sleep_totals # NOQA
|
||||||
|
from .tummytime_duration import tummytime_duration # NOQA
|
||||||
from .weight_weight import weight_weight # NOQA
|
from .weight_weight import weight_weight # NOQA
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from django.db.models import Count, Sum
|
||||||
|
from django.db.models.functions import TruncDate
|
||||||
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
import plotly.offline as plotly
|
||||||
|
import plotly.graph_objs as go
|
||||||
|
|
||||||
|
from core.utils import duration_parts
|
||||||
|
|
||||||
|
from reports import utils
|
||||||
|
|
||||||
|
|
||||||
|
def tummytime_duration(instances):
|
||||||
|
"""
|
||||||
|
Create a graph showing total duration of tummy time instances per day.
|
||||||
|
|
||||||
|
:param instances: a QuerySet of TummyTime instances.
|
||||||
|
:returns: a tuple of the the graph's html and javascript.
|
||||||
|
"""
|
||||||
|
totals = instances.annotate(date=TruncDate('start')) \
|
||||||
|
.values('date') \
|
||||||
|
.annotate(count=Count('id')) \
|
||||||
|
.annotate(sum=Sum('duration')) \
|
||||||
|
.order_by('-date')
|
||||||
|
|
||||||
|
sums = []
|
||||||
|
for total in totals:
|
||||||
|
sums.append(total['sum'])
|
||||||
|
|
||||||
|
trace_avg = go.Bar(
|
||||||
|
name=_('Total duration'),
|
||||||
|
x=list(totals.values_list('date', flat=True)),
|
||||||
|
y=[td.seconds/60 for td in sums],
|
||||||
|
hoverinfo='text',
|
||||||
|
text=[_duration_string_ms(td) for td in sums]
|
||||||
|
)
|
||||||
|
trace_count = go.Scatter(
|
||||||
|
name=_('Number of sessions'),
|
||||||
|
mode='markers',
|
||||||
|
x=list(totals.values_list('date', flat=True)),
|
||||||
|
y=list(totals.values_list('count', flat=True)),
|
||||||
|
yaxis='y2',
|
||||||
|
hoverinfo='y'
|
||||||
|
)
|
||||||
|
|
||||||
|
layout_args = utils.default_graph_layout_options()
|
||||||
|
layout_args['title'] = _('<b>Total Tummy Time Durations</b>')
|
||||||
|
layout_args['xaxis']['title'] = _('Date')
|
||||||
|
layout_args['xaxis']['rangeselector'] = utils.rangeselector_date()
|
||||||
|
layout_args['yaxis']['title'] = _('Total duration (minutes)')
|
||||||
|
layout_args['yaxis2'] = dict(layout_args['yaxis'])
|
||||||
|
layout_args['yaxis2']['title'] = _('Number of sessions')
|
||||||
|
layout_args['yaxis2']['overlaying'] = 'y'
|
||||||
|
layout_args['yaxis2']['side'] = 'right'
|
||||||
|
|
||||||
|
fig = go.Figure({
|
||||||
|
'data': [trace_avg, trace_count],
|
||||||
|
'layout': go.Layout(**layout_args)
|
||||||
|
})
|
||||||
|
output = plotly.plot(fig, output_type='div', include_plotlyjs=False)
|
||||||
|
return utils.split_graph_output(output)
|
||||||
|
|
||||||
|
|
||||||
|
def _duration_string_ms(duration):
|
||||||
|
"""
|
||||||
|
Format a "short" duration string with only minutes and seconds. This is
|
||||||
|
intended to fit better in smaller spaces on a graph.
|
||||||
|
:returns: a string of the form Xm.
|
||||||
|
"""
|
||||||
|
h, m, s = duration_parts(duration)
|
||||||
|
return '{}m{}s'.format(m, s)
|
|
@ -0,0 +1,9 @@
|
||||||
|
{% extends 'reports/report_base.html' %}
|
||||||
|
{% load i18n %}
|
||||||
|
|
||||||
|
{% block title %}{% trans "Total Tummy Time Durations" %} - {{ object }}{% endblock %}
|
||||||
|
|
||||||
|
{% block breadcrumbs %}
|
||||||
|
{{ block.super }}
|
||||||
|
<li class="breadcrumb-item active" aria-current="page">{% trans "Total Tummy Time Durations" %}</li>
|
||||||
|
{% endblock %}
|
|
@ -41,6 +41,11 @@ urlpatterns = [
|
||||||
views.SleepTotalsChildReport.as_view(),
|
views.SleepTotalsChildReport.as_view(),
|
||||||
name='report-sleep-totals-child'
|
name='report-sleep-totals-child'
|
||||||
),
|
),
|
||||||
|
path(
|
||||||
|
'children/<str:slug>/reports/tummytime/duration/',
|
||||||
|
views.TummyTimeDurationChildReport.as_view(),
|
||||||
|
name='report-tummytime-duration-child'
|
||||||
|
),
|
||||||
path(
|
path(
|
||||||
'children/<str:slug>/reports/weight/weight/',
|
'children/<str:slug>/reports/weight/weight/',
|
||||||
views.WeightWeightChildReport.as_view(),
|
views.WeightWeightChildReport.as_view(),
|
||||||
|
|
|
@ -109,6 +109,30 @@ class FeedingDurationChildReport(PermissionRequiredMixin, DetailView):
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
class TummyTimeDurationChildReport(PermissionRequiredMixin, DetailView):
|
||||||
|
"""
|
||||||
|
Graph of tummy time durations over time.
|
||||||
|
"""
|
||||||
|
model = models.Child
|
||||||
|
permission_required = ('core.view_child',)
|
||||||
|
template_name = 'reports/tummytime_duration.html'
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super(TummyTimeDurationChildReport, self).__init__()
|
||||||
|
self.html = ''
|
||||||
|
self.js = ''
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
context = super(TummyTimeDurationChildReport, self).get_context_data(
|
||||||
|
**kwargs)
|
||||||
|
child = context['object']
|
||||||
|
instances = models.TummyTime.objects.filter(child=child)
|
||||||
|
if instances:
|
||||||
|
context['html'], context['js'] = \
|
||||||
|
graphs.tummytime_duration(instances)
|
||||||
|
return context
|
||||||
|
|
||||||
|
|
||||||
class SleepPatternChildReport(PermissionRequiredMixin, DetailView):
|
class SleepPatternChildReport(PermissionRequiredMixin, DetailView):
|
||||||
"""
|
"""
|
||||||
Graph of sleep pattern comparing sleep to wake times by day.
|
Graph of sleep pattern comparing sleep to wake times by day.
|
||||||
|
|
Loading…
Reference in New Issue