remove caching, optimize rate/query
This commit is contained in:
parent
5eec999d41
commit
89f19ceae5
3 changed files with 5 additions and 27 deletions
28
asset.py
28
asset.py
|
@ -8,10 +8,9 @@ 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 MemoryCache
|
||||
|
||||
from decimal import Decimal
|
||||
from datetime import time
|
||||
from datetime import time, datetime
|
||||
from sql.functions import CurrentDate, CurrentTimestamp, Round, Extract
|
||||
from sql.conditionals import Case, Coalesce, NullIf
|
||||
from sql import Literal
|
||||
|
@ -120,8 +119,6 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
|||
readonly=True, model_name='investment.rate'),
|
||||
'get_rate_data')
|
||||
|
||||
_asset_cache = MemoryCache('investment.asset.values', duration=600, context=False)
|
||||
|
||||
@classmethod
|
||||
def __register__(cls, module_name):
|
||||
""" register and migrate
|
||||
|
@ -453,7 +450,7 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
|||
tab_today.rate,
|
||||
(tab_today.rate * 100.0 / NullIf(tab_rate2.rate, 0.00) - 100.0).as_('percent'),
|
||||
distinct_on=[tab_today.id],
|
||||
order_by=[tab_today.id, tab_today.date.desc]
|
||||
order_by=[tab_today.id, tab_rate2.date.desc]
|
||||
)
|
||||
return query
|
||||
|
||||
|
@ -557,21 +554,6 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
|||
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,
|
||||
|
@ -580,7 +562,7 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
|||
'change_month6': 180,
|
||||
'change_month12': 365,
|
||||
}[x],
|
||||
asset_ids=ids_todo
|
||||
asset_ids=asset_id_lst,
|
||||
)
|
||||
cursor.execute(*tab_percent)
|
||||
records = cursor.fetchall()
|
||||
|
@ -588,10 +570,6 @@ 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
|
||||
|
|
2
rate.py
2
rate.py
|
@ -17,7 +17,7 @@ class Rate(SymbolMixin, ModelSQL, ModelView):
|
|||
select=True, ondelete='CASCADE',
|
||||
model_name='investment.asset')
|
||||
date = fields.Date(string='Date', required=True, select=True)
|
||||
rate = fields.Numeric(string='Rate', required=True,
|
||||
rate = fields.Numeric(string='Rate', required=True, select=True,
|
||||
digits=(16, Eval('asset_digits', 4)),
|
||||
depends=['asset_digits'])
|
||||
|
||||
|
|
|
@ -375,7 +375,7 @@ class AssetTestCase(ModuleTestCase):
|
|||
}])
|
||||
self.assertEqual(asset1.rates[0].date, date(2022, 5, 16))
|
||||
self.assertEqual(asset1.rates[1].date, date(2022, 5, 10))
|
||||
#self.assertEqual(asset1.change_day1, None)
|
||||
self.assertEqual(asset1.change_day1, None)
|
||||
|
||||
@with_transaction()
|
||||
def test_asset_percentges_values(self):
|
||||
|
|
Loading…
Reference in a new issue