Skip to content

Commit ae08694

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

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

opensensor/collection_apis.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,27 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
228228
def create_model_instance(model: Type[BaseModel], data: dict):
229229
nested_fields = get_nested_fields(model)
230230

231-
# Handle flat models (like Pressure) that have a single main field
231+
# Handle flat models (like Pressure, LiquidLevel, pH) that have a single main field
232232
if len(model.__fields__) == 2 and "timestamp" in model.__fields__:
233233
main_field = next(field for field in model.__fields__ if field != "timestamp")
234234
lookup_field = (
235-
model.collection_name if hasattr(model, "collection_name") else model.__name__
235+
model.collection_name() if hasattr(model, "collection_name") else model.__name__
236236
)
237-
mongo_field = new_collections.get(lookup_field, main_field)
238-
if main_field not in data and mongo_field in data:
237+
mongo_field = new_collections.get(lookup_field, main_field.lower())
238+
239+
# Check if the mongo_field exists in the data
240+
if mongo_field in data:
239241
data[main_field] = data[mongo_field]
242+
elif main_field.lower() in data:
243+
# If the main_field (lowercase) exists in data, use it
244+
data[main_field] = data[main_field.lower()]
245+
else:
246+
# If neither the mongo_field nor the main_field exists, log an error
247+
logger.error(
248+
f"Field '{mongo_field}' or '{main_field}' not found in data for model {model.__name__}"
249+
)
250+
logger.error(f"Available fields in data: {list(data.keys())}")
251+
# You might want to set a default value or raise an exception here
240252

241253
for field_name, nested_model in nested_fields.items():
242254
if field_name in data:
@@ -350,10 +362,15 @@ def get_uniform_sample_pipeline(
350362
project_pipeline = create_nested_pipeline(response_model)
351363
project_pipeline["timestamp"] = "$timestamp"
352364

353-
# Handle flat models (like Pressure) that have a single main field
365+
# Handle flat models (like Pressure, LiquidLevel, pH) that have a single main field
354366
if len(response_model.__fields__) == 2 and "timestamp" in response_model.__fields__:
355367
main_field = next(field for field in response_model.__fields__ if field != "timestamp")
356-
mongo_field = new_collections.get(response_model.__name__, main_field.lower())
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())
357374
project_pipeline[main_field] = f"${mongo_field}"
358375
logger.debug(f"Mapping {mongo_field} to {main_field} for model {response_model.__name__}")
359376

0 commit comments

Comments
 (0)