fixes-refactoring-and-views

This commit is contained in:
tonyrewin 2022-11-15 15:04:22 +03:00
parent f089d4903b
commit e52a51fc13
9 changed files with 44 additions and 26 deletions

View File

@ -17,7 +17,7 @@ from resolvers.auth import confirm_email_handler
from services.main import storages_init from services.main import storages_init
from services.stat.reacted import ReactedStorage from services.stat.reacted import ReactedStorage
from services.stat.topicstat import TopicStat from services.stat.topicstat import TopicStat
from services.stat.views import Stat from services.stat.views import ViewStat
from services.zine.gittask import GitTask from services.zine.gittask import GitTask
from services.zine.shoutauthor import ShoutAuthorStorage from services.zine.shoutauthor import ShoutAuthorStorage
import_module("resolvers") import_module("resolvers")
@ -31,7 +31,7 @@ middleware = [
async def start_up(): async def start_up():
await redis.connect() await redis.connect()
views_stat_task = asyncio.create_task(Stat.worker()) views_stat_task = asyncio.create_task(ViewStat.worker())
print(views_stat_task) print(views_stat_task)
reacted_storage_task = asyncio.create_task(ReactedStorage.worker()) reacted_storage_task = asyncio.create_task(ReactedStorage.worker())
print(reacted_storage_task) print(reacted_storage_task)

View File

@ -12,7 +12,7 @@ from orm.shout import Shout, ShoutTopic, ShoutReactionsFollower
from orm.user import User from orm.user import User
from orm.topic import TopicFollower from orm.topic import TopicFollower
from services.stat.reacted import ReactedStorage from services.stat.reacted import ReactedStorage
from services.stat.views import Stat from services.stat.views import ViewStat
OLD_DATE = "2016-03-05 22:22:00.350000" OLD_DATE = "2016-03-05 22:22:00.350000"
ts = datetime.now() ts = datetime.now()
@ -340,7 +340,7 @@ async def migrate(entry, storage):
raise Exception("[migration] content_item.ratings error: \n%r" % content_rating) raise Exception("[migration] content_item.ratings error: \n%r" % content_rating)
# shout views # shout views
await Stat.increment(shout_dict["slug"], amount=entry.get("views", 1)) await ViewStat.increment(shout_dict["slug"], amount=entry.get("views", 1))
# del shout_dict['ratings'] # del shout_dict['ratings']
shout_dict["oid"] = entry.get("_id") shout_dict["oid"] = entry.get("_id")
storage["shouts"]["by_oid"][entry["_id"]] = shout_dict storage["shouts"]["by_oid"][entry["_id"]] = shout_dict

View File

@ -13,7 +13,7 @@ from services.stat.reacted import ReactedStorage
async def get_reaction_stat(reaction_id): async def get_reaction_stat(reaction_id):
return { return {
# "viewed": await Stat.get_reaction(reaction_id), # "viewed": await ViewStat.get_reaction(reaction_id),
"reacted": len(await ReactedStorage.get_reaction(reaction_id)), "reacted": len(await ReactedStorage.get_reaction(reaction_id)),
"rating": await ReactedStorage.get_reaction_rating(reaction_id), "rating": await ReactedStorage.get_reaction_rating(reaction_id),
"commented": len(await ReactedStorage.get_reaction_comments(reaction_id)), "commented": len(await ReactedStorage.get_reaction_comments(reaction_id)),

View File

@ -9,7 +9,7 @@ from orm.topic import Topic, TopicFollower
from services.zine.topics import TopicStorage from services.zine.topics import TopicStorage
from services.stat.reacted import ReactedStorage from services.stat.reacted import ReactedStorage
from services.stat.topicstat import TopicStat from services.stat.topicstat import TopicStat
from services.stat.views import Stat from services.stat.views import ViewStat
async def get_topic_stat(slug): async def get_topic_stat(slug):
@ -17,7 +17,7 @@ async def get_topic_stat(slug):
"shouts": len(TopicStat.shouts_by_topic.get(slug, {}).keys()), "shouts": len(TopicStat.shouts_by_topic.get(slug, {}).keys()),
"authors": len(TopicStat.authors_by_topic.get(slug, {}).keys()), "authors": len(TopicStat.authors_by_topic.get(slug, {}).keys()),
"followers": len(TopicStat.followers_by_topic.get(slug, {}).keys()), "followers": len(TopicStat.followers_by_topic.get(slug, {}).keys()),
"viewed": await Stat.get_topic(slug), "viewed": await ViewStat.get_topic(slug),
"reacted": len(await ReactedStorage.get_topic(slug)), "reacted": len(await ReactedStorage.get_topic(slug)),
"commented": len(await ReactedStorage.get_topic_comments(slug)), "commented": len(await ReactedStorage.get_topic_comments(slug)),
"rating": await ReactedStorage.get_topic_rating(slug) "rating": await ReactedStorage.get_topic_rating(slug)

View File

@ -67,7 +67,7 @@ async def load_shouts_by(_, info, by, amount=50, offset=0):
q = q.group_by(Shout.id, Reaction.id).order_by( q = q.group_by(Shout.id, Reaction.id).order_by(
desc(by.get("order") or "createdAt") desc(by.get("order") or "createdAt")
).limit(amount).offset(offset) ).limit(amount).offset(offset)
print(q)
shouts = [] shouts = []
with local_session() as session: with local_session() as session:
# post query stats and author's captions # post query stats and author's captions

View File

@ -1,4 +1,3 @@
from services.stat.views import Stat
from services.stat.reacted import ReactedStorage from services.stat.reacted import ReactedStorage
from services.auth.roles import RoleStorage from services.auth.roles import RoleStorage
from services.auth.users import UserStorage from services.auth.users import UserStorage
@ -10,7 +9,6 @@ from base.orm import local_session
async def storages_init(): async def storages_init():
with local_session() as session: with local_session() as session:
print('[main] initialize storages') print('[main] initialize storages')
await Stat.update()
ReactedStorage.init(session) ReactedStorage.init(session)
RoleStorage.init(session) RoleStorage.init(session)
UserStorage.init(session) UserStorage.init(session)

View File

@ -9,6 +9,7 @@ class SearchService:
@staticmethod @staticmethod
async def init(session): async def init(session):
async with SearchService.lock: async with SearchService.lock:
print('[search.service] init')
SearchService.cache = {} SearchService.cache = {}
@staticmethod @staticmethod

View File

@ -2,7 +2,7 @@ import asyncio
from base.orm import local_session from base.orm import local_session
from orm.reaction import ReactionKind, Reaction from orm.reaction import ReactionKind, Reaction
from services.zine.topics import TopicStorage from services.zine.topics import TopicStorage
from services.stat.views import Stat from services.stat.views import ViewStat
def kind_to_rate(kind) -> int: def kind_to_rate(kind) -> int:
@ -36,7 +36,7 @@ class ReactedStorage:
@staticmethod @staticmethod
async def get_shout_stat(slug): async def get_shout_stat(slug):
return { return {
"viewed": await Stat.get_shout(slug), "viewed": await ViewStat.get_shout(slug),
"reacted": len(await ReactedStorage.get_shout(slug)), "reacted": len(await ReactedStorage.get_shout(slug)),
"commented": len(await ReactedStorage.get_comments(slug)), "commented": len(await ReactedStorage.get_comments(slug)),
"rating": await ReactedStorage.get_rating(slug), "rating": await ReactedStorage.get_rating(slug),

View File

@ -1,7 +1,8 @@
from gql import gql, Client from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport from gql.transport.aiohttp import AIOHTTPTransport
import asyncio import asyncio
import json
from base.redis import redis
from services.zine.topics import TopicStorage from services.zine.topics import TopicStorage
query_ackee_views = gql( query_ackee_views = gql(
@ -33,7 +34,7 @@ query_ackee_views = gql(
) )
class Stat: class ViewStat:
lock = asyncio.Lock() lock = asyncio.Lock()
by_slugs = {} by_slugs = {}
by_topics = {} by_topics = {}
@ -46,9 +47,16 @@ class Stat:
# TODO: when the struture of paylod will be transparent # TODO: when the struture of paylod will be transparent
# TODO: perhaps ackee token getting here # TODO: perhaps ackee token getting here
self = Stat self = ViewStat
async with self.lock: async with self.lock:
domains = self.client.execute(query_ackee_views) self.by_topics = await redis.execute("GET", "views_by_topics")
if self.by_topics:
self.by_topics = json.loads(self.by_topics)
self.by_slugs = await redis.execute("GET", "views_by_shouts")
if self.by_slugs:
self.by_slugs = json.loads(self.by_slugs)
domains = await self.client.execute_async(query_ackee_views)
print("[stat.ackee] loaded domains") print("[stat.ackee] loaded domains")
print(domains) print(domains)
@ -56,13 +64,13 @@ class Stat:
@staticmethod @staticmethod
async def get_shout(shout_slug): async def get_shout(shout_slug):
self = Stat self = ViewStat
async with self.lock: async with self.lock:
return self.by_slugs.get(shout_slug) or 0 return self.by_slugs.get(shout_slug) or 0
@staticmethod @staticmethod
async def get_topic(topic_slug): async def get_topic(topic_slug):
self = Stat self = ViewStat
async with self.lock: async with self.lock:
shouts = self.by_topics.get(topic_slug) shouts = self.by_topics.get(topic_slug)
topic_views = 0 topic_views = 0
@ -72,28 +80,39 @@ class Stat:
@staticmethod @staticmethod
async def increment(shout_slug, amount=1): async def increment(shout_slug, amount=1):
self = Stat self = ViewStat
async with self.lock: async with self.lock:
self.by_slugs[shout_slug] = self.by_slugs.get(shout_slug) or 0 self.by_slugs[shout_slug] = self.by_slugs.get(shout_slug) or 0
self.by_slugs[shout_slug] += amount self.by_slugs[shout_slug] += amount
await redis.execute(
"SET",
f"views_by_shouts/{shout_slug}",
str(self.by_slugs[shout_slug])
)
shout_topics = await TopicStorage.get_topics_by_slugs([shout_slug, ]) shout_topics = await TopicStorage.get_topics_by_slugs([shout_slug, ])
for t in shout_topics: for t in shout_topics:
self.by_topics[t] = self.by_topics.get(t) or {} self.by_topics[t] = self.by_topics.get(t) or {}
self.by_topics[t][shout_slug] = self.by_topics[t].get(shout_slug) or 0 self.by_topics[t][shout_slug] = self.by_topics[t].get(shout_slug) or 0
self.by_topics[t][shout_slug] += amount self.by_topics[t][shout_slug] += amount
await redis.execute(
"SET",
f"views_by_topics/{t}/{shout_slug}",
str(self.by_topics[t][shout_slug])
)
@staticmethod @staticmethod
async def update(): async def reset():
self = Stat self = ViewStat
async with self.lock: self.by_topics = {}
self.load_views() self.by_slugs = {}
@staticmethod @staticmethod
async def worker(): async def worker():
self = ViewStat
while True: while True:
try: try:
await Stat.update() await self.load_views()
except Exception as err: except Exception as err:
print("[stat.ackee] : %s" % (err)) print("[stat.ackee] : %s" % (err))
print("[stat.ackee] renew period: %d minutes" % (Stat.period / 60)) print("[stat.ackee] renew period: %d minutes" % (ViewStat.period / 60))
await asyncio.sleep(Stat.period) await asyncio.sleep(self.period)