57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 | @router.get("/", response_model=PaginationResponse[TaskInfo])
async def query_tasks(
id: int | None = None,
version: str | None = None,
name: str | None = None,
task_type: TaskType | None = None,
category: str | None = None,
modality: str | None = None,
author: str | None = None,
resource_id: int | None = None,
pagination: PaginationRequest = Depends(get_pagination_params),
user: UserOAuth = Depends(current_superuser_act),
db: AsyncSession = Depends(get_async_db),
) -> dict[str, Any]:
"""
Query `TaskV2` and get information about related workflows and projects.
"""
# Prepare statements
stm = select(TaskV2).order_by(TaskV2.id)
stm_count = select(func.count(TaskV2.id))
if id is not None:
stm = stm.where(TaskV2.id == id)
stm_count = stm_count.where(TaskV2.id == id)
if version is not None:
stm = stm.where(TaskV2.version == version)
stm_count = stm_count.where(TaskV2.version == version)
if name is not None:
stm = stm.where(TaskV2.name.icontains(name))
stm_count = stm_count.where(TaskV2.name.icontains(name))
if task_type is not None:
stm = stm.where(TaskV2.type == task_type)
stm_count = stm_count.where(TaskV2.type == task_type)
if category is not None:
stm = stm.where(func.lower(TaskV2.category) == category.lower())
stm_count = stm_count.where(
func.lower(TaskV2.category) == category.lower()
)
if modality is not None:
stm = stm.where(func.lower(TaskV2.modality) == modality.lower())
stm_count = stm_count.where(
func.lower(TaskV2.modality) == modality.lower()
)
if author is not None:
stm = stm.where(TaskV2.authors.icontains(author))
stm_count = stm_count.where(TaskV2.authors.icontains(author))
if resource_id is not None:
stm = stm.join(
TaskGroupV2, TaskGroupV2.id == TaskV2.taskgroupv2_id
).where(TaskGroupV2.resource_id == resource_id)
stm_count = stm_count.join(
TaskGroupV2, TaskGroupV2.id == TaskV2.taskgroupv2_id
).where(TaskGroupV2.resource_id == resource_id)
response = await get_paginated_response(
stm=stm, stm_count=stm_count, pagination=pagination, db=db
)
task_info_list = []
for task in response.items:
stm = (
select(WorkflowV2)
.join(
WorkflowTaskV2,
WorkflowTaskV2.workflow_id == WorkflowV2.id,
)
.where(WorkflowTaskV2.task_id == task.id)
)
res = await db.execute(stm)
wf_list = res.scalars().all()
project_users = {}
for project_id in set([workflow.project_id for workflow in wf_list]):
res = await db.execute(
select(UserOAuth.id, UserOAuth.email)
.join(
LinkUserProjectV2,
LinkUserProjectV2.user_id == UserOAuth.id,
)
.where(LinkUserProjectV2.project_id == project_id)
.where(LinkUserProjectV2.is_owner.is_(True))
)
project_users[project_id] = [
ProjectUser(id=p_user[0], email=p_user[1])
for p_user in res.all()
]
task_info_list.append(
dict(
task=task.model_dump(),
relationships=[
dict(
workflow_id=workflow.id,
workflow_name=workflow.name,
project_id=workflow.project.id,
project_name=workflow.project.name,
project_users=project_users[workflow.project_id],
)
for workflow in wf_list
],
)
)
return dict(
total_count=response.total_count,
page_size=response.page_size,
current_page=response.current_page,
items=task_info_list,
)
|