Skip to content

Commit 1c9d99d

Browse files
committed
refactor to group first
1 parent 9d9c5e4 commit 1c9d99d

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

opensensor/collection_apis.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,6 @@ def get_vpd_pipeline(
200200
# The MongoDB aggregation pipeline for VPD calculation
201201
pipeline = [
202202
{"$match": match_clause},
203-
{
204-
"$addFields": {
205-
"group": {
206-
"$floor": {
207-
"$divide": [
208-
{"$subtract": ["$timestamp", start_date]},
209-
sampling_interval.total_seconds() * 1000,
210-
]
211-
}
212-
}
213-
}
214-
},
215203
{"$addFields": {"tempAsFloat": {"$toDouble": "$temp"}}},
216204
{"$addFields": {"rhAsFloat": {"$toDouble": "$rh"}}},
217205
{
@@ -242,15 +230,23 @@ def get_vpd_pipeline(
242230
}
243231
},
244232
{
245-
"$group": {
246-
"_id": "$group",
247-
"timestamp": {"$first": "$timestamp"},
248-
"vpd": {"$avg": "$vpd"},
233+
"$addFields": {
234+
"group": {
235+
"$floor": {
236+
"$divide": [
237+
{"$subtract": ["$timestamp", start_date]},
238+
sampling_interval.total_seconds() * 1000,
239+
]
240+
}
241+
}
249242
}
250243
},
251-
{"$project": project_projection},
252244
{"$sort": {"timestamp": 1}},
245+
{"$group": {"_id": "$group", "doc": {"$first": "$$ROOT"}}},
246+
{"$replaceRoot": {"newRoot": "$doc"}},
247+
{"$project": project_projection},
253248
]
249+
254250
return pipeline
255251

256252

0 commit comments

Comments
 (0)