-reactions.storage, +collectionShouts.query, fixes
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from typing_extensions import Self
|
||||
from sqlalchemy.types import Enum
|
||||
from sqlalchemy import Column, DateTime, ForeignKey, Integer
|
||||
from sqlalchemy.orm.attributes import flag_modified
|
||||
from base.orm import Base, local_session
|
||||
from orm.reaction import ReactionKind
|
||||
from orm.reaction import ReactionKind, kind_to_rate
|
||||
from orm.topic import ShoutTopic
|
||||
|
||||
class ReactedByDay(Base):
|
||||
__tablename__ = "reacted_by_day"
|
||||
@@ -12,42 +15,50 @@ class ReactedByDay(Base):
|
||||
reaction = Column(ForeignKey("reaction.id"), primary_key = True)
|
||||
shout = Column(ForeignKey('shout.slug'), primary_key=True)
|
||||
reply = Column(ForeignKey('reaction.id'), primary_key=True, nullable=True)
|
||||
kind = Column(ReactionKind, primary_key=True)
|
||||
kind: int = Column(Enum(ReactionKind), nullable=False, comment="Reaction kind")
|
||||
day = Column(DateTime, primary_key=True, default=datetime.now)
|
||||
|
||||
class ReactedStorage:
|
||||
reacted = {
|
||||
'shouts': {
|
||||
'total': 0,
|
||||
'today': 0,
|
||||
'month': 0,
|
||||
# TODO: need an opionated metrics list
|
||||
},
|
||||
'topics': {} # TODO: get sum reactions for all shouts in topic
|
||||
'shouts': {},
|
||||
'topics': {},
|
||||
'reactions': {}
|
||||
}
|
||||
this_day_reactions = {}
|
||||
rating = {
|
||||
'shouts': {},
|
||||
'topics': {},
|
||||
'reactions': {}
|
||||
}
|
||||
reactions = []
|
||||
to_flush = []
|
||||
period = 30*60 # sec
|
||||
lock = asyncio.Lock()
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
def prepare(session):
|
||||
self = ReactedStorage
|
||||
reactions = session.query(ReactedByDay).all()
|
||||
|
||||
for reaction in reactions:
|
||||
all_reactions = session.query(ReactedByDay).all()
|
||||
day_start = datetime.now().replace(hour=0, minute=0, second=0)
|
||||
for reaction in all_reactions:
|
||||
day = reaction.day
|
||||
shout = reaction.shout
|
||||
value = reaction.value
|
||||
if shout:
|
||||
old_value = self.reacted['shouts'].get(shout, 0)
|
||||
self.reacted['shouts'][shout] = old_value + value
|
||||
if not shout in self.this_day_reactions:
|
||||
self.this_day_reactions[shout] = reaction
|
||||
this_day_reaction = self.this_day_reactions[shout]
|
||||
if this_day_reaction.day < reaction.day:
|
||||
self.this_day_reactions[shout] = reaction
|
||||
topics = session.query(ShoutTopic.topic).where(ShoutTopic.shout == shout).all()
|
||||
kind = reaction.kind
|
||||
|
||||
self.reacted['shouts'][shout] = self.reacted['shouts'].get(shout, 0) + 1
|
||||
self.rating['shouts'][shout] = self.rating['shouts'].get(shout, 0) + kind_to_rate(kind)
|
||||
|
||||
for t in topics:
|
||||
self.reacted['topics'][t] = self.reacted['topics'].get(t, 0) + 1 # reactions amount
|
||||
self.rating['topics'][t] = self.rating['topics'].get(t, 0) + kind_to_rate(kind) # rating
|
||||
|
||||
if reaction.reply:
|
||||
self.reacted['reactions'][reaction.reply] = self.reacted['reactions'].get(reaction.reply, 0) + 1
|
||||
self.rating['reactions'][reaction.reply] = self.rating['reactions'].get(reaction.reply, 0) + kind_to_rate(reaction.kind)
|
||||
|
||||
print('[stat.reacted] %d shouts reacted' % len(self.reacted['shouts']))
|
||||
print('[stat.reacted] %d reactions reacted' % len(self.reacted['reactions']))
|
||||
|
||||
print('[service.reacted] watching %d shouts' % len(reactions))
|
||||
|
||||
@staticmethod
|
||||
async def get_shout(shout_slug):
|
||||
@@ -55,7 +66,24 @@ class ReactedStorage:
|
||||
async with self.lock:
|
||||
return self.reacted['shouts'].get(shout_slug, 0)
|
||||
|
||||
# NOTE: this method is never called
|
||||
@staticmethod
|
||||
async def get_topic(topic_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return self.reacted['topics'].get(topic_slug, 0)
|
||||
|
||||
@staticmethod
|
||||
async def get_rating(shout_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return self.reacted['shouts'].get(shout_slug, 0)
|
||||
|
||||
@staticmethod
|
||||
async def get_topic_rating(topic_slug):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
return self.rating['topics'].get(topic_slug, 0)
|
||||
|
||||
@staticmethod
|
||||
async def get_reaction(reaction_id):
|
||||
self = ReactedStorage
|
||||
@@ -63,63 +91,35 @@ class ReactedStorage:
|
||||
return self.reacted['reactions'].get(reaction_id, 0)
|
||||
|
||||
@staticmethod
|
||||
async def inc_shout(shout_slug):
|
||||
async def get_reaction_rating(reaction_id):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
this_day_reaction = self.this_day_reactions.get(shout_slug)
|
||||
day_start = datetime.now().replace(hour=0, minute=0, second=0)
|
||||
if not this_day_reaction or this_day_reaction.day < day_start:
|
||||
if this_day_reaction and getattr(this_day_reaction, "modified", False):
|
||||
self.to_flush.append(this_day_reaction)
|
||||
this_day_reaction = ReactedByDay.create(shout=shout_slug, value=1)
|
||||
self.this_day_reactions[shout_slug] = this_day_reaction
|
||||
else:
|
||||
this_day_reaction.value = this_day_reaction.value + 1
|
||||
this_day_reaction.modified = True
|
||||
old_value = self.reacted['shouts'].get(shout_slug, 0)
|
||||
self.reacted['shotus'][shout_slug] = old_value + 1
|
||||
return self.rating['reactions'].get(reaction_id, 0)
|
||||
|
||||
@staticmethod
|
||||
async def inc_reaction(shout_slug, reaction_id):
|
||||
async def increment(shout_slug, kind, reply_id = None):
|
||||
self = ReactedStorage
|
||||
reaction: ReactedByDay = None
|
||||
async with self.lock:
|
||||
this_day_reaction = self.this_day_reactions.get(reaction_id)
|
||||
day_start = datetime.now().replace(hour=0, minute=0, second=0)
|
||||
if not this_day_reaction or this_day_reaction.day < day_start:
|
||||
if this_day_reaction and getattr(this_day_reaction, "modified", False):
|
||||
self.to_flush.append(this_day_reaction)
|
||||
this_day_reaction = ReactedByDay.create(
|
||||
shout=shout_slug, reaction=reaction_id, value=1)
|
||||
self.this_day_reactions[shout_slug] = this_day_reaction
|
||||
else:
|
||||
this_day_reaction.value = this_day_reaction.value + 1
|
||||
this_day_reaction.modified = True
|
||||
old_value = self.reacted['shouts'].get(shout_slug, 0)
|
||||
self.reacted['shouts'][shout_slug] = old_value + 1
|
||||
old_value = self.reacted['reactions'].get(shout_slug, 0)
|
||||
self.reacted['reaction'][reaction_id] = old_value + 1
|
||||
|
||||
@staticmethod
|
||||
async def flush_changes(session):
|
||||
self = ReactedStorage
|
||||
async with self.lock:
|
||||
for reaction in self.this_day_reactions.values():
|
||||
if getattr(reaction, "modified", False):
|
||||
session.add(reaction)
|
||||
flag_modified(reaction, "value")
|
||||
reaction.modified = False
|
||||
for reaction in self.to_flush:
|
||||
session.add(reaction)
|
||||
self.to_flush.clear()
|
||||
session.commit()
|
||||
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, [])
|
||||
self.reacted['reaction'][reply_id].append(reaction)
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
while True:
|
||||
try:
|
||||
with local_session() as session:
|
||||
await ReactedStorage.flush_changes(session)
|
||||
print("[service.reacted] service flushed changes")
|
||||
ReactedStorage.prepare(session)
|
||||
print("[stat.reacted] updated")
|
||||
except Exception as err:
|
||||
print("[service.reacted] errror: %s" % (err))
|
||||
print("[stat.reacted] error: %s" % (err))
|
||||
raise err
|
||||
await asyncio.sleep(ReactedStorage.period)
|
||||
|
||||
@staticmethod
|
||||
def init(session):
|
||||
ReactedStorage.prepare(session)
|
@@ -1,5 +1,7 @@
|
||||
import asyncio
|
||||
from base.orm import local_session
|
||||
from services.stat.reacted import ReactedStorage
|
||||
from services.stat.viewed import ViewedStorage
|
||||
from services.zine.shoutauthor import ShoutAuthorStorage
|
||||
from orm.topic import ShoutTopic, TopicFollower
|
||||
from typing import Dict
|
||||
@@ -8,7 +10,6 @@ class TopicStat:
|
||||
shouts_by_topic = {}
|
||||
authors_by_topic = {}
|
||||
followers_by_topic = {}
|
||||
reactions_by_topic = {}
|
||||
lock = asyncio.Lock()
|
||||
period = 30*60 #sec
|
||||
|
||||
@@ -32,8 +33,8 @@ class TopicStat:
|
||||
else:
|
||||
self.authors_by_topic[topic] = set(authors)
|
||||
|
||||
print('[service.topicstat] authors sorted')
|
||||
print('[service.topicstat] shouts sorted')
|
||||
print('[stat.topics] authors sorted')
|
||||
print('[stat.topics] shouts sorted')
|
||||
|
||||
self.followers_by_topic = {}
|
||||
followings = session.query(TopicFollower)
|
||||
@@ -44,7 +45,7 @@ class TopicStat:
|
||||
self.followers_by_topic[topic].append(user)
|
||||
else:
|
||||
self.followers_by_topic[topic] = [user]
|
||||
print('[service.topicstat] followers sorted')
|
||||
print('[stat.topics] followers sorted')
|
||||
|
||||
@staticmethod
|
||||
async def get_shouts(topic):
|
||||
@@ -59,13 +60,14 @@ class TopicStat:
|
||||
shouts = self.shouts_by_topic.get(topic, [])
|
||||
followers = self.followers_by_topic.get(topic, [])
|
||||
authors = self.authors_by_topic.get(topic, [])
|
||||
reactions = self.reactions_by_topic.get(topic, [])
|
||||
|
||||
return {
|
||||
"shouts" : len(shouts),
|
||||
"authors" : len(authors),
|
||||
"followers" : len(followers),
|
||||
"reactions" : len(reactions)
|
||||
"viewed": ViewedStorage.get_topic(topic),
|
||||
"reacted" : ReactedStorage.get_topic(topic),
|
||||
"rating" : ReactedStorage.get_topic_rating(topic),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
@@ -76,8 +78,8 @@ class TopicStat:
|
||||
with local_session() as session:
|
||||
async with self.lock:
|
||||
await self.load_stat(session)
|
||||
print("[service.topicstat] updated")
|
||||
print("[stat.topics] updated")
|
||||
except Exception as err:
|
||||
print("[service.topicstat] errror: %s" % (err))
|
||||
print("[stat.topics] errror: %s" % (err))
|
||||
await asyncio.sleep(self.period)
|
||||
|
||||
|
@@ -3,6 +3,7 @@ from datetime import datetime
|
||||
from sqlalchemy import Column, DateTime, ForeignKey, Integer
|
||||
from sqlalchemy.orm.attributes import flag_modified
|
||||
from base.orm import Base, local_session
|
||||
from orm.topic import ShoutTopic
|
||||
|
||||
|
||||
class ViewedByDay(Base):
|
||||
@@ -17,7 +18,7 @@ class ViewedByDay(Base):
|
||||
class ViewedStorage:
|
||||
viewed = {
|
||||
'shouts': {},
|
||||
'topics': {} # TODO: get sum views for all shouts in topic
|
||||
'topics': {}
|
||||
}
|
||||
this_day_views = {}
|
||||
to_flush = []
|
||||
@@ -31,33 +32,36 @@ class ViewedStorage:
|
||||
|
||||
for view in views:
|
||||
shout = view.shout
|
||||
topics = session.query(ShoutTopic.topic).filter(ShoutTopic.shout == shout).all()
|
||||
value = view.value
|
||||
if shout:
|
||||
old_value = self.viewed['shouts'].get(shout, 0)
|
||||
self.viewed['shouts'][shout] = old_value + value
|
||||
for t in topics:
|
||||
old_topic_value = self.viewed['topics'].get(t, 0)
|
||||
self.viewed['topics'][t] = old_topic_value + value
|
||||
if not shout in self.this_day_views:
|
||||
self.this_day_views[shout] = view
|
||||
this_day_view = self.this_day_views[shout]
|
||||
if this_day_view.day < view.day:
|
||||
self.this_day_views[shout] = view
|
||||
|
||||
print('[service.viewed] watching %d shouts' % len(views))
|
||||
print('[stat.viewed] watching %d shouts' % len(views))
|
||||
|
||||
@staticmethod
|
||||
async def get_shout(shout_slug):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
return self.viewed['shouts'].get(shout_slug, 0)
|
||||
|
||||
# NOTE: this method is never called
|
||||
@staticmethod
|
||||
async def get_reaction(reaction_id):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
return self.viewed['reactions'].get(reaction_id, 0)
|
||||
|
||||
@staticmethod
|
||||
async def inc_shout(shout_slug):
|
||||
async def get_topic(topic_slug):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
return self.viewed['topics'].get(topic_slug, 0)
|
||||
|
||||
@staticmethod
|
||||
async def increment(shout_slug):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
this_day_view = self.this_day_views.get(shout_slug)
|
||||
@@ -71,27 +75,14 @@ class ViewedStorage:
|
||||
this_day_view.value = this_day_view.value + 1
|
||||
this_day_view.modified = True
|
||||
old_value = self.viewed['shouts'].get(shout_slug, 0)
|
||||
self.viewed['shotus'][shout_slug] = old_value + 1
|
||||
|
||||
@staticmethod
|
||||
async def inc_reaction(shout_slug, reaction_id):
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
this_day_view = self.this_day_views.get(reaction_id)
|
||||
day_start = datetime.now().replace(hour=0, minute=0, second=0)
|
||||
if not this_day_view or this_day_view.day < day_start:
|
||||
if this_day_view and getattr(this_day_view, "modified", False):
|
||||
self.to_flush.append(this_day_view)
|
||||
this_day_view = ViewedByDay.create(
|
||||
shout=shout_slug, reaction=reaction_id, value=1)
|
||||
self.this_day_views[shout_slug] = this_day_view
|
||||
else:
|
||||
this_day_view.value = this_day_view.value + 1
|
||||
this_day_view.modified = True
|
||||
old_value = self.viewed['shouts'].get(shout_slug, 0)
|
||||
self.viewed['shouts'][shout_slug] = old_value + 1
|
||||
old_value = self.viewed['reactions'].get(shout_slug, 0)
|
||||
self.viewed['reaction'][reaction_id] = old_value + 1
|
||||
with local_session() as session:
|
||||
topics = session.query(ShoutTopic.topic).where(ShoutTopic.shout == shout_slug).all()
|
||||
for t in topics:
|
||||
old_topic_value = self.viewed['topics'].get(t, 0)
|
||||
self.viewed['topics'][t] = old_topic_value + 1
|
||||
flag_modified(this_day_view, "value")
|
||||
|
||||
|
||||
@staticmethod
|
||||
async def flush_changes(session):
|
||||
@@ -113,7 +104,7 @@ class ViewedStorage:
|
||||
try:
|
||||
with local_session() as session:
|
||||
await ViewedStorage.flush_changes(session)
|
||||
print("[service.viewed] service flushed changes")
|
||||
print("[stat.viewed] service flushed changes")
|
||||
except Exception as err:
|
||||
print("[service.viewed] errror: %s" % (err))
|
||||
print("[stat.viewed] errror: %s" % (err))
|
||||
await asyncio.sleep(ViewedStorage.period)
|
||||
|
Reference in New Issue
Block a user