asset/rate: speed up percent-queries
This commit is contained in:
parent
8eaddc9b28
commit
c06c2b8260
2 changed files with 29 additions and 26 deletions
48
asset.py
48
asset.py
|
@ -473,8 +473,6 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
||||||
"""
|
"""
|
||||||
pool = Pool()
|
pool = Pool()
|
||||||
Rate = pool.get('investment.rate')
|
Rate = pool.get('investment.rate')
|
||||||
Asset2 = pool.get('investment.asset')
|
|
||||||
tab_asset = Asset2.__table__()
|
|
||||||
tab_rate1 = Rate.__table__()
|
tab_rate1 = Rate.__table__()
|
||||||
tab_rate2 = Rate.__table__()
|
tab_rate2 = Rate.__table__()
|
||||||
context = Transaction().context
|
context = Transaction().context
|
||||||
|
@ -482,17 +480,14 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
||||||
query_date = context.get('qdate', CurrentDate())
|
query_date = context.get('qdate', CurrentDate())
|
||||||
where_asset = tab_rate1.date <= query_date
|
where_asset = tab_rate1.date <= query_date
|
||||||
if isinstance(asset_ids, list):
|
if isinstance(asset_ids, list):
|
||||||
where_asset &= tab_asset.id.in_(asset_ids)
|
where_asset &= tab_rate1.asset.in_(asset_ids)
|
||||||
|
|
||||||
tab_today = tab_asset.join(
|
tab_today = tab_rate1.select(
|
||||||
tab_rate1,
|
tab_rate1.asset.as_('id'),
|
||||||
condition=tab_asset.id == tab_rate1.asset,
|
|
||||||
).select(
|
|
||||||
tab_asset.id,
|
|
||||||
tab_rate1.date,
|
tab_rate1.date,
|
||||||
tab_rate1.rate,
|
tab_rate1.rate,
|
||||||
distinct_on=[tab_asset.id],
|
distinct_on=[tab_rate1.asset],
|
||||||
order_by=[tab_asset.id, tab_rate1.date.desc],
|
order_by=[tab_rate1.asset, tab_rate1.date.desc],
|
||||||
where=where_asset,
|
where=where_asset,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -613,23 +608,24 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
|
||||||
exp = Decimal(Decimal(1) / 10 ** digits_percent)
|
exp = Decimal(Decimal(1) / 10 ** digits_percent)
|
||||||
asset_id_lst = [x.id for x in assets]
|
asset_id_lst = [x.id for x in assets]
|
||||||
|
|
||||||
for x in names:
|
if asset_id_lst and names:
|
||||||
tab_percent = cls.get_percentage_sql(
|
for x in names:
|
||||||
days={
|
tab_percent = cls.get_percentage_sql(
|
||||||
'change_day1': 0,
|
days={
|
||||||
'change_month1': 30,
|
'change_day1': 0,
|
||||||
'change_month3': 90,
|
'change_month1': 30,
|
||||||
'change_month6': 180,
|
'change_month3': 90,
|
||||||
'change_month12': 365,
|
'change_month6': 180,
|
||||||
}[x],
|
'change_month12': 365,
|
||||||
asset_ids=asset_id_lst,
|
}[x],
|
||||||
)
|
asset_ids=asset_id_lst,
|
||||||
cursor.execute(*tab_percent)
|
)
|
||||||
records = cursor.fetchall()
|
cursor.execute(*tab_percent)
|
||||||
|
records = cursor.fetchall()
|
||||||
|
|
||||||
for record in records:
|
for record in records:
|
||||||
result[x][record[0]] = record[3].quantize(exp) \
|
result[x][record[0]] = record[3].quantize(exp) \
|
||||||
if record[3] is not None else None
|
if record[3] is not None else None
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
7
rate.py
7
rate.py
|
@ -53,6 +53,13 @@ class Rate(SymbolMixin, ModelSQL, ModelView):
|
||||||
Index(
|
Index(
|
||||||
t,
|
t,
|
||||||
(t.rate, Index.Range())),
|
(t.rate, Index.Range())),
|
||||||
|
Index(
|
||||||
|
t,
|
||||||
|
(t.asset, Index.Equality())),
|
||||||
|
Index(
|
||||||
|
t,
|
||||||
|
(t.asset, Index.Equality()),
|
||||||
|
(t.date, Index.Range(order='DESC'))),
|
||||||
})
|
})
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
Loading…
Reference in a new issue