This commit is contained in:
Untone 2024-04-23 15:14:59 +03:00
parent f64d0a09a8
commit 0b4c0faa79
3 changed files with 10 additions and 39 deletions

View File

@ -1,5 +1,4 @@
from sqlalchemy import and_, distinct, func, select from sqlalchemy import and_, distinct, func, select
from sqlalchemy.orm import aliased
from orm.author import Author from orm.author import Author
from orm.community import Community, CommunityAuthor from orm.community import Community, CommunityAuthor
@ -9,31 +8,18 @@ from services.logger import root_logger as logger
from services.schema import query 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): def get_communities_from_query(q):
ccc = [] ccc = []
with local_session() as session: with local_session() as session:
for [c, shouts_stat, followers_stat] in session.execute(q): for [c, shouts_stat, followers_stat] in session.execute(q):
c.stat = { c.stat = {
"shouts": shouts_stat, "shouts": session.execute(
"followers": followers_stat, 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, # "commented": commented_stat,
} }
ccc.append(c) ccc.append(c)
@ -75,7 +61,6 @@ def community_unfollow(follower_id, slug):
@query.field("get_communities_all") @query.field("get_communities_all")
async def get_communities_all(_, _info): async def get_communities_all(_, _info):
q = select(Author) q = select(Author)
q = add_community_stat_columns(q)
return get_communities_from_query(q) return get_communities_from_query(q)
@ -83,7 +68,6 @@ async def get_communities_all(_, _info):
@query.field("get_community") @query.field("get_community")
async def get_community(_, _info, slug: str): async def get_community(_, _info, slug: str):
q = select(Community).where(Community.slug == slug) q = select(Community).where(Community.slug == slug)
q = add_community_stat_columns(q)
communities = get_communities_from_query(q) communities = get_communities_from_query(q)
return communities[0] return communities[0]

View File

@ -168,10 +168,7 @@ def get_with_stat(q):
entity.stat = stat entity.stat = stat
records.append(entity) records.append(entity)
except Exception as exc: except Exception as exc:
import traceback logger.error(exc, exc_info=True)
logger.error(exc, traceback.format_exc())
raise Exception(exc)
return records return records

View File

@ -1,5 +1,4 @@
import logging import logging
import colorlog import colorlog
# Define the color scheme # Define the color scheme
@ -9,6 +8,7 @@ color_scheme = {
"WARNING": "yellow", "WARNING": "yellow",
"ERROR": "red", "ERROR": "red",
"CRITICAL": "red,bg_white", "CRITICAL": "red,bg_white",
"DEFAULT": "white",
} }
# Define secondary log colors # Define secondary log colors
@ -17,7 +17,7 @@ secondary_colors = {
"asctime": {"DEBUG": "cyan"}, "asctime": {"DEBUG": "cyan"},
"process": {"DEBUG": "purple"}, "process": {"DEBUG": "purple"},
"module": {"DEBUG": "cyan,bg_blue"}, "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 # Define the log format string
@ -60,16 +60,6 @@ formatter = MultilineColoredFormatter(fmt_string, **fmt_config)
stream = logging.StreamHandler() stream = logging.StreamHandler()
stream.setFormatter(formatter) 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 # Set up the root logger with the same formatting
root_logger = logging.getLogger() root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG) root_logger.setLevel(logging.DEBUG)