core/services/viewed.py

212 lines
6.8 KiB
Python
Raw Normal View History

2022-11-18 17:54:37 +00:00
import asyncio
2024-01-13 12:44:56 +00:00
from logging import Logger
import time
2023-12-17 20:30:20 +00:00
from datetime import datetime, timedelta, timezone
2023-10-25 16:55:30 +00:00
from os import environ
2024-01-13 12:44:56 +00:00
import logging
2022-11-18 17:54:37 +00:00
from gql import Client, gql
2023-11-29 07:23:41 +00:00
from gql.transport.aiohttp import AIOHTTPTransport
2022-11-29 12:36:46 +00:00
2023-12-17 20:30:20 +00:00
from orm.shout import Shout, ShoutTopic
2023-11-22 18:23:15 +00:00
from orm.topic import Topic
2023-12-17 20:30:20 +00:00
from services.db import local_session
2022-11-21 22:23:16 +00:00
2024-01-13 12:44:56 +00:00
logging.basicConfig()
logger = logging.getLogger("\t[services.viewed]\t")
logger.setLevel(logging.DEBUG)
2023-10-05 18:46:18 +00:00
load_facts = gql(
2023-10-05 20:18:06 +00:00
""" query getDomains {
2022-11-21 22:23:16 +00:00
domains {
id
title
facts {
activeVisitors
viewsToday
viewsMonth
viewsYear
}
2023-10-05 20:18:06 +00:00
} } """
2023-10-05 18:46:18 +00:00
)
2022-11-21 22:23:16 +00:00
2023-10-05 18:46:18 +00:00
load_pages = gql(
2023-10-05 20:18:06 +00:00
""" query getDomains {
2022-11-21 22:23:16 +00:00
domains {
title
statistics {
pages(sorting: TOP) {
# id
count
# created
value
2022-11-18 17:54:37 +00:00
}
}
2023-10-05 20:18:06 +00:00
} } """
2023-10-05 18:46:18 +00:00
)
2023-10-05 20:18:06 +00:00
2023-10-05 23:03:36 +00:00
schema_str = open("schemas/ackee.graphql").read()
2022-11-21 22:23:16 +00:00
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):
2023-11-29 07:23:41 +00:00
transport = AIOHTTPTransport(
url="https://ackee.discours.io/api",
headers=headers,
2022-11-21 22:23:16 +00:00
)
2023-11-29 07:23:41 +00:00
return Client(schema=schema, transport=transport)
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 = {}
2023-11-03 10:10:22 +00:00
by_reactions = {}
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
2023-01-18 12:43:56 +00:00
period = 60 * 60 # every hour
2023-12-17 20:30:20 +00:00
client: Client | None = 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():
2023-10-05 18:46:18 +00:00
"""graphql client connection using permanent token"""
2022-11-22 07:29:54 +00:00
self = ViewedStorage
async with self.lock:
if token:
2023-12-17 20:30:20 +00:00
self.client = create_client({"Authorization": f"Bearer {token}"}, schema=schema_str)
2024-01-13 12:44:56 +00:00
logger.info(" * authorized permanently by ackee.discours.io: %s" % token)
2023-12-23 05:40:41 +00:00
views_stat_task = asyncio.create_task(self.worker())
2024-01-13 12:44:56 +00:00
logger.info(views_stat_task)
2022-11-22 07:29:54 +00:00
else:
2024-01-13 12:44:56 +00:00
logger.info(" * 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():
2023-10-05 18:46:18 +00:00
"""query all the pages from ackee sorted by views count"""
2024-01-13 12:44:56 +00:00
logger.info(" ⎧ updating ackee pages data ---")
2024-01-13 12:57:35 +00:00
try:
start = time.time()
self = ViewedStorage
if self.client:
# Use asyncio.run to execute asynchronous code in the main entry point
self.pages = await asyncio.to_thread(self.client.execute, load_pages)
domains = self.pages.get("domains", [])
# logger.debug(f" | domains: {domains}")
for domain in domains:
pages = domain.get("statistics", {}).get("pages", [])
if pages:
# logger.debug(f" | pages: {pages}")
shouts = {}
for page in pages:
p = page["value"].split("?")[0]
slug = p.split("discours.io/")[-1]
shouts[slug] = page["count"]
for slug in shouts.keys():
await ViewedStorage.increment(slug, shouts[slug])
logger.info("%d pages collected " % len(shouts.keys()))
end = time.time()
logger.info(" ⎪ update_pages took %fs " % (end - start))
except Exception:
import traceback
traceback.print_exc()
2022-11-21 22:23:16 +00:00
@staticmethod
async def get_facts():
self = ViewedStorage
2023-12-17 20:30:20 +00:00
facts = []
try:
if self.client:
async with self.lock:
2024-01-13 12:57:35 +00:00
facts = await self.client.execute(load_facts)
2023-12-17 20:30:20 +00:00
except Exception as er:
2024-01-13 12:44:56 +00:00
logger.error(f" - get_facts error: {er}")
2023-12-17 20:30:20 +00:00
return facts or []
2022-11-18 17:54:37 +00:00
2022-11-19 11:35:34 +00:00
@staticmethod
async def get_shout(shout_slug):
2023-10-05 18:46:18 +00:00
"""getting shout views metric by slug"""
2022-11-19 11:35:34 +00:00
self = ViewedStorage
async with self.lock:
2023-11-03 10:10:22 +00:00
return self.by_shouts.get(shout_slug, 0)
@staticmethod
async def get_reaction(shout_slug, reaction_id):
"""getting reaction views metric by slug"""
self = ViewedStorage
async with self.lock:
return self.by_reactions.get(shout_slug, {}).get(reaction_id, 0)
2022-11-19 11:35:34 +00:00
2022-11-21 05:18:50 +00:00
@staticmethod
async def get_topic(topic_slug):
2023-10-05 18:46:18 +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
2023-11-22 18:23:15 +00:00
def update_topics(shout_slug):
2023-10-05 18:46:18 +00:00
"""updates topics counters by shout slug"""
2022-11-22 13:58:55 +00:00
self = ViewedStorage
2023-11-22 18:23:15 +00:00
with local_session() as session:
2023-12-17 20:30:20 +00:00
for [_shout_topic, topic] in (
2023-11-22 18:23:15 +00:00
session.query(ShoutTopic, Topic).join(Topic).join(Shout).where(Shout.slug == shout_slug).all()
2023-11-03 10:10:22 +00:00
):
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
2023-10-05 18:46:18 +00:00
async def increment(shout_slug, amount=1, viewer="ackee"):
"""the only way to change views counter"""
2022-11-18 17:54:37 +00:00
self = ViewedStorage
async with self.lock:
2023-11-03 10:10:22 +00:00
self.by_shouts[shout_slug] = self.by_shouts.get(shout_slug, 0) + amount
self.update_topics(shout_slug)
@staticmethod
async def increment_reaction(shout_slug, reaction_id, amount=1, viewer="ackee"):
"""the only way to change views counter"""
self = ViewedStorage
async with self.lock:
self.by_reactions[shout_slug][reaction_id] = self.by_reactions[shout_slug].get(reaction_id, 0) + amount
self.update_topics(shout_slug)
2022-11-18 17:54:37 +00:00
@staticmethod
async def worker():
2023-10-05 18:46:18 +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
2023-10-05 22:45:32 +00:00
2023-01-18 12:43:56 +00:00
while True:
try:
2024-01-13 12:44:56 +00:00
logger.info(" - updating views...")
2023-01-18 12:43:56 +00:00
await self.update_pages()
failed = 0
except Exception:
failed += 1
2024-01-13 12:44:56 +00:00
logger.info(" - update failed #%d, wait 10 seconds" % failed)
2023-01-18 12:43:56 +00:00
if failed > 3:
2024-01-13 12:44:56 +00:00
logger.info(" - not trying to update anymore")
2023-01-18 12:43:56 +00:00
break
if failed == 0:
when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
t = format(when.astimezone().isoformat())
2024-01-13 12:44:56 +00:00
logger.info(" ⎩ next update: %s" % (t.split("T")[0] + " " + t.split("T")[1].split(".")[0]))
2023-01-18 12:43:56 +00:00
await asyncio.sleep(self.period)
else:
await asyncio.sleep(10)
2024-01-13 12:44:56 +00:00
logger.info(" - trying to update data again")