-reactions.storage, +collectionShouts.query, fixes

This commit is contained in:
2022-08-13 12:48:07 +03:00
parent 5859a4db40
commit f0b625af53
19 changed files with 277 additions and 381 deletions

View File

@@ -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)