From be4f14250c14ab419c2c75599e8ca627018063aa Mon Sep 17 00:00:00 2001 From: XANTRONIX Industrial Date: Mon, 7 Apr 2025 12:25:59 -0400 Subject: [PATCH] Implement SPCOutlook.for_timestamp() --- lib/xmet/spc.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/xmet/spc.py b/lib/xmet/spc.py index 9a0beb7..76da8ff 100644 --- a/lib/xmet/spc.py +++ b/lib/xmet/spc.py @@ -4,9 +4,9 @@ import shapely import datetime import cairo -from typing import Optional +from typing import Optional, Self -from xmet.db import Database, DatabaseTable +from xmet.db import Database, DatabaseTable, DatabaseOrder from xmet.coord import COORD_SYSTEM from xmet.city import City from xmet.map import EquirectMap, MAP_SCREEN_DIMENSIONS, MAP_BOUNDS @@ -284,7 +284,7 @@ class SPCOutlookCategoryArea(SPCOutlookArea): def sort_value(self): return self.__category_levels__[self.category] -class SPCOutlook(): +class SPCOutlook(DatabaseTable): __slots__ = ( 'id', 'timestamp_issued', 'timestamp_start', 'timestamp_end', 'day', 'text_raw', 'body', 'poly', 'probabilities', 'categories' @@ -298,7 +298,15 @@ class SPCOutlook(): 'day', 'text_raw', 'body' ) + __values_read__ = { + 'timestamp_issued': datetime.datetime.fromisoformat, + 'timestamp_start': datetime.datetime.fromisoformat, + 'timestamp_end': datetime.datetime.fromisoformat + } + def __init__(self): + super().__init__() + self.id = None self.timestamp_issued = None self.timestamp_start = None @@ -311,6 +319,29 @@ class SPCOutlook(): self.probabilities = list() self.categories = list() + @staticmethod + def for_timestamp(db: Database, valid: str, day: int) -> Self: + st = db.query(SPCOutlook, [ + 'timestamp_start >= ?', + 'day = ?' + ], (valid, day), (('timestamp_issued', DatabaseOrder.ASC),), 1) + + outlook = st.fetchone() + + st = db.get_many(SPCOutlookProbabilityArea, { + 'outlook_id': outlook.id + }) + + outlook.probabilities = list(st.fetchall()) + + st = db.get_many(SPCOutlookCategoryArea, { + 'outlook_id': outlook.id + }) + + outlook.categories = list(st.fetchall()) + + return outlook + def sorted_probabilities(self) -> list[SPCOutlookProbabilityArea]: return sorted(self.probabilities, key=lambda p: p.sort_value())