Files
Ai/dashboard_data/context.py
T
2026-03-22 01:09:09 +03:30

41 lines
1.2 KiB
Python

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 weather.models import WeatherForecast
try:
sensor = SensorData.objects.select_related("location").prefetch_related("plants").get(
uuid_sensor=sensor_id
)
except SensorData.DoesNotExist:
return None
location = sensor.location
depths = list(
SoilDepthData.objects.filter(soil_location=location).order_by("depth_label")
)
forecasts = list(
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])
return {
"sensor": sensor,
"location": location,
"depths": depths,
"forecasts": forecasts,
"history": history,
"plants": plants,
"irrigation_methods": irrigation_methods,
}