more-revalidation
All checks were successful
Deploy on push / deploy (push) Successful in 1m32s

This commit is contained in:
Untone 2025-02-03 23:16:50 +03:00
parent 9fe5fea238
commit 26b862d601
4 changed files with 47 additions and 12 deletions

View File

@ -1,3 +1,7 @@
#### [0.4.8]
- `Reaction.deleted_at` filter on `update_reaction` resolver added
- `triggers` module updated with `after_shout_handler`, `after_reaction_handler` for cache revalidation
#### [0.4.7] #### [0.4.7]
- `get_my_rates_shouts` resolver added with: - `get_my_rates_shouts` resolver added with:
- `shout_id` and `my_rate` fields in response - `shout_id` and `my_rate` fields in response

40
cache/triggers.py vendored
View File

@ -2,7 +2,7 @@ from sqlalchemy import event
from cache.revalidator import revalidation_manager from cache.revalidator import revalidation_manager
from orm.author import Author, AuthorFollower from orm.author import Author, AuthorFollower
from orm.reaction import Reaction from orm.reaction import Reaction, ReactionKind
from orm.shout import Shout, ShoutAuthor, ShoutReactionsFollower from orm.shout import Shout, ShoutAuthor, ShoutReactionsFollower
from orm.topic import Topic, TopicFollower from orm.topic import Topic, TopicFollower
from utils.logger import root_logger as logger from utils.logger import root_logger as logger
@ -43,6 +43,39 @@ def after_follower_handler(mapper, connection, target, is_delete=False):
revalidation_manager.mark_for_revalidation(target.follower, "authors") revalidation_manager.mark_for_revalidation(target.follower, "authors")
def after_shout_handler(mapper, connection, target):
"""Обработчик изменения статуса публикации"""
if not isinstance(target, Shout):
return
# Проверяем изменение статуса публикации
was_published = target.published_at is not None and target.deleted_at is None
if was_published:
# Обновляем счетчики для авторов
for author in target.authors:
revalidation_manager.mark_for_revalidation(author.id, "authors")
# Обновляем счетчики для тем
for topic in target.topics:
revalidation_manager.mark_for_revalidation(topic.id, "topics")
def after_reaction_handler(mapper, connection, target):
"""Обработчик для комментариев"""
if not isinstance(target, Reaction) or target.kind != ReactionKind.COMMENT.value:
return
# Проверяем что комментарий относится к опубликованному посту
shout = target.shout
if shout and shout.published_at and not shout.deleted_at:
# Обновляем счетчики для автора комментария
revalidation_manager.mark_for_revalidation(target.created_by.id, "authors")
# Обновляем счетчики для поста
revalidation_manager.mark_for_revalidation(shout.id, "shouts")
def events_register(): def events_register():
"""Регистрация обработчиков событий для всех сущностей.""" """Регистрация обработчиков событий для всех сущностей."""
event.listen(ShoutAuthor, "after_insert", mark_for_revalidation) event.listen(ShoutAuthor, "after_insert", mark_for_revalidation)
@ -64,6 +97,9 @@ def events_register():
event.listen(Reaction, "after_update", mark_for_revalidation) event.listen(Reaction, "after_update", mark_for_revalidation)
event.listen(Author, "after_update", mark_for_revalidation) event.listen(Author, "after_update", mark_for_revalidation)
event.listen(Topic, "after_update", mark_for_revalidation) event.listen(Topic, "after_update", mark_for_revalidation)
event.listen(Shout, "after_update", mark_for_revalidation) event.listen(Shout, "after_update", after_shout_handler)
event.listen(Reaction, "after_insert", after_reaction_handler)
event.listen(Reaction, "after_delete", after_reaction_handler)
logger.info("Event handlers registered successfully.") logger.info("Event handlers registered successfully.")

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "core" name = "core"
version = "0.4.7" version = "0.4.8"
description = "core module for discours.io" description = "core module for discours.io"
authors = ["discoursio devteam"] authors = ["discoursio devteam"]
license = "MIT" license = "MIT"

View File

@ -100,10 +100,7 @@ def add_author_stat_columns(q):
def get_topic_shouts_stat(topic_id: int) -> int: def get_topic_shouts_stat(topic_id: int) -> int:
""" """
Получает количество публикаций для указанной темы. Получает количество опубликованных постов для темы
:param topic_id: Идентификатор темы.
:return: Количество уникальных публикаций для темы.
""" """
q = ( q = (
select(func.count(distinct(ShoutTopic.shout))) select(func.count(distinct(ShoutTopic.shout)))
@ -116,7 +113,7 @@ def get_topic_shouts_stat(topic_id: int) -> int:
) )
) )
) )
# Выполнение запроса и получение результата
with local_session() as session: with local_session() as session:
result = session.execute(q).first() result = session.execute(q).first()
return result[0] if result else 0 return result[0] if result else 0
@ -198,10 +195,7 @@ def get_topic_comments_stat(topic_id: int) -> int:
def get_author_shouts_stat(author_id: int) -> int: def get_author_shouts_stat(author_id: int) -> int:
""" """
Получает количество публикаций для указанного автора. Получает количество опубликованных постов для автора
:param author_id: Идентификатор автора.
:return: Количество уникальных публикаций автора.
""" """
aliased_shout_author = aliased(ShoutAuthor) aliased_shout_author = aliased(ShoutAuthor)
aliased_shout = aliased(Shout) aliased_shout = aliased(Shout)
@ -214,6 +208,7 @@ def get_author_shouts_stat(author_id: int) -> int:
and_( and_(
aliased_shout_author.author == author_id, aliased_shout_author.author == author_id,
aliased_shout.published_at.is_not(None), aliased_shout.published_at.is_not(None),
aliased_shout.deleted_at.is_(None) # Добавляем проверку на удаление
) )
) )
) )