diff --git a/mood/statistics.py b/mood/statistics.py index 6125e94..6f2b442 100644 --- a/mood/statistics.py +++ b/mood/statistics.py @@ -8,7 +8,7 @@ from dateutil.relativedelta import relativedelta from .models import Status -def moodstats(mindate=None, maxdate=None, days=7): +def moodstats(user, mindate=None, maxdate=None, days=7): hv.extension('bokeh') maxdate = maxdate or timezone.now() @@ -27,7 +27,7 @@ def moodstats(mindate=None, maxdate=None, days=7): pointdict = {"date": [], "value": [], "color": []} - for status in Status.objects.filter(timestamp__gte=mindate, timestamp__lte=maxdate): + for status in Status.objects.filter(user=user, timestamp__gte=mindate, timestamp__lte=maxdate): if status.mood: pointdict["date"].append(status.timestamp) pointdict["value"].append(status.mood.value) @@ -48,6 +48,21 @@ def moodstats(mindate=None, maxdate=None, days=7): line = hv.Curve(pointtuples) output = points * line - output.opts(tools=["xwheel_zoom"]) + output.opts(tools=["xwheel_zoom"], ylim=(0, 5)) + + return output + +def activitystats(user, mindate=None, maxdate=None, days=7): + maxdate = maxdate or timezone.now() + mindate = mindate or (maxdate - relativedelta(days=days)) + + output = {} + + for status in Status.objects.filter(user=user, timestamp__gte=mindate, timestamp__lte=maxdate): + for activity in status.activity_set: + if activity in output.keys(): + output[activity] += 1 + else: + output[activity] = 1 return output \ No newline at end of file diff --git a/mood/templates/mood/statistics.html b/mood/templates/mood/statistics.html index 0990120..f62820f 100644 --- a/mood/templates/mood/statistics.html +++ b/mood/templates/mood/statistics.html @@ -19,6 +19,31 @@ + +
+
+ +
+
Activities
+
+ +
+ + + + + + {% for activity, count in activities.items %} + + + + + {% endfor %} +
ActivityCount
{{ activity }}{{ count }}
+
+
+
+ {% endblock %} \ No newline at end of file diff --git a/mood/views.py b/mood/views.py index 5ad8502..221f29e 100644 --- a/mood/views.py +++ b/mood/views.py @@ -9,7 +9,7 @@ from django.utils.decorators import method_decorator from .models import Status, Activity, Mood, StatusMedia, StatusActivity from .forms import StatusForm -from .statistics import moodstats +from .statistics import moodstats, activitystats from common.helpers import get_upload_path from common.templatetags.images import hvhtml @@ -330,8 +330,22 @@ class MoodStatisticsView(LoginRequiredMixin, TemplateView): template_name = "mood/statistics.html" def get_context_data(self, **kwargs): + startdate = self.request.GET.get("start") + enddate = self.request.GET.get("end") + + if enddate: + maxdate = datetime.strptime(enddate, "%Y-%m-%d") + else: + maxdate = timezone.now() + + if startdate: + mindate = datetime.strptime(startdate, "%Y-%m-%d") + else: + mindate = maxdate - relativedelta.relativedelta(weeks=1) + context = super().get_context_data(**kwargs) context["title"] = "Statistics" + context["activities"] = activitystats(self.request.user, mindate, maxdate) return context class MoodCSVView(LoginRequiredMixin, View): @@ -383,5 +397,5 @@ class MoodPlotView(LoginRequiredMixin, View): else: mindate = maxdate - relativedelta.relativedelta(weeks=1) - res.write(hvhtml(moodstats(mindate, maxdate))) + res.write(hvhtml(moodstats(request.user, mindate, maxdate))) return res \ No newline at end of file