last-comment-revert
Some checks failed
Deploy on push / deploy (push) Failing after 10s

This commit is contained in:
Untone 2024-08-06 18:18:51 +03:00
parent dfd476411f
commit 522718f3a1
5 changed files with 28 additions and 31 deletions

View File

@ -24,7 +24,7 @@ class User(Base):
# preferred_username = Column(String, nullable=False) # preferred_username = Column(String, nullable=False)
picture = Column(String) picture = Column(String)
revoked_timestamp = Column(Integer) revoked_timestamp = Column(Integer)
roles = Column(String, default="author, reader") roles = Column(String, default="author,reader")
signup_methods = Column(String, default="magic_link_login") signup_methods = Column(String, default="magic_link_login")
created_at = Column(Integer, default=lambda: int(time.time())) created_at = Column(Integer, default=lambda: int(time.time()))
updated_at = Column(Integer, default=lambda: int(time.time())) updated_at = Column(Integer, default=lambda: int(time.time()))

View File

@ -21,18 +21,14 @@ from services.viewed import ViewedStorage
def add_reaction_stat_columns(q, aliased_reaction): def add_reaction_stat_columns(q, aliased_reaction):
q = q.outerjoin(aliased_reaction, aliased_reaction.deleted_at.is_(None)).add_columns( q = q.outerjoin(
func.sum(aliased_reaction.id).label("reacted_stat"), aliased_reaction, aliased_reaction.deleted_at.is_(None)).add_columns(
func.sum(case((aliased_reaction.kind == str(ReactionKind.COMMENT.value), 1), else_=0)).label("comments_stat"), func.sum(aliased_reaction.id).label("reacted_stat"),
func.sum(case((aliased_reaction.kind == str(ReactionKind.LIKE.value), 1), else_=0)).label("likes_stat"), func.sum(case((aliased_reaction.kind == str(ReactionKind.COMMENT.value), 1), else_=0)).label("comments_stat"),
func.sum(case((aliased_reaction.kind == str(ReactionKind.DISLIKE.value), 1), else_=0)).label("dislikes_stat"), func.sum(case((aliased_reaction.kind == str(ReactionKind.LIKE.value), 1), else_=0)).label("likes_stat"),
func.max( func.sum(case((aliased_reaction.kind == str(ReactionKind.DISLIKE.value), 1), else_=0)).label("dislikes_stat"),
case( func.max(aliased_reaction.created_at).label("last_comment_stat")
(aliased_reaction.kind == str(ReactionKind.COMMENT.value), aliased_reaction.created_at), )
else_=None,
)
).label("last_comment_stat"),
)
return q return q
@ -252,7 +248,7 @@ async def update_reaction(_, info, reaction):
commented_stat, commented_stat,
likes_stat, likes_stat,
dislikes_stat, dislikes_stat,
last_comment, last_reacted_at,
] = result ] = result
if not r: if not r:
return {"error": "invalid reaction id"} return {"error": "invalid reaction id"}
@ -423,7 +419,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
commented_stat, commented_stat,
likes_stat, likes_stat,
dislikes_stat, dislikes_stat,
_last_comment, last_reacted_at,
] in result_rows: ] in result_rows:
reaction.created_by = author reaction.created_by = author
reaction.shout = shout reaction.shout = shout
@ -431,6 +427,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
"rating": int(likes_stat or 0) - int(dislikes_stat or 0), "rating": int(likes_stat or 0) - int(dislikes_stat or 0),
"reacted": reacted_stat, "reacted": reacted_stat,
"commented": commented_stat, "commented": commented_stat,
"last_reacted_at": last_reacted_at
} }
reactions.add(reaction) reactions.add(reaction)
@ -465,8 +462,8 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S
) )
q2 = add_reaction_stat_columns(q2, aliased(Reaction)) q2 = add_reaction_stat_columns(q2, aliased(Reaction))
# Sort shouts by the `last_comment` field # Sort shouts by the `last_reacted_at` field
combined_query = union(q1, q2).order_by(desc(text("last_comment_stat"))).limit(limit).offset(offset) combined_query = union(q1, q2).order_by(desc(text("last_reacted_at"))).limit(limit).offset(offset)
results = session.execute(combined_query).scalars() results = session.execute(combined_query).scalars()
for [ for [
@ -475,14 +472,14 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S
commented_stat, commented_stat,
likes_stat, likes_stat,
dislikes_stat, dislikes_stat,
last_comment, last_reacted_at,
] in results: ] in results:
shout.stat = { shout.stat = {
"viewed": await ViewedStorage.get_shout(shout.slug), "viewed": await ViewedStorage.get_shout(shout.slug),
"rating": int(likes_stat or 0) - int(dislikes_stat or 0), "rating": int(likes_stat or 0) - int(dislikes_stat or 0),
"reacted": reacted_stat, "reacted": reacted_stat,
"commented": commented_stat, "commented": commented_stat,
"last_comment": last_comment, "last_reacted_at": last_reacted_at,
} }
shouts.append(shout) shouts.append(shout)

View File

