2022-07-21 11:58:50 +00:00
|
|
|
import asyncio
|
2022-08-11 05:53:14 +00:00
|
|
|
from base.orm import local_session
|
2022-09-14 13:02:05 +00:00
|
|
|
from orm.shout import Shout
|
2022-08-13 09:48:07 +00:00
|
|
|
from services.stat.reacted import ReactedStorage
|
|
|
|
from services.stat.viewed import ViewedStorage
|
2022-08-11 09:09:57 +00:00
|
|
|
from services.zine.shoutauthor import ShoutAuthorStorage
|
2022-07-21 11:58:50 +00:00
|
|
|
from orm.topic import ShoutTopic, TopicFollower
|
2022-09-03 10:50:14 +00:00
|
|
|
|
|
|
|
|
2022-09-07 19:36:40 +00:00
|
|
|
def unique(list1):
|
|
|
|
|
|
|
|
# insert the list to the set
|
|
|
|
list_set = set(list1)
|
|
|
|
# convert the set to the list
|
|
|
|
unique_list = (list(list_set))
|
|
|
|
return unique_list
|
|
|
|
|
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
class TopicStat:
|
2022-09-03 10:50:14 +00:00
|
|
|
shouts_by_topic = {}
|
|
|
|
authors_by_topic = {}
|
|
|
|
followers_by_topic = {}
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
period = 30 * 60 # sec
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def load_stat(session):
|
|
|
|
self = TopicStat
|
|
|
|
shout_topics = session.query(ShoutTopic).all()
|
2022-09-14 13:02:05 +00:00
|
|
|
print('[stat.topics] shout topics amount', len(shout_topics))
|
2022-09-03 10:50:14 +00:00
|
|
|
for shout_topic in shout_topics:
|
2022-09-14 13:02:05 +00:00
|
|
|
|
|
|
|
# shouts by topics
|
2022-09-03 10:50:14 +00:00
|
|
|
topic = shout_topic.topic
|
|
|
|
shout = shout_topic.shout
|
2022-09-14 13:02:05 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
# authors by topics
|
2022-09-03 10:50:14 +00:00
|
|
|
authors = await ShoutAuthorStorage.get_authors(shout)
|
2022-09-14 13:02:05 +00:00
|
|
|
aaa = set(self.authors_by_topic.get(topic, []))
|
|
|
|
aaa.union(authors)
|
|
|
|
self.authors_by_topic[topic] = list(aaa)
|
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
print("[stat.topics] authors sorted")
|
|
|
|
print("[stat.topics] shouts sorted")
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
self.followers_by_topic = {}
|
|
|
|
followings = session.query(TopicFollower)
|
|
|
|
for flw in followings:
|
|
|
|
topic = flw.topic
|
|
|
|
user = flw.follower
|
2022-09-14 13:02:05 +00:00
|
|
|
if topic not in self.followers_by_topic:
|
|
|
|
self.followers_by_topic[topic] = []
|
|
|
|
self.followers_by_topic[topic].append(user)
|
2022-09-03 10:50:14 +00:00
|
|
|
print("[stat.topics] followers sorted")
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def get_shouts(topic):
|
|
|
|
self = TopicStat
|
|
|
|
async with self.lock:
|
|
|
|
return self.shouts_by_topic.get(topic, [])
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@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, [])
|
2022-09-14 15:56:49 +00:00
|
|
|
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),
|
|
|
|
}
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
|
|
|
async def worker():
|
|
|
|
self = TopicStat
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
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))
|
|
|
|
await asyncio.sleep(self.period)
|