core/services/stat/viewed.py

126 lines
3.9 KiB
Python
Raw Normal View History

2022-11-18 17:54:37 +00:00
import asyncio
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from base.orm import local_session
2022-11-19 11:35:34 +00:00
from sqlalchemy import func, select
2022-11-21 05:18:50 +00:00
from orm.shout import ShoutTopic
2022-11-18 17:54:37 +00:00
from orm.viewed import ViewedEntry
from ssl import create_default_context
query_ackee_views = gql(
"""
query getDomainsFacts {
domains {
statistics {
views {
id
count
}
pages {
id
count
created
}
}
facts {
activeVisitors
# averageViews
# averageDuration
viewsToday
viewsMonth
viewsYear
}
}
}
"""
)
ssl = create_default_context()
class ViewedStorage:
lock = asyncio.Lock()
2022-11-19 11:35:34 +00:00
by_shouts = {}
2022-11-21 05:18:50 +00:00
by_topics = {}
2022-11-18 17:54:37 +00:00
period = 5 * 60 # 5 minutes
client = None
transport = None
2022-11-20 07:48:40 +00:00
@staticmethod
def init():
ViewedStorage.transport = AIOHTTPTransport(url="https://ackee.discours.io/", ssl=ssl)
ViewedStorage.client = Client(transport=ViewedStorage.transport, fetch_schema_from_transport=True)
2022-11-18 17:54:37 +00:00
@staticmethod
2022-11-19 11:35:34 +00:00
async def update_views(session):
2022-11-18 17:54:37 +00:00
# TODO: when the struture of payload will be transparent
# TODO: perhaps ackee token getting here
2022-11-20 07:48:40 +00:00
self = ViewedStorage
2022-11-18 17:54:37 +00:00
async with self.lock:
domains = await self.client.execute_async(query_ackee_views)
print("[stat.ackee] loaded domains")
print(domains)
print('\n\n# TODO: something here...\n\n')
2022-11-19 11:35:34 +00:00
@staticmethod
async def get_shout(shout_slug):
self = ViewedStorage
async with self.lock:
r = self.by_shouts.get(shout_slug)
2022-11-21 05:18:50 +00:00
if not r:
2022-11-19 11:35:34 +00:00
with local_session() as session:
shout_views = 0
shout_views_q = select(func.sum(ViewedEntry.amount)).where(
ViewedEntry.shout == shout_slug
)
shout_views = session.execute(shout_views_q)
self.by_shouts[shout_slug] = shout_views
return shout_views
else:
return r
2022-11-21 05:18:50 +00:00
@staticmethod
async def get_topic(topic_slug):
self = ViewedStorage
topic_views = 0
async with self.lock:
topic_views_by_shouts = self.by_topics.get(topic_slug) or {}
for shout in topic_views_by_shouts:
topic_views += shout
return topic_views
2022-11-18 17:54:37 +00:00
@staticmethod
async def increment(shout_slug, amount=1, viewer='anonymous'):
self = ViewedStorage
async with self.lock:
with local_session() as session:
2022-11-19 11:35:34 +00:00
viewed = ViewedEntry.create(**{
2022-11-18 17:54:37 +00:00
"viewer": viewer,
2022-11-19 11:35:34 +00:00
"shout": shout_slug,
"amount": amount
2022-11-18 17:54:37 +00:00
})
session.add(viewed)
session.commit()
2022-11-19 11:35:34 +00:00
self.by_shouts[shout_slug] = self.by_shouts.get(shout_slug, 0) + amount
2022-11-21 05:18:50 +00:00
topics = session.query(ShoutTopic).where(ShoutTopic.shout == shout_slug).all()
for t in topics:
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]
2022-11-18 17:54:37 +00:00
@staticmethod
async def worker():
self = ViewedStorage
while True:
try:
with local_session() as session:
2022-11-19 11:35:34 +00:00
await self.update_views(session)
2022-11-20 07:48:40 +00:00
print("[stat.viewed] next renew in %d minutes" % (self.period / 60))
2022-11-18 17:54:37 +00:00
except Exception as err:
2022-11-20 07:48:40 +00:00
print("[stat.viewed] %s" % (err))
2022-11-18 17:54:37 +00:00
await asyncio.sleep(self.period)