[FIX] account_financial_report: optimize computation of analytic accounts

Installing the module in a big DB was very slow.

I applied several optimizations to the method for working better with big datasets:
- Prefetch all analytic account distribution.
- Batch writes as much as possible.

@moduon MT-4982
This commit is contained in:
Jairo Llopis
2024-02-01 09:45:00 +00:00
committed by chaule97
parent e6904a2449
commit 2671e21526

View File

@@ -1,5 +1,7 @@
# Copyright 2019 ACSONE SA/NV (<http://acsone.eu>) # Copyright 2019 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).- # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).-
from collections import defaultdict
from odoo import api, fields, models from odoo import api, fields, models
@@ -12,24 +14,26 @@ class AccountMoveLine(models.Model):
@api.depends("analytic_distribution") @api.depends("analytic_distribution")
def _compute_analytic_account_ids(self): def _compute_analytic_account_ids(self):
for record in self: # Prefetch all involved analytic accounts
if not record.analytic_distribution: with_distribution = self.filtered("analytic_distribution")
record.analytic_account_ids = False batch_by_analytic_account = defaultdict(list)
else: for record in with_distribution:
record.update( for account_id in map(int, record.analytic_distribution):
{ batch_by_analytic_account[account_id].append(record.id)
"analytic_account_ids": [ existing_account_ids = set(
( self.env["account.analytic.account"]
6, .browse(map(int, batch_by_analytic_account))
0, .exists()
self.env["account.analytic.account"] .ids
.browse([int(k) for k in record.analytic_distribution]) )
.exists() # Store them
.ids, self.analytic_account_ids = False
) for account_id, record_ids in batch_by_analytic_account.items():
] if account_id not in existing_account_ids:
} continue
) self.browse(record_ids).analytic_account_ids = [
fields.Command.link(account_id)
]
def init(self): def init(self):
""" """