10
10
EntityEidAttrValueOrHistory ,
11
11
EntityEidData ,
12
12
EntityEidList ,
13
+ EntityEidMasterRecord ,
14
+ EntityEidSnapshots ,
13
15
)
14
16
from dp3 .api .internal .helpers import api_to_dp3_datapoint
15
17
from dp3 .api .internal .models import (
@@ -28,6 +30,43 @@ async def check_etype(etype: str):
28
30
return etype
29
31
30
32
33
+ def get_eid_master_record_handler (
34
+ etype : str , eid : str , date_from : Optional [datetime ] = None , date_to : Optional [datetime ] = None
35
+ ):
36
+ """Handler for getting master record of EID"""
37
+ # TODO: This is probably not the most efficient way. Maybe gather only
38
+ # plain data from master record and then call `get_timeseries_history`
39
+ # for timeseries.
40
+ master_record = DB .get_master_record (etype , eid )
41
+ if "_id" in master_record :
42
+ del master_record ["_id" ]
43
+ if "#hash" in master_record :
44
+ del master_record ["#hash" ]
45
+
46
+ entity_attribs = MODEL_SPEC .attribs (etype )
47
+
48
+ # Get filtered timeseries data
49
+ for attr in master_record :
50
+ # Check for no longer existing attributes
51
+ if attr in entity_attribs and entity_attribs [attr ].t == AttrType .TIMESERIES :
52
+ master_record [attr ] = DB .get_timeseries_history (
53
+ etype , attr , eid , t1 = date_from , t2 = date_to
54
+ )
55
+
56
+ return master_record
57
+
58
+
59
+ def get_eid_snapshots_handler (
60
+ etype : str , eid : str , date_from : Optional [datetime ] = None , date_to : Optional [datetime ] = None
61
+ ):
62
+ """Handler for getting snapshots of EID"""
63
+ snapshots = list (DB .get_snapshots (etype , eid , t1 = date_from , t2 = date_to ))
64
+ for s in snapshots :
65
+ del s ["_id" ]
66
+
67
+ return snapshots
68
+
69
+
31
70
router = APIRouter (dependencies = [Depends (check_etype )])
32
71
33
72
# As variable, because otherwise generates ruff B008 error
@@ -104,38 +143,34 @@ async def get_eid_data(
104
143
105
144
Contains all snapshots and master record.
106
145
Snapshots are ordered by ascending creation time.
107
- """
108
- # Get master record
109
- # TODO: This is probably not the most efficient way. Maybe gather only
110
- # plain data from master record and then call `get_timeseries_history`
111
- # for timeseries.
112
- master_record = DB .get_master_record (etype , eid )
113
- if "_id" in master_record :
114
- del master_record ["_id" ]
115
- if "#hash" in master_record :
116
- del master_record ["#hash" ]
117
-
118
- entity_attribs = MODEL_SPEC .attribs (etype )
119
146
120
- # Get filtered timeseries data
121
- for attr in master_record :
122
- # Check for no longer existing attributes
123
- if attr in entity_attribs and entity_attribs [attr ].t == AttrType .TIMESERIES :
124
- master_record [attr ] = DB .get_timeseries_history (
125
- etype , attr , eid , t1 = date_from , t2 = date_to
126
- )
127
-
128
- # Get snapshots
129
- snapshots = list (DB .get_snapshots (etype , eid , t1 = date_from , t2 = date_to ))
130
- for s in snapshots :
131
- del s ["_id" ]
147
+ Combines function of `/{etype}/{eid}/master` and `/{etype}/{eid}/snapshots`.
148
+ """
149
+ master_record = get_eid_master_record_handler (etype , eid , date_from , date_to )
150
+ snapshots = get_eid_snapshots_handler (etype , eid , date_from , date_to )
132
151
133
152
# Whether this eid contains any data
134
153
empty = not master_record and len (snapshots ) == 0
135
154
136
155
return EntityEidData (empty = empty , master_record = master_record , snapshots = snapshots )
137
156
138
157
158
+ @router .get ("/{etype}/{eid}/master" )
159
+ async def get_eid_master_record (
160
+ etype : str , eid : str , date_from : Optional [datetime ] = None , date_to : Optional [datetime ] = None
161
+ ) -> EntityEidMasterRecord :
162
+ """Get master record of `etype`'s `eid`."""
163
+ return get_eid_master_record_handler (etype , eid , date_from , date_to )
164
+
165
+
166
+ @router .get ("/{etype}/{eid}/snapshots" )
167
+ async def get_eid_snapshots (
168
+ etype : str , eid : str , date_from : Optional [datetime ] = None , date_to : Optional [datetime ] = None
169
+ ) -> EntityEidSnapshots :
170
+ """Get snapshots of `etype`'s `eid`."""
171
+ return get_eid_snapshots_handler (etype , eid , date_from , date_to )
172
+
173
+
139
174
@router .get ("/{etype}/{eid}/get/{attr}" )
140
175
async def get_eid_attr_value (
141
176
etype : str ,
0 commit comments