54
55
56
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
164
165
166
167 | @router.get("/", response_model=PaginationResponse[TaskV2Info])
async def query_tasks(
id: int | None = None,
source: str | 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),
) -> PaginationResponse[TaskV2Info]:
"""
Query `TaskV2` and get information about related workflows and projects.
"""
# Assign pagination parameters
page = pagination.page
page_size = pagination.page_size
# 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 source is not None:
stm = stm.where(TaskV2.source.icontains(source))
stm_count = stm_count.where(TaskV2.source.icontains(source))
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)
.where(TaskGroupV2.id == TaskV2.taskgroupv2_id)
.where(TaskGroupV2.resource_id == resource_id)
)
stm_count = (
stm_count.join(TaskGroupV2)
.where(TaskGroupV2.id == TaskV2.taskgroupv2_id)
.where(TaskGroupV2.resource_id == resource_id)
)
# Find total number of elements
res_total_count = await db.execute(stm_count)
total_count = res_total_count.scalar()
if page_size is None:
page_size = total_count
else:
stm = stm.offset((page - 1) * page_size).limit(page_size)
# Get `page_size` rows
res = await db.execute(stm)
task_list = res.scalars().all()
task_info_list = []
for task in task_list:
stm = (
select(WorkflowV2)
.join(WorkflowTaskV2)
.where(WorkflowTaskV2.workflow_id == WorkflowV2.id)
.where(WorkflowTaskV2.task_id == task.id)
)
res = await db.execute(stm)
wf_list = res.scalars().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=[
dict(id=user.id, email=user.email)
for user in workflow.project.user_list
],
)
for workflow in wf_list
],
)
)
return PaginationResponse[TaskV2Info](
total_count=total_count,
page_size=page_size,
current_page=page,
items=task_info_list,
)
|