Skip to content

Commit 9d2966e

Browse files
committed
attempt to generalize the collection projection
1 parent cf5fc9e commit 9d2966e

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

opensensor/collection_apis.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from datetime import datetime, timedelta, timezone
23
from typing import Generic, List, Optional, Type, TypeVar, get_args, get_origin
34

@@ -35,6 +36,8 @@
3536
)
3637
from opensensor.utils.units import convert_temperature
3738

39+
logger = logging.getLogger(__name__)
40+
3841
T = TypeVar("T", bound=BaseModel)
3942

4043
old_collections = {
@@ -228,8 +231,9 @@ def create_model_instance(model: Type[BaseModel], data: dict):
228231
# Handle flat models (like Pressure) that have a single main field
229232
if len(model.__fields__) == 2 and "timestamp" in model.__fields__:
230233
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]
233237

234238
for field_name, nested_model in nested_fields.items():
235239
if field_name in data:
@@ -239,6 +243,8 @@ def create_model_instance(model: Type[BaseModel], data: dict):
239243
]
240244
else:
241245
data[field_name] = create_model_instance(nested_model, data[field_name])
246+
247+
logger.debug(f"Creating instance of {model.__name__} with data: {data}")
242248
return model(**data)
243249

244250

@@ -344,9 +350,9 @@ def get_uniform_sample_pipeline(
344350
# Handle flat models (like Pressure) that have a single main field
345351
if len(response_model.__fields__) == 2 and "timestamp" in response_model.__fields__:
346352
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__}")
350356

351357
pipeline = [
352358
{"$match": match_clause},
@@ -368,6 +374,7 @@ def get_uniform_sample_pipeline(
368374
{"$sort": {"timestamp": 1}},
369375
]
370376

377+
logger.debug(f"Pipeline for {response_model.__name__}: {pipeline}")
371378
return pipeline
372379

373380

0 commit comments

Comments
 (0)