book/line: logging, model: cache-write skip existing values

This commit is contained in:
Frederik Jaeckel 2023-03-04 21:24:19 +01:00
parent 440e4c66d5
commit 0a5c0585a2
4 changed files with 81 additions and 35 deletions

40
book.py
View file

@ -11,12 +11,15 @@ from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.report import Report
from decimal import Decimal
from datetime import date
from datetime import date, datetime
from sql.aggregate import Sum
from sql.conditionals import Case
from .model import order_name_hierarchical, sub_ids_hierarchical, \
AnyInArray, CACHEKEY_CURRENCY, CACHEKEY_CASHBOOK
import logging
logger = logging.getLogger(__name__)
STATES = {
'readonly': Eval('state', '') != 'open',
@ -262,15 +265,15 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
}
return recname
def get_cachekeys_by_hierarchy(self):
""" generate keys for all cashbooks above us
"""
CBook = Pool().get('cashbook.book')
# ~ def get_cachekeys_by_hierarchy(self):
# ~ """ generate keys for all cashbooks above us
# ~ """
# ~ CBook = Pool().get('cashbook.book')
return [CACHEKEY_CASHBOOK % x.id
for x in CBook.search([
('parent', 'parent_of', [self.id]),
], order=[('id', 'ASC')])]
# ~ return [CACHEKEY_CASHBOOK % x.id
# ~ for x in CBook.search([
# ~ ('parent', 'parent_of', [self.id]),
# ~ ], order=[('id', 'ASC')])]
@classmethod
def get_balance_of_cashbook_sql(cls):
@ -362,6 +365,12 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
cursor = Transaction().connection.cursor()
context = Transaction().context
logger.warning('## get_balance_cashbook-GO %(time)s %(ids)s %(name)s' % {
'time': datetime.now().isoformat(),
'ids': str([x.id for x in cashbooks]),
'name': names,
})
result = {
x:{y.id: Decimal('0.0') for y in cashbooks}
for x in ['balance', 'balance_all', 'balance_ref']}
@ -381,7 +390,7 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
query = [{
'model': 'cashbook.line',
'query': [('cashbook.parent', 'child_of', [x.id])],
'cachekey': CACHEKEY_CASHBOOK % x.id,
#'cachekey': CACHEKEY_CASHBOOK % x.id,
}, {
'model': 'currency.currency.rate',
'query': [('currency.id', '=', x.currency.id)],
@ -392,9 +401,15 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
}
# read from cache
logger.warning('## get_balance_cashbook-KEYS %(time)s' % {
'time': datetime.now().isoformat(),
})
(todo_cashbook, result) = MemCache.read_from_cache(
cashbooks, cache_keys, names, result)
if len(todo_cashbook) == 0:
logger.warning('## get_balance_cashbook-HIT %(time)s' % {
'time': datetime.now().isoformat(),
})
return result
# query balances of cashbooks and sub-cashbooks
@ -430,7 +445,10 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView):
result['balance_ref'][record[0]] += Currency.compute(
record[2], record[5], record[3])
MemCache.store_result(cashbooks, cache_keys, result)
MemCache.store_result(cashbooks, cache_keys, result, todo_cashbook)
logger.warning('## get_balance_cashbook-END %(time)s' % {
'time': datetime.now().isoformat(),
})
return result
@fields.depends('btype')