From d31a834e7fb392e7726cc807d2134fb23782c0b5 Mon Sep 17 00:00:00 2001 From: Jakob de Maeyer Date: Mon, 14 Oct 2024 16:18:02 +0200 Subject: [PATCH] Fall back to partial fill-up for current_weather endpoint --- brightsky/query.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/brightsky/query.py b/brightsky/query.py index dfe5c8a..55219d5 100644 --- a/brightsky/query.py +++ b/brightsky/query.py @@ -157,14 +157,16 @@ def current_weather( } -def _current_weather(source_ids, not_null=None): +def _current_weather(source_ids, not_null=None, partial=False): params = { 'source_ids': source_ids, 'source_ids_tuple': tuple(source_ids), } where = "source_id IN %(source_ids_tuple)s" if not_null: - where += ''.join(f" AND {element} IS NOT NULL" for element in not_null) + glue = ' OR ' if partial else ' AND ' + extra = glue.join(f"{element} IS NOT NULL" for element in not_null) + where += f'AND ({extra})' sql = f""" SELECT * FROM current_weather @@ -174,6 +176,12 @@ def _current_weather(source_ids, not_null=None): """ rows = _make_dicts(fetch(sql, params)) if not rows: + if not_null and not partial: + return _current_weather( + source_ids, + not_null=not_null, + partial=True, + ) return {} return rows[0]