Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Harden and document the measurement aggregates

Roland Geider43e41d1ca231
diff --git a/wger/measurements/api/aggregates.py b/wger/measurements/api/aggregates.py
index 13afe253f..7e8bd0edb 100644
--- a/wger/measurements/api/aggregates.py
+++ b/wger/measurements/api/aggregates.py
@@ -37,6 +37,7 @@ from django.db.models import (
Min,
Sum,
)
+from django.db.models.fields.json import KeyTextTransform
from django.db.models.functions import (
Cast,
Coalesce,
@@ -92,9 +93,12 @@ def _bounds_expression(key: str, aggregate):
stands for in `extra_data`, and contributes that rather than its mean, so
condensing an aggregate keeps the true extremes.
"""
+ # Read as text rather than as JSON: Postgres refuses to cast a JSON string
+ # to numeric, even a numeric one, and rows predating the write validation
+ # can hold `"48"`
return aggregate(
Coalesce(
- Cast(F(f'extra_data__{key}'), DecimalField(max_digits=8, decimal_places=2)),
+ Cast(KeyTextTransform(key, 'extra_data'), DecimalField(max_digits=8, decimal_places=2)),
F('value'),
)
)
@@ -134,34 +138,21 @@ def bucket_rows(queryset, unit: str, tz, max_points: int = DEFAULT_MAX_POINTS) -
if unit not in BUCKET_UNITS:
raise InvalidBucket(f'Unknown bucket: {unit}')
- rows = (
+ return list(
queryset.annotate(
start=BUCKET_UNITS[unit]('date', tzinfo=tz),
- stored_unit=F('extra_data__unit'),
+ unit=F('extra_data__unit'),
)
- .values('category', 'start', 'stored_unit')
+ .values('category', 'start', 'unit')
.annotate(
count=Count('id'),
- total=Sum('value'),
- low=_bounds_expression('min', Min),
- high=_bounds_expression('max', Max),
+ sum=Sum('value'),
+ min=_bounds_expression('min', Min),
+ max=_bounds_expression('max', Max),
)
.order_by('start')
)
- return [
- {
- 'category': row['category'],
- 'start': row['start'],
- 'unit': row['stored_unit'],
- 'count': row['count'],
- 'sum': row['total'],
- 'min': row['low'],
- 'max': row['high'],
- }
- for row in rows
- ]
-
def value_count_rows(queryset, tz, summed_per_day: bool = False) -> list[dict]:
"""
@@ -175,19 +166,21 @@ def value_count_rows(queryset, tz, summed_per_day: bool = False) -> list[dict]:
than single readings, for the metrics whose samples mean nothing alone.
"""
if summed_per_day:
+ # A day is one value here, so the totals are counted in python: the
+ # database has already grouped them into days
daily = (
- queryset.annotate(day=TruncDay('date', tzinfo=tz), stored_unit=F('extra_data__unit'))
- .values('category', 'day', 'stored_unit')
+ queryset.annotate(day=TruncDay('date', tzinfo=tz), unit=F('extra_data__unit'))
+ .values('category', 'day', 'unit')
.annotate(value=Sum('value'), newest=Max('date'))
)
counted: dict[tuple, dict] = {}
for row in daily:
- key = (row['category'], row['stored_unit'], row['value'])
+ key = (row['category'], row['unit'], row['value'])
entry = counted.setdefault(
key,
{
'category': row['category'],
- 'unit': row['stored_unit'],
+ 'unit': row['unit'],
'value': row['value'],
'count': 0,
'newest': row['newest'],
@@ -197,20 +190,9 @@ def value_count_rows(queryset, tz, summed_per_day: bool = False) -> list[dict]:
entry['newest'] = max(entry['newest'], row['newest'])
return sorted(counted.values(), key=lambda r: (str(r['category']), r['value']))
- rows = (
- queryset.annotate(stored_unit=F('extra_data__unit'))
- .values('category', 'stored_unit', 'value')
+ return list(
+ queryset.annotate(unit=F('extra_data__unit'))
+ .values('category', 'unit', 'value')
.annotate(count=Count('id'), newest=Max('date'))
.order_by('value')
)
-
- return [
- {
- 'category': row['category'],
- 'unit': row['stored_unit'],
- 'value': row['value'],
- 'count': row['count'],
- 'newest': row['newest'],
- }
- for row in rows
- ]
diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.py
index dac06486d..73c57984d 100644
--- a/wger/measurements/api/serializers.py
+++ b/wger/measurements/api/serializers.py
@@ -269,9 +269,24 @@ class MeasurementSerializer(serializers.ModelSerializer):
def validate_extra_data(self, extra_data):
"""
- The unit key holds the unit the value was entered in
+ The unit key holds the unit the value was entered in, min and max the
+ range a daily aggregate summarises.
+
+ Those two have to be numbers: the chart aggregate casts them to a
+ decimal in SQL, and Postgres refuses to cast a JSON string, even a
+ numeric one. A single entry written with `"48"` would take down every
+ chart read of its category.
"""
- return validate_json_object(extra_data, 'extra_data', EXTRA_DATA_MAX_BYTES)
+ validate_json_object(extra_data, 'extra_data', EXTRA_DATA_MAX_BYTES)
+
+ for key in ('min', 'max'):
+ value = (extra_data or {}).get(key)
+ if value is not None and not isinstance(value, (int, float)):
+ raise serializers.ValidationError(
+ f'The {key} of an aggregate has to be a number, not {type(value).__name__}'
+ )
+
+ return extra_data
def validate(self, data):
"""
diff --git a/wger/measurements/api/views.py b/wger/measurements/api/views.py
index 2d081f1cf..50146aba6 100644
--- a/wger/measurements/api/views.py
+++ b/wger/measurements/api/views.py
@@ -23,6 +23,10 @@ from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
# Third Party
+from drf_spectacular.utils import (
+ OpenApiParameter,
+ extend_schema,
+)
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
@@ -130,6 +134,29 @@ class MeasurementViewSet(WgerOwnerObjectModelViewSet):
except ValueError:
raise InvalidBucket('max_points must be a number')
+ @extend_schema(
+ summary='Read the entries condensed into chart points',
+ parameters=[
+ OpenApiParameter(
+ 'bucket',
+ description='Calendar unit to condense into. The default picks the finest one '
+ 'that keeps the series under max_points.',
+ enum=['auto', 'hour', 'day', 'week', 'month'],
+ ),
+ OpenApiParameter(
+ 'tz',
+ description='IANA name of the zone the buckets are cut in, the server zone by '
+ 'default. The column is UTC, and a reading after midnight belongs to the day '
+ 'the user had it.',
+ ),
+ OpenApiParameter(
+ 'max_points',
+ type=int,
+ description=f'Points the auto bucket aims for, {DEFAULT_MAX_POINTS} by default',
+ ),
+ ],
+ responses={200: BucketSerializer(many=True)},
+ )
@action(detail=False, methods=['get'], serializer_class=BucketSerializer)
def aggregate(self, request):
"""
@@ -154,6 +181,19 @@ class MeasurementViewSet(WgerOwnerObjectModelViewSet):
return Response(BucketSerializer(rows, many=True).data)
+ @extend_schema(
+ summary='Read how often each value occurred',
+ parameters=[
+ OpenApiParameter('tz', description='IANA name of the zone the days are cut in'),
+ OpenApiParameter(
+ 'summed_per_day',
+ type=bool,
+ description='Count daily totals rather than single readings, for the metrics '
+ 'whose samples mean nothing on their own (steps, sleep)',
+ ),
+ ],
+ responses={200: ValueCountSerializer(many=True)},
+ )
@action(
detail=False,
url_path='value-counts',
diff --git a/wger/measurements/tests/test_aggregates.py b/wger/measurements/tests/test_aggregates.py
index 091054a55..2f655e4d4 100644
--- a/wger/measurements/tests/test_aggregates.py
+++ b/wger/measurements/tests/test_aggregates.py
@@ -190,3 +190,18 @@ class AggregateApiTestCase(WgerTestCase):
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]['value'], '5000.00')
self.assertEqual(rows[0]['count'], 2)
+
+ def test_a_bound_stored_as_a_string_still_reads(self):
+ # Postgres refuses to cast a JSON string to numeric, even a numeric
+ # one, so a row predating the write validation would otherwise take
+ # every chart read of its category down with it
+ self.add(
+ timezone.make_aware(datetime.datetime(2026, 5, 4, 8)),
+ 70,
+ extra={'min': '48', 'max': '165'},
+ )
+
+ rows = self.aggregate(bucket='day', tz='UTC')
+
+ self.assertEqual(rows[0]['min'], '48.00')
+ self.assertEqual(rows[0]['max'], '165.00')
diff --git a/wger/measurements/tests/test_measurements.py b/wger/measurements/tests/test_measurements.py
index 56959db21..32226f658 100644
--- a/wger/measurements/tests/test_measurements.py
+++ b/wger/measurements/tests/test_measurements.py
@@ -14,6 +14,7 @@
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
# Standard Library
+import json
from decimal import Decimal
# Django
@@ -464,3 +465,45 @@ class MeasurementExtraDataApiTestCase(WgerTestCase):
)
self.assertEqual(response.status_code, 201)
+
+
+class AggregateBoundValidationTestCase(WgerTestCase):
+ """
+ The min and max of a daily aggregate have to be numbers: the chart
+ aggregate casts them in SQL, and a string breaks every read of the category
+ """
+
+ category_id = 'cccccccc-cccc-cccc-cccc-000000000002'
+
+ def setUp(self):
+ super().setUp()
+ self.user_login('test')
+
+ def post(self, extra_data):
+ return self.client.post(
+ reverse('measurement-list'),
+ json.dumps(
+ {
+ 'category': self.category_id,
+ 'date': '2026-05-04T08:00:00Z',
+ 'value': '70.00',
+ 'extra_data': extra_data,
+ }
+ ),
+ content_type='application/json',
+ )
+
+ def test_a_numeric_bound_is_accepted(self):
+ response = self.post({'min': 48, 'max': 165.5})
+
+ self.assertEqual(response.status_code, 201, response.data)
+
+ def test_a_bound_written_as_a_string_is_refused(self):
+ for extra_data in ({'min': '48'}, {'max': 'abc'}):
+ response = self.post(extra_data)
+
+ self.assertEqual(response.status_code, 400)
+ self.assertIn('extra_data', response.data)
+
+ def test_extra_data_without_bounds_is_untouched(self):
+ self.assertEqual(self.post({'unit': 'kg', 'source_name': 'Withings'}).status_code, 201)