[FIX]performance issue: _compute_sale_order_count

This commit simplifies the code and improves the performance
of _compute_sale_order_count.

execution time from O(n^2) to pretty close to O(n)

Project : Performance Issues
Task : Geostaff : Success Pack 5 (100h)  (opw-1912303)
This commit is contained in:
Moens Alexandre
2019-01-29 13:08:43 +00:00
parent cc4d57ff14
commit 26e5d0f219
+2 -5
View File
@@ -16,12 +16,9 @@ class ResPartner(models.Model):
def _compute_sale_order_count(self):
sale_data = self.env['sale.order'].read_group(domain=[('partner_id', 'child_of', self.ids)],
fields=['partner_id'], groupby=['partner_id'])
# read to keep the child/parent relation while aggregating the read_group result in the loop
partner_child_ids = self.read(['child_ids'])
mapped_data = dict([(m['partner_id'][0], m['partner_id_count']) for m in sale_data])
for partner in self:
# let's obtain the partner id and all its child ids from the read up there
partner_ids = filter(lambda r: r['id'] == partner.id, partner_child_ids)[0]
partner_ids = [partner_ids.get('id')] + partner_ids.get('child_ids')
# let's obtain the partner id and all its child ids
partner_ids = [partner.id] + partner.child_ids.ids
# then we can sum for all the partner's child
partner.sale_order_count = sum(mapped_data.get(child, 0) for child in partner_ids)