diff --git a/resolvers/zine/reactions.py b/resolvers/zine/reactions.py index c6d801c2..2558187f 100644 --- a/resolvers/zine/reactions.py +++ b/resolvers/zine/reactions.py @@ -199,33 +199,13 @@ async def delete_reaction(_, info, rid): return {} -@query.field("loadReactionsBy") -async def load_reactions_by(_, info, by, limit=50, offset=0): - """ - :param by: { - shout: 'some-slug' - author: 'discours', - topic: 'culture', - body: 'something else', - stat: 'rating' | 'comments' | 'reacted' | 'views', - days: 30 - } - :param limit: int amount of shouts - :param offset: int offset in this order - :return: Reaction[] - """ - - q = select(Reaction).join( - Shout - ).where( - Reaction.deletedAt.is_(None) - ) - if by.get("slug"): +def prepare_reactions(q, by, user=None): + if by.get("shout"): q = q.filter(Shout.slug == by["slug"]) else: if by.get("reacted"): - user = info.context["request"].user - q = q.filter(Reaction.createdBy == user.slug) + if user: + q = q.filter(Reaction.createdBy == user.slug) if by.get("author"): q = q.filter(Reaction.createdBy == by["author"]) if by.get("topic"): @@ -240,8 +220,40 @@ async def load_reactions_by(_, info, by, limit=50, offset=0): q = q.filter(Reaction.createdAt > before) q = q.group_by(Reaction.id).order_by( - desc(by.get("order") or Reaction.createdAt) - ).limit(limit).offset(offset) + desc(by.get("sort") or Reaction.createdAt) + ) + return q + + +@query.field("loadReactionsBy") +async def load_reactions_by(_, info, by, limit=50, offset=0): + """ + :param by: { + shout: 'some-slug' + author: 'discours', + topic: 'culture', + body: 'something else' | true, + sort: 'rating' | 'comments' | 'reacted' | 'views', + days: 30 + } + :param limit: int amount of shouts + :param offset: int offset in this order + :return: Reaction[] + """ + user = None + try: + user = info.context["request"].user + except Exception: + pass + + q = select(Reaction).join( + Shout, + Reaction.shout == Shout.slug + ).where( + Reaction.deletedAt.is_(None) + ) + q = prepare_reactions(q, by, user) + q = q.limit(limit).offset(offset) rrr = [] with local_session() as session: diff --git a/schema.graphql b/schema.graphql index 0a6385bb..d31c8fe8 100644 --- a/schema.graphql +++ b/schema.graphql @@ -255,7 +255,7 @@ input ReactionBy { author: String order: String days: Int - stat: String + sort: String } ################################### Query diff --git a/services/stat/viewed.py b/services/stat/viewed.py index eaab0438..7fe1b3b3 100644 --- a/services/stat/viewed.py +++ b/services/stat/viewed.py @@ -99,18 +99,20 @@ class ViewedStorage: period = 24 * 60 * 60 # one time a day client = None auth_result = None + disabled = False @staticmethod async def init(): - if token: - self = ViewedStorage - async with self.lock: + self = ViewedStorage + async with self.lock: + if token: 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") + else: + print("[stat.viewed] please set ACKEE_TOKEN") + self.disabled = True @staticmethod async def update(session): @@ -203,26 +205,29 @@ class ViewedStorage: @staticmethod async def worker(): - self = ViewedStorage failed = 0 - while True: - try: - with local_session() as session: - # await self.update(session) - await self.update_pages(session) - failed = 0 - except Exception: - failed += 1 - print("[stat.viewed] update failed #%d, wait 10 seconds" % failed) - if failed > 3: - print("[stat.viewed] not trying to update anymore") - break - if failed == 0: - when = datetime.now(timezone.utc) + timedelta(seconds=self.period) - t = format(when.astimezone().isoformat()) - t = t.split("T")[0] + " " + t.split("T")[1].split(".")[0] - print("[stat.viewed] next update: %s" % t) - await asyncio.sleep(self.period) - else: - await asyncio.sleep(10) - print("[stat.viewed] trying to update data again...") + self = ViewedStorage + if self.disabled: + return + async with self.lock: + while True: + try: + with local_session() as session: + # await self.update(session) + await self.update_pages(session) + failed = 0 + except Exception: + failed += 1 + print("[stat.viewed] update failed #%d, wait 10 seconds" % failed) + if failed > 3: + print("[stat.viewed] not trying to update anymore") + break + if failed == 0: + when = datetime.now(timezone.utc) + timedelta(seconds=self.period) + t = format(when.astimezone().isoformat()) + t = t.split("T")[0] + " " + t.split("T")[1].split(".")[0] + print("[stat.viewed] next update: %s" % t) + await asyncio.sleep(self.period) + else: + await asyncio.sleep(10) + print("[stat.viewed] trying to update data again...")