From 50055d612656709858475bcac99cfc6ed2e4d9da Mon Sep 17 00:00:00 2001 From: Rafa Morant Date: Wed, 21 Jun 2023 14:00:28 +0200 Subject: [PATCH] [FIX] helpdesk_mgmt_project: improve performance counting tickets --- helpdesk_mgmt_project/README.rst | 4 ++++ helpdesk_mgmt_project/models/project.py | 19 +++++++++++---- helpdesk_mgmt_project/models/project_task.py | 24 ++++++++++++++----- helpdesk_mgmt_project/readme/CONTRIBUTORS.rst | 4 ++++ .../static/description/index.html | 6 ++++- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/helpdesk_mgmt_project/README.rst b/helpdesk_mgmt_project/README.rst index 1cd2ea04bf..bb54e0f138 100644 --- a/helpdesk_mgmt_project/README.rst +++ b/helpdesk_mgmt_project/README.rst @@ -74,6 +74,10 @@ Contributors * Manuel Regidor +* `ALBA Software `_: + + * Rafa Morant + Maintainers ~~~~~~~~~~~ diff --git a/helpdesk_mgmt_project/models/project.py b/helpdesk_mgmt_project/models/project.py index 13109eb76e..cf524e319b 100644 --- a/helpdesk_mgmt_project/models/project.py +++ b/helpdesk_mgmt_project/models/project.py @@ -20,8 +20,19 @@ class ProjectProject(models.Model): @api.depends("ticket_ids", "ticket_ids.stage_id") def _compute_ticket_count(self): + HelpdeskTicket = self.env["helpdesk.ticket"] + domain = [("project_id", "in", self.ids)] + fields = ["project_id"] + groupby = ["project_id"] + counts = { + pr["project_id"][0]: pr["project_id_count"] + for pr in HelpdeskTicket.read_group(domain, fields, groupby) + } + domain.append(("closed", "=", False)) + counts_todo = { + pr["project_id"][0]: pr["project_id_count"] + for pr in HelpdeskTicket.read_group(domain, fields, groupby) + } for record in self: - record.ticket_count = len(record.ticket_ids) - record.todo_ticket_count = len( - record.ticket_ids.filtered(lambda ticket: not ticket.closed) - ) + record.ticket_count = counts.get(record.id, 0) + record.todo_ticket_count = counts_todo.get(record.id, 0) diff --git a/helpdesk_mgmt_project/models/project_task.py b/helpdesk_mgmt_project/models/project_task.py index 919747b735..5e8ffd0af7 100644 --- a/helpdesk_mgmt_project/models/project_task.py +++ b/helpdesk_mgmt_project/models/project_task.py @@ -20,18 +20,30 @@ class ProjectTask(models.Model): @api.depends("ticket_ids", "ticket_ids.stage_id") def _compute_ticket_count(self): + HelpdeskTicket = self.env["helpdesk.ticket"] + invname = "task_id" + domain = [(invname, "in", self.ids)] + fields = [invname] + groupby = [invname] + counts = { + pr[invname][0]: pr[f"{invname}_count"] + for pr in HelpdeskTicket.read_group(domain, fields, groupby) + } + domain.append(("closed", "=", False)) + counts_todo = { + pr[invname][0]: pr[f"{invname}_count"] + for pr in HelpdeskTicket.read_group(domain, fields, groupby) + } for record in self: - record.ticket_count = len(record.ticket_ids) - record.todo_ticket_count = len( - record.ticket_ids.filtered(lambda ticket: not ticket.closed) - ) + record.ticket_count = counts.get(record.id, 0) + record.todo_ticket_count = counts_todo.get(record.id, 0) def action_view_ticket(self): result = self.env["ir.actions.act_window"]._for_xml_id( "helpdesk_mgmt.action_helpdesk_ticket_kanban_from_dashboard" ) # choose the view_mode accordingly - if not self.ticket_ids or len(self.ticket_ids) > 1: + if not self.ticket_ids or self.ticket_count > 1: result["domain"] = "[('id','in',%s)]" % (self.ticket_ids.ids) res = self.env.ref("helpdesk_mgmt.ticket_view_tree", False) tree_view = [(res and res.id or False, "tree")] @@ -41,7 +53,7 @@ def action_view_ticket(self): ] else: result["views"] = tree_view - elif len(self.ticket_ids) == 1: + elif self.ticket_count == 1: res = self.env.ref("helpdesk_mgmt.ticket_view_form", False) form_view = [(res and res.id or False, "form")] if "views" in result: diff --git a/helpdesk_mgmt_project/readme/CONTRIBUTORS.rst b/helpdesk_mgmt_project/readme/CONTRIBUTORS.rst index eaa2b34a5e..557940de99 100644 --- a/helpdesk_mgmt_project/readme/CONTRIBUTORS.rst +++ b/helpdesk_mgmt_project/readme/CONTRIBUTORS.rst @@ -17,3 +17,7 @@ * `Sygel `_: * Manuel Regidor + +* `ALBA Software `_: + + * Rafa Morant diff --git a/helpdesk_mgmt_project/static/description/index.html b/helpdesk_mgmt_project/static/description/index.html index a7b2e33e0f..b9311f296c 100644 --- a/helpdesk_mgmt_project/static/description/index.html +++ b/helpdesk_mgmt_project/static/description/index.html @@ -3,7 +3,7 @@ - + Helpdesk Project