[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) <tde@openerp.com>
This commit is contained in:
Thibault Delavallée
2020-10-06 17:38:01 +00:00
parent 9a22e636f9
commit c5a7b1f8a6
2 changed files with 16 additions and 8 deletions
+13 -5
View File
@@ -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
@@ -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,