Skip to content

Commit 1f2c396

Browse files
committed
[IMP] queue_job_batch: improve perf
1 parent 879d1a7 commit 1f2c396

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

queue_job_batch/models/queue_job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class QueueJob(models.Model):
1010
_inherit = "queue.job"
1111

12-
job_batch_id = fields.Many2one("queue.job.batch")
12+
job_batch_id = fields.Many2one("queue.job.batch", index=True)
1313

1414
@api.model_create_multi
1515
def create(self, vals_list):

queue_job_batch/models/queue_job_batch.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,29 @@ def get_new_batch(self, name, **kwargs):
101101

102102
@api.depends("job_ids.state")
103103
def _compute_job_count(self):
104+
grouped = self.env["queue.job"].read_group(
105+
[("job_batch_id", "in", self.ids)],
106+
["job_batch_id", "state"],
107+
["job_batch_id", "state"],
108+
lazy=False,
109+
)
110+
counts = {}
111+
for g in grouped:
112+
batch_id = g["job_batch_id"][0]
113+
counts.setdefault(batch_id, {})
114+
counts[batch_id][g["state"]] = g["__count"]
115+
104116
for rec in self:
105-
jobs_by_state = rec.job_ids.grouped("state")
106-
rec.job_count = len(rec.job_ids)
107-
rec.failed_job_count = len(jobs_by_state.get("failed", []))
108-
rec.finished_job_count = len(jobs_by_state.get("done", []))
109-
rec.completeness = rec.finished_job_count / max(1, rec.job_count)
110-
rec.failed_percentage = rec.failed_job_count / max(1, rec.job_count)
117+
by_state = counts.get(rec.id, {})
118+
total = sum(by_state.values())
119+
done = by_state.get("done", 0)
120+
failed = by_state.get("failed", 0)
121+
122+
rec.job_count = total
123+
rec.failed_job_count = failed
124+
rec.finished_job_count = done
125+
rec.completeness = done / max(1, total)
126+
rec.failed_percentage = failed / max(1, total)
111127

112128
@api.model
113129
def _to_store_fnames(self):

0 commit comments

Comments
 (0)