Revert "Feature/lint"

This commit is contained in:
Kosta
2023-10-27 00:07:35 +03:00
committed by GitHub
parent 05136699ee
commit b142949805
70 changed files with 1465 additions and 1223 deletions

View File

@@ -1,17 +1,18 @@
from base.orm import local_session
from datetime import datetime, timedelta, timezone
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from orm import Topic
from orm.shout import Shout, ShoutTopic
import asyncio
import time
from datetime import timedelta, timezone, datetime
from os import environ, path
from ssl import create_default_context
import asyncio
import time
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from sqlalchemy import func
load_facts = gql(
"""
from base.orm import local_session
from orm import User, Topic
from orm.shout import ShoutTopic, Shout
load_facts = gql("""
query getDomains {
domains {
id
@@ -24,11 +25,9 @@ query getDomains {
}
}
}
"""
)
""")
load_pages = gql(
"""
load_pages = gql("""
query getDomains {
domains {
title
@@ -42,9 +41,8 @@ query getDomains {
}
}
}
"""
)
schema_str = open(path.dirname(__file__) + "/ackee.graphql").read()
""")
schema_str = open(path.dirname(__file__) + '/ackee.graphql').read()
token = environ.get("ACKEE_TOKEN", "")
@@ -54,8 +52,8 @@ def create_client(headers=None, schema=None):
transport=AIOHTTPTransport(
url="https://ackee.discours.io/api",
ssl=create_default_context(),
headers=headers,
),
headers=headers
)
)
@@ -73,13 +71,13 @@ class ViewedStorage:
@staticmethod
async def init():
"""graphql client connection using permanent token"""
""" graphql client connection using permanent token """
self = ViewedStorage
async with self.lock:
if token:
self.client = create_client(
{"Authorization": "Bearer %s" % str(token)}, schema=schema_str
)
self.client = create_client({
"Authorization": "Bearer %s" % str(token)
}, schema=schema_str)
print("[stat.viewed] * authorized permanentely by ackee.discours.io: %s" % token)
else:
print("[stat.viewed] * please set ACKEE_TOKEN")
@@ -87,7 +85,7 @@ class ViewedStorage:
@staticmethod
async def update_pages():
"""query all the pages from ackee sorted by views count"""
""" query all the pages from ackee sorted by views count """
print("[stat.viewed] ⎧ updating ackee pages data ---")
start = time.time()
self = ViewedStorage
@@ -98,7 +96,7 @@ class ViewedStorage:
try:
for page in self.pages:
p = page["value"].split("?")[0]
slug = p.split("discours.io/")[-1]
slug = p.split('discours.io/')[-1]
shouts[slug] = page["count"]
for slug in shouts.keys():
await ViewedStorage.increment(slug, shouts[slug])
@@ -120,7 +118,7 @@ class ViewedStorage:
# unused yet
@staticmethod
async def get_shout(shout_slug):
"""getting shout views metric by slug"""
""" getting shout views metric by slug """
self = ViewedStorage
async with self.lock:
shout_views = self.by_shouts.get(shout_slug)
@@ -138,7 +136,7 @@ class ViewedStorage:
@staticmethod
async def get_topic(topic_slug):
"""getting topic views value summed"""
""" getting topic views value summed """
self = ViewedStorage
topic_views = 0
async with self.lock:
@@ -148,28 +146,24 @@ class ViewedStorage:
@staticmethod
def update_topics(session, shout_slug):
"""updates topics counters by shout slug"""
""" updates topics counters by shout slug """
self = ViewedStorage
for [shout_topic, topic] in (
session.query(ShoutTopic, Topic)
.join(Topic)
.join(Shout)
.where(Shout.slug == shout_slug)
.all()
):
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="ackee"):
"""the only way to change views counter"""
async def increment(shout_slug, amount=1, viewer='ackee'):
""" the only way to change views counter """
self = ViewedStorage
async with self.lock:
# TODO optimize, currenty we execute 1 DB transaction per shout
with local_session() as session:
shout = session.query(Shout).where(Shout.slug == shout_slug).one()
if viewer == "old-discours":
if viewer == 'old-discours':
# this is needed for old db migration
if shout.viewsOld == amount:
print(f"viewsOld amount: {amount}")
@@ -191,7 +185,7 @@ class ViewedStorage:
@staticmethod
async def worker():
"""async task worker"""
""" async task worker """
failed = 0
self = ViewedStorage
if self.disabled:
@@ -211,10 +205,9 @@ class ViewedStorage:
if failed == 0:
when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
t = format(when.astimezone().isoformat())
print(
"[stat.viewed] ⎩ next update: %s"
% (t.split("T")[0] + " " + t.split("T")[1].split(".")[0])
)
print("[stat.viewed] ⎩ next update: %s" % (
t.split("T")[0] + " " + t.split("T")[1].split(".")[0]
))
await asyncio.sleep(self.period)
else:
await asyncio.sleep(10)