2024-08-08 14:54:02 +08:00
|
|
|
import uuid
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
from flask_sqlalchemy.pagination import Pagination
|
|
|
|
from sqlalchemy import and_, or_
|
|
|
|
|
|
|
|
from extensions.ext_database import db
|
2024-10-21 10:43:49 +08:00
|
|
|
from models import App, EndUser, WorkflowAppLog, WorkflowRun
|
|
|
|
from models.enums import CreatedByRole
|
|
|
|
from models.workflow import WorkflowRunStatus
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
class WorkflowAppService:
|
|
|
|
def get_paginate_workflow_app_logs(self, app_model: App, args: dict) -> Pagination:
|
|
|
|
"""
|
|
|
|
Get paginate workflow app logs
|
|
|
|
:param app: app model
|
|
|
|
:param args: request args
|
|
|
|
:return:
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
query = db.select(WorkflowAppLog).where(
|
|
|
|
WorkflowAppLog.tenant_id == app_model.tenant_id, WorkflowAppLog.app_id == app_model.id
|
2024-04-08 18:51:46 +08:00
|
|
|
)
|
|
|
|
|
2024-10-21 10:43:49 +08:00
|
|
|
status = WorkflowRunStatus.value_of(args.get("status", "")) if args.get("status") else None
|
2024-08-26 13:43:57 +08:00
|
|
|
keyword = args["keyword"]
|
2024-08-08 14:54:02 +08:00
|
|
|
if keyword or status:
|
2024-08-26 13:43:57 +08:00
|
|
|
query = query.join(WorkflowRun, WorkflowRun.id == WorkflowAppLog.workflow_run_id)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2024-08-08 14:54:02 +08:00
|
|
|
if keyword:
|
|
|
|
keyword_like_val = f"%{args['keyword'][:30]}%"
|
2024-04-08 18:51:46 +08:00
|
|
|
keyword_conditions = [
|
2024-08-08 14:54:02 +08:00
|
|
|
WorkflowRun.inputs.ilike(keyword_like_val),
|
|
|
|
WorkflowRun.outputs.ilike(keyword_like_val),
|
2024-04-08 18:51:46 +08:00
|
|
|
# filter keyword by end user session id if created by end user role
|
2024-08-26 13:43:57 +08:00
|
|
|
and_(WorkflowRun.created_by_role == "end_user", EndUser.session_id.ilike(keyword_like_val)),
|
2024-04-08 18:51:46 +08:00
|
|
|
]
|
|
|
|
|
2024-08-08 14:54:02 +08:00
|
|
|
# filter keyword by workflow run id
|
|
|
|
keyword_uuid = self._safe_parse_uuid(keyword)
|
|
|
|
if keyword_uuid:
|
|
|
|
keyword_conditions.append(WorkflowRun.id == keyword_uuid)
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
query = query.outerjoin(
|
|
|
|
EndUser,
|
2024-10-21 10:43:49 +08:00
|
|
|
and_(WorkflowRun.created_by == EndUser.id, WorkflowRun.created_by_role == CreatedByRole.END_USER),
|
2024-04-08 18:51:46 +08:00
|
|
|
).filter(or_(*keyword_conditions))
|
|
|
|
|
|
|
|
if status:
|
|
|
|
# join with workflow_run and filter by status
|
2024-08-26 13:43:57 +08:00
|
|
|
query = query.filter(WorkflowRun.status == status.value)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
query = query.order_by(WorkflowAppLog.created_at.desc())
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
pagination = db.paginate(query, page=args["page"], per_page=args["limit"], error_out=False)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
return pagination
|
2024-08-08 14:54:02 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _safe_parse_uuid(value: str):
|
|
|
|
# fast check
|
|
|
|
if len(value) < 32:
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
return uuid.UUID(value)
|
|
|
|
except ValueError:
|
|
|
|
return None
|