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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue