1010from notebooker .constants import JobStatus , NotebookResultComplete , NotebookResultError , NotebookResultPending
1111
1212logger = 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 )
1316
1417
1518class MongoResultSerializer :
@@ -238,9 +241,7 @@ def get_all_results(
238241 base_filter .update (mongo_filter )
239242 if since :
240243 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
244245 results = self .library .find (base_filter , projection ).sort ("update_time" , - 1 ).limit (limit )
245246 for res in results :
246247 if res :
@@ -253,8 +254,19 @@ def get_all_result_keys(self, limit: int = 0, mongo_filter: Optional[Dict] = Non
253254 base_filter = {"status" : {"$ne" : JobStatus .DELETED .value }}
254255 if mongo_filter :
255256 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 :
258270 keys .append ((result ["report_name" ], result ["job_id" ]))
259271 return keys
260272
@@ -313,6 +325,7 @@ def get_latest_successful_job_ids_for_name_all_params(self, report_name: str) ->
313325 results = self .library .aggregate (
314326 [
315327 {"$match" : mongo_filter },
328+ {"$project" : REMOVE_PAYLOAD_FIELDS_PROJECTION },
316329 {"$sort" : {"update_time" : - 1 }},
317330 {"$group" : {"_id" : "$overrides" , "job_id" : {"$first" : "$job_id" }}},
318331 ]
0 commit comments