fixes-for-inbox-auth-and-startup-faster
This commit is contained in:
@@ -3,7 +3,7 @@ import time
|
||||
from base.orm import local_session
|
||||
from orm.shout import Shout, ShoutTopic, ShoutAuthor
|
||||
from orm.topic import TopicFollower
|
||||
from sqlalchemy.sql.expression import select
|
||||
# from sqlalchemy.sql.expression import select
|
||||
|
||||
|
||||
class TopicStat:
|
||||
@@ -20,21 +20,19 @@ class TopicStat:
|
||||
start = time.time()
|
||||
self = TopicStat
|
||||
shout_topics = session.query(ShoutTopic, Shout).join(Shout).all()
|
||||
all_shout_authors = session.query(ShoutAuthor).all()
|
||||
print("[stat.topics] %d links for shouts" % len(shout_topics))
|
||||
for [shout_topic, shout] in shout_topics:
|
||||
tpc = shout_topic.topic
|
||||
# shouts by topics
|
||||
# shout = session.query(Shout).where(Shout.slug == shout_topic.shout).first()
|
||||
self.shouts_by_topic[tpc] = self.shouts_by_topic.get(tpc, dict())
|
||||
self.shouts_by_topic[tpc][shout.slug] = shout
|
||||
|
||||
# authors by topics
|
||||
shout_authors = filter(lambda asa: asa.shout == shout.slug, all_shout_authors)
|
||||
|
||||
self.authors_by_topic[tpc] = self.authors_by_topic.get(tpc, dict())
|
||||
for sa in shout_authors:
|
||||
self.authors_by_topic[tpc][sa.shout] = sa.caption
|
||||
authors = session.query(
|
||||
ShoutAuthor.user, ShoutAuthor.caption
|
||||
).filter(
|
||||
ShoutAuthor.shout == shout.slug
|
||||
).all()
|
||||
for a in authors:
|
||||
self.authors_by_topic[tpc][a[0]] = a[1]
|
||||
|
||||
self.followers_by_topic = {}
|
||||
followings = session.query(TopicFollower).all()
|
||||
|
46
services/zine/shoutauthor.py
Normal file
46
services/zine/shoutauthor.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import asyncio
|
||||
from base.orm import local_session
|
||||
from orm.shout import ShoutAuthor, Shout
|
||||
|
||||
|
||||
class ShoutAuthorStorage:
|
||||
authors_by_shout = {}
|
||||
lock = asyncio.Lock()
|
||||
period = 30 * 60 # sec
|
||||
|
||||
@staticmethod
|
||||
async def load_captions(session):
|
||||
self = ShoutAuthorStorage
|
||||
sas = session.query(ShoutAuthor).join(Shout).all()
|
||||
for sa in sas:
|
||||
self.authors_by_shout[sa.shout] = self.authors_by_shout.get(sa.shout, [])
|
||||
self.authors_by_shout[sa.shout].append([sa.user, sa.caption])
|
||||
print("[zine.authors] %d shouts indexed by authors" % len(self.authors_by_shout))
|
||||
|
||||
@staticmethod
|
||||
async def get_authors(shout):
|
||||
self = ShoutAuthorStorage
|
||||
async with self.lock:
|
||||
return self.authors_by_shout.get(shout, [])
|
||||
|
||||
@staticmethod
|
||||
async def get_author_caption(shout, author):
|
||||
self = ShoutAuthorStorage
|
||||
async with self.lock:
|
||||
for a in self.authors_by_shout.get(shout, []):
|
||||
if author in a:
|
||||
return a[1]
|
||||
return {"error": "author caption not found"}
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
self = ShoutAuthorStorage
|
||||
while True:
|
||||
try:
|
||||
with local_session() as session:
|
||||
async with self.lock:
|
||||
await self.load_captions(session)
|
||||
print("[zine.authors] index by authors was updated")
|
||||
except Exception as err:
|
||||
print("[zine.authors] error indexing by author: %s" % (err))
|
||||
await asyncio.sleep(self.period)
|
Reference in New Issue
Block a user