Skip to content

Commit ecb3894

Browse files
committed
Remove duplicate mongo update call
1 parent 8fb472a commit ecb3894

File tree

1 file changed

+38
-50
lines changed

1 file changed

+38
-50
lines changed

backend/btrixcloud/crawlconfigs.py

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -570,59 +570,47 @@ async def stats_recompute_last(self, cid: UUID, size: int, inc_crawls: int = 1):
570570

571571
running_crawl = await self.get_running_crawl(cid)
572572

573-
# If crawl is running, lastCrawl* stats are already for running crawl
574-
# so only increment size and crawl counts
575-
if running_crawl:
576-
result = await self.crawl_configs.find_one_and_update(
577-
{"_id": cid, "inactive": {"$ne": True}},
578-
{
579-
"$inc": {
580-
"totalSize": size,
581-
"crawlCount": inc_crawls,
582-
"crawlSuccessfulCount": inc_crawls,
583-
},
584-
},
573+
# If crawl is running, lastCrawl* stats are already for running crawl,
574+
# so there's nothing to update other than size and crawl count
575+
if not running_crawl:
576+
match_query = {
577+
"cid": cid,
578+
"finished": {"$ne": None},
579+
"inactive": {"$ne": True},
580+
}
581+
last_crawl = await self.crawls.find_one(
582+
match_query, sort=[("finished", pymongo.DESCENDING)]
585583
)
586-
return result is not None
587-
588-
match_query = {
589-
"cid": cid,
590-
"finished": {"$ne": None},
591-
"inactive": {"$ne": True},
592-
}
593-
last_crawl = await self.crawls.find_one(
594-
match_query, sort=[("finished", pymongo.DESCENDING)]
595-
)
596584

597-
# Update to reflect last crawl
598-
if last_crawl:
599-
last_crawl_finished = last_crawl.get("finished")
600-
601-
update_query["lastCrawlId"] = str(last_crawl.get("_id"))
602-
update_query["lastCrawlStartTime"] = last_crawl.get("started")
603-
update_query["lastStartedBy"] = last_crawl.get("userid")
604-
update_query["lastStartedByName"] = last_crawl.get("userName")
605-
update_query["lastCrawlTime"] = last_crawl_finished
606-
update_query["lastCrawlState"] = last_crawl.get("state")
607-
update_query["lastCrawlSize"] = sum(
608-
file_.get("size", 0) for file_ in last_crawl.get("files", [])
609-
)
610-
update_query["lastCrawlStopping"] = False
611-
update_query["isCrawlRunning"] = False
585+
# Update to reflect last crawl
586+
if last_crawl:
587+
last_crawl_finished = last_crawl.get("finished")
588+
589+
update_query["lastCrawlId"] = str(last_crawl.get("_id"))
590+
update_query["lastCrawlStartTime"] = last_crawl.get("started")
591+
update_query["lastStartedBy"] = last_crawl.get("userid")
592+
update_query["lastStartedByName"] = last_crawl.get("userName")
593+
update_query["lastCrawlTime"] = last_crawl_finished
594+
update_query["lastCrawlState"] = last_crawl.get("state")
595+
update_query["lastCrawlSize"] = sum(
596+
file_.get("size", 0) for file_ in last_crawl.get("files", [])
597+
)
598+
update_query["lastCrawlStopping"] = False
599+
update_query["isCrawlRunning"] = False
612600

613-
if last_crawl_finished:
614-
update_query["lastRun"] = last_crawl_finished
615-
# If no last crawl exists and no running crawl, reset stats
616-
else:
617-
update_query["lastCrawlId"] = None
618-
update_query["lastCrawlStartTime"] = None
619-
update_query["lastStartedBy"] = None
620-
update_query["lastStartedByName"] = None
621-
update_query["lastCrawlTime"] = None
622-
update_query["lastCrawlState"] = None
623-
update_query["lastCrawlSize"] = 0
624-
update_query["lastRun"] = None
625-
update_query["isCrawlRunning"] = False
601+
if last_crawl_finished:
602+
update_query["lastRun"] = last_crawl_finished
603+
# If no last crawl exists and no running crawl, reset stats
604+
else:
605+
update_query["lastCrawlId"] = None
606+
update_query["lastCrawlStartTime"] = None
607+
update_query["lastStartedBy"] = None
608+
update_query["lastStartedByName"] = None
609+
update_query["lastCrawlTime"] = None
610+
update_query["lastCrawlState"] = None
611+
update_query["lastCrawlSize"] = 0
612+
update_query["lastRun"] = None
613+
update_query["isCrawlRunning"] = False
626614

627615
result = await self.crawl_configs.find_one_and_update(
628616
{"_id": cid, "inactive": {"$ne": True}},

0 commit comments

Comments
 (0)