Immer noch nicht richtig gut, also noch **WIP**
This commit is contained in:
85
api/main.py
85
api/main.py
@@ -383,24 +383,44 @@ async def get_daily_aggregated_data(
|
||||
conn = get_db_connection()
|
||||
try:
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
0 as id,
|
||||
date_trunc('day', datetime) as datetime,
|
||||
AVG(temperature) as temperature,
|
||||
ROUND(AVG(humidity)) as humidity,
|
||||
AVG(pressure) as pressure,
|
||||
AVG(wind_speed * 1.60934) as wind_speed,
|
||||
MAX(wind_gust * 1.60934) as wind_gust,
|
||||
AVG(wind_dir) as wind_dir,
|
||||
AVG(rain) as rain,
|
||||
AVG(rain_rate) as rain_rate,
|
||||
MAX(received_at) as received_at
|
||||
FROM weather_data
|
||||
WHERE datetime >= NOW() - make_interval(days => %s)
|
||||
GROUP BY date_trunc('day', datetime)
|
||||
ORDER BY datetime ASC
|
||||
""", (days,))
|
||||
# Bei 365 Tagen: alle verfügbaren Daten zurückgeben
|
||||
if days >= 365:
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
0 as id,
|
||||
date_trunc('day', datetime) as datetime,
|
||||
AVG(temperature) as temperature,
|
||||
ROUND(AVG(humidity)) as humidity,
|
||||
AVG(pressure) as pressure,
|
||||
AVG(wind_speed * 1.60934) as wind_speed,
|
||||
MAX(wind_gust * 1.60934) as wind_gust,
|
||||
AVG(wind_dir) as wind_dir,
|
||||
AVG(rain) as rain,
|
||||
AVG(rain_rate) as rain_rate,
|
||||
MAX(received_at) as received_at
|
||||
FROM weather_data
|
||||
GROUP BY date_trunc('day', datetime)
|
||||
ORDER BY datetime ASC
|
||||
""")
|
||||
else:
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
0 as id,
|
||||
date_trunc('day', datetime) as datetime,
|
||||
AVG(temperature) as temperature,
|
||||
ROUND(AVG(humidity)) as humidity,
|
||||
AVG(pressure) as pressure,
|
||||
AVG(wind_speed * 1.60934) as wind_speed,
|
||||
MAX(wind_gust * 1.60934) as wind_gust,
|
||||
AVG(wind_dir) as wind_dir,
|
||||
AVG(rain) as rain,
|
||||
AVG(rain_rate) as rain_rate,
|
||||
MAX(received_at) as received_at
|
||||
FROM weather_data
|
||||
WHERE datetime >= NOW() - make_interval(days => %s)
|
||||
GROUP BY date_trunc('day', datetime)
|
||||
ORDER BY datetime ASC
|
||||
""", (days,))
|
||||
results = cursor.fetchall()
|
||||
|
||||
return [dict(row) for row in results]
|
||||
@@ -440,15 +460,26 @@ async def get_weekly_rain_data(
|
||||
conn = get_db_connection()
|
||||
try:
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
date_trunc('week', datetime) as week_start,
|
||||
SUM(rain) as total_rain
|
||||
FROM weather_data
|
||||
WHERE datetime >= NOW() - make_interval(days => %s)
|
||||
GROUP BY date_trunc('week', datetime)
|
||||
ORDER BY week_start ASC
|
||||
""", (days,))
|
||||
# Bei 365 Tagen: alle verfügbaren Daten zurückgeben
|
||||
if days >= 365:
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
date_trunc('week', datetime) as week_start,
|
||||
SUM(rain) as total_rain
|
||||
FROM weather_data
|
||||
GROUP BY date_trunc('week', datetime)
|
||||
ORDER BY week_start ASC
|
||||
""")
|
||||
else:
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
date_trunc('week', datetime) as week_start,
|
||||
SUM(rain) as total_rain
|
||||
FROM weather_data
|
||||
WHERE datetime >= NOW() - make_interval(days => %s)
|
||||
GROUP BY date_trunc('week', datetime)
|
||||
ORDER BY week_start ASC
|
||||
""", (days,))
|
||||
results = cursor.fetchall()
|
||||
|
||||
return [dict(row) for row in results]
|
||||
|
||||
Reference in New Issue
Block a user