sentry-init-fix
All checks were successful
Deploy on push / deploy (push) Successful in 23s

This commit is contained in:
Untone 2024-04-08 09:17:05 +03:00
parent 8aa133aab1
commit aa1693cc16
5 changed files with 85 additions and 62 deletions

View File

@ -1,5 +1,4 @@
import os import os
import sentry_sdk
from importlib import import_module from importlib import import_module
from os.path import exists from os.path import exists
@ -10,6 +9,7 @@ from starlette.routing import Route
from services.rediscache import redis from services.rediscache import redis
from services.schema import resolvers from services.schema import resolvers
from services.sentry import start_sentry
from services.viewed import ViewedStorage from services.viewed import ViewedStorage
from services.webhook import WebhookEndpoint from services.webhook import WebhookEndpoint
from settings import DEV_SERVER_PID_FILE_NAME, MODE from settings import DEV_SERVER_PID_FILE_NAME, MODE
@ -17,8 +17,6 @@ from settings import DEV_SERVER_PID_FILE_NAME, MODE
import_module('resolvers') import_module('resolvers')
schema = make_executable_schema(load_schema_from_path('schema/'), resolvers) schema = make_executable_schema(load_schema_from_path('schema/'), resolvers)
# Initialize GlitchTip SDK with DSN
sentry_sdk.init("https://e8b4aabe17db4a7bbf703304cda33892@glitchtip.discours.io/1")
async def start(): async def start():
if MODE == 'development': if MODE == 'development':
@ -39,9 +37,9 @@ app = Starlette(
redis.connect, redis.connect,
ViewedStorage.init, ViewedStorage.init,
# search_service.info, # search_service.info,
# start_sentry, start_sentry,
start, start,
], ],
on_shutdown=[redis.disconnect], on_shutdown=[redis.disconnect],
debug=True, debug=True,
) )

View File

@ -59,7 +59,9 @@ async def get_author(_, _info, slug='', author_id=0):
author = None author = None
author_dict = None author_dict = None
try: try:
author_query = select(Author).filter(or_(Author.slug == slug, Author.id == author_id)) author_query = select(Author).filter(
or_(Author.slug == slug, Author.id == author_id)
)
result = await get_authors_with_stat_cached(author_query) result = await get_authors_with_stat_cached(author_query)
if not result: if not result:
raise ValueError('Author not found') raise ValueError('Author not found')
@ -68,7 +70,7 @@ async def get_author(_, _info, slug='', author_id=0):
logger.debug(f'found @{slug} with id {author_id}') logger.debug(f'found @{slug} with id {author_id}')
if isinstance(author, Author): if isinstance(author, Author):
if not author.stat: if not author.stat:
[author] = get_with_stat(author_query) # FIXME: with_rating=True) [author] = get_with_stat(author_query) # FIXME: with_rating=True)
if author: if author:
await set_author_cache(author.dict()) await set_author_cache(author.dict())
logger.debug('updated author stored in cache') logger.debug('updated author stored in cache')
@ -290,8 +292,12 @@ async def get_author_followers(_, _info, slug: str):
try: try:
with local_session() as session: with local_session() as session:
author_alias = aliased(Author) author_alias = aliased(Author)
author_id = session.query(author_alias.id).filter(author_alias.slug == slug) result = (
if author_id: session.query(author_alias).filter(author_alias.slug == slug).first()
)
if result:
[author] = result
author_id = author.id
cached = await redis.execute('GET', f'author:{author_id}:followers') cached = await redis.execute('GET', f'author:{author_id}:followers')
if not cached: if not cached:
author_follower_alias = aliased(AuthorFollower, name='af') author_follower_alias = aliased(AuthorFollower, name='af')

View File

@ -35,7 +35,7 @@ async def get_my_shout(_, info, shout_id: int):
) )
if not shout: if not shout:
return {'error': 'no shout found', 'shout': None} return {'error': 'no shout found', 'shout': None}
if not shout.published_at: if not bool(shout.published_at):
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
if not author: if not author:
return {'error': 'no author found', 'shout': None} return {'error': 'no author found', 'shout': None}
@ -241,7 +241,7 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
c = 1 c = 1
while same_slug_shout is not None: while same_slug_shout is not None:
c += 1 c += 1
slug += f'-{c}' slug = f'{slug}-{c}'
same_slug_shout = ( same_slug_shout = (
session.query(Shout).filter(Shout.slug == slug).first() session.query(Shout).filter(Shout.slug == slug).first()
) )

View File

