virtual-score-column-fix
All checks were successful
Deploy to core / deploy (push) Successful in 1m44s

This commit is contained in:
Untone 2024-01-29 00:28:04 +03:00
parent b92431e802
commit 18fc08f6c8
2 changed files with 28 additions and 107 deletions

View File

@ -1,7 +1,7 @@
import logging import logging
from sqlalchemy import bindparam, distinct, or_ from sqlalchemy import bindparam, distinct, literal_column, or_
from sqlalchemy.orm import aliased, joinedload, selectinload from sqlalchemy.orm import aliased, column_property, joinedload, selectinload
from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
@ -82,9 +82,7 @@ async def get_shout(_, _info, slug=None, shout_id=None):
'rating': int(likes_stat or 0) - int(dislikes_stat or 0), 'rating': int(likes_stat or 0) - int(dislikes_stat or 0),
} }
for author_caption in ( for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug):
session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug)
):
for author in shout.authors: for author in shout.authors:
if author.id == author_caption.author: if author.id == author_caption.author:
author.caption = author_caption.caption author.caption = author_caption.caption
@ -105,9 +103,7 @@ async def get_shout(_, _info, slug=None, shout_id=None):
shout.main_topic = main_topic[0] shout.main_topic = main_topic[0]
return shout return shout
except Exception: except Exception:
raise HTTPException( raise HTTPException(status_code=404, detail=f'shout {slug or shout_id} not found')
status_code=404, detail=f'shout {slug or shout_id} not found'
)
@query.field('load_shouts_by') @query.field('load_shouts_by')
@ -153,9 +149,7 @@ async def load_shouts_by(_, _info, options):
# order # order
order_by = options.get('order_by', Shout.published_at) order_by = options.get('order_by', Shout.published_at)
query_order_by = ( query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(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))
# limit offset # limit offset
@ -248,20 +242,15 @@ async def load_shouts_feed(_, info, options):
with local_session() as session: with local_session() as session:
reader = session.query(Author).filter(Author.user == user_id).first() reader = session.query(Author).filter(Author.user == user_id).first()
if reader: if reader:
reader_followed_authors = select(AuthorFollower.author).where( reader_followed_authors = select(AuthorFollower.author).where(AuthorFollower.follower == reader.id)
AuthorFollower.follower == reader.id reader_followed_topics = select(TopicFollower.topic).where(TopicFollower.follower == reader.id)
)
reader_followed_topics = select(TopicFollower.topic).where(
TopicFollower.follower == reader.id
)
subquery = ( subquery = (
select(Shout.id) select(Shout.id)
.where(Shout.id == ShoutAuthor.shout) .where(Shout.id == ShoutAuthor.shout)
.where(Shout.id == ShoutTopic.shout) .where(Shout.id == ShoutTopic.shout)
.where( .where(
(ShoutAuthor.author.in_(reader_followed_authors)) (ShoutAuthor.author.in_(reader_followed_authors)) | (ShoutTopic.topic.in_(reader_followed_topics))
| (ShoutTopic.topic.in_(reader_followed_topics))
) )
) )
@ -286,24 +275,15 @@ async def load_shouts_feed(_, info, options):
order_by = options.get('order_by', Shout.published_at) order_by = options.get('order_by', Shout.published_at)
query_order_by = ( query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
)
offset = options.get('offset', 0) offset = options.get('offset', 0)
limit = options.get('limit', 10) limit = options.get('limit', 10)
q = ( q = q.group_by(Shout.id).order_by(nulls_last(query_order_by)).limit(limit).offset(offset)
q.group_by(Shout.id)
.order_by(nulls_last(query_order_by))
.limit(limit)
.offset(offset)
)
# print(q.compile(compile_kwargs={"literal_binds": True})) # print(q.compile(compile_kwargs={"literal_binds": True}))
for [shout, reacted_stat, commented_stat, _last_comment] in session.execute( for [shout, reacted_stat, commented_stat, _last_comment] in session.execute(q).unique():
q
).unique():
main_topic = ( main_topic = (
session.query(Topic.slug) session.query(Topic.slug)
.join( .join(
@ -335,12 +315,12 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0):
results = await SearchService.search(text, limit, offset) results = await SearchService.search(text, limit, offset)
results_dict = {r['slug']: r for r in results} results_dict = {r['slug']: r for r in results}
found_keys = list(results_dict.keys()) found_keys = list(results_dict.keys())
shouts_data = []
with local_session() as session: with local_session() as session:
# Define a virtual column 'score' using column_property
Shout.score = column_property(literal_column(f"({results_dict.get(Shout.slug, {}).get('score', 0)})"))
results = ( results = (
session.query(Shout) session.query(Shout)
.join(ShoutAuthor, Shout.id == ShoutAuthor.shout)
.join(ShoutTopic, Shout.id == ShoutTopic.shout) .join(ShoutTopic, Shout.id == ShoutTopic.shout)
.options( .options(
joinedload(Shout.authors), joinedload(Shout.authors),
@ -354,49 +334,13 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0):
) )
.limit(limit) .limit(limit)
.offset(offset) .offset(offset)
.order_by(desc(Shout.score)) # Order by the virtual 'score' column
.all() .all()
) )
logger.debug(f'search found {len(results)} results') logger.debug(f'search found {len(results)} results')
for shout in results:
shout_data = shout.dict() return results
shout_slug = shout_data.get('slug', '')
topic = (
session.query(Topic)
.join(
ShoutTopic,
and_(
ShoutTopic.topic == Topic.id,
ShoutTopic.shout == shout.id,
ShoutTopic.main == True,
),
)
.first()
)
if topic:
shout_data['main_topic'] = topic
authors = (
session.query(Author, ShoutAuthor, ShoutTopic)
.join(
ShoutTopic,
and_(
ShoutAuthor.author == Author.id,
ShoutTopic.shout == shout.id,
),
)
.all()
)
if authors:
shout_data['authors'] = authors
score = results_dict.get(shout_slug, {}).get('score', 0)
shout_data['score'] = score # Add the score to the dictionary
shouts_data.append(shout_data)
shouts_data = sorted(
shouts_data,
key=lambda x: float(x.get('score', '0')),
reverse=True,
)
return shouts_data
else:
return [] return []
@ -414,9 +358,7 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
and_( and_(
Reaction.shout == Shout.id, Reaction.shout == Shout.id,
Reaction.replyTo.is_(None), Reaction.replyTo.is_(None),
Reaction.kind.in_( Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
[ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]
),
), ),
) )
.outerjoin(Author, Author.user == bindparam('user_id')) .outerjoin(Author, Author.user == bindparam('user_id'))
@ -485,9 +427,7 @@ async def load_shouts_random_top(_, _info, options):
aliased_reaction = aliased(Reaction) aliased_reaction = aliased(Reaction)
subquery = ( subquery = select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None))
select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None))
)
subquery = apply_filters(subquery, options.get('filters', {})) subquery = apply_filters(subquery, options.get('filters', {}))
subquery = subquery.group_by(Shout.id).order_by( subquery = subquery.group_by(Shout.id).order_by(

View File

@ -60,9 +60,7 @@ class ViewedStorage:
if os.path.exists(VIEWS_FILEPATH): if os.path.exists(VIEWS_FILEPATH):
file_timestamp = os.path.getctime(VIEWS_FILEPATH) file_timestamp = os.path.getctime(VIEWS_FILEPATH)
self.start_date = datetime.fromtimestamp(file_timestamp).strftime( self.start_date = datetime.fromtimestamp(file_timestamp).strftime('%Y-%m-%d')
'%Y-%m-%d'
)
# Запуск фоновой задачи # Запуск фоновой задачи
asyncio.create_task(self.worker()) asyncio.create_task(self.worker())
@ -78,9 +76,7 @@ class ViewedStorage:
with open(VIEWS_FILEPATH, 'r') as file: with open(VIEWS_FILEPATH, 'r') as file:
precounted_views = json.load(file) precounted_views = json.load(file)
self.views_by_shout.update(precounted_views) self.views_by_shout.update(precounted_views)
logger.info( logger.info(f' * {len(precounted_views)} публикаций с просмотрами успешно загружены.')
f' * {len(precounted_views)} публикаций с просмотрами успешно загружены.'
)
except Exception as e: except Exception as e:
logger.error(f'Ошибка загрузки предварительно подсчитанных просмотров: {e}') logger.error(f'Ошибка загрузки предварительно подсчитанных просмотров: {e}')
@ -98,9 +94,7 @@ class ViewedStorage:
property=f'properties/{GOOGLE_PROPERTY_ID}', property=f'properties/{GOOGLE_PROPERTY_ID}',
dimensions=[Dimension(name='pagePath')], dimensions=[Dimension(name='pagePath')],
metrics=[Metric(name='screenPageViews')], metrics=[Metric(name='screenPageViews')],
date_ranges=[ date_ranges=[DateRange(start_date=self.start_date, end_date='today')],
DateRange(start_date=self.start_date, end_date='today')
],
) )
response = self.analytics_client.run_report(request) response = self.analytics_client.run_report(request)
if response and isinstance(response.rows, list): if response and isinstance(response.rows, list):
@ -117,9 +111,7 @@ class ViewedStorage:
views_count = int(row.metric_values[0].value) views_count = int(row.metric_values[0].value)
# Обновление данных в хранилище # Обновление данных в хранилище
self.views_by_shout[slug] = self.views_by_shout.get( self.views_by_shout[slug] = self.views_by_shout.get(slug, 0)
slug, 0
)
self.views_by_shout[slug] += views_count self.views_by_shout[slug] += views_count
self.update_topics(slug) self.update_topics(slug)
@ -178,20 +170,12 @@ class ViewedStorage:
# Обновление тем и авторов с использованием вспомогательной функции # Обновление тем и авторов с использованием вспомогательной функции
for [_shout_topic, topic] in ( for [_shout_topic, topic] in (
session.query(ShoutTopic, Topic) session.query(ShoutTopic, Topic).join(Topic).join(Shout).where(Shout.slug == shout_slug).all()
.join(Topic)
.join(Shout)
.where(Shout.slug == shout_slug)
.all()
): ):
update_groups(self.shouts_by_topic, topic.slug, shout_slug) update_groups(self.shouts_by_topic, topic.slug, shout_slug)
for [_shout_topic, author] in ( for [_shout_topic, author] in (
session.query(ShoutAuthor, Author) session.query(ShoutAuthor, Author).join(Author).join(Shout).where(Shout.slug == shout_slug).all()
.join(Author)
.join(Shout)
.where(Shout.slug == shout_slug)
.all()
): ):
update_groups(self.shouts_by_author, author.slug, shout_slug) update_groups(self.shouts_by_author, author.slug, shout_slug)
@ -216,10 +200,7 @@ class ViewedStorage:
if failed == 0: if failed == 0:
when = datetime.now(timezone.utc) + timedelta(seconds=self.period) when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
t = format(when.astimezone().isoformat()) t = format(when.astimezone().isoformat())
logger.info( logger.info(' ⎩ Следующее обновление: %s' % (t.split('T')[0] + ' ' + t.split('T')[1].split('.')[0]))
' ⎩ Следующее обновление: %s'
% (t.split('T')[0] + ' ' + t.split('T')[1].split('.')[0])
)
await asyncio.sleep(self.period) await asyncio.sleep(self.period)
else: else:
await asyncio.sleep(10) await asyncio.sleep(10)