Skip to content

Commit cf5fc9e

Browse files
committed
attempt to correct API for nested data model retrieval.
1 parent f38c113 commit cf5fc9e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

opensensor/collection_apis.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ def create_nested_pipeline(model: Type[BaseModel], prefix=""):
224224

225225
def create_model_instance(model: Type[BaseModel], data: dict):
226226
nested_fields = get_nested_fields(model)
227+
228+
# Handle flat models (like Pressure) that have a single main field
229+
if len(model.__fields__) == 2 and "timestamp" in model.__fields__:
230+
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__])
233+
227234
for field_name, nested_model in nested_fields.items():
228235
if field_name in data:
229236
if isinstance(data[field_name], list):
@@ -334,6 +341,13 @@ def get_uniform_sample_pipeline(
334341
project_pipeline = create_nested_pipeline(response_model)
335342
project_pipeline["timestamp"] = "$timestamp"
336343

344+
# Handle flat models (like Pressure) that have a single main field
345+
if len(response_model.__fields__) == 2 and "timestamp" in response_model.__fields__:
346+
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+
)
350+
337351
pipeline = [
338352
{"$match": match_clause},
339353
{

0 commit comments

Comments
 (0)