@@ -105,6 +105,17 @@ def create_partition(
105
105
return super ().__new__ (cls , clsname , bases , attrs )
106
106
107
107
108
+ def default_horizon_minutes (context ):
109
+ columns = context .get_current_parameters ()
110
+ target_time = columns ["target_time" ]
111
+ created_utc = columns ["created_utc" ]
112
+
113
+ if created_utc is None :
114
+ created_utc = datetime .now (tz = target_time .tzinfo )
115
+ delta = (target_time - created_utc ).total_seconds () / 60.0 # convert to minutes
116
+ return delta
117
+
118
+
108
119
class ForecastValueSQLMixin (CreatedMixin ):
109
120
"""One Forecast of generation at one timestamp
110
121
@@ -119,6 +130,17 @@ class ForecastValueSQLMixin(CreatedMixin):
119
130
# Want to keep it as json so that we can store different properties for different forecasts
120
131
properties = Column (MutableDict .as_mutable (JSON ), nullable = True )
121
132
133
+ # we want to store the forecast horizon. This is the difference between target_time and created_utc.
134
+ # By storing the value, and index, queries should be faster when looking for an N hour forecast.
135
+ # I tried to use server_default = "CAST(EXTRACT(EPOCH FROM (target_time - created_utc))/60 as INT)"
136
+ # but this does not work.
137
+ horizon_minutes = Column (
138
+ Integer ,
139
+ nullable = True ,
140
+ index = True ,
141
+ default = default_horizon_minutes ,
142
+ )
143
+
122
144
@declared_attr
123
145
def forecast_id (self ):
124
146
"""Link with Forecast table"""
0 commit comments