speedup: indexes, caching
This commit is contained in:
parent
86e6c33cc1
commit
624a5bff55
11 changed files with 352 additions and 32 deletions
59
book.py
59
book.py
|
@ -13,9 +13,9 @@ from trytond.report import Report
|
|||
from decimal import Decimal
|
||||
from datetime import date
|
||||
from sql.aggregate import Sum
|
||||
from sql.conditionals import Case, Coalesce
|
||||
from sql.functions import CurrentDate
|
||||
from .model import order_name_hierarchical, sub_ids_hierarchical, AnyInArray
|
||||
from sql.conditionals import Case
|
||||
from .model import order_name_hierarchical, sub_ids_hierarchical, \
|
||||
AnyInArray, CACHEKEY_CURRENCY
|
||||
|
||||
|
||||
STATES = {
|
||||
|
@ -48,12 +48,12 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
|
|||
__name__ = 'cashbook.book'
|
||||
|
||||
company = fields.Many2One(string='Company', model_name='company.company',
|
||||
required=True, ondelete="RESTRICT")
|
||||
required=True, select=True, ondelete="RESTRICT")
|
||||
name = fields.Char(string='Name', required=True,
|
||||
states=STATES, depends=DEPENDS)
|
||||
description = fields.Text(string='Description',
|
||||
states=STATES, depends=DEPENDS)
|
||||
btype = fields.Many2One(string='Type',
|
||||
btype = fields.Many2One(string='Type', select=True,
|
||||
help='A cash book with type can contain postings. Without type is a view.',
|
||||
model_name='cashbook.type', ondelete='RESTRICT',
|
||||
states={
|
||||
|
@ -346,12 +346,15 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
|
|||
Currency = pool.get('currency.currency')
|
||||
Company = pool.get('company.company')
|
||||
IrDate = pool.get('ir.date')
|
||||
MemCache = pool.get('cashbook.memcache')
|
||||
tab_book = Book2.__table__()
|
||||
tab_comp = Company.__table__()
|
||||
cursor = Transaction().connection.cursor()
|
||||
context = Transaction().context
|
||||
|
||||
result = {x:{y.id: Decimal('0.0') for y in cashbooks} for x in names}
|
||||
result = {
|
||||
x:{y.id: Decimal('0.0') for y in cashbooks}
|
||||
for x in ['balance', 'balance_all', 'balance_ref']}
|
||||
|
||||
# deny invalid date in context
|
||||
query_date = context.get('date', IrDate.today())
|
||||
|
@ -361,6 +364,28 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
|
|||
except :
|
||||
query_date = IrDate.today()
|
||||
|
||||
cache_keys = {
|
||||
x.id: MemCache.get_key_by_record(
|
||||
name = 'get_balance_cashbook',
|
||||
record = x,
|
||||
query = [{
|
||||
'model': 'cashbook.line',
|
||||
'query': [('cashbook.parent', 'child_of', [x.id])],
|
||||
}, {
|
||||
'model': 'currency.currency.rate',
|
||||
'query': [('currency.id', '=', x.currency.id)],
|
||||
'cachekey': CACHEKEY_CURRENCY % x.currency.id,
|
||||
}, ],
|
||||
addkeys = [query_date.isoformat()])
|
||||
for x in cashbooks
|
||||
}
|
||||
|
||||
# read from cache
|
||||
(todo_cashbook, result) = MemCache.read_from_cache(
|
||||
cashbooks, cache_keys, names, result)
|
||||
if len(todo_cashbook) == 0:
|
||||
return result
|
||||
|
||||
# query balances of cashbooks and sub-cashbooks
|
||||
with Transaction().set_context({
|
||||
'date': query_date,
|
||||
|
@ -381,26 +406,20 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
|
|||
Sum(tab_line.balance).as_('balance'),
|
||||
Sum(tab_line.balance_all).as_('balance_all'),
|
||||
group_by=[tab_book.id, tab_line.currency, tab_comp.currency],
|
||||
where=tab_book.id.in_([x.id for x in cashbooks]),
|
||||
where=tab_book.id.in_([x.id for x in todo_cashbook]),
|
||||
)
|
||||
cursor.execute(*query)
|
||||
records = cursor.fetchall()
|
||||
|
||||
for record in records:
|
||||
values = {
|
||||
'balance': Currency.compute(
|
||||
record[2], record[4], record[1],
|
||||
),
|
||||
'balance_all': Currency.compute(
|
||||
record[2], record[5], record[1],
|
||||
),
|
||||
'balance_ref': Currency.compute(
|
||||
record[2], record[5], record[3],
|
||||
),
|
||||
}
|
||||
result['balance'][record[0]] += Currency.compute(
|
||||
record[2], record[4], record[1])
|
||||
result['balance_all'][record[0]] += Currency.compute(
|
||||
record[2], record[5], record[1])
|
||||
result['balance_ref'][record[0]] += Currency.compute(
|
||||
record[2], record[5], record[3])
|
||||
|
||||
for name in names:
|
||||
result[name][record[0]] += values[name]
|
||||
MemCache.store_result(cashbooks, cache_keys, result)
|
||||
return result
|
||||
|
||||
@fields.depends('btype')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue