stat-fix
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user