2022-08-11 09:09:57 +00:00
|
|
|
import asyncio
|
|
|
|
from datetime import datetime
|
2022-08-13 09:48:07 +00:00
|
|
|
from sqlalchemy.types import Enum
|
2022-08-14 12:48:35 +00:00
|
|
|
from sqlalchemy import Column, DateTime, ForeignKey
|
2022-08-13 16:19:16 +00:00
|
|
|
# from sqlalchemy.orm.attributes import flag_modified
|
2022-08-14 12:48:35 +00:00
|
|
|
from sqlalchemy import Enum
|
|
|
|
import enum
|
2022-08-11 09:09:57 +00:00
|
|
|
from base.orm import Base, local_session
|
2022-08-13 09:48:07 +00:00
|
|
|
from orm.topic import ShoutTopic
|
2022-08-11 09:09:57 +00:00
|
|
|
|
2022-08-14 12:48:35 +00:00
|
|
|
class ReactionKind(enum.Enum):
|
|
|
|
AGREE = 1 # +1
|
|
|
|
DISAGREE = 2 # -1
|
|
|
|
PROOF = 3 # +1
|
|
|
|
DISPROOF = 4 # -1
|
|
|
|
ASK = 5 # +0 bookmark
|
|
|
|
PROPOSE = 6 # +0
|
|
|
|
QUOTE = 7 # +0 bookmark
|
|
|
|
COMMENT = 8 # +0
|
|
|
|
ACCEPT = 9 # +1
|
|
|
|
REJECT = 0 # -1
|
|
|
|
LIKE = 11 # +1
|
|
|
|
DISLIKE = 12 # -1
|
|
|
|
# TYPE = <reaction index> # rating diff
|
|
|
|
|
|
|
|
def kind_to_rate(kind) -> int:
|
|
|
|
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 ReactedByDay(Base):
|
|
|
|
__tablename__ = "reacted_by_day"
|
|
|
|
|
|
|
|
id = None
|
|
|
|
reaction = Column(ForeignKey("reaction.id"), primary_key = True)
|
|
|
|
shout = Column(ForeignKey('shout.slug'), primary_key=True)
|
2022-08-14 03:07:11 +00:00
|
|
|
replyTo = Column(ForeignKey('reaction.id'), nullable=True)
|
2022-08-13 09:48:07 +00:00
|
|
|
kind: int = Column(Enum(ReactionKind), nullable=False, comment="Reaction kind")
|
2022-08-11 09:09:57 +00:00
|
|
|
day = Column(DateTime, primary_key=True, default=datetime.now)
|
|
|
|
|
|
|
|
class ReactedStorage:
|
|
|
|
reacted = {
|
2022-08-13 09:48:07 +00:00
|
|
|
'shouts': {},
|
|
|
|
'topics': {},
|
|
|
|
'reactions': {}
|
2022-08-11 09:09:57 +00:00
|
|
|
}
|
2022-08-13 09:48:07 +00:00
|
|
|
rating = {
|
|
|
|
'shouts': {},
|
|
|
|
'topics': {},
|
|
|
|
'reactions': {}
|
|
|
|
}
|
|
|
|
reactions = []
|
2022-08-11 09:09:57 +00:00
|
|
|
to_flush = []
|
|
|
|
period = 30*60 # sec
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
|
|
|
|
@staticmethod
|
2022-08-13 10:05:46 +00:00
|
|
|
def init(session):
|
2022-08-11 09:09:57 +00:00
|
|
|
self = ReactedStorage
|
2022-08-13 09:48:07 +00:00
|
|
|
all_reactions = session.query(ReactedByDay).all()
|
2022-08-14 12:48:35 +00:00
|
|
|
print('[stat.reacted] %d reactions total' % len(all_reactions))
|
|
|
|
for reaction in all_reactions:
|
2022-08-11 09:09:57 +00:00
|
|
|
shout = reaction.shout
|
2022-08-13 09:48:07 +00:00
|
|
|
topics = session.query(ShoutTopic.topic).where(ShoutTopic.shout == shout).all()
|
|
|
|
kind = reaction.kind
|
|
|
|
|
2022-08-13 16:19:16 +00:00
|
|
|
self.reacted['shouts'][shout] = self.reacted['shouts'].get(shout, [])
|
|
|
|
self.reacted['shouts'][shout].append(reaction)
|
2022-08-13 09:48:07 +00:00
|
|
|
self.rating['shouts'][shout] = self.rating['shouts'].get(shout, 0) + kind_to_rate(kind)
|
|
|
|
|
|
|
|
for t in topics:
|
2022-08-13 16:19:16 +00:00
|
|
|
self.reacted['topics'][t] = self.reacted['topics'].get(t, [])
|
|
|
|
self.reacted['topics'][t].append(reaction)
|
2022-08-13 09:48:07 +00:00
|
|
|
self.rating['topics'][t] = self.rating['topics'].get(t, 0) + kind_to_rate(kind) # rating
|
|
|
|
|
2022-08-13 16:19:16 +00:00
|
|
|
if reaction.replyTo:
|
|
|
|
self.reacted['reactions'][reaction.replyTo] = self.reacted['reactions'].get(reaction.replyTo, [])
|
|
|
|
self.reacted['reactions'][reaction.replyTo].append(reaction)
|
|
|
|
self.rating['reactions'][reaction.replyTo] = self.rating['reactions'].get(reaction.replyTo, 0) + kind_to_rate(reaction.kind)
|
|
|
|
ttt = self.reacted['topics'].values()
|
|
|
|
print('[stat.reacted] %d topics reacted' % len(ttt))
|
2022-08-13 09:48:07 +00:00
|
|
|
print('[stat.reacted] %d shouts reacted' % len(self.reacted['shouts']))
|
|
|
|
print('[stat.reacted] %d reactions reacted' % len(self.reacted['reactions']))
|
2022-08-11 09:09:57 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_shout(shout_slug):
|
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
2022-08-13 16:19:16 +00:00
|
|
|
return self.reacted['shouts'].get(shout_slug, [])
|
2022-08-11 09:09:57 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-08-13 09:48:07 +00:00
|
|
|
async def get_topic(topic_slug):
|
2022-08-11 09:09:57 +00:00
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
2022-08-13 16:19:16 +00:00
|
|
|
return self.reacted['topics'].get(topic_slug, [])
|
2022-08-11 09:09:57 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-08-13 16:19:16 +00:00
|
|
|
async def get_reaction(reaction_id):
|
2022-08-11 09:09:57 +00:00
|
|
|
self = ReactedStorage
|
|
|
|
async with self.lock:
|
2022-08-13 16:19:16 +00:00
|
|
|
return self.reacted['reactions'].get(reaction_id, [])
|
2022-08-11 09:09:57 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-08-13 16:19:16 +00:00
|
|
|
async def get_rating(shout_slug):
|
2022-08-11 09:09:57 +00:00
|
|
|
self = ReactedStorage
|
2022-08-14 03:19:32 +00:00
|
|
|
rating = 0
|
2022-08-11 09:09:57 +00:00
|
|
|
async with self.lock:
|
2022-08-14 03:19:32 +00:00
|
|
|
for r in self.reacted['shouts'].get(shout_slug, []):
|
|
|
|
rating = rating + kind_to_rate(r.kind)
|
|
|
|
return rating
|
2022-08-11 09:09:57 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-08-13 16:19:16 +00:00
|
|
|
async def get_topic_rating(topic_slug):
|
2022-08-11 09:09:57 +00:00
|
|
|
self = ReactedStorage
|
2022-08-14 03:19:32 +00:00
|
|
|
rating = 0
|
2022-08-11 09:09:57 +00:00
|
|
|
async with self.lock:
|
2022-08-14 03:19:32 +00:00
|
|
|
for r in self.reacted['topics'].get(topic_slug, []):
|
|
|
|
rating = rating + kind_to_rate(r.kind)
|
|
|
|
return rating
|
2022-08-13 09:48:07 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def get_reaction_rating(reaction_id):
|
|
|
|
self = ReactedStorage
|
2022-08-14 03:19:32 +00:00
|
|
|
rating = 0
|
2022-08-13 09:48:07 +00:00
|
|
|
async with self.lock:
|
2022-08-14 03:19:32 +00:00
|
|
|
for r in self.reacted['reactions'].get(reaction_id, []):
|
|
|
|
rating = rating + kind_to_rate(r.kind)
|
|
|
|
return rating
|
2022-08-13 09:48:07 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def increment(shout_slug, kind, reply_id = None):
|
|
|
|
self = ReactedStorage
|
|
|
|
reaction: ReactedByDay = None
|
|
|
|
async with self.lock:
|
2022-08-13 10:05:46 +00:00
|
|
|
with local_session() as session:
|
|
|
|
reaction = ReactedByDay.create(shout=shout_slug, kind=kind, reply=reply_id)
|
|
|
|
self.reacted['shouts'][shout_slug] = self.reacted['shouts'].get(shout_slug, [])
|
|
|
|
self.reacted['shouts'][shout_slug].append(reaction)
|
|
|
|
if reply_id:
|
|
|
|
self.reacted['reaction'][reply_id] = self.reacted['reactions'].get(shout_slug, [])
|
2022-08-13 16:19:16 +00:00
|
|
|
self.reacted['reaction'][reply_id].append(reaction)
|
|
|
|
self.rating['reactions'][reply_id] = self.rating['reactions'].get(reply_id, 0) + kind_to_rate(kind)
|
|
|
|
else:
|
|
|
|
self.rating['shouts'][shout_slug] = self.rating['shouts'].get(shout_slug, 0) + kind_to_rate(kind)
|