11import uuid
22from datetime import datetime
3- from typing import Optional , Literal , Union
3+ from typing import Optional , Literal
44
55from ninja import Field , Query
66
1515)
1616
1717
18- WindowUnits = Literal ["minutes" , "hours" , "days" ]
18+ WindowIntervalUnits = Literal ["minutes" , "hours" , "days" ]
19+ RuleType = Literal ["range" , "rate_of_change" , "persistence" , "missing_data" ]
1920
2021
2122class MonitoringRuleOrderBy (OrderByField ):
@@ -36,123 +37,48 @@ class MonitoringRuleQueryParameters(CollectionQueryParameters):
3637 )
3738
3839
39- # --- Per-type response schemas ---
40-
41- class RangeRuleResponse (BaseGetResponse ):
42- rule_type : Literal ["range" ] = Field (alias = "type" )
40+ class MonitoringRuleResponse (BaseGetResponse ):
41+ id : uuid .UUID
42+ datastream : DatastreamSummaryResponse
43+ rule_type : RuleType
44+ last_checked_at : Optional [datetime ] = None
4345 min_value : Optional [float ] = None
4446 max_value : Optional [float ] = None
47+ window_interval : Optional [int ] = None
48+ window_interval_units : Optional [WindowIntervalUnits ] = None
4549
50+ @staticmethod
51+ def resolve_datastream (obj ):
52+ return obj .datastream
4653
47- class RateOfChangeRuleResponse (BaseGetResponse ):
48- rule_type : Literal ["rate_of_change" ] = Field (alias = "type" )
49- max_change : float
50- window : int
51- window_units : WindowUnits
52-
53-
54- class PersistenceRuleResponse (BaseGetResponse ):
55- rule_type : Literal ["persistence" ] = Field (alias = "type" )
56- persist_value : Optional [float ] = None
57- window : int
58- window_units : WindowUnits
59-
60-
61- class MissingDataRuleResponse (BaseGetResponse ):
62- rule_type : Literal ["missing_data" ] = Field (alias = "type" )
63- window : int
64- window_units : WindowUnits
65-
66-
67- # --- Per-type post body schemas ---
68-
69- class RangeRulePostBody (BasePostBody , RangeRuleResponse ):
70- ...
71-
72-
73- class RateOfChangeRulePostBody (BasePostBody , RateOfChangeRuleResponse ):
74- ...
75-
76-
77- class PersistenceRulePostBody (BasePostBody , PersistenceRuleResponse ):
78- ...
79-
80-
81- class MissingDataRulePostBody (BasePostBody , MissingDataRuleResponse ):
82- ...
83-
84-
85- # --- Per-type patch body schemas ---
86-
87- class RangeRulePatchBody (BasePatchBody , RangeRuleResponse ):
88- ...
89-
90-
91- class RateOfChangeRulePatchBody (BasePatchBody , RateOfChangeRuleResponse ):
92- ...
93-
94-
95- class PersistenceRulePatchBody (BasePatchBody , PersistenceRuleResponse ):
96- ...
97-
98-
99- class MissingDataRulePatchBody (BasePatchBody , MissingDataRuleResponse ):
100- ...
101-
102-
103- # --- Shared resolve_rule helper ---
104-
105- def _resolve_rule (obj ) -> dict :
106- return {
107- "rule_type" : obj .rule_type ,
108- "min_value" : obj .min_value ,
109- "max_value" : obj .max_value ,
110- "max_change" : obj .max_change ,
111- "persist_value" : obj .persist_value ,
112- "window" : obj .window ,
113- "window_units" : obj .window_units ,
114- }
115-
116-
117- # --- Top-level rule schemas ---
11854
11955class MonitoringRuleDetailResponse (BaseGetResponse ):
120- """Rule details without the datastream — used when nested under a MonitoredDatastreamResponse."""
12156 id : uuid .UUID
57+ rule_type : RuleType
12258 last_checked_at : Optional [datetime ] = None
123- rule : Union [RangeRuleResponse , RateOfChangeRuleResponse , PersistenceRuleResponse , MissingDataRuleResponse ]
124-
125- @staticmethod
126- def resolve_rule (obj ):
127- return _resolve_rule (obj )
59+ min_value : Optional [float ] = None
60+ max_value : Optional [float ] = None
61+ window_interval : Optional [int ] = None
62+ window_interval_units : Optional [WindowIntervalUnits ] = None
12863
12964
13065class MonitoredDatastreamResponse (BaseGetResponse ):
131- """A datastream and all its rules for a given monitoring task."""
13266 datastream : DatastreamSummaryResponse
13367 rules : list [MonitoringRuleDetailResponse ]
13468
13569
136- class MonitoringRuleResponse (BaseGetResponse ):
137- id : uuid .UUID
138- datastream : DatastreamSummaryResponse
139- last_checked_at : Optional [datetime ] = None
140- rule : Union [RangeRuleResponse , RateOfChangeRuleResponse , PersistenceRuleResponse , MissingDataRuleResponse ]
141-
142- @staticmethod
143- def resolve_datastream (obj ):
144- return obj .datastream
145-
146- @staticmethod
147- def resolve_rule (obj ):
148- return _resolve_rule (obj )
149-
150-
15170class MonitoringRulePostBody (BasePostBody ):
15271 uid : uuid .UUID | Unset = Field (Unset , alias = "id" )
15372 datastream_id : uuid .UUID
154- rule : Union [RangeRulePostBody , RateOfChangeRulePostBody , PersistenceRulePostBody , MissingDataRulePostBody ]
73+ rule_type : RuleType
74+ min_value : Optional [float ] = None
75+ max_value : Optional [float ] = None
76+ window_interval : Optional [int ] = None
77+ window_interval_units : Optional [WindowIntervalUnits ] = None
15578
15679
15780class MonitoringRulePatchBody (BasePatchBody ):
158- rule : Union [RangeRulePatchBody , RateOfChangeRulePatchBody , PersistenceRulePatchBody , MissingDataRulePatchBody ]
81+ min_value : Optional [float ]
82+ max_value : Optional [float ]
83+ window_interval : Optional [int ]
84+ window_interval_units : Optional [WindowIntervalUnits ]
0 commit comments