@ -103,7 +103,7 @@ async def get_shout(_, info, slug: str):
commented_stat, commented_stat,
likes_stat, likes_stat,
dislikes_stat, dislikes_stat,
last_comment, last_reaction_at,
] = results ] = results
shout.stat = { shout.stat = {
@ -111,7 +111,7 @@ async def get_shout(_, info, slug: str):
"reacted": reacted_stat, "reacted": reacted_stat,
"commented": commented_stat, "commented": commented_stat,
"rating": int(likes_stat or 0) - int(dislikes_stat or 0), "rating": int(likes_stat or 0) - int(dislikes_stat or 0),
"last_comment": last_comment, "last_reacted_at": last_reaction_at,
} }
for author_caption in ( for author_caption in (
@ -164,7 +164,7 @@ async def load_shouts_by(_, _info, options):
} }
offset: 0 offset: 0
limit: 50 limit: 50
order_by: "likes" | "followers" | "comments" | "last_comment" order_by: "likes" | "followers" | "comments" | "last_reacted_at"
order_by_desc: true order_by_desc: true
} }
@ -188,7 +188,7 @@ async def load_shouts_by(_, _info, options):
# order # order
order_by = Shout.featured_at if filters.get("featured") else Shout.published_at order_by = Shout.featured_at if filters.get("featured") else Shout.published_at
order_str = options.get("order_by") order_str = options.get("order_by")
if order_str in ["likes", "followers", "comments", "last_comment"]: if order_str in ["likes", "followers", "comments", "last_reacted_at"]:
q = q.order_by(desc(text(f"{order_str}_stat"))) q = q.order_by(desc(text(f"{order_str}_stat")))
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by) query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
q = q.order_by(nulls_last(query_order_by)) q = q.order_by(nulls_last(query_order_by))
@ -206,7 +206,7 @@ async def load_shouts_by(_, _info, options):
commented_stat, commented_stat,
likes_stat, likes_stat,
dislikes_stat, dislikes_stat,
last_comment, last_reacted_at,
] in session.execute(q).unique(): ] in session.execute(q).unique():
main_topic = ( main_topic = (
session.query(Topic.slug) session.query(Topic.slug)
@ -228,7 +228,7 @@ async def load_shouts_by(_, _info, options):
"reacted": reacted_stat, "reacted": reacted_stat,
"commented": commented_stat, "commented": commented_stat,
"rating": int(likes_stat) - int(dislikes_stat), "rating": int(likes_stat) - int(dislikes_stat),
"last_comment": last_comment, "last_reacted_at": last_reacted_at,
} }
shouts.append(shout) shouts.append(shout)
@ -270,7 +270,7 @@ async def load_shouts_feed(_, info, options):
commented_stat, commented_stat,
likes_stat, likes_stat,
dislikes_stat, dislikes_stat,
last_comment, last_reacted_at,
] in session.execute(q).unique(): ] in session.execute(q).unique():
main_topic = ( main_topic = (
session.query(Topic.slug) session.query(Topic.slug)
@ -292,7 +292,7 @@ async def load_shouts_feed(_, info, options):
"reacted": reacted_stat, "reacted": reacted_stat,
"commented": commented_stat, "commented": commented_stat,
"rating": likes_stat - dislikes_stat, "rating": likes_stat - dislikes_stat,
"last_comment": last_comment, "last_reacted_at": last_reacted_at,
} }
shouts.append(shout) shouts.append(shout)
@ -365,7 +365,7 @@ async def get_shouts_from_query(q, author_id=None):
commented_stat, commented_stat,
likes_stat, likes_stat,
dislikes_stat, dislikes_stat,
last_comment, last_reacted_at,
] in session.execute(q, {"author_id": author_id}).unique(): ] in session.execute(q, {"author_id": author_id}).unique():
shouts.append(shout) shouts.append(shout)
shout.stat = { shout.stat = {
@ -373,7 +373,7 @@ async def get_shouts_from_query(q, author_id=None):
"reacted": reacted_stat, "reacted": reacted_stat,
"commented": commented_stat, "commented": commented_stat,
"rating": int(likes_stat or 0) - int(dislikes_stat or 0), "rating": int(likes_stat or 0) - int(dislikes_stat or 0),
"last_comment": last_comment, "last_reacted_at": last_reacted_at,
} }
return shouts return shouts

View File

@ -97,7 +97,7 @@ type Stat {
rating: Int rating: Int
commented: Int commented: Int
ranking: Int ranking: Int
last_comment: Int last_reacted_at: Int
} }
type Community { type Community {

View File

@ -24,7 +24,7 @@ engine = create_engine(
max_overflow=20, max_overflow=20,
pool_timeout=30, # Время ожидания свободного соединения pool_timeout=30, # Время ожидания свободного соединения
pool_recycle=1800, # Время жизни соединения pool_recycle=1800, # Время жизни соединения
connect_args={"sslmode": "disable"} connect_args={"sslmode": "disable"},
) )
inspector = inspect(engine) inspector = inspect(engine)
configure_mappers() configure_mappers()