Add rolling average to mood statistics chart
Add ordering clause to Status objects
This commit is contained in:
parent
2ffe276304
commit
e1078bbd4c
3 changed files with 15 additions and 6 deletions
|
@ -20,6 +20,9 @@ class Mood(models.Model):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
class Status(models.Model):
|
class Status(models.Model):
|
||||||
|
class Meta:
|
||||||
|
ordering = ["timestamp"]
|
||||||
|
|
||||||
user = models.ForeignKey(get_user_model(), models.CASCADE)
|
user = models.ForeignKey(get_user_model(), models.CASCADE)
|
||||||
timestamp = models.DateTimeField(default=timezone.now)
|
timestamp = models.DateTimeField(default=timezone.now)
|
||||||
mood = models.ForeignKey(Mood, models.SET_NULL, null=True)
|
mood = models.ForeignKey(Mood, models.SET_NULL, null=True)
|
||||||
|
|
|
@ -4,6 +4,7 @@ import pandas as pd
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from bokeh.models import HoverTool
|
from bokeh.models import HoverTool
|
||||||
|
from holoviews.operation import timeseries
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
|
|
||||||
from .models import Status, Mood
|
from .models import Status, Mood
|
||||||
|
@ -28,7 +29,7 @@ def moodstats(user, mindate=None, maxdate=None, days=7):
|
||||||
pointdict = {"date": [], "value": [], "color": []}
|
pointdict = {"date": [], "value": [], "color": []}
|
||||||
|
|
||||||
|
|
||||||
for status in Status.objects.filter(user=user, timestamp__gte=mindate, timestamp__lte=maxdate):
|
for status in Status.objects.filter(user=user):
|
||||||
if status.mood:
|
if status.mood:
|
||||||
pointdict["date"].append(status.timestamp)
|
pointdict["date"].append(status.timestamp)
|
||||||
pointdict["value"].append(status.mood.value)
|
pointdict["value"].append(status.mood.value)
|
||||||
|
@ -48,10 +49,14 @@ def moodstats(user, mindate=None, maxdate=None, days=7):
|
||||||
|
|
||||||
line = hv.Curve(pointtuples)
|
line = hv.Curve(pointtuples)
|
||||||
|
|
||||||
maxy = Mood.objects.filter(user=user).latest("value").value + 1
|
maxval = Mood.objects.filter(user=user).latest("value").value
|
||||||
|
maxy = maxval + min(maxval * 0.1, 1)
|
||||||
|
|
||||||
output = points * line
|
maxx = maxdate.timestamp() * 1000
|
||||||
output.opts(tools=["xwheel_zoom"], ylim=(0, maxy))
|
minx = maxx - (60*60*24*7) * 1000
|
||||||
|
|
||||||
|
output = points * line * timeseries.rolling(line, rolling_window=7)
|
||||||
|
output.opts(ylim=(0, maxy), xlim=(minx, maxx))
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
|
@ -16,5 +16,6 @@ python-telegram-bot
|
||||||
python-dateutil
|
python-dateutil
|
||||||
matrix-nio
|
matrix-nio
|
||||||
holoviews
|
holoviews
|
||||||
bokeh==2.3.0dev13
|
bokeh
|
||||||
panel==0.11.0a16
|
panel
|
||||||
|
scipy
|
Loading…
Reference in a new issue