From 51c96b0b7e0803d9c7efa2ce22c0a8576e7aac1d Mon Sep 17 00:00:00 2001 From: hjlarry Date: Sun, 18 Jan 2026 10:12:43 +0800 Subject: [PATCH] fix CI --- api/models/comment.py | 16 ++++++++++++++ api/services/workflow_comment_service.py | 27 ++++-------------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/api/models/comment.py b/api/models/comment.py index 54489fcd38..21ccfa13db 100644 --- a/api/models/comment.py +++ b/api/models/comment.py @@ -71,6 +71,10 @@ class WorkflowComment(Base): return self._created_by_account_cache return db.session.get(Account, self.created_by) + def cache_created_by_account(self, account: Account | None) -> None: + """Cache creator account to avoid extra queries.""" + self._created_by_account_cache = account + @property def resolved_by_account(self): """Get resolver account.""" @@ -80,6 +84,10 @@ class WorkflowComment(Base): return db.session.get(Account, self.resolved_by) return None + def cache_resolved_by_account(self, account: Account | None) -> None: + """Cache resolver account to avoid extra queries.""" + self._resolved_by_account_cache = account + @property def reply_count(self): """Get reply count.""" @@ -152,6 +160,10 @@ class WorkflowCommentReply(Base): return self._created_by_account_cache return db.session.get(Account, self.created_by) + def cache_created_by_account(self, account: Account | None) -> None: + """Cache creator account to avoid extra queries.""" + self._created_by_account_cache = account + class WorkflowCommentMention(Base): """Workflow comment mention model. @@ -192,3 +204,7 @@ class WorkflowCommentMention(Base): if hasattr(self, "_mentioned_user_account_cache"): return self._mentioned_user_account_cache return db.session.get(Account, self.mentioned_user_id) + + def cache_mentioned_user_account(self, account: Account | None) -> None: + """Cache mentioned account to avoid extra queries.""" + self._mentioned_user_account_cache = account diff --git a/api/services/workflow_comment_service.py b/api/services/workflow_comment_service.py index 4ae3989393..40e0df7e87 100644 --- a/api/services/workflow_comment_service.py +++ b/api/services/workflow_comment_service.py @@ -1,6 +1,5 @@ import logging from collections.abc import Sequence -from typing import Protocol, cast from sqlalchemy import desc, select from sqlalchemy.orm import Session, selectinload @@ -15,19 +14,6 @@ from models.account import Account logger = logging.getLogger(__name__) -class _CommentAccountCache(Protocol): - _created_by_account_cache: Account | None - _resolved_by_account_cache: Account | None - - -class _ReplyAccountCache(Protocol): - _created_by_account_cache: Account | None - - -class _MentionAccountCache(Protocol): - _mentioned_user_account_cache: Account | None - - class WorkflowCommentService: """Service for managing workflow comments.""" @@ -79,17 +65,12 @@ class WorkflowCommentService: # Cache accounts on objects for comment in comments: - comment_cache = cast(_CommentAccountCache, comment) - comment_cache._created_by_account_cache = account_map.get(comment.created_by) - comment_cache._resolved_by_account_cache = ( - account_map.get(comment.resolved_by) if comment.resolved_by else None - ) + comment.cache_created_by_account(account_map.get(comment.created_by)) + comment.cache_resolved_by_account(account_map.get(comment.resolved_by) if comment.resolved_by else None) for reply in comment.replies: - reply_cache = cast(_ReplyAccountCache, reply) - reply_cache._created_by_account_cache = account_map.get(reply.created_by) + reply.cache_created_by_account(account_map.get(reply.created_by)) for mention in comment.mentions: - mention_cache = cast(_MentionAccountCache, mention) - mention_cache._mentioned_user_account_cache = account_map.get(mention.mentioned_user_id) + mention.cache_mentioned_user_account(account_map.get(mention.mentioned_user_id)) @staticmethod def get_comment(tenant_id: str, app_id: str, comment_id: str, session: Session | None = None) -> WorkflowComment: