Browse Source

[IMP] mis_builder performance: fetch only balances that we need

plus cleanup autopep8 abuse
pull/90/head
Stéphane Bidoul 10 years ago
parent
commit
9970ffed89
  1. 55
      mis_builder/models/mis_builder.py

55
mis_builder/models/mis_builder.py

@ -69,14 +69,22 @@ def _python_bal_var(account_code):
def _get_bal_vars_in_expr(expr): def _get_bal_vars_in_expr(expr):
return re.findall(expr, r'\bbal_\w+')
return re.findall(r'\bbal_\w+', expr)
def _get_vars_in_expr(expr, varnames=None): def _get_vars_in_expr(expr, varnames=None):
if not varnames: if not varnames:
return [] return []
varnames_re = r'\b' + r'\b|\b'.join(varnames) + r'\b' varnames_re = r'\b' + r'\b|\b'.join(varnames) + r'\b'
return re.findall(expr, varnames_re)
return re.findall(varnames_re, expr)
def _get_bal_vars_in_report(report):
res = set()
for kpi in report.kpi_ids:
for bal_var in _get_bal_vars_in_expr(kpi.expression):
res.add(bal_var)
return res
class mis_report_kpi(orm.Model): class mis_report_kpi(orm.Model):
@ -274,8 +282,8 @@ class mis_report_query(orm.Model):
ids, ['field_ids'], 20), }), ids, ['field_ids'], 20), }),
'date_field': fields.many2one('ir.model.fields', required=True, 'date_field': fields.many2one('ir.model.fields', required=True,
string='Date field', string='Date field',
domain=[('ttype', 'in', ('date',
'datetime'))]),
domain=[('ttype', 'in',
('date', 'datetime'))]),
'domain': fields.char(string='Domain'), 'domain': fields.char(string='Domain'),
'report_id': fields.many2one('mis.report', string='Report'), 'report_id': fields.many2one('mis.report', string='Report'),
} }
@ -385,22 +393,16 @@ class mis_report_instance_period(orm.Model):
all_period_ids = period_obj.search( all_period_ids = period_obj.search(
cr, uid, cr, uid,
[('special', '=', False), [('special', '=', False),
'|', (
'company_id', '=',
False),
('company_id', '=',
c.company_id.id)],
'|', ('company_id', '=', False),
('company_id', '=', c.company_id.id)],
order='date_start', order='date_start',
context=context) context=context)
current_period_ids = period_obj.search( current_period_ids = period_obj.search(
cr, uid, cr, uid,
[('special', '=', False), [('special', '=', False),
('date_start', '<=', d), ('date_start', '<=', d),
('date_stop',
'>=', d),
'|',
('company_id',
'=', False),
('date_stop', '>=', d),
'|', ('company_id', '=', False),
('company_id', '=', c.company_id.id)], ('company_id', '=', c.company_id.id)],
context=context) context=context)
if not current_period_ids: if not current_period_ids:
@ -497,7 +499,7 @@ class mis_report_instance_period(orm.Model):
'Period name should be unique by report'), 'Period name should be unique by report'),
] ]
def _fetch_balances(self, cr, uid, c, context=None):
def _fetch_balances(self, cr, uid, c, bal_vars, context=None):
""" fetch the general account balances for the given period """ fetch the general account balances for the given period
returns a dictionary {bal_<account.code>: account.balance} returns a dictionary {bal_<account.code>: account.balance}
@ -520,7 +522,13 @@ class mis_report_instance_period(orm.Model):
cr, uid, cr, uid,
['|', ('company_id', '=', False), ['|', ('company_id', '=', False),
('company_id', '=', c.company_id.id)], ('company_id', '=', c.company_id.id)],
context=context)
context=search_ctx)
# first fetch all codes and filter the one we need
account_datas = account_obj.read(
cr, uid, account_ids, ['code', ], context=search_ctx)
account_ids = [a['id'] for a in account_datas
if _python_bal_var(a['code']) in bal_vars]
# fetch balances
account_datas = account_obj.read( account_datas = account_obj.read(
cr, uid, account_ids, ['code', 'balance'], context=search_ctx) cr, uid, account_ids, ['code', 'balance'], context=search_ctx)
balances = {} balances = {}
@ -559,14 +567,7 @@ class mis_report_instance_period(orm.Model):
return res return res
def _get_bal_vars_in_report(self, report):
res = set()
for kpi in report.kpi_ids:
for bal_var in _get_bal_vars_in_expr(kpi.expression):
res.add(bal_var)
return res
def _compute(self, cr, uid, c, context=None):
def _compute(self, cr, uid, c, bal_vars, context=None):
if context is None: if context is None:
context = {} context = {}
@ -582,7 +583,7 @@ class mis_report_instance_period(orm.Model):
'len': len, 'len': len,
'avg': lambda l: sum(l) / float(len(l)), 'avg': lambda l: sum(l) / float(len(l)),
} }
localdict.update(self._fetch_balances(cr, uid, c, context=context))
localdict.update(self._fetch_balances(cr, uid, c, bal_vars, context=context))
localdict.update(self._fetch_queries(cr, uid, c, context=context)) localdict.update(self._fetch_queries(cr, uid, c, context=context))
for kpi in c.report_instance_id.report_id.kpi_ids: for kpi in c.report_instance_id.report_id.kpi_ids:
@ -727,6 +728,8 @@ class mis_report_instance(orm.Model):
period_values = {} period_values = {}
bal_vars = _get_bal_vars_in_report(r.report_id)
for period in r.period_ids: for period in r.period_ids:
# add the column header # add the column header
header['']['cols'].append(dict( header['']['cols'].append(dict(
@ -743,7 +746,7 @@ class mis_report_instance(orm.Model):
period.date_from)) period.date_from))
# compute kpi values # compute kpi values
values = report_instance_period_obj._compute( values = report_instance_period_obj._compute(
cr, uid, period, context=context)
cr, uid, period, bal_vars, context=context)
period_values[period.name] = values period_values[period.name] = values
for key in values: for key in values:
content[key]['cols'].append(values[key]) content[key]['cols'].append(values[key])

Loading…
Cancel
Save