-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathoptions.py
More file actions
756 lines (591 loc) · 25.5 KB
/
options.py
File metadata and controls
756 lines (591 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
"""Query conditions and orders for scheduler sessions, kernels, and users."""
from __future__ import annotations
from collections.abc import Collection
from typing import TYPE_CHECKING
from uuid import UUID
import sqlalchemy as sa
from ai.backend.manager.models.scaling_group.row import ScalingGroupRow
if TYPE_CHECKING:
from ai.backend.common.data.filter_specs import (
StringMatchSpec,
UUIDEqualMatchSpec,
UUIDInMatchSpec,
)
from ai.backend.manager.data.kernel.types import KernelStatusInMatchSpec
from ai.backend.common.types import AgentId, KernelId, SessionId
from ai.backend.manager.data.kernel.types import KernelStatus
from ai.backend.manager.data.session.types import KernelMatchType, SessionStatus
from ai.backend.manager.models.image import ImageRow
from ai.backend.manager.models.kernel import KernelRow
from ai.backend.manager.models.session import SessionRow
from ai.backend.manager.models.user import UserRow
from ai.backend.manager.repositories.base import QueryCondition, QueryOrder
# Default lookback period for fair share calculation (28 days)
DEFAULT_LOOKBACK_DAYS = 28
class SessionConditions:
"""Query conditions for sessions."""
@staticmethod
def by_ids(session_ids: Collection[SessionId]) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
return SessionRow.id.in_(session_ids)
return inner
@staticmethod
def by_statuses(statuses: Collection[SessionStatus]) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
return SessionRow.status.in_(statuses)
return inner
@staticmethod
def by_scaling_group(scaling_group: str) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
return SessionRow.scaling_group_name == scaling_group
return inner
@staticmethod
def by_name_contains(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = SessionRow.name.ilike(f"%{spec.value}%")
else:
condition = SessionRow.name.like(f"%{spec.value}%")
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_name_equals(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = sa.func.lower(SessionRow.name) == spec.value.lower()
else:
condition = SessionRow.name == spec.value
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_name_starts_with(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = SessionRow.name.ilike(f"{spec.value}%")
else:
condition = SessionRow.name.like(f"{spec.value}%")
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_name_ends_with(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = SessionRow.name.ilike(f"%{spec.value}")
else:
condition = SessionRow.name.like(f"%{spec.value}")
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_domain_name_contains(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = SessionRow.domain_name.ilike(f"%{spec.value}%")
else:
condition = SessionRow.domain_name.like(f"%{spec.value}%")
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_domain_name_equals(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = sa.func.lower(SessionRow.domain_name) == spec.value.lower()
else:
condition = SessionRow.domain_name == spec.value
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_domain_name_starts_with(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = SessionRow.domain_name.ilike(f"{spec.value}%")
else:
condition = SessionRow.domain_name.like(f"{spec.value}%")
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_domain_name_ends_with(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = SessionRow.domain_name.ilike(f"%{spec.value}")
else:
condition = SessionRow.domain_name.like(f"%{spec.value}")
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_scaling_group_contains(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = SessionRow.scaling_group_name.ilike(f"%{spec.value}%")
else:
condition = SessionRow.scaling_group_name.like(f"%{spec.value}%")
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_scaling_group_equals(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = sa.func.lower(SessionRow.scaling_group_name) == spec.value.lower()
else:
condition = SessionRow.scaling_group_name == spec.value
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_scaling_group_starts_with(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = SessionRow.scaling_group_name.ilike(f"{spec.value}%")
else:
condition = SessionRow.scaling_group_name.like(f"{spec.value}%")
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
@staticmethod
def by_scaling_group_ends_with(spec: StringMatchSpec) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.case_insensitive:
condition = SessionRow.scaling_group_name.ilike(f"%{spec.value}")
else:
condition = SessionRow.scaling_group_name.like(f"%{spec.value}")
if spec.negated:
condition = sa.not_(condition)
return condition
return inner
# Promotion handler conditions - optimized with EXISTS subqueries
# These check kernel status conditions without loading kernel data
@staticmethod
def all_kernels_in_statuses(statuses: Collection[KernelStatus]) -> QueryCondition:
"""Filter sessions where ALL kernels are in the specified statuses.
Uses NOT EXISTS to check that no kernel is outside the specified statuses.
Also requires at least one kernel exists (sessions without kernels are excluded).
"""
def inner() -> sa.sql.expression.ColumnElement[bool]:
# Subquery: check if any kernel is NOT in the specified statuses
kernel_not_in_statuses = (
sa.select(sa.literal(1))
.select_from(KernelRow)
.where(
KernelRow.session_id == SessionRow.id,
KernelRow.status.notin_(statuses),
)
.exists()
)
# Subquery: check if session has at least one kernel
has_kernels = (
sa.select(sa.literal(1))
.select_from(KernelRow)
.where(KernelRow.session_id == SessionRow.id)
.exists()
)
# ALL: no kernel outside statuses AND has at least one kernel
return sa.and_(~kernel_not_in_statuses, has_kernels)
return inner
@staticmethod
def any_kernel_in_statuses(statuses: Collection[KernelStatus]) -> QueryCondition:
"""Filter sessions where at least one kernel is in the specified statuses.
Uses EXISTS to check that at least one kernel is in the specified statuses.
"""
def inner() -> sa.sql.expression.ColumnElement[bool]:
# Subquery: check if any kernel is in the specified statuses
return (
sa.select(sa.literal(1))
.select_from(KernelRow)
.where(
KernelRow.session_id == SessionRow.id,
KernelRow.status.in_(statuses),
)
.exists()
)
return inner
@staticmethod
def no_kernel_in_statuses(statuses: Collection[KernelStatus]) -> QueryCondition:
"""Filter sessions where no kernel is in the specified statuses.
Uses NOT EXISTS to check that no kernel is in the specified statuses.
Also requires at least one kernel exists (sessions without kernels are excluded).
"""
def inner() -> sa.sql.expression.ColumnElement[bool]:
# Subquery: check if any kernel is in the specified statuses
kernel_in_statuses = (
sa.select(sa.literal(1))
.select_from(KernelRow)
.where(
KernelRow.session_id == SessionRow.id,
KernelRow.status.in_(statuses),
)
.exists()
)
# Subquery: check if session has at least one kernel
has_kernels = (
sa.select(sa.literal(1))
.select_from(KernelRow)
.where(KernelRow.session_id == SessionRow.id)
.exists()
)
# NOT_ANY: no kernel in statuses AND has at least one kernel
return sa.and_(~kernel_in_statuses, has_kernels)
return inner
@staticmethod
def by_kernel_match(
statuses: Collection[KernelStatus],
match_type: KernelMatchType,
) -> QueryCondition:
"""Filter sessions by kernel status match type.
Args:
statuses: Kernel statuses to check against
match_type: How to match kernel statuses (ALL/ANY/NOT_ANY)
Returns:
QueryCondition for the specified match type
"""
match match_type:
case KernelMatchType.ALL:
return SessionConditions.all_kernels_in_statuses(statuses)
case KernelMatchType.ANY:
return SessionConditions.any_kernel_in_statuses(statuses)
case KernelMatchType.NOT_ANY:
return SessionConditions.no_kernel_in_statuses(statuses)
@staticmethod
def by_id_filter_equals(spec: UUIDEqualMatchSpec) -> QueryCondition:
"""Factory for id equality filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.negated:
return SessionRow.id != SessionId(spec.value)
return SessionRow.id == SessionId(spec.value)
return inner
@staticmethod
def by_id_filter_in(spec: UUIDInMatchSpec) -> QueryCondition:
"""Factory for id IN filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
session_ids = [SessionId(v) for v in spec.values]
if spec.negated:
return SessionRow.id.notin_(session_ids)
return SessionRow.id.in_(session_ids)
return inner
@staticmethod
def by_status_in(statuses: Collection[SessionStatus]) -> QueryCondition:
"""Factory for status IN filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
return SessionRow.status.in_(statuses)
return inner
@staticmethod
def by_status_not_in(statuses: Collection[SessionStatus]) -> QueryCondition:
"""Factory for status NOT IN filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
return SessionRow.status.notin_(statuses)
return inner
@staticmethod
def by_owner_id_filter_equals(spec: UUIDEqualMatchSpec) -> QueryCondition:
"""Factory for owner_id equality filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.negated:
return SessionRow.user_uuid != spec.value
return SessionRow.user_uuid == spec.value
return inner
@staticmethod
def by_owner_id_filter_in(spec: UUIDInMatchSpec) -> QueryCondition:
"""Factory for owner_id IN filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.negated:
return SessionRow.user_uuid.notin_(spec.values)
return SessionRow.user_uuid.in_(spec.values)
return inner
@staticmethod
def by_group_id_filter_equals(spec: UUIDEqualMatchSpec) -> QueryCondition:
"""Factory for group (project) ID equality filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.negated:
return SessionRow.group_id != spec.value
return SessionRow.group_id == spec.value
return inner
@staticmethod
def by_group_id_filter_in(spec: UUIDInMatchSpec) -> QueryCondition:
"""Factory for group (project) ID IN filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.negated:
return SessionRow.group_id.notin_(spec.values)
return SessionRow.group_id.in_(spec.values)
return inner
@staticmethod
def by_agent_id(agent_id: AgentId) -> QueryCondition:
"""Filter sessions that have kernels running on the given agent."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
return sa.literal(agent_id) == sa.any_(SessionRow.agent_ids)
return inner
@staticmethod
def by_cursor_forward(cursor_id: str) -> QueryCondition:
"""Cursor condition for forward pagination (after cursor).
Uses subquery to get created_at of the cursor row and compare.
"""
def inner() -> sa.sql.expression.ColumnElement[bool]:
subquery = (
sa.select(SessionRow.created_at).where(SessionRow.id == cursor_id).scalar_subquery()
)
return SessionRow.created_at < subquery
return inner
@staticmethod
def by_cursor_backward(cursor_id: str) -> QueryCondition:
"""Cursor condition for backward pagination (before cursor).
Uses subquery to get created_at of the cursor row and compare.
"""
def inner() -> sa.sql.expression.ColumnElement[bool]:
subquery = (
sa.select(SessionRow.created_at).where(SessionRow.id == cursor_id).scalar_subquery()
)
return SessionRow.created_at > subquery
return inner
class SessionOrders:
"""Query orders for sessions."""
@staticmethod
def created_at(ascending: bool = True) -> QueryOrder:
if ascending:
return SessionRow.created_at.asc()
return SessionRow.created_at.desc()
@staticmethod
def id(ascending: bool = True) -> QueryOrder:
if ascending:
return SessionRow.id.asc()
return SessionRow.id.desc()
@staticmethod
def terminated_at(ascending: bool = True) -> QueryOrder:
if ascending:
return SessionRow.terminated_at.asc()
return SessionRow.terminated_at.desc()
@staticmethod
def status(ascending: bool = True) -> QueryOrder:
if ascending:
return SessionRow.status.asc()
return SessionRow.status.desc()
@staticmethod
def name(ascending: bool = True) -> QueryOrder:
if ascending:
return SessionRow.name.asc()
return SessionRow.name.desc()
class KernelConditions:
"""Query conditions for kernels."""
@staticmethod
def by_id(kernel_id: KernelId) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
return KernelRow.id == kernel_id
return inner
@staticmethod
def by_ids(kernel_ids: Collection[KernelId]) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
return KernelRow.id.in_(kernel_ids)
return inner
@staticmethod
def by_id_filter_equals(spec: UUIDEqualMatchSpec) -> QueryCondition:
"""Factory for id equality filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.negated:
return KernelRow.id != KernelId(spec.value)
return KernelRow.id == KernelId(spec.value)
return inner
@staticmethod
def by_id_filter_in(spec: UUIDInMatchSpec) -> QueryCondition:
"""Factory for id IN filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
kernel_ids = [KernelId(v) for v in spec.values]
if spec.negated:
return KernelRow.id.notin_(kernel_ids)
return KernelRow.id.in_(kernel_ids)
return inner
@staticmethod
def by_session_ids(session_ids: Collection[SessionId]) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
return KernelRow.session_id.in_(session_ids)
return inner
@staticmethod
def by_session_id_filter_equals(spec: UUIDEqualMatchSpec) -> QueryCondition:
"""Factory for session_id equality filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.negated:
return KernelRow.session_id != SessionId(spec.value)
return KernelRow.session_id == SessionId(spec.value)
return inner
@staticmethod
def by_session_id_filter_in(spec: UUIDInMatchSpec) -> QueryCondition:
"""Factory for session_id IN filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
session_ids = [SessionId(v) for v in spec.values]
if spec.negated:
return KernelRow.session_id.notin_(session_ids)
return KernelRow.session_id.in_(session_ids)
return inner
@staticmethod
def by_statuses(statuses: Collection[KernelStatus]) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
return KernelRow.status.in_(statuses)
return inner
@staticmethod
def by_status_filter_in(spec: KernelStatusInMatchSpec) -> QueryCondition:
"""Factory for status IN filter."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
if spec.negated:
return KernelRow.status.notin_(spec.values)
return KernelRow.status.in_(spec.values)
return inner
@staticmethod
def by_scaling_group(scaling_group: str) -> QueryCondition:
"""Filter kernels by scaling group."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
return KernelRow.scaling_group == scaling_group
return inner
@staticmethod
def by_agent_id(agent_id: AgentId) -> QueryCondition:
"""Filter kernels by agent ID."""
def inner() -> sa.sql.expression.ColumnElement[bool]:
return KernelRow.agent == agent_id
return inner
@staticmethod
def by_cursor_forward(cursor_id: str) -> QueryCondition:
"""Cursor condition for forward pagination (after cursor).
Uses subquery to get created_at of the cursor row and compare.
"""
def inner() -> sa.sql.expression.ColumnElement[bool]:
subquery = (
sa.select(KernelRow.created_at).where(KernelRow.id == cursor_id).scalar_subquery()
)
return KernelRow.created_at < subquery
return inner
@staticmethod
def by_cursor_backward(cursor_id: str) -> QueryCondition:
"""Cursor condition for backward pagination (before cursor).
Uses subquery to get created_at of the cursor row and compare.
"""
def inner() -> sa.sql.expression.ColumnElement[bool]:
subquery = (
sa.select(KernelRow.created_at).where(KernelRow.id == cursor_id).scalar_subquery()
)
return KernelRow.created_at > subquery
return inner
@staticmethod
def for_fair_share_observation(
scaling_group: str,
) -> QueryCondition:
"""Filter kernels that need fair share observation.
Includes:
1. Running kernels (terminated_at IS NULL) with starts_at set
2. Recently terminated kernels with unobserved periods
(terminated_at > last_observed_at, within lookback window)
The lookback_days is fetched from scaling_groups.fair_share_spec via subquery.
Args:
scaling_group: The scaling group to filter
Returns:
QueryCondition for fair share observation targets
"""
def inner() -> sa.sql.expression.ColumnElement[bool]:
# Subquery to get lookback_days from scaling_group's fair_share_spec
# Falls back to DEFAULT_LOOKBACK_DAYS if not set
lookback_days_subquery = (
sa.select(
sa.func.coalesce(
sa.cast(
ScalingGroupRow.fair_share_spec["lookback_days"].as_string(),
sa.Integer,
),
DEFAULT_LOOKBACK_DAYS,
)
)
.where(ScalingGroupRow.name == scaling_group)
.scalar_subquery()
)
# Calculate lookback cutoff using the subquery
lookback_cutoff = sa.func.now() - sa.func.make_interval(0, 0, 0, lookback_days_subquery)
return sa.and_(
KernelRow.scaling_group == scaling_group,
KernelRow.starts_at.isnot(None), # Must have started
sa.or_(
# Running kernels (not yet terminated)
KernelRow.terminated_at.is_(None),
# Terminated kernels with unobserved period, within lookback
sa.and_(
KernelRow.terminated_at
> sa.func.coalesce(
KernelRow.last_observed_at,
KernelRow.starts_at,
),
KernelRow.terminated_at >= lookback_cutoff,
),
),
)
return inner
class KernelOrders:
"""Query orders for kernels."""
@staticmethod
def cluster_idx(ascending: bool = True) -> QueryOrder:
if ascending:
return KernelRow.cluster_idx.asc()
return KernelRow.cluster_idx.desc()
@staticmethod
def created_at(ascending: bool = True) -> QueryOrder:
if ascending:
return KernelRow.created_at.asc()
return KernelRow.created_at.desc()
@staticmethod
def terminated_at(ascending: bool = True) -> QueryOrder:
if ascending:
return KernelRow.terminated_at.asc()
return KernelRow.terminated_at.desc()
@staticmethod
def status(ascending: bool = True) -> QueryOrder:
if ascending:
return KernelRow.status.asc()
return KernelRow.status.desc()
@staticmethod
def cluster_mode(ascending: bool = True) -> QueryOrder:
if ascending:
return KernelRow.cluster_mode.asc()
return KernelRow.cluster_mode.desc()
@staticmethod
def cluster_hostname(ascending: bool = True) -> QueryOrder:
if ascending:
return KernelRow.cluster_hostname.asc()
return KernelRow.cluster_hostname.desc()
class UserConditions:
"""Query conditions for users."""
@staticmethod
def by_uuids(user_uuids: Collection[UUID]) -> QueryCondition:
def inner() -> sa.sql.expression.ColumnElement[bool]:
return UserRow.uuid.in_(user_uuids)
return inner
class ImageConditions:
"""Query conditions for images."""
@staticmethod
def by_identifiers(identifiers: Collection[tuple[str, str]]) -> QueryCondition:
"""Filter images by list of (canonical, architecture) tuples.
Args:
identifiers: Collection of (canonical, architecture) tuples
Returns:
QueryCondition that matches any of the identifiers
"""
def inner() -> sa.sql.expression.ColumnElement[bool]:
if not identifiers:
return sa.literal(False)
conditions = [
sa.and_(
ImageRow.name == canonical,
ImageRow.architecture == architecture,
)
for canonical, architecture in identifiers
]
return sa.or_(*conditions)
return inner