caching für change_day1/month1/...
This commit is contained in:
parent
8cfa54f693
commit
8503fd3e32
3 changed files with 66 additions and 11 deletions
37
asset.py
37
asset.py
|
@ -8,6 +8,8 @@ from trytond.transaction import Transaction
|
|||
from trytond.pool import Pool
|
||||
from trytond.pyson import Eval, Bool, And, If, Date
|
||||
from trytond.report import Report
|
||||
from trytond.cache import Cache
|
||||
|
||||
from decimal import Decimal
|
||||
from datetime import time
|
||||
from sql.functions import CurrentDate, CurrentTimestamp, Round, Extract
|
||||
|
@ -118,6 +120,8 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
|||
readonly=True, model_name='investment.rate'),
|
||||
'get_rate_data')
|
||||
|
||||
_asset_cache = Cache('investment.asset.values', duration=6*3600, context=False)
|
||||
|
||||
@classmethod
|
||||
def __register__(cls, module_name):
|
||||
""" register and migrate
|
||||
|
@ -160,6 +164,17 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
|||
AssetSourceRel.create(to_create)
|
||||
asset_table.drop_column('updtsource')
|
||||
|
||||
@classmethod
|
||||
def values_cache_clear(cls, id_assets, names):
|
||||
""" set cache-values to None
|
||||
"""
|
||||
for fname in names:
|
||||
for id1 in id_assets:
|
||||
cls._asset_cache.set('%(fname)s-%(id)d' % {
|
||||
'fname': fname,
|
||||
'id': id1,
|
||||
}, None)
|
||||
|
||||
@classmethod
|
||||
def view_attributes(cls):
|
||||
return super().view_attributes() + [
|
||||
|
@ -550,8 +565,24 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
|||
|
||||
result = {x:{y.id: None for y in assets} for x in names}
|
||||
exp = Decimal(Decimal(1) / 10 ** digits_percent)
|
||||
asset_id_lst = [x.id for x in assets]
|
||||
|
||||
for x in names:
|
||||
# try to get values from cache
|
||||
ids_todo = []
|
||||
for id1 in asset_id_lst:
|
||||
c_val = cls._asset_cache.get('%(fname)s-%(id)d' % {
|
||||
'fname': x,
|
||||
'id': id1,
|
||||
})
|
||||
if c_val is None:
|
||||
ids_todo.append(id1)
|
||||
else :
|
||||
result[x][id1] = c_val
|
||||
|
||||
if len(ids_todo) == 0:
|
||||
continue
|
||||
|
||||
tab_percent = cls.get_percentage_sql(
|
||||
days={
|
||||
'change_day1': 0,
|
||||
|
@ -560,7 +591,7 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
|||
'change_month6': 180,
|
||||
'change_month12': 365,
|
||||
}[x],
|
||||
asset_ids=[x.id for x in assets]
|
||||
asset_ids=ids_todo
|
||||
)
|
||||
cursor.execute(*tab_percent)
|
||||
records = cursor.fetchall()
|
||||
|
@ -568,6 +599,10 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
|||
for record in records:
|
||||
result[x][record[0]] = record[3].quantize(exp) \
|
||||
if record[3] is not None else None
|
||||
cls._asset_cache.set('%(fname)s-%(id)d' % {
|
||||
'fname': x,
|
||||
'id': record[0],
|
||||
}, result[x][record[0]])
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
|
|
|
@ -201,16 +201,6 @@ class ImportWizard(Wizard):
|
|||
'investment.msg_err_unknown_content',
|
||||
linetxt = line,
|
||||
))
|
||||
|
||||
# ~ print('- found %d records' % len(result))
|
||||
# ~ print('- dates from %s to %s' % (
|
||||
# ~ min_date.isoformat() if min_date is not None else '-',
|
||||
# ~ max_date.isoformat() if max_date is not None else '-',
|
||||
# ~ ))
|
||||
# ~ print('- rates from %s to %s' % (
|
||||
# ~ str(min_rate) if min_rate is not None else '-',
|
||||
# ~ str(max_rate) if max_rate is not None else '-',
|
||||
# ~ ))
|
||||
return (result, max_date, min_date)
|
||||
|
||||
# end ImportWizard
|
||||
|
|
30
rate.py
30
rate.py
|
@ -87,4 +87,34 @@ class Rate(SymbolMixin, ModelSQL, ModelView):
|
|||
result[n][record[0]] = r1[n]
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def create(cls, vlist):
|
||||
""" clear cache
|
||||
"""
|
||||
Asset = Pool().get('investment.asset')
|
||||
|
||||
Asset.values_cache_clear(
|
||||
[x['asset'] for x in vlist],
|
||||
['change_day1', 'change_month1', 'change_month3',
|
||||
'change_month6', 'change_month12'],
|
||||
)
|
||||
return super(Rate, cls).create(vlist)
|
||||
|
||||
@classmethod
|
||||
def write(cls, *args):
|
||||
""" clear cache
|
||||
"""
|
||||
Asset = Pool().get('investment.asset')
|
||||
|
||||
asset_ids = []
|
||||
actions = iter(args)
|
||||
for rates, values in zip(actions, actions):
|
||||
asset_ids.extend([x.asset.id for x in rates])
|
||||
|
||||
if len(asset_ids) > 0:
|
||||
Asset.values_cache_clear(asset_ids,
|
||||
['change_day1', 'change_month1', 'change_month3',
|
||||
'change_month6', 'change_month12'])
|
||||
super(Rate, cls).write(*args)
|
||||
|
||||
# Rate
|
||||
|
|
Loading…
Reference in a new issue