fix adg logic to use database data instead of using api
This commit is contained in:
1 parent
76a74fee0a
commit
31ab4e4780
3 files changed
+51
-12
No files matched your search
@@ -305,16 +305,6 @@ def sync_chicken_weight_for_cycle(
|
||||
doc_weight = int(cycle.doc_in_weight)
|
||||
dates = sorted({d for d in weight_by_date if start <= d <= end} | {start})
|
||||
|
||||
prev_weight = float(doc_weight)
|
||||
# Seed previous-day weight from any row before the first upsert date.
|
||||
existing_before = (
|
||||
ChickenWeight.objects.filter(cycle=cycle, date__lt=dates[0])
|
||||
.order_by("-date")
|
||||
.first()
|
||||
)
|
||||
if existing_before is not None:
|
||||
prev_weight = float(existing_before.average_weight)
|
||||
|
||||
upserted: list[date] = []
|
||||
for day in dates:
|
||||
age = (day - start).days
|
||||
@@ -331,7 +321,18 @@ def sync_chicken_weight_for_cycle(
|
||||
average_weight = float(metrics["average_weight"])
|
||||
uniformity = float(metrics["uniformity"])
|
||||
chicken_count = int(metrics["chicken_count"])
|
||||
adg = round(average_weight - prev_weight, 2)
|
||||
|
||||
prev_row = (
|
||||
ChickenWeight.objects.filter(
|
||||
cycle=cycle,
|
||||
date__lt=day,
|
||||
average_weight__gt=0,
|
||||
)
|
||||
.order_by("-date")
|
||||
.first()
|
||||
)
|
||||
prev_avg = float(prev_row.average_weight) if prev_row is not None else float(doc_weight)
|
||||
adg = round(average_weight - prev_avg, 2)
|
||||
chicken_area = float(metrics.get("chicken_area", 0.0))
|
||||
|
||||
defaults = {
|
||||
@@ -349,7 +350,6 @@ def sync_chicken_weight_for_cycle(
|
||||
defaults=defaults,
|
||||
)
|
||||
upserted.append(day)
|
||||
prev_weight = average_weight
|
||||
|
||||
if upserted:
|
||||
from apps.operations.services.kpi_rollups import refresh_kpi_source_fields
|
||||
|
||||
@@ -405,6 +405,45 @@ class SyncChickenWeightTests(TestCase):
|
||||
ChickenWeight.objects.filter(cycle=self.cycle, date=date(2026, 8, 20)).exists()
|
||||
)
|
||||
|
||||
@patch("apps.operations.services.chicken_counting_edge.timezone.localdate")
|
||||
@patch(
|
||||
"apps.operations.services.chicken_counting_edge.ChickenCountingEdgeClient.fetch_weight_predictions"
|
||||
)
|
||||
def test_adg_uses_latest_database_weight_when_edge_payload_has_gaps(
|
||||
self, mock_pred, mock_today
|
||||
):
|
||||
mock_today.return_value = date(2026, 9, 10)
|
||||
# Pre-seed day 5 in database with 100g
|
||||
ChickenWeight.objects.create(
|
||||
cycle=self.cycle,
|
||||
date=date(2026, 9, 7),
|
||||
age=5,
|
||||
doc_weight=35,
|
||||
average_weight=100.0,
|
||||
chicken_count=49000,
|
||||
uniformity=20.0,
|
||||
average_daily_gain=15.0,
|
||||
)
|
||||
# Edge API only returns day 6 with 115g (no earlier days in payload)
|
||||
mock_pred.return_value = [
|
||||
{
|
||||
"date": "2026-09-08",
|
||||
"location": "K1-L1",
|
||||
"coop": "K1",
|
||||
"camera_id": "FUSED",
|
||||
"predicted_weight_g": 115.0,
|
||||
"uniformity": 22.0,
|
||||
"n_valid_samples": 1000,
|
||||
}
|
||||
]
|
||||
|
||||
sync_chicken_weight_for_cycle(self.cycle)
|
||||
|
||||
day6 = ChickenWeight.objects.get(cycle=self.cycle, date=date(2026, 9, 8))
|
||||
self.assertEqual(day6.average_weight, 115.0)
|
||||
# ADG must subtract day 5 from database (115.0 - 100.0 = 15.0), not DOC weight (35.0)
|
||||
self.assertAlmostEqual(day6.average_daily_gain, 15.0)
|
||||
|
||||
@patch("apps.operations.services.chicken_counting_edge.timezone.localdate")
|
||||
@patch(
|
||||
"apps.operations.services.chicken_counting_edge.ChickenCountingEdgeClient.fetch_weight_predictions"
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 339 KiB After Width: | Height: | Size: 344 KiB |
Reference in new issue
Block a user