From c5a7b1f8a683a028e775779f3ad61e21fb377196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Mon, 22 Jun 2020 14:26:31 +0000 Subject: [PATCH] [IMP] crm: improve handle_salesmen_assignment performances Handle salesmen allows to assign responsible to leads using a round robin like algorithm. Currently a write on each lead is done to update its team and responsible, even if number of leads is far greater than number of available users to assign. Current implementation can be simplified by doing one write per sales person to assign on its batch of leads, or a single write if no salesmen has to be assigned. Performance counter on a mass convert wizard on 50 leads show that we gain about 9% of queries (125 on 1471) with this simple optimization. Task ID-2351604 PR #59084 closes odoo/odoo#59324 X-original-commit: 973a2998666813ca4fbe90f1b486c1b18091c4e7 Signed-off-by: Thibault Delavallee (tde) --- addons/crm/models/crm_lead.py | 18 +++++++++++++----- addons/crm/tests/test_crm_lead_convert_mass.py | 6 +++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 9d4244b58b7..8a8b618edf6 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -1246,11 +1246,19 @@ class Lead(models.Model): :param int team_id: salesteam to assign """ update_vals = {'team_id': team_id} if team_id else {} - for index, lead in enumerate(self): - if user_ids: - update_vals['user_id'] = user_ids[index % len(user_ids)] - if update_vals: - lead.write(update_vals) + if not user_ids: + self.write(update_vals) + else: + lead_ids = self.ids + steps = len(user_ids) + # pass 1 : lead_ids[0:6:3] = [L1,L4] + # pass 2 : lead_ids[1:6:3] = [L2,L5] + # pass 3 : lead_ids[2:6:3] = [L3,L6] + # ... + for idx in range(0, steps): + subset_ids = lead_ids[idx:len(lead_ids):steps] + update_vals['user_id'] = user_ids[idx] + self.env['crm.lead'].browse(subset_ids).write(update_vals) # ------------------------------------------------------------ # TOOLS diff --git a/addons/crm/tests/test_crm_lead_convert_mass.py b/addons/crm/tests/test_crm_lead_convert_mass.py index 05c347301a6..d24de28b39e 100644 --- a/addons/crm/tests/test_crm_lead_convert_mass.py +++ b/addons/crm/tests/test_crm_lead_convert_mass.py @@ -24,7 +24,7 @@ class TestLeadConvertMass(crm_common.TestLeadConvertMassCommon): with self.assertQueryCount(user_sales_manager=0): test_leads = self.env['crm.lead'].browse(test_leads.ids) - with self.assertQueryCount(user_sales_manager=330): + with self.assertQueryCount(user_sales_manager=252): test_leads.handle_salesmen_assignment(user_ids=user_ids, team_id=False) self.assertEqual(test_leads.team_id, self.sales_team_convert | self.sales_team_1) @@ -42,7 +42,7 @@ class TestLeadConvertMass(crm_common.TestLeadConvertMassCommon): with self.assertQueryCount(user_sales_manager=0): test_leads = self.env['crm.lead'].browse(test_leads.ids) - with self.assertQueryCount(user_sales_manager=342): + with self.assertQueryCount(user_sales_manager=217): test_leads.handle_salesmen_assignment(user_ids=user_ids, team_id=team_id) self.assertEqual(test_leads.team_id, self.sales_team_convert) @@ -166,7 +166,7 @@ class TestLeadConvertMass(crm_common.TestLeadConvertMassCommon): test_leads = self._create_leads_batch(count=50, user_ids=[False]) user_ids = self.assign_users.ids - with self.assertQueryCount(user_sales_manager=1472): + with self.assertQueryCount(user_sales_manager=1347): mass_convert = self.env['crm.lead2opportunity.partner.mass'].with_context({ 'active_model': 'crm.lead', 'active_ids': test_leads.ids,