62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from .growth_simulation import (
|
||
|
|
CurrentFarmChartSimulator,
|
||
|
|
GrowthSimulationError,
|
||
|
|
_fa_engine_name,
|
||
|
|
_fa_model_name,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def build_yield_prediction_payload(
|
||
|
|
*,
|
||
|
|
farm_uuid: str,
|
||
|
|
plant_name: str | None = None,
|
||
|
|
irrigation_recommendation: dict[str, Any] | None = None,
|
||
|
|
fertilization_recommendation: dict[str, Any] | None = None,
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
simulator = CurrentFarmChartSimulator()
|
||
|
|
result = simulator.simulate(
|
||
|
|
farm_uuid=farm_uuid,
|
||
|
|
plant_name=plant_name,
|
||
|
|
irrigation_recommendation=irrigation_recommendation,
|
||
|
|
fertilization_recommendation=fertilization_recommendation,
|
||
|
|
)
|
||
|
|
yield_estimate = float((result.get("metrics") or {}).get("yield_estimate") or 0.0)
|
||
|
|
predicted_yield_tons = round(max(yield_estimate / 1000.0, 0.0), 2)
|
||
|
|
return {
|
||
|
|
"farm_uuid": farm_uuid,
|
||
|
|
"plant_name": result.get("plant_name"),
|
||
|
|
"predictedYieldTons": predicted_yield_tons,
|
||
|
|
"predictedYieldRaw": round(yield_estimate, 2),
|
||
|
|
"unit": "تن",
|
||
|
|
"sourceUnit": "کیلوگرم در هکتار",
|
||
|
|
"simulationEngine": _fa_engine_name(result.get("engine")),
|
||
|
|
"simulationModel": _fa_model_name(result.get("model_name")),
|
||
|
|
"scenarioId": result.get("scenario_id"),
|
||
|
|
"simulationWarning": result.get("simulation_warning"),
|
||
|
|
"supportingMetrics": result.get("metrics") or {},
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
class YieldPredictionService:
|
||
|
|
def get_yield_prediction(
|
||
|
|
self,
|
||
|
|
*,
|
||
|
|
farm_uuid: str,
|
||
|
|
plant_name: str | None = None,
|
||
|
|
irrigation_recommendation: dict[str, Any] | None = None,
|
||
|
|
fertilization_recommendation: dict[str, Any] | None = None,
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
try:
|
||
|
|
return build_yield_prediction_payload(
|
||
|
|
farm_uuid=farm_uuid,
|
||
|
|
plant_name=plant_name,
|
||
|
|
irrigation_recommendation=irrigation_recommendation,
|
||
|
|
fertilization_recommendation=fertilization_recommendation,
|
||
|
|
)
|
||
|
|
except GrowthSimulationError:
|
||
|
|
raise
|