Skip to content

Commit b3f991d

Browse files
committed
refactor to sample first
1 parent 1c9d99d commit b3f991d

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

opensensor/collection_apis.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,6 @@ def get_vpd_pipeline(
188188
):
189189
sampling_interval = timedelta(minutes=resolution)
190190
match_clause = get_initial_match_clause(device_ids, device_name, start_date, end_date)
191-
project_projection = {
192-
"_id": False,
193-
"timestamp": "$timestamp",
194-
"vpd": "$vpd",
195-
}
196-
# We ensure both temperature and humidity exist for the calculation of VPD
197191
match_clause["temp"] = {"$exists": True}
198192
match_clause["rh"] = {"$exists": True}
199193

@@ -202,6 +196,26 @@ def get_vpd_pipeline(
202196
{"$match": match_clause},
203197
{"$addFields": {"tempAsFloat": {"$toDouble": "$temp"}}},
204198
{"$addFields": {"rhAsFloat": {"$toDouble": "$rh"}}},
199+
{
200+
"$addFields": {
201+
"group": {
202+
"$floor": {
203+
"$divide": [
204+
{"$subtract": ["$timestamp", start_date]},
205+
sampling_interval.total_seconds() * 1000,
206+
]
207+
}
208+
}
209+
}
210+
},
211+
{
212+
"$group": {
213+
"_id": "$group",
214+
"averageTemp": {"$avg": "$tempAsFloat"},
215+
"averageRH": {"$avg": "$rhAsFloat"},
216+
"timestamp": {"$first": "$timestamp"},
217+
}
218+
},
205219
{
206220
"$addFields": {
207221
"satvp": {
@@ -210,8 +224,8 @@ def get_vpd_pipeline(
210224
{
211225
"$exp": {
212226
"$multiply": [
213-
{"$divide": [17.27, {"$add": ["$tempAsFloat", 237.3]}]},
214-
"$tempAsFloat",
227+
{"$divide": [17.27, {"$add": ["$averageTemp", 237.3]}]},
228+
"$averageTemp",
215229
]
216230
}
217231
},
@@ -224,29 +238,20 @@ def get_vpd_pipeline(
224238
"vpd": {
225239
"$multiply": [
226240
"$satvp",
227-
{"$subtract": [1.0, {"$divide": ["$rhAsFloat", 100.0]}]},
241+
{"$subtract": [1.0, {"$divide": ["$averageRH", 100.0]}]},
228242
]
229243
}
230244
}
231245
},
232246
{
233-
"$addFields": {
234-
"group": {
235-
"$floor": {
236-
"$divide": [
237-
{"$subtract": ["$timestamp", start_date]},
238-
sampling_interval.total_seconds() * 1000,
239-
]
240-
}
241-
}
247+
"$project": {
248+
"_id": False,
249+
"timestamp": "$timestamp",
250+
"vpd": "$vpd",
242251
}
243252
},
244253
{"$sort": {"timestamp": 1}},
245-
{"$group": {"_id": "$group", "doc": {"$first": "$$ROOT"}}},
246-
{"$replaceRoot": {"newRoot": "$doc"}},
247-
{"$project": project_projection},
248254
]
249-
250255
return pipeline
251256

252257

0 commit comments

Comments
 (0)