16
16
JSON ,
17
17
Boolean ,
18
18
Column ,
19
+ Computed ,
19
20
DateTime ,
20
21
Float ,
21
22
ForeignKey ,
@@ -105,26 +106,6 @@ def create_partition(
105
106
return super ().__new__ (cls , clsname , bases , attrs )
106
107
107
108
108
- def default_horizon_minutes (context ):
109
- """Make a default horizon minutes for the ForecastValueSQLMixin"""
110
- columns = context .get_current_parameters ()
111
- target_time = columns ["target_time" ]
112
- created_utc = columns ["created_utc" ]
113
-
114
- if created_utc is None :
115
- created_utc = datetime .now (tz = timezone .utc )
116
-
117
- if isinstance (target_time , str ):
118
- target_time = datetime .fromisoformat (target_time )
119
-
120
- # make sure they both have tz, or not
121
- if target_time .tzinfo is None :
122
- created_utc = created_utc .replace (tzinfo = None )
123
-
124
- delta = (target_time - created_utc ).total_seconds () / 60.0 # convert to minutes
125
- return delta
126
-
127
-
128
109
class ForecastValueSQLMixin (CreatedMixin ):
129
110
"""One Forecast of generation at one timestamp
130
111
@@ -142,14 +123,11 @@ class ForecastValueSQLMixin(CreatedMixin):
142
123
# we want to store the forecast horizon.
143
124
# This is the difference between target_time and created_utc.
144
125
# By storing the value queries should be faster when looking for an N hour forecast.
145
- # I tried to use
146
- # server_default = "CAST(EXTRACT(EPOCH FROM (target_time - created_utc))/60 as INT)"
147
- # but this does not work.
148
126
horizon_minutes = Column (
149
127
Integer ,
150
- nullable = True ,
128
+ Computed ("CAST(EXTRACT(EPOCH FROM target_time - created_utc)/60 as INT)" ),
129
+ nullable = False ,
151
130
index = True ,
152
- default = default_horizon_minutes ,
153
131
)
154
132
155
133
@declared_attr
0 commit comments