diff --git a/app/api/operator/stats.py b/app/api/operator/stats.py index ab80310..396c90c 100644 --- a/app/api/operator/stats.py +++ b/app/api/operator/stats.py @@ -5,6 +5,7 @@ from decimal import Decimal from fastapi import APIRouter, Depends, HTTPException, Query from ... import machines, payments, unpaid +from ...machine_stats import pace from ...cart_pricing import designer_hours from ...core import db from ...core.auth import operator @@ -336,7 +337,7 @@ def machine_output(c, start, end): made = float(sum(row['metres'] for row in mine)) result.append({'id': m['id'], 'name': m['name'], 'active': m['active'], 'orders': len(mine), 'metres': made, 'printing_hours': round(hours_in, 2), - 'metres_per_hour': round(made / hours_in, 1) if hours_in > 0 else None, + 'metres_per_hour': pace(made, hours_in), 'last_finished': last[m['id']].isoformat() if m['id'] in last else None, 'now': {state: m['states'].get(state, 0) for state in ('tra', 'imp')}}) loose = [row for row in done if row['machine_id'] is None] diff --git a/app/machine_stats.py b/app/machine_stats.py index 8ff0129..10882ed 100644 --- a/app/machine_stats.py +++ b/app/machine_stats.py @@ -41,6 +41,17 @@ INK_ITEMS = tuple(item for item in SUPPLIES if item.startswith('ink_')) # Average month, for the monthly contribution since installation. _MONTH_SECONDS = Decimal('30.4375') * 86400 +# Below a quarter of an hour in Imprimindo the pace is noise: an order moved +# through in seconds made a machine show 110671,5 m/h (16/11). No pace then. +MIN_PACE_HOURS = 0.25 + + +def pace(metres, hours): + """Metres per hour, one decimal; None below MIN_PACE_HOURS of printing.""" + if not hours or float(hours) < MIN_PACE_HOURS: + return None + return round(float(metres) / float(hours), 1) + def month_bounds(month): """'2026-11' (None: the current month, local) -> (first day, first day of @@ -79,8 +90,7 @@ def ratios(printed, hours_running=None, supplies_cents=0, parts_cents=0, rolls=N return int((Decimal(cents) / printed).quantize(Decimal('1'), rounding=ROUND_HALF_UP)) result = { - 'metres_per_hour': (round(float(printed) / float(hours_running), 1) - if hours_running else None), + 'metres_per_hour': pace(printed, hours_running), 'metres_per_roll': round(float(printed / Decimal(str(rolls))), 1) if rolls else None, 'ink_per_100m': { item: (None if quantity is None or not printed diff --git a/tests/test_machine_stats.py b/tests/test_machine_stats.py index d9680ac..c4672d8 100644 --- a/tests/test_machine_stats.py +++ b/tests/test_machine_stats.py @@ -3,7 +3,7 @@ import unittest from datetime import datetime, timedelta from zoneinfo import ZoneInfo -from app.machine_stats import month_bounds, ratios +from app.machine_stats import month_bounds, pace, ratios SAO_PAULO = ZoneInfo('America/Sao_Paulo') @@ -74,5 +74,18 @@ class RatiosTest(unittest.TestCase): self.assertIsNone(value['payback_months']) + +class PaceTest(unittest.TestCase): + def test_seconds_in_imprimindo_give_no_pace(self): + # 16/11: an order moved through Imprimindo in seconds showed 110671,5 m/h. + self.assertIsNone(pace(1.01, 10 / 3600)) + self.assertIsNone(pace(5, 0)) + self.assertIsNone(pace(5, None)) + + def test_a_quarter_of_an_hour_gives_the_pace(self): + self.assertEqual(pace(5, 0.25), 20.0) + self.assertEqual(pace(1.5, 0.5), 3.0) + + if __name__ == '__main__': unittest.main()