1
+ import logging
1
2
from datetime import datetime , timedelta , timezone
2
3
from typing import Generic , List , Optional , Type , TypeVar , get_args , get_origin
3
4
35
36
)
36
37
from opensensor .utils .units import convert_temperature
37
38
39
+ logger = logging .getLogger (__name__ )
40
+
38
41
T = TypeVar ("T" , bound = BaseModel )
39
42
40
43
old_collections = {
@@ -228,8 +231,9 @@ def create_model_instance(model: Type[BaseModel], data: dict):
228
231
# Handle flat models (like Pressure) that have a single main field
229
232
if len (model .__fields__ ) == 2 and "timestamp" in model .__fields__ :
230
233
main_field = next (field for field in model .__fields__ if field != "timestamp" )
231
- if main_field not in data and model .__name__ in new_collections :
232
- data [main_field ] = data .get (new_collections [model .__name__ ])
234
+ mongo_field = new_collections .get (model .__name__ , main_field .lower ())
235
+ if main_field not in data and mongo_field in data :
236
+ data [main_field ] = data [mongo_field ]
233
237
234
238
for field_name , nested_model in nested_fields .items ():
235
239
if field_name in data :
@@ -239,6 +243,8 @@ def create_model_instance(model: Type[BaseModel], data: dict):
239
243
]
240
244
else :
241
245
data [field_name ] = create_model_instance (nested_model , data [field_name ])
246
+
247
+ logger .debug (f"Creating instance of { model .__name__ } with data: { data } " )
242
248
return model (** data )
243
249
244
250
@@ -344,9 +350,9 @@ def get_uniform_sample_pipeline(
344
350
# Handle flat models (like Pressure) that have a single main field
345
351
if len (response_model .__fields__ ) == 2 and "timestamp" in response_model .__fields__ :
346
352
main_field = next (field for field in response_model .__fields__ if field != "timestamp" )
347
- project_pipeline [ main_field ] = (
348
- f"${ new_collections . get ( response_model . __name__ , main_field ) } "
349
- )
353
+ mongo_field = new_collections . get ( response_model . __name__ , main_field . lower ())
354
+ project_pipeline [ main_field ] = f"${ mongo_field } "
355
+ logger . debug ( f"Mapping { mongo_field } to { main_field } for model { response_model . __name__ } " )
350
356
351
357
pipeline = [
352
358
{"$match" : match_clause },
@@ -368,6 +374,7 @@ def get_uniform_sample_pipeline(
368
374
{"$sort" : {"timestamp" : 1 }},
369
375
]
370
376
377
+ logger .debug (f"Pipeline for { response_model .__name__ } : { pipeline } " )
371
378
return pipeline
372
379
373
380
0 commit comments