fix weight ai insight target weight data using mr. mike instead of cp707

This commit is contained in:
Alberto-Audrix committed 2026-10-01 16:59:58 +07:00
1 parent bc2b260065
commit 05465b0382
5 files changed
+83 -11

No files matched your search

@@ -394,7 +394,7 @@ def analyze_fcr(actual_fcr: Any, day_age: Any) -> dict[str, Any]:
}
def analyze_bw(actual_bw_g: Any, day_age: Any) -> dict[str, Any]:
def analyze_bw(actual_bw_g: Any, day_age: Any, target_bw_g: Any = None) -> dict[str, Any]:
try:
day_int = int(day_age)
except (TypeError, ValueError):
@@ -420,7 +420,11 @@ def analyze_bw(actual_bw_g: Any, day_age: Any) -> dict[str, Any]:
# 0 g is a missing/placeholder reading, not a real bird weight.
if actual is not None and actual <= 0:
actual = None
std_bw = get_bw_standard_by_day(day_age)
std_bw = _as_float(target_bw_g)
if std_bw is None or std_bw <= 0:
std_bw = get_bw_standard_by_day(day_age)
if actual is None or std_bw is None:
return {
"actual": actual,
@@ -437,24 +441,24 @@ def analyze_bw(actual_bw_g: Any, day_age: Any) -> dict[str, Any]:
if delta_pct < -15:
status = "critical"
message = (
f"Bobot badan aktual {actual:g}g jauh di bawah standar CP 707 ({std_bw:g}g) "
f"Bobot badan aktual {actual:g}g jauh di bawah standar ({std_bw:g}g) "
f"pada hari ke-{day_age}. Deviasi {abs(delta_pct):.1f}%. Lakukan grading segera."
)
elif delta_pct < -5:
status = "warning"
message = (
f"Bobot badan aktual {actual:g}g sedikit di bawah standar CP 707 ({std_bw:g}g) "
f"Bobot badan aktual {actual:g}g sedikit di bawah standar ({std_bw:g}g) "
f"pada hari ke-{day_age}. Deviasi {abs(delta_pct):.1f}%. Tingkatkan stimulasi pakan."
)
elif delta_pct > 10:
status = "ok"
message = (
f"Bobot badan aktual {actual:g}g melampaui standar CP 707 ({std_bw:g}g) "
f"Bobot badan aktual {actual:g}g melampaui standar ({std_bw:g}g) "
f"pada hari ke-{day_age}. Pertumbuhan sangat baik."
)
else:
status = "ok"
message = f"Bobot badan aktual {actual:g}g sesuai standar CP 707 ({std_bw:g}g) pada hari ke-{day_age}."
message = f"Bobot badan aktual {actual:g}g sesuai standar ({std_bw:g}g) pada hari ke-{day_age}."
return {
"actual": actual,
@@ -1089,7 +1093,8 @@ def build_cp707_standard_block(context_data: dict[str, Any] | None) -> str:
ip = get_ip_standard_by_day(day)
ip_range = get_ip_standard_range()
bw = get_bw_standard_by_day(day)
target_bw_ctx = _resolve_target_bw(context_data, day)
bw = target_bw_ctx if target_bw_ctx is not None else get_bw_standard_by_day(day)
fcr = get_fcr_standard_by_day(day)
mort = get_mortality_cum_std_by_day(day)
@@ -1279,6 +1284,30 @@ def _resolve_iot(context_pack: dict[str, Any]) -> dict[str, Any]:
return context_pack
def _resolve_target_bw(context_pack: dict[str, Any] | None, day_age: int | None) -> float | None:
if not isinstance(context_pack, dict):
return None
direct = _first_positive_number(
context_pack.get("target_bw_gram"),
context_pack.get("targetWeight"),
context_pack.get("bobot_target_gram"),
context_pack.get("target_gram"),
context_pack.get("target_weight"),
)
if direct is not None:
return direct
tren = context_pack.get("tren_harian")
if isinstance(tren, list) and day_age is not None:
for row in tren:
if isinstance(row, dict) and row.get("hari") == day_age:
t = _first_positive_number(row.get("target_gram"), row.get("target_bw_gram"), row.get("target"))
if t is not None:
return t
return None
def analyze_with_cp707_standards(context_pack: dict[str, Any] | None, topic: str | None = None) -> dict[str, Any]:
"""Analisis komprehensif; toleran terhadap field InsightContext rebuild dan pack lama.
Topic-scoped: only grades metrics in TOPIC_METRICS[topic]. Unknown emitted only for
@@ -1294,8 +1323,9 @@ def analyze_with_cp707_standards(context_pack: dict[str, Any] | None, topic: str
# Body weight grading
if "bw" in metrics_to_grade:
actual_bw = _resolve_bw_grams(pack)
target_bw = _resolve_target_bw(pack, day_age)
if day_age is not None:
result["analyses"]["bw"] = analyze_bw(actual_bw, day_age)
result["analyses"]["bw"] = analyze_bw(actual_bw, day_age, target_bw)
else:
result["analyses"]["bw"] = {"status": "unknown", "message": "Umur tidak tersedia untuk grading bobot badan."}