add getTopicAuthors

This commit is contained in:
knst-kotov
2021-12-13 21:05:00 +03:00
parent ab9990a616
commit 64a728de41
4 changed files with 59 additions and 2 deletions

View File

@@ -158,6 +158,11 @@ class TopicStat:
else:
self.shouts_by_topic[topic] = [shout]
async def get_shouts(topic):
self = TopicStat
async with self.lock:
return self.shouts_by_topic.get(topic, [])
@staticmethod
async def get_stat(topic):
self = TopicStat
@@ -186,6 +191,44 @@ class TopicStat:
print("TopicStat worker: error = %s" % (err))
await asyncio.sleep(self.period)
class ShoutAuthorStorage:
authors_by_shout = {}
lock = asyncio.Lock()
period = 30*60 #sec
@staticmethod
async def load(session):
self = ShoutAuthorStorage
authors = session.query(ShoutAuthor)
for author in authors:
user = author.user
shout = author.shout
if shout in self.authors_by_shout:
self.authors_by_shout[shout].append(user)
else:
self.authors_by_shout[shout] = [user]
@staticmethod
async def get_authors(shout):
self = ShoutAuthorStorage
async with self.lock:
return self.authors_by_shout.get(shout, [])
@staticmethod
async def worker():
self = ShoutAuthorStorage
print("ShoutAuthorStorage worker start")
while True:
try:
print("ShoutAuthorStorage worker: load stat")
with local_session() as session:
async with self.lock:
await self.load(session)
except Exception as err:
print("ShoutAuthorStorage worker: error = %s" % (err))
await asyncio.sleep(self.period)
class Shout(Base):
__tablename__ = 'shout'