10
10
from notebooker .constants import JobStatus , NotebookResultComplete , NotebookResultError , NotebookResultPending
11
11
12
12
logger = getLogger (__name__ )
13
+ REMOVE_ID_PROJECTION = {"_id" : 0 }
14
+ REMOVE_PAYLOAD_FIELDS_PROJECTION = {"raw_html_resources" : 0 , "raw_html" : 0 , "raw_ipynb_json" : 0 }
15
+ REMOVE_PAYLOAD_FIELDS_AND_ID_PROJECTION = dict (REMOVE_PAYLOAD_FIELDS_PROJECTION , ** REMOVE_ID_PROJECTION )
13
16
14
17
15
18
class MongoResultSerializer :
@@ -238,9 +241,7 @@ def get_all_results(
238
241
base_filter .update (mongo_filter )
239
242
if since :
240
243
base_filter .update ({"update_time" : {"$gt" : since }})
241
- projection = (
242
- {"_id" : 0 } if load_payload else {"raw_html_resources" : 0 , "raw_html" : 0 , "raw_ipynb_json" : 0 , "_id" : 0 }
243
- )
244
+ projection = REMOVE_ID_PROJECTION if load_payload else REMOVE_PAYLOAD_FIELDS_AND_ID_PROJECTION
244
245
results = self .library .find (base_filter , projection ).sort ("update_time" , - 1 ).limit (limit )
245
246
for res in results :
246
247
if res :
@@ -253,8 +254,19 @@ def get_all_result_keys(self, limit: int = 0, mongo_filter: Optional[Dict] = Non
253
254
base_filter = {"status" : {"$ne" : JobStatus .DELETED .value }}
254
255
if mongo_filter :
255
256
base_filter .update (mongo_filter )
256
- projection = {"report_name" : 1 , "job_id" : 1 , "_id" : 0 }
257
- for result in self .library .find (base_filter , projection ).sort ("update_time" , - 1 ).limit (limit ):
257
+ results = self .library .aggregate (
258
+ [
259
+ stage
260
+ for stage in (
261
+ {"$match" : base_filter },
262
+ {"$sort" : {"update_time" : - 1 }},
263
+ {"$limit" : limit } if limit else {},
264
+ {"$project" : {"report_name" : 1 , "job_id" : 1 }},
265
+ )
266
+ if stage
267
+ ]
268
+ )
269
+ for result in results :
258
270
keys .append ((result ["report_name" ], result ["job_id" ]))
259
271
return keys
260
272
@@ -313,6 +325,7 @@ def get_latest_successful_job_ids_for_name_all_params(self, report_name: str) ->
313
325
results = self .library .aggregate (
314
326
[
315
327
{"$match" : mongo_filter },
328
+ {"$project" : REMOVE_PAYLOAD_FIELDS_PROJECTION },
316
329
{"$sort" : {"update_time" : - 1 }},
317
330
{"$group" : {"_id" : "$overrides" , "job_id" : {"$first" : "$job_id" }}},
318
331
]
0 commit comments