stat-fix
This commit is contained in:
parent
f64d0a09a8
commit
0b4c0faa79
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user