From 46d0ea46a83d2c62ba16483d182f1fba685598b4 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 5 Dec 2025 13:36:55 +0100 Subject: [PATCH] Refactor weather update query in WeatherWorker: Simplified the SQL logic for assigning random weather types to regions by removing unnecessary joins and using a subquery for random selection. Enhanced comments for clarity on the individual weather assignment process for each region. --- src/worker/weather.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/worker/weather.rs b/src/worker/weather.rs index 0d7ecd6..e9a7dc8 100644 --- a/src/worker/weather.rs +++ b/src/worker/weather.rs @@ -12,6 +12,7 @@ pub struct WeatherWorker { // Query zum Aktualisieren des Wetters für alle Regionen // Wählt für jede Region ein zufälliges Wetter aus allen verfügbaren Wettertypen aus +// Wichtig: Jede Region bekommt ein individuelles, zufälliges Wetter const QUERY_UPDATE_WEATHER: &str = r#" WITH all_regions AS ( SELECT DISTINCT r.id AS region_id @@ -19,21 +20,16 @@ const QUERY_UPDATE_WEATHER: &str = r#" JOIN falukant_type.region tr ON r.region_type_id = tr.id WHERE tr.label_tr = 'city' ), - all_weather_types AS ( - SELECT id AS weather_type_id, ROW_NUMBER() OVER (ORDER BY RANDOM()) AS rn - FROM falukant_type.weather - ), random_weather AS ( SELECT ar.region_id, - awt.weather_type_id + ( + SELECT wt.id + FROM falukant_type.weather wt + ORDER BY RANDOM() + LIMIT 1 + ) AS weather_type_id FROM all_regions ar - CROSS JOIN LATERAL ( - SELECT weather_type_id - FROM all_weather_types - ORDER BY RANDOM() - LIMIT 1 - ) awt ) INSERT INTO falukant_data.weather (region_id, weather_type_id) SELECT rw.region_id, rw.weather_type_id