2022-08-11 09:09:57 +00:00
|
|
|
import asyncio
|
2022-09-19 13:50:43 +00:00
|
|
|
from base.orm import local_session
|
|
|
|
from orm.reaction import ReactionKind, Reaction
|
|
|
|
from services.zine.topics import TopicStorage
|
2022-11-18 18:22:10 +00:00
|
|
|
from services.stat.viewed import ViewedStorage
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-08-14 12:48:35 +00:00
|
|
|
|
|
|
|
def kind_to_rate(kind) -> int:
|
2022-09-03 10:50:14 +00:00
|
|
|
if kind in [
|
|
|
|
ReactionKind.AGREE,
|
|
|
|
ReactionKind.LIKE,
|
|
|
|
ReactionKind.PROOF,
|
|
|
|
ReactionKind.ACCEPT,
|
|
|
|
]:
|
|
|
|
return 1
|
|
|
|
elif kind in [
|
|
|
|
ReactionKind.DISAGREE,
|
|
|
|
ReactionKind.DISLIKE,
|
|
|
|
ReactionKind.DISPROOF,
|
|
|
|
ReactionKind.REJECT,
|
|
|
|
]:
|
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2022-08-11 09:09:57 +00:00
|
|
|
class ReactedStorage:
|
2022-10-23 12:48:08 +00:00
|
|
|
reacted = {"shouts": {}, "topics": {}, "reactions": {}, "authors": {}}
|
2022-09-03 10:50:14 +00:00
|
|
|
rating = {"shouts": {}, "topics": {}, "reactions": {}}
|
|
|
|
reactions = []
|
|
|
|
to_flush = []
|
|
|
|
period = 30 * 60 # sec
|
|
|
|
lock = asyncio.Lock()
|
2022-09-19 13:50:43 +00:00
|
|
|
modified_shouts = set([])
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-11-15 02:36:30 +00:00
|
|
|
@staticmethod
|
|
|
|
async def get_shout_stat(slug):
|
|
|
|
return {
|
2022-11-18 18:22:10 +00:00
|
|
|
"viewed": await ViewedStorage.get_shout(slug),
|
2022-11-15 02:36:30 +00:00
|
|
|
"reacted": len(await ReactedStorage.get_shout(slug)),
|
|
|
|
"commented": len(await ReactedStorage.get_comments(slug)),
|
|
|
|
"rating": await ReactedStorage.get_rating(slug),
|
|
|
|
}
|
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def get_shout(shout_slug):
|
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
|
|
|
return self.reacted["shouts"].get(shout_slug, [])
|
|
|
|
|
2022-10-23 12:48:08 +00:00
|
|
|
@staticmethod
|
|
|
|
async def get_author(user_slug):
|
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
|
|
|
return self.reacted["authors"].get(user_slug, [])
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_shouts_by_author(user_slug):
|
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
|
|
|
author_reactions = self.reacted["authors"].get(user_slug, [])
|
|
|
|
shouts = []
|
|
|
|
for r in author_reactions:
|
|
|
|
if r.shout not in shouts:
|
|
|
|
shouts.append(r.shout)
|
|
|
|
return shouts
|
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def get_topic(topic_slug):
|
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
|
|
|
return self.reacted["topics"].get(topic_slug, [])
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_comments(shout_slug):
|
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
|
|
|
return list(
|
2022-09-19 13:50:43 +00:00
|
|
|
filter(lambda r: bool(r.body), self.reacted["shouts"].get(shout_slug, {}))
|
2022-09-03 10:50:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_topic_comments(topic_slug):
|
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
|
|
|
return list(
|
2022-09-19 13:50:43 +00:00
|
|
|
filter(lambda r: bool(r.body), self.reacted["topics"].get(topic_slug, []))
|
2022-09-03 10:50:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_reaction_comments(reaction_id):
|
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
|
|
|
return list(
|
2022-09-04 17:20:38 +00:00
|
|
|
filter(
|
2022-09-19 13:50:43 +00:00
|
|
|
lambda r: bool(r.body), self.reacted["reactions"].get(reaction_id, {})
|
2022-09-04 17:20:38 +00:00
|
|
|
)
|
2022-09-03 10:50:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_reaction(reaction_id):
|
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
|
|
|
return self.reacted["reactions"].get(reaction_id, [])
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_rating(shout_slug):
|
|
|
|
self = ReactedStorage
|
|
|
|
rating = 0
|
|
|
|
async with self.lock:
|
|
|
|
for r in self.reacted["shouts"].get(shout_slug, []):
|
|
|
|
rating = rating + kind_to_rate(r.kind)
|
|
|
|
return rating
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_topic_rating(topic_slug):
|
|
|
|
self = ReactedStorage
|
|
|
|
rating = 0
|
|
|
|
async with self.lock:
|
|
|
|
for r in self.reacted["topics"].get(topic_slug, []):
|
|
|
|
rating = rating + kind_to_rate(r.kind)
|
|
|
|
return rating
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_reaction_rating(reaction_id):
|
|
|
|
self = ReactedStorage
|
|
|
|
rating = 0
|
|
|
|
async with self.lock:
|
|
|
|
for r in self.reacted["reactions"].get(reaction_id, []):
|
|
|
|
rating = rating + kind_to_rate(r.kind)
|
|
|
|
return rating
|
|
|
|
|
|
|
|
@staticmethod
|
2022-09-04 17:20:38 +00:00
|
|
|
async def react(reaction):
|
2022-09-19 13:50:43 +00:00
|
|
|
ReactedStorage.modified_shouts.add(reaction.shout)
|
2022-09-04 17:20:38 +00:00
|
|
|
|
2022-09-19 13:50:43 +00:00
|
|
|
@staticmethod
|
|
|
|
async def recount(reactions):
|
|
|
|
self = ReactedStorage
|
|
|
|
for r in reactions:
|
2022-10-23 12:48:08 +00:00
|
|
|
# renew reactions by shout
|
2022-09-19 13:50:43 +00:00
|
|
|
self.reacted["shouts"][r.shout] = self.reacted["shouts"].get(r.shout, [])
|
|
|
|
self.reacted["shouts"][r.shout].append(r)
|
2022-10-23 12:48:08 +00:00
|
|
|
# renew reactions by author
|
|
|
|
self.reacted["authors"][r.createdBy] = self.reacted["authors"].get(r.createdBy, [])
|
|
|
|
self.reacted["authors"][r.createdBy].append(r)
|
|
|
|
# renew reactions by topic
|
2022-09-19 13:50:43 +00:00
|
|
|
shout_topics = await TopicStorage.get_topics_by_slugs([r.shout, ])
|
|
|
|
for t in shout_topics:
|
|
|
|
self.reacted["topics"][t] = self.reacted["topics"].get(t, [])
|
|
|
|
self.reacted["topics"][t].append(r)
|
|
|
|
self.rating["topics"][t] = \
|
|
|
|
self.rating["topics"].get(t, 0) + kind_to_rate(r.kind)
|
|
|
|
if r.replyTo:
|
2022-10-23 12:48:08 +00:00
|
|
|
# renew reactions replies
|
2022-09-19 13:50:43 +00:00
|
|
|
self.reacted["reactions"][r.replyTo] = \
|
|
|
|
self.reacted["reactions"].get(r.replyTo, [])
|
|
|
|
self.reacted["reactions"][r.replyTo].append(r)
|
|
|
|
self.rating["reactions"][r.replyTo] = \
|
|
|
|
self.rating["reactions"].get(r.replyTo, 0) + kind_to_rate(r.kind)
|
|
|
|
else:
|
|
|
|
# renew shout rating
|
|
|
|
self.rating["shouts"][r.shout] = \
|
|
|
|
self.rating["shouts"].get(r.shout, 0) + kind_to_rate(r.kind)
|
2022-09-03 10:50:14 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def init(session):
|
|
|
|
self = ReactedStorage
|
2022-09-19 13:50:43 +00:00
|
|
|
all_reactions = session.query(Reaction).all()
|
2022-10-14 09:25:45 +00:00
|
|
|
self.modified_shouts = list(set([r.shout for r in all_reactions]))
|
2022-11-17 06:25:26 +00:00
|
|
|
print("[stat.reacted] %d shouts with reactions" % len(self.modified_shouts))
|
2022-09-04 17:20:38 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-09-19 13:50:43 +00:00
|
|
|
async def recount_changed(session):
|
2022-09-07 16:56:26 +00:00
|
|
|
self = ReactedStorage
|
2022-09-04 17:20:38 +00:00
|
|
|
async with self.lock:
|
2022-11-17 06:25:26 +00:00
|
|
|
sss = list(self.modified_shouts)
|
|
|
|
c = 0
|
|
|
|
for slug in sss:
|
2022-09-19 13:50:43 +00:00
|
|
|
siblings = session.query(Reaction).where(Reaction.shout == slug).all()
|
2022-11-17 06:25:26 +00:00
|
|
|
c += len(siblings)
|
2022-09-19 13:50:43 +00:00
|
|
|
await self.recount(siblings)
|
|
|
|
|
2022-11-17 06:25:26 +00:00
|
|
|
print("[stat.reacted] %d reactions total" % c)
|
2022-10-14 09:25:45 +00:00
|
|
|
print("[stat.reacted] %d shouts" % len(self.modified_shouts))
|
|
|
|
print("[stat.reacted] %d topics" % len(self.reacted["topics"].values()))
|
|
|
|
print("[stat.reacted] %d shouts" % len(self.reacted["shouts"]))
|
2022-10-30 07:43:33 +00:00
|
|
|
print("[stat.reacted] %d authors" % len(self.reacted["authors"].values()))
|
2022-11-17 06:25:26 +00:00
|
|
|
print("[stat.reacted] %d reactions replied" % len(self.reacted["reactions"]))
|
2022-09-19 13:50:43 +00:00
|
|
|
self.modified_shouts = set([])
|
2022-09-04 17:20:38 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def worker():
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
with local_session() as session:
|
2022-09-19 13:50:43 +00:00
|
|
|
await ReactedStorage.recount_changed(session)
|
2022-09-04 17:20:38 +00:00
|
|
|
except Exception as err:
|
2022-09-19 13:50:43 +00:00
|
|
|
print("[stat.reacted] recount error %s" % (err))
|
2022-09-04 17:20:38 +00:00
|
|
|
await asyncio.sleep(ReactedStorage.period)
|