stats refactored
This commit is contained in:
@@ -1,17 +1,15 @@
|
||||
import asyncio
|
||||
|
||||
from base.orm import local_session
|
||||
from orm.shout import Shout
|
||||
from orm.topic import ShoutTopic, TopicFollower
|
||||
from services.stat.reacted import ReactedStorage
|
||||
from services.stat.viewed import ViewedStorage
|
||||
from orm.shout import Shout, ShoutTopic
|
||||
from orm.topic import TopicFollower
|
||||
from services.zine.shoutauthor import ShoutAuthorStorage
|
||||
|
||||
|
||||
class TopicStat:
|
||||
shouts_by_topic = {}
|
||||
authors_by_topic = {}
|
||||
followers_by_topic = {}
|
||||
shouts_by_topic = {} # Shout object stored
|
||||
authors_by_topic = {} # User
|
||||
followers_by_topic = {} # User
|
||||
lock = asyncio.Lock()
|
||||
period = 30 * 60 # sec
|
||||
|
||||
@@ -19,38 +17,33 @@ class TopicStat:
|
||||
async def load_stat(session):
|
||||
self = TopicStat
|
||||
shout_topics = session.query(ShoutTopic).all()
|
||||
print("[stat.topics] shout topics amount", len(shout_topics))
|
||||
print("[stat.topics] shouts linked %d times" % len(shout_topics))
|
||||
for shout_topic in shout_topics:
|
||||
|
||||
tpc = shout_topic.topic
|
||||
# shouts by topics
|
||||
topic = shout_topic.topic
|
||||
shout = shout_topic.shout
|
||||
sss = set(self.shouts_by_topic.get(topic, []))
|
||||
shout = session.query(Shout).where(Shout.slug == shout).first()
|
||||
sss.union(
|
||||
[
|
||||
shout,
|
||||
]
|
||||
)
|
||||
self.shouts_by_topic[topic] = list(sss)
|
||||
shout = session.query(Shout).where(Shout.slug == shout_topic.shout).first()
|
||||
self.shouts_by_topic[tpc] = self.shouts_by_topic.get(tpc, [])
|
||||
if shout not in self.shouts_by_topic[tpc]:
|
||||
self.shouts_by_topic[tpc].append(shout)
|
||||
|
||||
# authors by topics
|
||||
authors = await ShoutAuthorStorage.get_authors(shout)
|
||||
aaa = set(self.authors_by_topic.get(topic, []))
|
||||
aaa.union(authors)
|
||||
self.authors_by_topic[topic] = list(aaa)
|
||||
authors = await ShoutAuthorStorage.get_authors(shout.slug)
|
||||
self.authors_by_topic[tpc] = self.authors_by_topic.get(tpc, [])
|
||||
for a in authors:
|
||||
if a not in self.authors_by_topic[tpc]:
|
||||
self.authors_by_topic[tpc].append(a)
|
||||
|
||||
print("[stat.topics] authors sorted")
|
||||
print("[stat.topics] shouts sorted")
|
||||
print("[stat.topics] shouts indexed by %d topics" % len(self.shouts_by_topic.keys()))
|
||||
print("[stat.topics] authors indexed by %d topics" % len(self.authors_by_topic.keys()))
|
||||
|
||||
self.followers_by_topic = {}
|
||||
followings = session.query(TopicFollower)
|
||||
followings = session.query(TopicFollower).all()
|
||||
for flw in followings:
|
||||
topic = flw.topic
|
||||
user = flw.follower
|
||||
if topic not in self.followers_by_topic:
|
||||
self.followers_by_topic[topic] = []
|
||||
self.followers_by_topic[topic].append(user)
|
||||
self.followers_by_topic[topic] = self.followers_by_topic.get(topic, [])
|
||||
if user not in self.followers_by_topic[topic]:
|
||||
self.followers_by_topic[topic].append(user)
|
||||
print("[stat.topics] followers sorted")
|
||||
|
||||
@staticmethod
|
||||
@@ -59,23 +52,6 @@ class TopicStat:
|
||||
async with self.lock:
|
||||
return self.shouts_by_topic.get(topic, [])
|
||||
|
||||
@staticmethod
|
||||
async def get_stat(topic):
|
||||
self = TopicStat
|
||||
async with self.lock:
|
||||
shouts = self.shouts_by_topic.get(topic, [])
|
||||
followers = self.followers_by_topic.get(topic, [])
|
||||
authors = self.authors_by_topic.get(topic, [])
|
||||
return {
|
||||
"shouts": len(shouts),
|
||||
"authors": len(authors),
|
||||
"followers": len(followers),
|
||||
"viewed": await ViewedStorage.get_topic(topic),
|
||||
"reacted": len(await ReactedStorage.get_topic(topic)),
|
||||
"commented": len(await ReactedStorage.get_topic_comments(topic)),
|
||||
"rating": await ReactedStorage.get_topic_rating(topic),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
self = TopicStat
|
||||
@@ -84,7 +60,6 @@ class TopicStat:
|
||||
with local_session() as session:
|
||||
async with self.lock:
|
||||
await self.load_stat(session)
|
||||
print("[stat.topics] periodical update")
|
||||
except Exception as err:
|
||||
print("[stat.topics] errror: %s" % (err))
|
||||
raise Exception(err)
|
||||
await asyncio.sleep(self.period)
|
||||
|
Reference in New Issue
Block a user