fix: Refactors data aggregation for heatmap

Replaces count annotation with custom aggregation logic to
correctly count status entries per date in heatmap data.

Improves data processing by calculating occurrences manually.
Removes previous annotation and modifies output format.

This ensures data accuracy before encoding into JSON response.
This commit is contained in:
Kumi 2024-11-20 07:18:40 +01:00
parent 7f79a79a17
commit bc8da6b1cc
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -15,6 +15,7 @@ from django.utils import timezone
from django.views.decorators.clickjacking import xframe_options_sameorigin
from django.utils.decorators import method_decorator
from django.db.models import Count
from django.db.models.functions import TruncDate
from .models import Status, Activity, Mood, StatusMedia, StatusActivity
from .forms import StatusForm
@ -561,17 +562,18 @@ class MoodCountHeatmapJSONView(LoginRequiredMixin, View):
else:
mindate = maxdate - relativedelta.relativedelta(years=1)
data = (
Status.objects.filter(
data = Status.objects.filter(
user=request.user, timestamp__gte=mindate, timestamp__lte=maxdate
)
.annotate(value=Count("id"))
)
# TODO: Should eventually change this so that it returns a *color* as a value and the count as a tooltip
data = [{"date": d.timestamp.strftime("%Y-%m-%d"), "value": d.value} for d in data]
output = {}
res.write(json.dumps(data))
for entry in data:
date = entry.timestamp.strftime("%Y-%m-%d")
output[date] = output.get(date, 0) + 1
output = [{"date": key, "value": value} for key, value in output.items()]
res.write(json.dumps(output))
return res