2022-07-21 11:58:50 +00:00
|
|
|
import asyncio
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from sqlalchemy import and_, desc, func, select
|
2022-08-13 09:48:07 +00:00
|
|
|
from sqlalchemy.orm import selectinload
|
2022-08-11 05:53:14 +00:00
|
|
|
from base.orm import local_session
|
2022-07-21 11:58:50 +00:00
|
|
|
from orm.reaction import Reaction
|
2022-09-07 16:19:06 +00:00
|
|
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
2022-08-13 09:48:07 +00:00
|
|
|
from services.stat.reacted import ReactedStorage
|
2022-08-11 09:09:57 +00:00
|
|
|
from services.stat.viewed import ViewedByDay
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ShoutsCache:
|
2022-09-03 10:50:14 +00:00
|
|
|
limit = 200
|
|
|
|
period = 60 * 60 # 1 hour
|
|
|
|
lock = asyncio.Lock()
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-07 16:19:06 +00:00
|
|
|
recent_published = []
|
|
|
|
recent_all = []
|
|
|
|
recent_reacted = []
|
|
|
|
top_month = []
|
|
|
|
top_overall = []
|
|
|
|
top_viewed = []
|
|
|
|
|
|
|
|
by_author = {}
|
|
|
|
by_topic = {}
|
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def prepare_recent_published():
|
|
|
|
with local_session() as session:
|
|
|
|
stmt = (
|
|
|
|
select(Shout)
|
|
|
|
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
2022-09-03 11:10:28 +00:00
|
|
|
.where(bool(Shout.publishedAt))
|
2022-09-03 10:50:14 +00:00
|
|
|
.order_by(desc("publishedAt"))
|
|
|
|
.limit(ShoutsCache.limit)
|
|
|
|
)
|
|
|
|
shouts = []
|
|
|
|
for row in session.execute(stmt):
|
|
|
|
shout = row.Shout
|
|
|
|
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
|
|
|
shouts.append(shout)
|
|
|
|
async with ShoutsCache.lock:
|
|
|
|
ShoutsCache.recent_published = shouts
|
|
|
|
print("[zine.cache] %d recently published shouts " % len(shouts))
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def prepare_recent_all():
|
|
|
|
with local_session() as session:
|
|
|
|
stmt = (
|
|
|
|
select(Shout)
|
|
|
|
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
|
|
|
.order_by(desc("createdAt"))
|
|
|
|
.limit(ShoutsCache.limit)
|
|
|
|
)
|
|
|
|
shouts = []
|
|
|
|
for row in session.execute(stmt):
|
|
|
|
shout = row.Shout
|
|
|
|
# shout.topics = [t.slug for t in shout.topics]
|
|
|
|
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
|
|
|
shouts.append(shout)
|
|
|
|
async with ShoutsCache.lock:
|
|
|
|
ShoutsCache.recent_all = shouts
|
|
|
|
print("[zine.cache] %d recently created shouts " % len(shouts))
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def prepare_recent_reacted():
|
|
|
|
with local_session() as session:
|
|
|
|
stmt = (
|
|
|
|
select(Shout, func.max(Reaction.createdAt).label("reactionCreatedAt"))
|
|
|
|
.options(
|
|
|
|
selectinload(Shout.authors),
|
|
|
|
selectinload(Shout.topics),
|
|
|
|
)
|
|
|
|
.join(Reaction, Reaction.shout == Shout.slug)
|
2022-09-03 11:10:28 +00:00
|
|
|
.where(and_(bool(Shout.publishedAt), bool(Reaction.deletedAt)))
|
2022-09-03 10:50:14 +00:00
|
|
|
.group_by(Shout.slug)
|
|
|
|
.order_by(desc("reactionCreatedAt"))
|
|
|
|
.limit(ShoutsCache.limit)
|
|
|
|
)
|
|
|
|
shouts = []
|
|
|
|
for row in session.execute(stmt):
|
|
|
|
shout = row.Shout
|
|
|
|
# shout.topics = [t.slug for t in shout.topics]
|
|
|
|
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
|
|
|
shouts.append(shout)
|
|
|
|
async with ShoutsCache.lock:
|
|
|
|
ShoutsCache.recent_reacted = shouts
|
|
|
|
print("[zine.cache] %d recently reacted shouts " % len(shouts))
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def prepare_top_overall():
|
|
|
|
with local_session() as session:
|
|
|
|
# with reacted times counter
|
|
|
|
stmt = (
|
|
|
|
select(Shout, func.count(Reaction.id).label("reacted"))
|
|
|
|
.options(
|
|
|
|
selectinload(Shout.authors),
|
|
|
|
selectinload(Shout.topics),
|
|
|
|
selectinload(Shout.reactions),
|
|
|
|
)
|
|
|
|
.join(Reaction)
|
2022-09-03 11:10:28 +00:00
|
|
|
.where(and_(bool(Shout.publishedAt), bool(Reaction.deletedAt)))
|
2022-09-03 10:50:14 +00:00
|
|
|
.group_by(Shout.slug)
|
|
|
|
.order_by(desc("reacted"))
|
|
|
|
.limit(ShoutsCache.limit)
|
|
|
|
)
|
|
|
|
shouts = []
|
|
|
|
# with rating synthetic counter
|
|
|
|
for row in session.execute(stmt):
|
|
|
|
shout = row.Shout
|
|
|
|
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
|
|
|
shouts.append(shout)
|
|
|
|
shouts.sort(key=lambda shout: shout.rating, reverse=True)
|
|
|
|
async with ShoutsCache.lock:
|
|
|
|
print("[zine.cache] %d top shouts " % len(shouts))
|
|
|
|
ShoutsCache.top_overall = shouts
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def prepare_top_month():
|
|
|
|
month_ago = datetime.now() - timedelta(days=30)
|
|
|
|
with local_session() as session:
|
|
|
|
stmt = (
|
|
|
|
select(Shout, func.count(Reaction.id).label("reacted"))
|
|
|
|
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
|
|
|
.join(Reaction)
|
2022-09-03 11:10:28 +00:00
|
|
|
.where(and_(Shout.createdAt > month_ago, bool(Reaction.deletedAt)))
|
2022-09-03 10:50:14 +00:00
|
|
|
.group_by(Shout.slug)
|
|
|
|
.order_by(desc("reacted"))
|
|
|
|
.limit(ShoutsCache.limit)
|
|
|
|
)
|
|
|
|
shouts = []
|
|
|
|
for row in session.execute(stmt):
|
|
|
|
shout = row.Shout
|
|
|
|
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
|
|
|
shouts.append(shout)
|
|
|
|
shouts.sort(key=lambda shout: shout.rating, reverse=True)
|
|
|
|
async with ShoutsCache.lock:
|
|
|
|
print("[zine.cache] %d top month shouts " % len(shouts))
|
|
|
|
ShoutsCache.top_month = shouts
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def prepare_top_viewed():
|
|
|
|
month_ago = datetime.now() - timedelta(days=30)
|
|
|
|
with local_session() as session:
|
|
|
|
stmt = (
|
|
|
|
select(Shout, func.sum(ViewedByDay.value).label("viewed"))
|
|
|
|
.options(selectinload(Shout.authors), selectinload(Shout.topics))
|
|
|
|
.join(ViewedByDay)
|
2022-09-03 11:10:28 +00:00
|
|
|
.where(and_(Shout.createdAt > month_ago, bool(Reaction.deletedAt)))
|
2022-09-03 10:50:14 +00:00
|
|
|
.group_by(Shout.slug)
|
|
|
|
.order_by(desc("viewed"))
|
|
|
|
.limit(ShoutsCache.limit)
|
|
|
|
)
|
|
|
|
shouts = []
|
|
|
|
for row in session.execute(stmt):
|
|
|
|
shout = row.Shout
|
|
|
|
shout.rating = await ReactedStorage.get_rating(shout.slug) or 0
|
|
|
|
shouts.append(shout)
|
|
|
|
# shouts.sort(key = lambda shout: shout.viewed, reverse = True)
|
|
|
|
async with ShoutsCache.lock:
|
|
|
|
print("[zine.cache] %d top viewed shouts " % len(shouts))
|
|
|
|
ShoutsCache.top_viewed = shouts
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-07 16:19:06 +00:00
|
|
|
@staticmethod
|
|
|
|
async def prepare_by_author():
|
|
|
|
shouts_by_author = {}
|
|
|
|
with local_session() as session:
|
|
|
|
|
|
|
|
for a in session.query(ShoutAuthor).all():
|
|
|
|
|
|
|
|
shout = session.query(Shout).filter(Shout.slug == a.shout).first()
|
|
|
|
|
2022-09-07 16:30:06 +00:00
|
|
|
if not shouts_by_author.get(a.user):
|
|
|
|
shouts_by_author[a.user] = []
|
2022-09-07 16:19:06 +00:00
|
|
|
|
2022-09-07 16:30:06 +00:00
|
|
|
if shout not in shouts_by_author[a.user]:
|
|
|
|
shouts_by_author[a.user].append(shout)
|
2022-09-07 16:19:06 +00:00
|
|
|
async with ShoutsCache.lock:
|
2022-09-07 16:30:06 +00:00
|
|
|
print("[zine.cache] indexed by %d authors " % len(shouts_by_author.keys()))
|
2022-09-07 16:19:06 +00:00
|
|
|
ShoutsCache.by_author = shouts_by_author
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def prepare_by_topic():
|
|
|
|
shouts_by_topic = {}
|
|
|
|
with local_session() as session:
|
|
|
|
|
|
|
|
for t in session.query(ShoutTopic).all():
|
|
|
|
|
|
|
|
shout = session.query(Shout).filter(Shout.slug == t.shout).first()
|
|
|
|
|
2022-09-07 16:30:06 +00:00
|
|
|
if not shouts_by_topic.get(t.topic):
|
2022-09-07 16:19:06 +00:00
|
|
|
shouts_by_topic[t.topic] = []
|
|
|
|
|
|
|
|
if shout not in shouts_by_topic[t.topic]:
|
2022-09-07 16:30:06 +00:00
|
|
|
shouts_by_topic[t.topic].append(shout)
|
|
|
|
|
2022-09-07 16:19:06 +00:00
|
|
|
async with ShoutsCache.lock:
|
|
|
|
print("[zine.cache] indexed by %d topics " % len(shouts_by_topic.keys()))
|
|
|
|
ShoutsCache.by_topic = shouts_by_topic
|
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def worker():
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
await ShoutsCache.prepare_top_month()
|
|
|
|
await ShoutsCache.prepare_top_overall()
|
|
|
|
await ShoutsCache.prepare_top_viewed()
|
2022-09-07 16:19:06 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
await ShoutsCache.prepare_recent_published()
|
|
|
|
await ShoutsCache.prepare_recent_all()
|
|
|
|
await ShoutsCache.prepare_recent_reacted()
|
2022-09-07 16:19:06 +00:00
|
|
|
|
|
|
|
await ShoutsCache.prepare_by_author()
|
|
|
|
await ShoutsCache.prepare_by_topic()
|
2022-09-03 10:50:14 +00:00
|
|
|
print("[zine.cache] periodical update")
|
|
|
|
except Exception as err:
|
|
|
|
print("[zine.cache] error: %s" % (err))
|
|
|
|
raise err
|
|
|
|
await asyncio.sleep(ShoutsCache.period)
|