From 0b4c0faa79e8dfe149b01b84feab8a742357663b Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 23 Apr 2024 15:14:59 +0300 Subject: [PATCH] stat-fix --- resolvers/community.py | 30 +++++++----------------------- resolvers/stat.py | 5 +---- services/logger.py | 14 ++------------ 3 files changed, 10 insertions(+), 39 deletions(-) diff --git a/resolvers/community.py b/resolvers/community.py index 60bdb1c8..219b5604 100644 --- a/resolvers/community.py +++ b/resolvers/community.py @@ -1,5 +1,4 @@ from sqlalchemy import and_, distinct, func, select -from sqlalchemy.orm import aliased from orm.author import Author from orm.community import Community, CommunityAuthor @@ -9,31 +8,18 @@ from services.logger import root_logger as logger from services.schema import query -def add_community_stat_columns(q): - community_followers = aliased(CommunityAuthor) - shout_community_aliased = aliased(ShoutCommunity) - - q = q.outerjoin(shout_community_aliased).add_columns( - func.count(distinct(shout_community_aliased.shout)).label("shouts_stat") - ) - q = q.outerjoin( - community_followers, community_followers.author == Author.id - ).add_columns( - func.count(distinct(community_followers.follower)).label("followers_stat") - ) - - q = q.group_by(Author.id) - - return q - - def get_communities_from_query(q): ccc = [] with local_session() as session: for [c, shouts_stat, followers_stat] in session.execute(q): c.stat = { - "shouts": shouts_stat, - "followers": followers_stat, + "shouts": session.execute( + select(func.count(distinct(ShoutCommunity.shout))).filter( + ShoutCommunity.community == c.id + ) + ), + # "authors": session.execute(select(func.count(distinct(ShoutCommunity.shout))).filter(ShoutCommunity.community == c.id)), + # "followers": session.execute(select(func.count(distinct(ShoutCommunity.shout))).filter(ShoutCommunity.community == c.id)), # "commented": commented_stat, } ccc.append(c) @@ -75,7 +61,6 @@ def community_unfollow(follower_id, slug): @query.field("get_communities_all") async def get_communities_all(_, _info): q = select(Author) - q = add_community_stat_columns(q) return get_communities_from_query(q) @@ -83,7 +68,6 @@ async def get_communities_all(_, _info): @query.field("get_community") async def get_community(_, _info, slug: str): q = select(Community).where(Community.slug == slug) - q = add_community_stat_columns(q) communities = get_communities_from_query(q) return communities[0] diff --git a/resolvers/stat.py b/resolvers/stat.py index e50818f0..c340df1d 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -168,10 +168,7 @@ def get_with_stat(q): entity.stat = stat records.append(entity) except Exception as exc: - import traceback - - logger.error(exc, traceback.format_exc()) - raise Exception(exc) + logger.error(exc, exc_info=True) return records diff --git a/services/logger.py b/services/logger.py index cd45f860..3954b7f7 100644 --- a/services/logger.py +++ b/services/logger.py @@ -1,5 +1,4 @@ import logging - import colorlog # Define the color scheme @@ -9,6 +8,7 @@ color_scheme = { "WARNING": "yellow", "ERROR": "red", "CRITICAL": "red,bg_white", + "DEFAULT": "white", } # Define secondary log colors @@ -17,7 +17,7 @@ secondary_colors = { "asctime": {"DEBUG": "cyan"}, "process": {"DEBUG": "purple"}, "module": {"DEBUG": "cyan,bg_blue"}, - "funcName": {"DEBUG": "light_white,bg_blue"}, # Add this line + "funcName": {"DEBUG": "light_white,bg_blue"}, } # Define the log format string @@ -60,16 +60,6 @@ formatter = MultilineColoredFormatter(fmt_string, **fmt_config) stream = logging.StreamHandler() stream.setFormatter(formatter) - -def get_colorful_logger(name="main"): - # Create and configure the logger - logger = logging.getLogger(name) - logger.setLevel(logging.DEBUG) - logger.addHandler(stream) - - return logger - - # Set up the root logger with the same formatting root_logger = logging.getLogger() root_logger.setLevel(logging.DEBUG)