Merge pull request #50 from Discours/prepaare-comments-optimization
more optimizations
This commit is contained in:
@@ -4,12 +4,26 @@ from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy.sql.expression import desc, asc, select, case
|
||||
from base.orm import local_session
|
||||
from base.resolvers import query
|
||||
from orm.shout import Shout
|
||||
from orm import ViewedEntry
|
||||
from orm.shout import Shout, ShoutAuthor
|
||||
from orm.reaction import Reaction, ReactionKind
|
||||
from services.zine.shoutauthor import ShoutAuthorStorage
|
||||
from services.stat.reacted import ReactedStorage
|
||||
|
||||
|
||||
def add_rating_column(q):
|
||||
return q.join(Reaction).add_columns(sa.func.sum(case(
|
||||
(Reaction.kind == ReactionKind.AGREE, 1),
|
||||
(Reaction.kind == ReactionKind.DISAGREE, -1),
|
||||
(Reaction.kind == ReactionKind.PROOF, 1),
|
||||
(Reaction.kind == ReactionKind.DISPROOF, -1),
|
||||
(Reaction.kind == ReactionKind.ACCEPT, 1),
|
||||
(Reaction.kind == ReactionKind.REJECT, -1),
|
||||
(Reaction.kind == ReactionKind.LIKE, 1),
|
||||
(Reaction.kind == ReactionKind.DISLIKE, -1),
|
||||
else_=0
|
||||
)).label('rating'))
|
||||
|
||||
|
||||
def apply_filters(q, filters, user=None):
|
||||
filters = {} if filters is None else filters
|
||||
if filters.get("reacted") and user:
|
||||
@@ -35,15 +49,21 @@ def apply_filters(q, filters, user=None):
|
||||
@query.field("loadShout")
|
||||
async def load_shout(_, info, slug):
|
||||
with local_session() as session:
|
||||
shout = session.query(Shout).options(
|
||||
q = select(Shout).options(
|
||||
# TODO add cation
|
||||
joinedload(Shout.authors),
|
||||
joinedload(Shout.topics),
|
||||
).filter(
|
||||
)
|
||||
q = add_rating_column(q)
|
||||
q = q.filter(
|
||||
Shout.slug == slug
|
||||
).filter(
|
||||
Shout.deletedAt.is_(None)
|
||||
).one()
|
||||
).group_by(Shout.id)
|
||||
|
||||
[shout, rating] = session.execute(q).unique().one()
|
||||
|
||||
shout.stat = await ReactedStorage.get_shout_stat(shout.slug, rating)
|
||||
|
||||
return shout
|
||||
|
||||
@@ -84,22 +104,12 @@ async def load_shouts_by(_, info, options):
|
||||
)
|
||||
user = info.context["request"].user
|
||||
q = apply_filters(q, options.get("filters"), user)
|
||||
q = q.join(Reaction).add_columns(sa.func.sum(case(
|
||||
(Reaction.kind == ReactionKind.AGREE, 1),
|
||||
(Reaction.kind == ReactionKind.DISAGREE, -1),
|
||||
(Reaction.kind == ReactionKind.PROOF, 1),
|
||||
(Reaction.kind == ReactionKind.DISPROOF, -1),
|
||||
(Reaction.kind == ReactionKind.ACCEPT, 1),
|
||||
(Reaction.kind == ReactionKind.REJECT, -1),
|
||||
(Reaction.kind == ReactionKind.LIKE, 1),
|
||||
(Reaction.kind == ReactionKind.DISLIKE, -1),
|
||||
else_=0
|
||||
)).label('rating'))
|
||||
q = add_rating_column(q)
|
||||
|
||||
o = options.get("order_by")
|
||||
if o:
|
||||
q = q.add_columns(sa.func.count(Reaction.id).label(o))
|
||||
if o == 'comments':
|
||||
q = q.add_columns(sa.func.count(Reaction.id).label(o))
|
||||
q = q.join(Reaction, Shout.slug == Reaction.shout)
|
||||
q = q.filter(Reaction.body.is_not(None))
|
||||
elif o == 'reacted':
|
||||
@@ -108,12 +118,17 @@ async def load_shouts_by(_, info, options):
|
||||
).add_columns(
|
||||
sa.func.max(Reaction.createdAt).label(o)
|
||||
)
|
||||
elif o == 'views':
|
||||
q = q.join(ViewedEntry)
|
||||
q = q.add_columns(sa.func.sum(ViewedEntry.amount).label(o))
|
||||
order_by = o
|
||||
else:
|
||||
order_by = Shout.createdAt
|
||||
|
||||
order_by_desc = True if options.get('order_by_desc') is None else options.get('order_by_desc')
|
||||
|
||||
with_author_captions = False if options.get('with_author_captions') is None else options.get('with_author_captions')
|
||||
|
||||
query_order_by = desc(order_by) if order_by_desc else asc(order_by)
|
||||
offset = options.get("offset", 0)
|
||||
limit = options.get("limit", 10)
|
||||
@@ -124,9 +139,25 @@ async def load_shouts_by(_, info, options):
|
||||
|
||||
for shout in shouts:
|
||||
shout.stat = await ReactedStorage.get_shout_stat(shout.slug, shout.rating)
|
||||
|
||||
del shout.rating
|
||||
for author in shout.authors:
|
||||
author.caption = await ShoutAuthorStorage.get_author_caption(shout.slug, author.slug)
|
||||
|
||||
author_captions = {}
|
||||
|
||||
if with_author_captions:
|
||||
author_captions_result = session.query(ShoutAuthor).where(
|
||||
ShoutAuthor.shout.in_(map(lambda s: s.slug, shouts))).all()
|
||||
|
||||
for author_captions_result_item in author_captions_result:
|
||||
if author_captions.get(author_captions_result_item.shout) is None:
|
||||
author_captions[author_captions_result_item.shout] = {}
|
||||
|
||||
author_captions[
|
||||
author_captions_result_item.shout
|
||||
][
|
||||
author_captions_result_item.user
|
||||
] = author_captions_result_item.caption
|
||||
|
||||
for author in shout.authors:
|
||||
author.caption = author_captions[shout.slug][author.slug]
|
||||
|
||||
return shouts
|
||||
|
@@ -7,11 +7,11 @@ from auth.authenticate import login_required
|
||||
from base.orm import local_session
|
||||
from base.resolvers import mutation, query
|
||||
from orm.reaction import Reaction
|
||||
from orm.shout import ShoutAuthor
|
||||
from orm.topic import Topic, TopicFollower
|
||||
from orm.user import AuthorFollower, Role, User, UserRating, UserRole
|
||||
from services.stat.reacted import ReactedStorage
|
||||
from services.stat.topicstat import TopicStat
|
||||
from services.zine.shoutauthor import ShoutAuthor
|
||||
|
||||
# from .community import followed_communities
|
||||
from resolvers.inbox.unread import get_total_unread_counter
|
||||
|
Reference in New Issue
Block a user