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]
|
||||
|
||||
@@ -4,17 +4,17 @@ from datetime import date
|
||||
def load_dashboard_context(sensor_id: str) -> dict | None:
|
||||
from irrigation.models import IrrigationMethod
|
||||
from location_data.models import SoilDepthData
|
||||
from sensor_data.models import SensorData, SensorDataHistory
|
||||
from farm_data.models import SensorData
|
||||
from weather.models import WeatherForecast
|
||||
|
||||
try:
|
||||
sensor = SensorData.objects.select_related("location").prefetch_related("plants").get(
|
||||
uuid_sensor=sensor_id
|
||||
sensor = SensorData.objects.select_related("center_location").prefetch_related("plants").get(
|
||||
farm_uuid=sensor_id
|
||||
)
|
||||
except SensorData.DoesNotExist:
|
||||
return None
|
||||
|
||||
location = sensor.location
|
||||
location = sensor.center_location
|
||||
depths = list(
|
||||
SoilDepthData.objects.filter(soil_location=location).order_by("depth_label")
|
||||
)
|
||||
@@ -22,9 +22,6 @@ def load_dashboard_context(sensor_id: str) -> dict | None:
|
||||
WeatherForecast.objects.filter(location=location, forecast_date__gte=date.today())
|
||||
.order_by("forecast_date")[:7]
|
||||
)
|
||||
history = list(
|
||||
SensorDataHistory.objects.filter(uuid_sensor=sensor_id).order_by("-recorded_at")[:30]
|
||||
)
|
||||
plants = list(sensor.plants.all())
|
||||
irrigation_methods = list(IrrigationMethod.objects.all()[:5])
|
||||
|
||||
@@ -33,8 +30,7 @@ def load_dashboard_context(sensor_id: str) -> dict | None:
|
||||
"location": location,
|
||||
"depths": depths,
|
||||
"forecasts": forecasts,
|
||||
"history": history,
|
||||
"history": [],
|
||||
"plants": plants,
|
||||
"irrigation_methods": irrigation_methods,
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ def _farm_profile_from_context(context: dict) -> dict:
|
||||
irrigation_methods = context.get("irrigation_methods", [])
|
||||
|
||||
return {
|
||||
"sensor_id": str(getattr(sensor, "uuid_sensor", "")) if sensor else "",
|
||||
"sensor_id": str(getattr(sensor, "farm_uuid", "")) if sensor else "",
|
||||
"crop_type": getattr(plants[0], "name", None) if plants else None,
|
||||
"region": {
|
||||
"latitude": float(location.latitude) if location else None,
|
||||
|
||||
Reference in New Issue
Block a user