@ -120,19 +120,19 @@ def get_author_rating_old(session, author: Author):
def get_author_rating_shouts(session, author: Author) -> int: def get_author_rating_shouts(session, author: Author) -> int:
q = ( q = (
select( select(
func.coalesce(func.sum( func.coalesce(
case( func.sum(
(Reaction.kind == ReactionKind.LIKE.value, 1), case(
(Reaction.kind == ReactionKind.DISLIKE.value, -1), (Reaction.kind == ReactionKind.LIKE.value, 1),
else_=0 (Reaction.kind == ReactionKind.DISLIKE.value, -1),
) else_=0,
), 0).label('shouts_rating') )
),
0,
).label('shouts_rating')
) )
.select_from(Reaction) .select_from(Reaction)
.outerjoin( .outerjoin(Shout, Shout.authors.any(id=author.id))
Shout,
Shout.authors.any(id=author.id)
)
.outerjoin( .outerjoin(
Reaction, Reaction,
and_( and_(
@ -150,13 +150,16 @@ def get_author_rating_comments(session, author: Author) -> int:
replied_comment = aliased(Reaction) replied_comment = aliased(Reaction)
q = ( q = (
select( select(
func.coalesce(func.sum( func.coalesce(
case( func.sum(
(Reaction.kind == ReactionKind.LIKE.value, 1), case(
(Reaction.kind == ReactionKind.DISLIKE.value, -1), (Reaction.kind == ReactionKind.LIKE.value, 1),
else_=0 (Reaction.kind == ReactionKind.DISLIKE.value, -1),
) else_=0,
), 0).label('shouts_rating') )
),
0,
).label('shouts_rating')
) )
.select_from(Reaction) .select_from(Reaction)
.outerjoin( .outerjoin(
@ -164,7 +167,9 @@ def get_author_rating_comments(session, author: Author) -> int:
and_( and_(
replied_comment.kind == ReactionKind.COMMENT.value, replied_comment.kind == ReactionKind.COMMENT.value,
replied_comment.created_by == author.id, replied_comment.created_by == author.id,
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]), Reaction.kind.in_(
[ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]
),
Reaction.reply_to == replied_comment.id, Reaction.reply_to == replied_comment.id,
Reaction.deleted_at.is_(None), Reaction.deleted_at.is_(None),
), ),
@ -179,26 +184,27 @@ def add_author_rating_columns(q, group_list):
# old karma # old karma
q = q.outerjoin(AuthorRating, AuthorRating.author == Author.id) q = q.outerjoin(AuthorRating, AuthorRating.author == Author.id)
q = q.add_columns(func.sum(case((AuthorRating.plus == true(), 1), else_=-1)).label('rating')) q = q.add_columns(
func.sum(case((AuthorRating.plus == true(), 1), else_=-1)).label('rating')
)
# by shouts rating # by shouts rating
shout_reaction = aliased(Reaction) shout_reaction = aliased(Reaction)
shouts_rating_subq = ( shouts_rating_subq = (
select( select(
Author.id, Author.id,
func.coalesce(func.sum( func.coalesce(
case( func.sum(
(shout_reaction.kind == ReactionKind.LIKE.value, 1), case(
(shout_reaction.kind == ReactionKind.DISLIKE.value, -1), (shout_reaction.kind == ReactionKind.LIKE.value, 1),
else_=0 (shout_reaction.kind == ReactionKind.DISLIKE.value, -1),
else_=0,
)
) )
)).label('shouts_rating') ).label('shouts_rating'),
) )
.select_from(shout_reaction) .select_from(shout_reaction)
.outerjoin( .outerjoin(Shout, Shout.authors.any(id=Author.id))
Shout,
Shout.authors.any(id=Author.id)
)
.outerjoin( .outerjoin(
shout_reaction, shout_reaction,
and_( and_(
@ -218,25 +224,35 @@ def add_author_rating_columns(q, group_list):
# by comments # by comments
replied_comment = aliased(Reaction) replied_comment = aliased(Reaction)
reaction_2 = aliased(Reaction) reaction_2 = aliased(Reaction)
comments_subq = select( comments_subq = (
Author.id, select(
func.coalesce(func.sum( Author.id,
case( func.coalesce(
(reaction_2.kind == ReactionKind.LIKE.value, 1), func.sum(
(reaction_2.kind == ReactionKind.DISLIKE.value, -1), case(
else_=0 (reaction_2.kind == ReactionKind.LIKE.value, 1),
) (reaction_2.kind == ReactionKind.DISLIKE.value, -1),
)).label('comments_rating'), else_=0,
).select_from(reaction_2).outerjoin( )
replied_comment, )
and_( ).label('comments_rating'),
replied_comment.kind == ReactionKind.COMMENT.value,
replied_comment.created_by == Author.id,
reaction_2.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
reaction_2.reply_to == replied_comment.id,
reaction_2.deleted_at.is_(None)
) )
).group_by(Author.id).subquery() .select_from(reaction_2)
.outerjoin(
replied_comment,
and_(
replied_comment.kind == ReactionKind.COMMENT.value,
replied_comment.created_by == Author.id,
reaction_2.kind.in_(
[ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]
),
reaction_2.reply_to == replied_comment.id,
reaction_2.deleted_at.is_(None),
),
)
.group_by(Author.id)
.subquery()
)
q = q.outerjoin(comments_subq, Author.id == comments_subq.c.id) q = q.outerjoin(comments_subq, Author.id == comments_subq.c.id)
q = q.add_columns(comments_subq.c.comments_rating) q = q.add_columns(comments_subq.c.comments_rating)

View File

@ -87,8 +87,7 @@ def add_author_stat_columns(q, with_rating=False):
# Create a subquery for comments count # Create a subquery for comments count
sub_comments = ( sub_comments = (
select( select(
Author.id, Author.id, func.coalesce(func.count(Reaction.id)).label('comments_count')
func.coalesce(func.count(Reaction.id)).label('comments_count')
) )
.outerjoin( .outerjoin(
Reaction, Reaction,
@ -155,7 +154,11 @@ async def get_authors_with_stat_cached(q):
with local_session() as session: with local_session() as session:
for [x] in session.execute(q): for [x] in session.execute(q):
stat_str = await redis.execute('GET', f'author:{x.id}') stat_str = await redis.execute('GET', f'author:{x.id}')
x.stat = json.loads(stat_str).get('stat') if isinstance(stat_str, str) else {} x.stat = (
json.loads(stat_str).get('stat')
if isinstance(stat_str, str)
else {}
)
records.append(x) records.append(x)
except Exception as exc: except Exception as exc:
raise Exception(exc) raise Exception(exc)