core/services/stat/viewed.py

209 lines
6.8 KiB
Python
Raw Normal View History

2022-11-18 17:54:37 +00:00
import asyncio
import time
2022-11-21 22:23:16 +00:00
from datetime import timedelta, timezone, datetime
2022-11-18 17:54:37 +00:00
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from base.orm import local_session
2022-11-23 11:30:44 +00:00
from sqlalchemy import func
2022-11-29 12:36:46 +00:00
from orm import User, Topic
from orm.shout import ShoutTopic, Shout
2022-11-18 17:54:37 +00:00
from orm.viewed import ViewedEntry
from ssl import create_default_context
2022-11-21 22:23:16 +00:00
from os import environ, path
load_facts = gql("""
query getDomains {
domains {
id
title
facts {
activeVisitors
viewsToday
viewsMonth
viewsYear
}
}
}
""")
load_pages = gql("""
query getDomains {
domains {
title
statistics {
pages(sorting: TOP) {
# id
count
# created
value
2022-11-18 17:54:37 +00:00
}
}
}
2022-11-21 22:23:16 +00:00
}
""")
schema_str = open(path.dirname(__file__) + '/ackee.graphql').read()
token = environ.get("ACKEE_TOKEN", "")
2022-11-18 17:54:37 +00:00
2022-11-21 22:23:16 +00:00
def create_client(headers=None, schema=None):
return Client(
schema=schema,
transport=AIOHTTPTransport(
url="https://ackee.discours.io/api",
ssl=create_default_context(),
headers=headers
)
)
2022-11-18 17:54:37 +00:00
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-21 22:23:16 +00:00
views = None
2022-11-22 13:58:55 +00:00
pages = None
2022-11-21 22:23:16 +00:00
domains = None
period = 24 * 60 * 60 # one time a day
2022-11-18 17:54:37 +00:00
client = None
2022-11-21 22:23:16 +00:00
auth_result = None
2022-11-22 07:29:54 +00:00
disabled = False
2022-11-18 17:54:37 +00:00
2022-11-20 07:48:40 +00:00
@staticmethod
2022-11-21 22:23:16 +00:00
async def init():
2022-11-22 13:58:55 +00:00
""" graphql client connection using permanent token """
2022-11-22 07:29:54 +00:00
self = ViewedStorage
async with self.lock:
if token:
2022-11-22 06:20:01 +00:00
self.client = create_client({
"Authorization": "Bearer %s" % str(token)
}, schema=schema_str)
2022-11-28 08:47:39 +00:00
print("[stat.viewed] * authorized permanentely by ackee.discours.io: %s" % token)
2022-11-22 07:29:54 +00:00
else:
2022-11-28 08:47:39 +00:00
print("[stat.viewed] * please set ACKEE_TOKEN")
2022-11-22 07:29:54 +00:00
self.disabled = True
2022-11-20 07:48:40 +00:00
2022-11-21 22:23:16 +00:00
@staticmethod
async def update_pages():
2022-11-22 13:58:55 +00:00
""" query all the pages from ackee sorted by views count """
2022-11-28 14:55:30 +00:00
print("[stat.viewed] ⎧ updating ackee pages data ---")
start = time.time()
2022-11-20 07:48:40 +00:00
self = ViewedStorage
2022-11-26 07:46:06 +00:00
try:
self.pages = await self.client.execute_async(load_pages)
self.pages = self.pages["domains"][0]["statistics"]["pages"]
shouts = {}
2022-11-21 22:23:16 +00:00
try:
2022-11-26 07:46:06 +00:00
for page in self.pages:
p = page["value"].split("?")[0]
slug = p.split('discours.io/')[-1]
shouts[slug] = page["count"]
for slug, v in shouts:
await ViewedStorage.increment(slug, v)
except Exception:
pass
2022-11-28 08:47:39 +00:00
print("[stat.viewed] ⎪ %d pages collected " % len(shouts.keys()))
2022-11-26 07:46:06 +00:00
except Exception as e:
raise e
2022-11-21 22:23:16 +00:00
end = time.time()
2022-11-28 08:47:39 +00:00
print("[stat.viewed] ⎪ update_pages took %fs " % (end - start))
2022-11-21 22:23:16 +00:00
@staticmethod
async def get_facts():
self = ViewedStorage
async with self.lock:
return self.client.execute_async(load_facts)
2022-11-18 17:54:37 +00:00
2022-11-29 12:36:46 +00:00
# unused yet
2022-11-19 11:35:34 +00:00
@staticmethod
async def get_shout(shout_slug):
2022-11-22 13:58:55 +00:00
""" getting shout views metric by slug """
2022-11-19 11:35:34 +00:00
self = ViewedStorage
async with self.lock:
2022-11-22 13:58:55 +00:00
shout_views = self.by_shouts.get(shout_slug)
if not shout_views:
shout_views = 0
2022-11-19 11:35:34 +00:00
with local_session() as session:
2022-11-23 11:30:44 +00:00
try:
2022-11-29 12:36:46 +00:00
shout = session.query(Shout).where(Shout.slug == shout_slug).one()
2022-11-23 11:30:44 +00:00
shout_views = session.query(func.sum(ViewedEntry.amount)).where(
2022-11-30 06:27:12 +00:00
ViewedEntry.shout == shout.id
2022-11-23 11:30:44 +00:00
).all()[0][0]
self.by_shouts[shout_slug] = shout_views
self.update_topics(session, shout_slug)
except Exception as e:
raise e
2022-11-22 13:58:55 +00:00
return shout_views
2022-11-19 11:35:34 +00:00
2022-11-21 05:18:50 +00:00
@staticmethod
async def get_topic(topic_slug):
2022-11-22 13:58:55 +00:00
""" getting topic views value summed """
2022-11-21 05:18:50 +00:00
self = ViewedStorage
topic_views = 0
async with self.lock:
2022-11-22 13:58:55 +00:00
for shout_slug in self.by_topics.get(topic_slug, {}).keys():
topic_views += self.by_topics[topic_slug].get(shout_slug, 0)
2022-11-21 05:18:50 +00:00
return topic_views
2022-11-22 13:58:55 +00:00
@staticmethod
def update_topics(session, shout_slug):
""" updates topics counters by shout slug """
self = ViewedStorage
2022-11-29 12:36:46 +00:00
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]
2022-11-22 13:58:55 +00:00
2022-11-18 17:54:37 +00:00
@staticmethod
async def increment(shout_slug, amount=1, viewer='anonymous'):
2022-11-22 13:58:55 +00:00
""" the only way to change views counter """
2022-11-18 17:54:37 +00:00
self = ViewedStorage
async with self.lock:
with local_session() as session:
2022-11-29 12:36:46 +00:00
shout = session.query(Shout).where(Shout.slug == shout_slug).one()
viewer = session.query(User).where(User.slug == viewer).one()
2022-11-19 11:35:34 +00:00
viewed = ViewedEntry.create(**{
"viewer": viewer.id,
2022-11-30 06:27:12 +00:00
"shout": shout.id,
2022-11-19 11:35:34 +00:00
"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-22 13:58:55 +00:00
self.update_topics(session, shout_slug)
2022-11-18 17:54:37 +00:00
@staticmethod
async def worker():
2022-11-22 13:58:55 +00:00
""" async task worker """
2022-11-21 22:23:16 +00:00
failed = 0
2022-11-22 07:29:54 +00:00
self = ViewedStorage
if self.disabled:
return
async with self.lock:
while True:
try:
2022-11-28 14:55:30 +00:00
print("[stat.viewed] - updating views...")
await self.update_pages()
failed = 0
2022-11-22 07:29:54 +00:00
except Exception:
failed += 1
2022-11-28 14:55:30 +00:00
print("[stat.viewed] - update failed #%d, wait 10 seconds" % failed)
2022-11-22 07:29:54 +00:00
if failed > 3:
2022-11-28 14:55:30 +00:00
print("[stat.viewed] - not trying to update anymore")
2022-11-22 07:29:54 +00:00
break
if failed == 0:
when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
t = format(when.astimezone().isoformat())
2022-11-28 08:47:39 +00:00
print("[stat.viewed] ⎩ next update: %s" % (
2022-11-22 13:58:55 +00:00
t.split("T")[0] + " " + t.split("T")[1].split(".")[0]
))
2022-11-22 07:29:54 +00:00
await asyncio.sleep(self.period)
else:
await asyncio.sleep(10)
2022-11-28 14:55:30 +00:00
print("[stat.viewed] - trying to update data again")