UPDATE
This commit is contained in:
@@ -41,16 +41,6 @@ def _normalize_to_ideal_score(value: float | None, minimum: float, ideal: float,
|
||||
|
||||
|
||||
def _resolve_profile(context: dict[str, Any]) -> tuple[dict[str, dict[str, float]], str]:
|
||||
location = context.get("location")
|
||||
if location is not None:
|
||||
location_profile = getattr(location, "ideal_sensor_profile", None) or {}
|
||||
if location_profile:
|
||||
merged = {
|
||||
metric: {**DEFAULT_IDEAL_SENSOR_PROFILE.get(metric, {}), **location_profile.get(metric, {})}
|
||||
for metric in set(DEFAULT_IDEAL_SENSOR_PROFILE) | set(location_profile)
|
||||
}
|
||||
return merged, "location"
|
||||
|
||||
plants = context.get("plants", [])
|
||||
for plant in plants:
|
||||
plant_profile = getattr(plant, "health_profile", None) or {}
|
||||
|
||||
@@ -3,7 +3,7 @@ from __future__ import annotations
|
||||
from math import sqrt
|
||||
from typing import Any
|
||||
|
||||
from sensor_data.models import SensorData, SensorDataHistory
|
||||
from farm_data.models import SensorData
|
||||
|
||||
|
||||
QUALITY_REAL = "REAL"
|
||||
@@ -61,9 +61,9 @@ def _latest_sensor_measurement(sensor: Any, histories: list[Any]) -> dict[str, A
|
||||
series = _sensor_time_series(sensor, histories)
|
||||
latest = series[-1] if series else {"timestamp": None, "value": None, "quality_flag": QUALITY_MISSING}
|
||||
return {
|
||||
"sensor_id": str(sensor.uuid_sensor),
|
||||
"latitude": float(sensor.location.latitude),
|
||||
"longitude": float(sensor.location.longitude),
|
||||
"sensor_id": str(sensor.farm_uuid),
|
||||
"latitude": float(sensor.center_location.latitude),
|
||||
"longitude": float(sensor.center_location.longitude),
|
||||
"depth": None,
|
||||
"timestamp": latest["timestamp"],
|
||||
"soil_moisture_value": latest["value"],
|
||||
@@ -99,7 +99,7 @@ def _grid_axis(min_value: float, max_value: float) -> list[float]:
|
||||
|
||||
def _load_sensor_network(current_sensor: Any) -> list[Any]:
|
||||
plant_ids = list(current_sensor.plants.values_list("id", flat=True))
|
||||
queryset = SensorData.objects.select_related("location").prefetch_related("plants")
|
||||
queryset = SensorData.objects.select_related("center_location").prefetch_related("plants")
|
||||
if plant_ids:
|
||||
queryset = queryset.filter(plants__id__in=plant_ids).distinct()
|
||||
return list(queryset)
|
||||
@@ -118,14 +118,8 @@ def build_soil_moisture_heatmap(sensor_id: str, context: dict | None = None, ai_
|
||||
}
|
||||
|
||||
sensors = _load_sensor_network(current_sensor)
|
||||
sensor_ids = [sensor.uuid_sensor for sensor in sensors]
|
||||
history_rows = SensorDataHistory.objects.filter(uuid_sensor__in=sensor_ids).order_by("-recorded_at")[:200]
|
||||
history_map: dict[Any, list[Any]] = {}
|
||||
for row in history_rows:
|
||||
history_map.setdefault(row.uuid_sensor, []).append(row)
|
||||
|
||||
sensor_points = [
|
||||
_latest_sensor_measurement(sensor, history_map.get(sensor.uuid_sensor, []))
|
||||
_latest_sensor_measurement(sensor, [])
|
||||
for sensor in sensors
|
||||
]
|
||||
valid_sensor_points = [point for point in sensor_points if point["soil_moisture_value"] is not None]
|
||||
|
||||
Reference in New Issue
Block a user