lbry-sdk/lbry/db/trending.py

26 lines
974 B
Python
Raw Normal View History

2020-10-19 10:47:26 -03:00
from sqlalchemy import select
from sqlalchemy.sql import func
from lbry.db.query_context import event_emitter, ProgressContext
2020-12-07 07:39:27 -05:00
from lbry.db.tables import Trend, Support, Claim
2020-10-19 10:47:26 -03:00
WINDOW = 576 # a day
@event_emitter("blockchain.sync.trending.update", "steps")
def calculate_trending(height, p: ProgressContext):
with p.ctx.engine.begin() as ctx:
2020-12-07 07:39:27 -05:00
ctx.execute(Trend.delete())
start = height - WINDOW
trending = func.sum(Support.c.amount * (WINDOW - (height - Support.c.height)))
sql = (
select([Claim.c.claim_hash, trending, trending, trending, 4])
.where(
(Support.c.claim_hash == Claim.c.claim_hash) &
(Support.c.height <= height) &
(Support.c.height >= start)
).group_by(Claim.c.claim_hash)
)
ctx.execute(Trend.insert().from_select(
['claim_hash', 'trend_global', 'trend_local', 'trend_mixed', 'trend_group'], sql
))