1818 union ,
1919)
2020from sqlalchemy .dialects .postgresql import insert
21- from sqlalchemy .orm import selectinload
21+ from sqlalchemy .orm import aliased , selectinload
2222
2323from data_rentgen .db .models import Job , Run , RunStartReason , RunStatus , User
2424from data_rentgen .db .repositories .base import Repository
8585
8686
8787class RunRepository (Repository [Run ]):
88- async def paginate ( # noqa: PLR0912, C901
88+ async def paginate ( # noqa: PLR0912, C901, PLR0915
8989 self ,
9090 page : int ,
9191 page_size : int ,
9292 since : datetime | None ,
9393 until : datetime | None ,
9494 run_ids : Collection [UUID ],
95- job_id : int | None ,
96- parent_run_id : UUID | None ,
97- search_query : str | None ,
98- job_type : Collection [str ],
99- job_location_id : int | None ,
100- status : Collection [RunStatus ],
101- started_by_user : Collection [str ] | None ,
95+ parent_run_ids : Collection [UUID ],
96+ job_ids : Collection [int ],
97+ job_types : Collection [str ],
98+ job_location_ids : Collection [int ],
99+ statuses : Collection [RunStatus ],
100+ started_by_users : Collection [str ],
102101 started_since : datetime | None ,
103102 started_until : datetime | None ,
104103 ended_since : datetime | None ,
105104 ended_until : datetime | None ,
105+ search_query : str | None ,
106106 ) -> PaginationDTO [Run ]:
107107 # do not use `tuple_(Run.created_at, Run.id).in_(...),
108108 # as this is too complex filter for Postgres to make an optimal query plan
@@ -135,12 +135,12 @@ async def paginate( # noqa: PLR0912, C901
135135 Run .id <= get_max_uuid (until ),
136136 ]
137137
138- if job_id :
139- where .append (Run .job_id == job_id )
140- if parent_run_id :
141- where .append (Run .parent_run_id == parent_run_id )
142- if status :
143- where .append (Run .status == any_ (status )) # type: ignore[arg-type]
138+ if job_ids :
139+ where .append (Run .job_id == any_ ( list ( job_ids ))) # type: ignore[arg-type]
140+ if parent_run_ids :
141+ where .append (Run .parent_run_id == any_ ( list ( parent_run_ids ))) # type: ignore[arg-type]
142+ if statuses :
143+ where .append (Run .status == any_ (statuses )) # type: ignore[arg-type]
144144 if started_since :
145145 where .append (Run .started_at >= started_since )
146146 if started_until :
@@ -159,10 +159,11 @@ async def paginate( # noqa: PLR0912, C901
159159 ts_match (Run .search_vector , tsquery ),
160160 * where ,
161161 )
162+ job_search = aliased (Job , name = "job_search" )
162163 job_stmt = (
163- select (Run , ts_rank (Job .search_vector , tsquery ).label ("search_rank" ))
164- .join (Job , Job .id == Run .job_id )
165- .where (ts_match (Job .search_vector , tsquery ), * where )
164+ select (Run , ts_rank (job_search .search_vector , tsquery ).label ("search_rank" ))
165+ .join (job_search , job_search .id == Run .job_id )
166+ .where (ts_match (job_search .search_vector , tsquery ), * where )
166167 )
167168
168169 union_cte = union (run_stmt , job_stmt ).cte ()
@@ -179,20 +180,21 @@ async def paginate( # noqa: PLR0912, C901
179180 query = select (Run ).where (* where )
180181 order_by = [Run .created_at .desc (), Run .id .desc ()]
181182
182- job_where = []
183- if job_type :
184- job_where . append ( Job . type == any_ ( list ( job_type ))) # type: ignore[arg-type]
185- if job_location_id is not None :
186- job_where . append ( Job . location_id == job_location_id )
187- if job_where :
188- query = query .join ( Job , and_ ( Run . job_id == Job . id , * job_where ))
183+ if job_types or job_location_ids :
184+ job = aliased ( Job , name = " job_type" )
185+ query = query . join ( job , and_ ( Run . job_id == job . id ))
186+ if job_types :
187+ query = query . where ( job . type == any_ ( list ( job_types ))) # type: ignore[arg-type]
188+ if job_location_ids :
189+ query = query .where ( job . location_id == any_ ( list ( job_location_ids ))) # type: ignore[arg-type]
189190
190- if started_by_user :
191+ if started_by_users :
192+ usernames_lower = [name .lower () for name in started_by_users ]
191193 query = query .join (
192194 User ,
193195 and_ (
194196 Run .started_by_user_id == User .id ,
195- func .lower (User .name ) == any_ ([ name . lower () for name in started_by_user ] ), # type: ignore[arg-type]
197+ func .lower (User .name ) == any_ (usernames_lower ), # type: ignore[arg-type]
196198 ),
197199 )
198200
0 commit comments