Skip to content

Commit 4e58c0f

Browse files
committed
attempt to generalize the collection projection
1 parent ae08694 commit 4e58c0f

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

opensensor/collection_apis.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -362,17 +362,19 @@ def get_uniform_sample_pipeline(
362362
project_pipeline = create_nested_pipeline(response_model)
363363
project_pipeline["timestamp"] = "$timestamp"
364364

365-
# Handle flat models (like Pressure, LiquidLevel, pH) that have a single main field
366-
if len(response_model.__fields__) == 2 and "timestamp" in response_model.__fields__:
367-
main_field = next(field for field in response_model.__fields__ if field != "timestamp")
368-
lookup_field = (
369-
response_model.collection_name()
370-
if hasattr(response_model, "collection_name")
371-
else response_model.__name__
372-
)
373-
mongo_field = new_collections.get(lookup_field, main_field.lower())
374-
project_pipeline[main_field] = f"${mongo_field}"
375-
logger.debug(f"Mapping {mongo_field} to {main_field} for model {response_model.__name__}")
365+
# # Handle flat models (like Pressure, LiquidLevel, pH) that have a single main field
366+
# if len(response_model.__fields__) == 2 and "timestamp" in response_model.__fields__:
367+
# main_field = next(field for field in response_model.__fields__ if field != "timestamp")
368+
# lookup_field = (
369+
# response_model.collection_name()
370+
# if hasattr(response_model, "collection_name")
371+
# else response_model.__name__
372+
# )
373+
# mongo_field = new_collections.get(lookup_field, main_field.lower())
374+
# project_pipeline[main_field] = f"${mongo_field}"
375+
# logger.info(f"Mapping {mongo_field} to {main_field} for model {response_model.__name__}")
376+
377+
logger.info(f"Project pipeline for {response_model.__name__}: {project_pipeline}")
376378

377379
pipeline = [
378380
{"$match": match_clause},
@@ -394,7 +396,7 @@ def get_uniform_sample_pipeline(
394396
{"$sort": {"timestamp": 1}},
395397
]
396398

397-
logger.debug(f"Pipeline for {response_model.__name__}: {pipeline}")
399+
logger.info(f"Pipeline for {response_model.__name__}: {pipeline}")
398400
return pipeline
399401

400402

opensensor/collections.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class LiquidLevel(TimestampModel):
5151

5252
liquid: bool
5353

54+
@classmethod
55+
def collection_name(cls):
56+
return "liquid"
57+
5458

5559
class RelayStatus(BaseModel):
5660
position: int
@@ -64,6 +68,10 @@ class RelayBoard(TimestampModel):
6468

6569
relays: List[RelayStatus]
6670

71+
@classmethod
72+
def collection_name(cls):
73+
return "relays"
74+
6775

6876
class Moisture(TimestampModel):
6977
readings: List[float | Decimal | int] | str

0 commit comments

Comments
 (0)