remove logging, add trytond.conf-settings

This commit is contained in:
Frederik Jaeckel 2023-03-05 10:26:39 +01:00
parent da7fc9cb65
commit e96aa6fad7
3 changed files with 29 additions and 50 deletions

69
book.py
View file

@ -9,18 +9,29 @@ from trytond.pyson import Eval, Or, Len, Bool, If
from trytond.modules.cashbook.book import STATES2, DEPENDS2 from trytond.modules.cashbook.book import STATES2, DEPENDS2
from trytond.transaction import Transaction from trytond.transaction import Transaction
from trytond.report import Report from trytond.report import Report
from trytond.config import config
from decimal import Decimal from decimal import Decimal
from datetime import timedelta, datetime from datetime import timedelta
from sql import Literal from sql import Literal
from sql.functions import CurrentDate from sql.functions import CurrentDate
from sql.aggregate import Sum from sql.aggregate import Sum
from sql.conditionals import Case, Coalesce from sql.conditionals import Case, Coalesce
from trytond.modules.cashbook.model import sub_ids_hierarchical,\ from trytond.modules.cashbook.model import sub_ids_hierarchical,\
CACHEKEY_CURRENCY, CACHEKEY_CASHBOOK, AnyInArray CACHEKEY_CURRENCY, AnyInArray
from .asset import CACHEKEY_ASSETRATE from .asset import CACHEKEY_ASSETRATE
import logging
logger = logging.getLogger(__name__) # enable/disable caching of cachekey for 'currency.rate'
if config.get('cashbook', 'cache_currency', default='yes').lower() in ['yes', '1', 'true']:
ENA_CURRKEY = True
else :
ENA_CURRKEY = False
# enable/disable caching of cachekey for 'currency.rate'
if config.get('cashbook', 'cache_asset', default='yes').lower() in ['yes', '1', 'true']:
ENA_ASSETKEY = True
else :
ENA_ASSETKEY = False
class Book(SymbolMixin, metaclass=PoolMeta): class Book(SymbolMixin, metaclass=PoolMeta):
@ -276,12 +287,6 @@ class Book(SymbolMixin, metaclass=PoolMeta):
'yield_sales', 'yield_fee_12m', 'yield_dividend_12m', 'yield_sales', 'yield_fee_12m', 'yield_dividend_12m',
'yield_sales_12m']} 'yield_sales_12m']}
logger.warning('## get_yield_data-GO %(time)s %(ids)s %(name)s' % {
'time': datetime.now().isoformat(),
'ids': str([x.id for x in cashbooks]),
'name': names,
})
def quantize_val(value, digits): def quantize_val(value, digits):
""" quantize... """ quantize...
""" """
@ -297,30 +302,23 @@ class Book(SymbolMixin, metaclass=PoolMeta):
query = [{ query = [{
'model': 'cashbook.line', 'model': 'cashbook.line',
'query': [('cashbook.parent', 'child_of', [x.id])], 'query': [('cashbook.parent', 'child_of', [x.id])],
#'cachekey': CACHEKEY_CASHBOOK % x.id,
}, { }, {
'model': 'currency.currency.rate', 'model': 'currency.currency.rate',
'query': [('currency.id', '=', x.currency.id)], 'query': [('currency.id', '=', x.currency.id)],
'cachekey': CACHEKEY_CURRENCY % x.currency.id, 'cachekey' if ENA_CURRKEY else 'disabled': CACHEKEY_CURRENCY % x.currency.id,
}, { }, {
'model': 'investment.rate', 'model': 'investment.rate',
'query': [('asset.id', '=', x.asset.id)], 'query': [('asset.id', '=', x.asset.id)],
#'cachekey': CACHEKEY_ASSETRATE % x.asset.id, 'cachekey' if ENA_ASSETKEY else 'disabled': CACHEKEY_ASSETRATE % x.asset.id,
} if x.asset is not None else {}], } if x.asset is not None else {}],
addkeys = [query_date.isoformat()]) addkeys = [query_date.isoformat()])
for x in cashbooks for x in cashbooks
} }
logger.warning('## get_yield_data-KEYS %(time)s' % {
'time': datetime.now().isoformat(),
})
# read from cache # read from cache
(todo_cashbook, result) = MemCache.read_from_cache( (todo_cashbook, result) = MemCache.read_from_cache(
cashbooks, cache_keys, names, result) cashbooks, cache_keys, names, result)
if len(todo_cashbook) == 0: if len(todo_cashbook) == 0:
logger.warning('## get_yield_data-HIT %(time)s' % {
'time': datetime.now().isoformat(),
})
return result return result
# results for 'total' # results for 'total'
@ -353,9 +351,6 @@ class Book(SymbolMixin, metaclass=PoolMeta):
# store to cache # store to cache
MemCache.store_result(cashbooks, cache_keys, result, todo_cashbook) MemCache.store_result(cashbooks, cache_keys, result, todo_cashbook)
logger.warning('## get_yield_data-END %(time)s' % {
'time': datetime.now().isoformat(),
})
return {x:result[x] for x in names} return {x:result[x] for x in names}
@classmethod @classmethod
@ -446,12 +441,6 @@ class Book(SymbolMixin, metaclass=PoolMeta):
'digits'] 'digits']
} }
logger.warning('## get_asset_quantity-GO %(time)s %(ids)s %(name)s' % {
'time': datetime.now().isoformat(),
'ids': str([x.id for x in cashbooks]),
'name': names,
})
cache_keys = { cache_keys = {
x.id: MemCache.get_key_by_record( x.id: MemCache.get_key_by_record(
name = 'get_asset_quantity', name = 'get_asset_quantity',
@ -459,15 +448,14 @@ class Book(SymbolMixin, metaclass=PoolMeta):
query = [{ query = [{
'model': 'cashbook.line', 'model': 'cashbook.line',
'query': [('cashbook.parent', 'child_of', [x.id])], 'query': [('cashbook.parent', 'child_of', [x.id])],
#'cachekey': CACHEKEY_CASHBOOK % x.id,
}, { }, {
'model': 'currency.currency.rate', 'model': 'currency.currency.rate',
'query': [('currency.id', '=', x.currency.id)], 'query': [('currency.id', '=', x.currency.id)],
'cachekey': CACHEKEY_CURRENCY % x.currency.id, 'cachekey' if ENA_CURRKEY else 'disabled': CACHEKEY_CURRENCY % x.currency.id,
}, { }, {
'model': 'investment.rate', 'model': 'investment.rate',
'query': [('asset.id', '=', x.asset.id)], 'query': [('asset.id', '=', x.asset.id)],
#'cachekey': CACHEKEY_ASSETRATE % x.asset.id, 'cachekey' if ENA_ASSETKEY else 'disabled': CACHEKEY_ASSETRATE % x.asset.id,
} if x.asset is not None else {}], } if x.asset is not None else {}],
addkeys=[ addkeys=[
str(company_currency), str(company_currency),
@ -477,15 +465,9 @@ class Book(SymbolMixin, metaclass=PoolMeta):
} }
# read from cache # read from cache
logger.warning('## get_asset_quantity-KEYS %(time)s' % {
'time': datetime.now().isoformat(),
})
(todo_cashbook, result) = MemCache.read_from_cache( (todo_cashbook, result) = MemCache.read_from_cache(
cashbooks, cache_keys, names, result) cashbooks, cache_keys, names, result)
if len(todo_cashbook) == 0: if len(todo_cashbook) == 0:
logger.warning('## get_asset_quantity-HIT %(time)s' % {
'time': datetime.now().isoformat(),
})
return result return result
def values_from_record(rdata): def values_from_record(rdata):
@ -546,10 +528,6 @@ class Book(SymbolMixin, metaclass=PoolMeta):
for name in values.keys(): for name in values.keys():
result[name][book_id] = values[name] result[name][book_id] = values[name]
logger.warning('## get_asset_quantity-PART1 %(time)s' % {
'time': datetime.now().isoformat(),
})
# add aggregated values of cashbooks without type # add aggregated values of cashbooks without type
aggr_names = ['current_value', 'current_value_ref', aggr_names = ['current_value', 'current_value_ref',
'diff_amount', 'diff_percent'] 'diff_amount', 'diff_percent']
@ -612,7 +590,7 @@ class Book(SymbolMixin, metaclass=PoolMeta):
result['digits'][book_id] = record[5] result['digits'][book_id] = record[5]
result[name][book_id] += value result[name][book_id] += value
# diff_prcent # diff_percent
for id_book in ids_nonebtypes: for id_book in ids_nonebtypes:
c_val = result['current_value_ref'][id_book] c_val = result['current_value_ref'][id_book]
p_amount = result['purchase_amount_ref'][id_book] p_amount = result['purchase_amount_ref'][id_book]
@ -626,15 +604,8 @@ class Book(SymbolMixin, metaclass=PoolMeta):
).quantize(Decimal(str(1/10 ** digits))) ).quantize(Decimal(str(1/10 ** digits)))
result['digits'][id_book] = None result['digits'][id_book] = None
logger.warning('## get_asset_quantity-PART2 %(time)s' % {
'time': datetime.now().isoformat(),
})
# store to cache # store to cache
MemCache.store_result(cashbooks, cache_keys, result, todo_cashbook) MemCache.store_result(cashbooks, cache_keys, result, todo_cashbook)
logger.warning('## get_asset_quantity-END %(time)s' % {
'time': datetime.now().isoformat(),
})
return {x:result[x] for x in names} return {x:result[x] for x in names}
@fields.depends('id') @fields.depends('id')

8
docs/settings.txt Normal file
View file

@ -0,0 +1,8 @@
settings in tytond.conf
[cashbook]
# Enables caching of the cache key for asset rates.
# Reduces the time required to determine the cache
# key for asset values.
# default: yes
cache_asset = yes

View file

@ -99,7 +99,7 @@ setup(name='%s_%s' % (PREFIX, MODULE),
package_data={ package_data={
'trytond.modules.%s' % MODULE: (info.get('xml', []) 'trytond.modules.%s' % MODULE: (info.get('xml', [])
+ ['tryton.cfg', 'locale/*.po', 'tests/*.py', + ['tryton.cfg', 'locale/*.po', 'tests/*.py',
'view/*.xml', 'view/*.xml', 'docs/*.txt',
'versiondep.txt', 'README.rst']), 'versiondep.txt', 'README.rst']),
}, },