schemaup
This commit is contained in:
@@ -1,72 +0,0 @@
|
||||
import asyncio
|
||||
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
|
||||
|
||||
|
||||
class TopicStat:
|
||||
# by slugs
|
||||
shouts_by_topic = {} # Shout object stored
|
||||
authors_by_topic = {} # User
|
||||
followers_by_topic = {} # User
|
||||
#
|
||||
lock = asyncio.Lock()
|
||||
period = 30 * 60 # sec
|
||||
|
||||
@staticmethod
|
||||
async def load_stat(session):
|
||||
print("[stat.topics] ⎧ loading stat -------")
|
||||
ts = time.time()
|
||||
self = TopicStat
|
||||
shout_topics = session.query(ShoutTopic, Shout).join(Shout).all() # ~ 10 secs
|
||||
print("[stat.topics] ⎪ shout topics joined query took %fs " % (time.time() - ts))
|
||||
print("[stat.topics] ⎪ indexing %d links..." % len(shout_topics))
|
||||
for [shout_topic, shout] in shout_topics:
|
||||
tpc = shout_topic.topic
|
||||
self.shouts_by_topic[tpc] = self.shouts_by_topic.get(tpc, dict())
|
||||
self.shouts_by_topic[tpc][shout.slug] = shout
|
||||
self.authors_by_topic[tpc] = self.authors_by_topic.get(tpc, dict())
|
||||
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()
|
||||
print("[stat.topics] ⎪ indexing %d followings..." % len(followings))
|
||||
for flw in followings:
|
||||
topic = flw.topic
|
||||
userslug = flw.follower
|
||||
self.followers_by_topic[topic] = self.followers_by_topic.get(topic, dict())
|
||||
self.followers_by_topic[topic][userslug] = userslug
|
||||
|
||||
@staticmethod
|
||||
async def get_shouts(topic):
|
||||
self = TopicStat
|
||||
async with self.lock:
|
||||
return self.shouts_by_topic.get(topic, dict())
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
self = TopicStat
|
||||
first_run = True
|
||||
while True:
|
||||
try:
|
||||
with local_session() as session:
|
||||
ts = time.time()
|
||||
async with self.lock:
|
||||
await self.load_stat(session)
|
||||
print("[stat.topics] ⎩ load_stat took %fs " % (time.time() - ts))
|
||||
except Exception as err:
|
||||
raise Exception(err)
|
||||
if first_run:
|
||||
# sleep for period + 1 min after first run
|
||||
# to distribute load on server by workers with the same period
|
||||
await asyncio.sleep(60)
|
||||
first_run = False
|
||||
await asyncio.sleep(self.period)
|
@@ -5,7 +5,9 @@ from gql import Client, gql
|
||||
from gql.transport.aiohttp import AIOHTTPTransport
|
||||
from base.orm import local_session
|
||||
from sqlalchemy import func
|
||||
from orm.shout import ShoutTopic
|
||||
|
||||
from orm import User, Topic
|
||||
from orm.shout import ShoutTopic, Shout
|
||||
from orm.viewed import ViewedEntry
|
||||
from ssl import create_default_context
|
||||
from os import environ, path
|
||||
@@ -113,6 +115,7 @@ class ViewedStorage:
|
||||
async with self.lock:
|
||||
return self.client.execute_async(load_facts)
|
||||
|
||||
# unused yet
|
||||
@staticmethod
|
||||
async def get_shout(shout_slug):
|
||||
""" getting shout views metric by slug """
|
||||
@@ -123,8 +126,9 @@ class ViewedStorage:
|
||||
shout_views = 0
|
||||
with local_session() as session:
|
||||
try:
|
||||
shout = session.query(Shout).where(Shout.slug == shout_slug).one()
|
||||
shout_views = session.query(func.sum(ViewedEntry.amount)).where(
|
||||
ViewedEntry.shout == shout_slug
|
||||
ViewedEntry.shout == shout.id
|
||||
).all()[0][0]
|
||||
self.by_shouts[shout_slug] = shout_views
|
||||
self.update_topics(session, shout_slug)
|
||||
@@ -147,11 +151,12 @@ class ViewedStorage:
|
||||
def update_topics(session, shout_slug):
|
||||
""" updates topics counters by shout slug """
|
||||
self = ViewedStorage
|
||||
for t in session.query(ShoutTopic).where(ShoutTopic.shout == shout_slug).all():
|
||||
tpc = t.topic
|
||||
if not self.by_topics.get(tpc):
|
||||
self.by_topics[tpc] = {}
|
||||
self.by_topics[tpc][shout_slug] = self.by_shouts[shout_slug]
|
||||
for [shout_topic, topic] in session.query(ShoutTopic, Topic).join(Topic).join(Shout).where(
|
||||
Shout.slug == shout_slug
|
||||
).all():
|
||||
if not self.by_topics.get(topic.slug):
|
||||
self.by_topics[topic.slug] = {}
|
||||
self.by_topics[topic.slug][shout_slug] = self.by_shouts[shout_slug]
|
||||
|
||||
@staticmethod
|
||||
async def increment(shout_slug, amount=1, viewer='anonymous'):
|
||||
@@ -159,9 +164,12 @@ class ViewedStorage:
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
with local_session() as session:
|
||||
shout = session.query(Shout).where(Shout.slug == shout_slug).one()
|
||||
viewer = session.query(User).where(User.slug == viewer).one()
|
||||
|
||||
viewed = ViewedEntry.create(**{
|
||||
"viewer": viewer,
|
||||
"shout": shout_slug,
|
||||
"viewerId": viewer.id,
|
||||
"shout": shout.id,
|
||||
"amount": amount
|
||||
})
|
||||
session.add(viewed)
|
||||
|
Reference in New Issue
Block a user