align with fetching conversion that for infinite pagination

pull/18581/head
AlexChimTrm 1 year ago
parent 7471802eb7
commit 0bdc1cec45

@ -109,12 +109,13 @@ class ConversationVariablesApi(Resource):
conversation_id = str(c_id)
parser = reqparse.RequestParser()
parser.add_argument("last_id", type=uuid_value, location="args")
parser.add_argument("limit", type=int_range(1, 100), required=False, default=20, location="args")
args = parser.parse_args()
try:
return ConversationService.get_conversational_variable(
app_model, conversation_id, end_user, args["limit"]
app_model, conversation_id, end_user, args["limit"], args["last_id"]
)
except services.errors.conversation.ConversationNotExistsError:
raise NotFound("Conversation Not Exists.")

@ -12,7 +12,11 @@ from libs.infinite_scroll_pagination import InfiniteScrollPagination
from models import ConversationVariable
from models.account import Account
from models.model import App, Conversation, EndUser, Message
from services.errors.conversation import ConversationNotExistsError, LastConversationNotExistsError
from services.errors.conversation import (
ConversationNotExistsError,
LastConversationNotExistsError,
ConversationVariableNotExistsError,
)
from services.errors.message import MessageNotExistsError
@ -175,6 +179,7 @@ class ConversationService:
conversation_id: str,
user: Optional[Union[Account, EndUser]],
limit: int,
last_id: Optional[str],
) -> InfiniteScrollPagination:
conversation = cls.get_conversation(app_model, conversation_id, user)
@ -185,13 +190,23 @@ class ConversationService:
.order_by(ConversationVariable.created_at)
)
total_count = db.session.scalar(select(func.count()).select_from(stmt))
has_more = total_count > limit
with Session(db.engine) as session:
if last_id:
last_variable = session.scalar(stmt.where(ConversationVariable.id == last_id))
if not last_variable:
raise ConversationVariableNotExistsError()
stmt = stmt.limit(limit)
# Filter for variables created after the last_id
stmt = stmt.where(ConversationVariable.created_at > last_variable.created_at)
with Session(db.engine) as session:
rows = session.scalars(stmt).all()
# Apply limit to query
query_stmt = stmt.limit(limit) # Get one extra to check if there are more
rows = session.scalars(query_stmt).all()
has_more = False
if len(rows) > limit:
has_more = True
rows = rows[:limit] # Remove the extra item
variables = [
{

@ -11,3 +11,7 @@ class ConversationNotExistsError(BaseServiceError):
class ConversationCompletedError(Exception):
pass
class ConversationVariableNotExistsError(BaseServiceError):
pass

Loading…
Cancel
Save