plexus-python-pulse-demeter 1.1.140__py3-none-any.whl → 1.1.142__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- plexus/pulse/demeter/app/api/robotic/catalog_endpoints.py +642 -74
- plexus/pulse/demeter/app/api/robotic/pipeline_endpoints.py +132 -67
- plexus/pulse/demeter/app/models/__init__.py +22 -3
- plexus/pulse/demeter/app/models/autodrive/catalog.py +0 -9
- plexus/pulse/demeter/app/models/autodrive/inventory.py +0 -9
- plexus/pulse/demeter/app/models/autodrive/tagging.py +0 -21
- plexus/pulse/demeter/app/models/robotic/catalog.py +14 -39
- plexus/pulse/demeter/app/models/robotic/pipeline.py +8 -34
- plexus/pulse/demeter/tools/main.py +1 -1
- plexus/pulse/demeter/utils/datasetutils.py +679 -0
- plexus/pulse/demeter/utils/datautils.py +10 -674
- {plexus_python_pulse_demeter-1.1.140.dist-info → plexus_python_pulse_demeter-1.1.142.dist-info}/METADATA +1 -1
- {plexus_python_pulse_demeter-1.1.140.dist-info → plexus_python_pulse_demeter-1.1.142.dist-info}/RECORD +16 -15
- {plexus_python_pulse_demeter-1.1.140.dist-info → plexus_python_pulse_demeter-1.1.142.dist-info}/WHEEL +0 -0
- {plexus_python_pulse_demeter-1.1.140.dist-info → plexus_python_pulse_demeter-1.1.142.dist-info}/entry_points.txt +0 -0
- {plexus_python_pulse_demeter-1.1.140.dist-info → plexus_python_pulse_demeter-1.1.142.dist-info}/top_level.txt +0 -0
|
@@ -1,34 +1,45 @@
|
|
|
1
1
|
import datetime
|
|
2
2
|
import uuid as py_uuid
|
|
3
|
-
from collections.abc import Callable
|
|
4
|
-
from typing import Literal
|
|
3
|
+
from collections.abc import Callable, Iterable
|
|
4
|
+
from typing import Literal, Self
|
|
5
5
|
|
|
6
6
|
import pydantic as pdt
|
|
7
7
|
import sqlalchemy as sa
|
|
8
8
|
import sqlalchemy.exc as sa_exc
|
|
9
9
|
import sqlalchemy.orm as sa_orm
|
|
10
|
-
from fastapi import APIRouter, Depends, HTTPException
|
|
11
|
-
from
|
|
10
|
+
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
11
|
+
from jsonschema import Draft202012Validator
|
|
12
|
+
from jsonschema.exceptions import SchemaError, ValidationError
|
|
13
|
+
from plexus.pulse.common.utils.apiutils import database_transaction
|
|
14
|
+
from plexus.pulse.common.utils.ormutils import make_changing_model_mixin, make_revision_model_mixin
|
|
12
15
|
from sqlmodel import SQLModel
|
|
13
16
|
|
|
14
17
|
from plexus.pulse.demeter.app.api import Pagination
|
|
15
18
|
from plexus.pulse.demeter.app.core.database import make_session
|
|
16
|
-
from plexus.pulse.demeter.app.models.robotic.catalog import Artifact, DELETED_FLAG
|
|
17
19
|
from plexus.pulse.demeter.app.models.robotic.catalog import (
|
|
20
|
+
Acceptance,
|
|
21
|
+
Artifact,
|
|
18
22
|
ArtifactFormatSpecTable,
|
|
19
23
|
ArtifactTable,
|
|
20
24
|
Bag,
|
|
25
|
+
BagFormatSpec,
|
|
21
26
|
BagFormatSpecTable,
|
|
22
27
|
BagTable,
|
|
23
28
|
Episode,
|
|
29
|
+
EpisodeFormatSpec,
|
|
24
30
|
EpisodeFormatSpecTable,
|
|
25
31
|
EpisodeTable,
|
|
32
|
+
Robot,
|
|
33
|
+
RobotSetupSpec,
|
|
34
|
+
RobotSetupSpecTable,
|
|
26
35
|
RobotTable,
|
|
27
36
|
Tag,
|
|
28
37
|
TagTable,
|
|
29
38
|
TagsetSpecTable,
|
|
30
39
|
TagsetTable,
|
|
31
40
|
)
|
|
41
|
+
from plexus.pulse.demeter.app.models.robotic.catalog import DELETED_FLAG
|
|
42
|
+
from plexus.pulse.demeter.app.models.robotic.pipeline import PipelineTable
|
|
32
43
|
|
|
33
44
|
router = APIRouter(prefix="/pulse_demeter/robotic/catalog", tags=["robotic", "catalog"])
|
|
34
45
|
|
|
@@ -52,22 +63,568 @@ def get_uuid_generator() -> Callable[[type[SQLModel]], py_uuid.UUID]:
|
|
|
52
63
|
return uuid_generator
|
|
53
64
|
|
|
54
65
|
|
|
66
|
+
class RobotWithTitles(Robot, make_revision_model_mixin()):
|
|
67
|
+
setup_spec_title: str | None = None
|
|
68
|
+
|
|
69
|
+
@classmethod
|
|
70
|
+
def with_titles(cls, db: sa_orm.Session, rows: Iterable[RobotTable]) -> list[Self]:
|
|
71
|
+
rows = list(rows)
|
|
72
|
+
if not rows:
|
|
73
|
+
return []
|
|
74
|
+
setup_spec_names = {row.setup_spec for row in rows}
|
|
75
|
+
setup_spec_titles = dict(
|
|
76
|
+
db
|
|
77
|
+
.query(RobotSetupSpecTable.name, RobotSetupSpecTable.title)
|
|
78
|
+
.filter(RobotSetupSpecTable.name.in_(setup_spec_names))
|
|
79
|
+
.all()
|
|
80
|
+
)
|
|
81
|
+
result = []
|
|
82
|
+
for row in rows:
|
|
83
|
+
item = cls.model_validate(row.model_dump())
|
|
84
|
+
item.setup_spec_title = setup_spec_titles.get(row.setup_spec)
|
|
85
|
+
result.append(item)
|
|
86
|
+
return result
|
|
87
|
+
|
|
88
|
+
|
|
55
89
|
class BagWithTitles(Bag, make_changing_model_mixin()):
|
|
56
90
|
format_spec_title: str | None = None
|
|
57
91
|
|
|
92
|
+
@classmethod
|
|
93
|
+
def with_titles(cls, db: sa_orm.Session, rows: Iterable[BagTable]) -> list[Self]:
|
|
94
|
+
rows = list(rows)
|
|
95
|
+
if not rows:
|
|
96
|
+
return []
|
|
97
|
+
format_spec_names = {row.format_spec for row in rows}
|
|
98
|
+
format_spec_titles = dict(
|
|
99
|
+
db
|
|
100
|
+
.query(BagFormatSpecTable.name, BagFormatSpecTable.title)
|
|
101
|
+
.filter(BagFormatSpecTable.name.in_(format_spec_names))
|
|
102
|
+
.all()
|
|
103
|
+
)
|
|
104
|
+
result = []
|
|
105
|
+
for row in rows:
|
|
106
|
+
item = cls.model_validate(row.model_dump())
|
|
107
|
+
item.format_spec_title = format_spec_titles.get(row.format_spec)
|
|
108
|
+
result.append(item)
|
|
109
|
+
return result
|
|
110
|
+
|
|
58
111
|
|
|
59
112
|
class EpisodeWithTitles(Episode, make_changing_model_mixin()):
|
|
60
113
|
format_spec_title: str | None = None
|
|
61
114
|
|
|
115
|
+
@classmethod
|
|
116
|
+
def with_titles(cls, db: sa_orm.Session, rows: Iterable[EpisodeTable]) -> list[Self]:
|
|
117
|
+
rows = list(rows)
|
|
118
|
+
if not rows:
|
|
119
|
+
return []
|
|
120
|
+
format_spec_names = {row.format_spec for row in rows}
|
|
121
|
+
format_spec_titles = dict(
|
|
122
|
+
db
|
|
123
|
+
.query(EpisodeFormatSpecTable.name, EpisodeFormatSpecTable.title)
|
|
124
|
+
.filter(EpisodeFormatSpecTable.name.in_(format_spec_names))
|
|
125
|
+
.all()
|
|
126
|
+
)
|
|
127
|
+
result = []
|
|
128
|
+
for row in rows:
|
|
129
|
+
item = cls.model_validate(row.model_dump())
|
|
130
|
+
item.format_spec_title = format_spec_titles.get(row.format_spec)
|
|
131
|
+
result.append(item)
|
|
132
|
+
return result
|
|
133
|
+
|
|
62
134
|
|
|
63
135
|
class TagWithTitles(Tag, make_changing_model_mixin()):
|
|
64
136
|
title: str | None = None
|
|
65
137
|
tagset_spec_title: str | None = None
|
|
66
138
|
|
|
139
|
+
@classmethod
|
|
140
|
+
def with_titles(cls, db: sa_orm.Session, rows: Iterable[TagTable]) -> list[Self]:
|
|
141
|
+
rows = list(rows)
|
|
142
|
+
if not rows:
|
|
143
|
+
return []
|
|
144
|
+
tagset_spec_names = {row.tagset_spec for row in rows}
|
|
145
|
+
tagset_spec_titles = dict(
|
|
146
|
+
db
|
|
147
|
+
.query(TagsetSpecTable.name, TagsetSpecTable.title)
|
|
148
|
+
.filter(TagsetSpecTable.name.in_(tagset_spec_names))
|
|
149
|
+
.all()
|
|
150
|
+
)
|
|
151
|
+
keys = {(row.tagset_spec, row.tag) for row in rows}
|
|
152
|
+
titles = {
|
|
153
|
+
(namespace, tag): title
|
|
154
|
+
for namespace, tag, title in (
|
|
155
|
+
db
|
|
156
|
+
.query(TagsetTable.tagset_spec, TagsetTable.tag, TagsetTable.title)
|
|
157
|
+
.filter(sa.tuple_(TagsetTable.tagset_spec, TagsetTable.tag).in_(keys))
|
|
158
|
+
.all()
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
result = []
|
|
162
|
+
for row in rows:
|
|
163
|
+
item = cls.model_validate(row.model_dump())
|
|
164
|
+
item.tagset_spec_title = tagset_spec_titles.get(row.tagset_spec)
|
|
165
|
+
item.title = titles.get((row.tagset_spec, row.tag))
|
|
166
|
+
result.append(item)
|
|
167
|
+
return result
|
|
168
|
+
|
|
67
169
|
|
|
68
170
|
class ArtifactWithTitles(Artifact, make_changing_model_mixin()):
|
|
69
171
|
format_spec_title: str | None = None
|
|
70
172
|
|
|
173
|
+
@classmethod
|
|
174
|
+
def with_titles(cls, db: sa_orm.Session, rows: Iterable[ArtifactTable]) -> list[Self]:
|
|
175
|
+
rows = list(rows)
|
|
176
|
+
if not rows:
|
|
177
|
+
return []
|
|
178
|
+
format_spec_names = {row.format_spec for row in rows}
|
|
179
|
+
format_spec_titles = dict(
|
|
180
|
+
db
|
|
181
|
+
.query(ArtifactFormatSpecTable.name, ArtifactFormatSpecTable.title)
|
|
182
|
+
.filter(ArtifactFormatSpecTable.name.in_(format_spec_names))
|
|
183
|
+
.all()
|
|
184
|
+
)
|
|
185
|
+
result = []
|
|
186
|
+
for row in rows:
|
|
187
|
+
item = cls.model_validate(row.model_dump())
|
|
188
|
+
item.format_spec_title = format_spec_titles.get(row.format_spec)
|
|
189
|
+
result.append(item)
|
|
190
|
+
return result
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
@router.post("/robot_setup_specs/", response_model=RobotSetupSpecTable, status_code=201)
|
|
194
|
+
def add_robot_setup_spec(
|
|
195
|
+
spec: RobotSetupSpec,
|
|
196
|
+
db: sa_orm.Session = Depends(make_session),
|
|
197
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
198
|
+
) -> RobotSetupSpecTable:
|
|
199
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
200
|
+
current_dt = clock(db)
|
|
201
|
+
db_spec = RobotSetupSpecTable(**spec.model_dump(), created_at=current_dt, updated_at=current_dt)
|
|
202
|
+
db.add(db_spec)
|
|
203
|
+
db.flush()
|
|
204
|
+
return db_spec
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
@router.get("/robot_setup_specs/", response_model=list[RobotSetupSpecTable])
|
|
208
|
+
def list_robot_setup_specs(db: sa_orm.Session = Depends(make_session)) -> list[RobotSetupSpecTable]:
|
|
209
|
+
return db.query(RobotSetupSpecTable).order_by(RobotSetupSpecTable.name).all()
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
@router.get("/robot_setup_specs/{name}", response_model=RobotSetupSpecTable)
|
|
213
|
+
def read_robot_setup_spec(name: str, db: sa_orm.Session = Depends(make_session)) -> RobotSetupSpecTable:
|
|
214
|
+
db_spec = db.query(RobotSetupSpecTable).filter(RobotSetupSpecTable.name == name).one_or_none()
|
|
215
|
+
if db_spec is None:
|
|
216
|
+
raise HTTPException(status_code=404, detail="robot_setup_spec not found")
|
|
217
|
+
return db_spec
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
@router.post("/bag_format_specs/", response_model=BagFormatSpecTable, status_code=201)
|
|
221
|
+
def add_bag_format_spec(
|
|
222
|
+
spec: BagFormatSpec,
|
|
223
|
+
db: sa_orm.Session = Depends(make_session),
|
|
224
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
225
|
+
) -> BagFormatSpecTable:
|
|
226
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
227
|
+
current_dt = clock(db)
|
|
228
|
+
db_spec = BagFormatSpecTable(**spec.model_dump(), created_at=current_dt, updated_at=current_dt)
|
|
229
|
+
db.add(db_spec)
|
|
230
|
+
db.flush()
|
|
231
|
+
return db_spec
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
@router.get("/bag_format_specs/", response_model=list[BagFormatSpecTable])
|
|
235
|
+
def list_bag_format_specs(db: sa_orm.Session = Depends(make_session)) -> list[BagFormatSpecTable]:
|
|
236
|
+
return db.query(BagFormatSpecTable).order_by(BagFormatSpecTable.name).all()
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
@router.get("/bag_format_specs/{name}", response_model=BagFormatSpecTable)
|
|
240
|
+
def read_bag_format_spec(name: str, db: sa_orm.Session = Depends(make_session)) -> BagFormatSpecTable:
|
|
241
|
+
db_spec = db.query(BagFormatSpecTable).filter(BagFormatSpecTable.name == name).one_or_none()
|
|
242
|
+
if db_spec is None:
|
|
243
|
+
raise HTTPException(status_code=404, detail="bag_format_spec not found")
|
|
244
|
+
return db_spec
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
@router.post("/episode_format_specs/", response_model=EpisodeFormatSpecTable, status_code=201)
|
|
248
|
+
def add_episode_format_spec(
|
|
249
|
+
spec: EpisodeFormatSpec,
|
|
250
|
+
db: sa_orm.Session = Depends(make_session),
|
|
251
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
252
|
+
) -> EpisodeFormatSpecTable:
|
|
253
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
254
|
+
current_dt = clock(db)
|
|
255
|
+
db_spec = EpisodeFormatSpecTable(**spec.model_dump(), created_at=current_dt, updated_at=current_dt)
|
|
256
|
+
db.add(db_spec)
|
|
257
|
+
db.flush()
|
|
258
|
+
return db_spec
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
@router.get("/episode_format_specs/", response_model=list[EpisodeFormatSpecTable])
|
|
262
|
+
def list_episode_format_specs(db: sa_orm.Session = Depends(make_session)) -> list[EpisodeFormatSpecTable]:
|
|
263
|
+
return db.query(EpisodeFormatSpecTable).order_by(EpisodeFormatSpecTable.name).all()
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
@router.get("/episode_format_specs/{name}", response_model=EpisodeFormatSpecTable)
|
|
267
|
+
def read_episode_format_spec(name: str, db: sa_orm.Session = Depends(make_session)) -> EpisodeFormatSpecTable:
|
|
268
|
+
db_spec = db.query(EpisodeFormatSpecTable).filter(EpisodeFormatSpecTable.name == name).one_or_none()
|
|
269
|
+
if db_spec is None:
|
|
270
|
+
raise HTTPException(status_code=404, detail="episode_format_spec not found")
|
|
271
|
+
return db_spec
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def _validate_components(schema: dict, components: dict):
|
|
275
|
+
try:
|
|
276
|
+
Draft202012Validator.check_schema(schema)
|
|
277
|
+
Draft202012Validator(schema).validate(components)
|
|
278
|
+
except (SchemaError, ValidationError) as exc:
|
|
279
|
+
raise HTTPException(status_code=422, detail=str(exc)) from exc
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
@router.post("/robots/", response_model=RobotWithTitles, status_code=201)
|
|
283
|
+
def register_robot(
|
|
284
|
+
robot: Robot,
|
|
285
|
+
db: sa_orm.Session = Depends(make_session),
|
|
286
|
+
generate_uuid: Callable[[type[SQLModel]], py_uuid.UUID] = Depends(get_uuid_generator),
|
|
287
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
288
|
+
) -> RobotWithTitles:
|
|
289
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
290
|
+
db_spec = db.query(RobotSetupSpecTable).filter(RobotSetupSpecTable.name == robot.setup_spec).one_or_none()
|
|
291
|
+
if db_spec is None:
|
|
292
|
+
raise HTTPException(status_code=404, detail="robot_setup_spec not found")
|
|
293
|
+
|
|
294
|
+
_validate_components(db_spec.components_schema, robot.components)
|
|
295
|
+
|
|
296
|
+
current_dt = clock(db)
|
|
297
|
+
|
|
298
|
+
values = robot.model_dump()
|
|
299
|
+
if "uuid" not in robot.model_fields_set:
|
|
300
|
+
values["uuid"] = generate_uuid(RobotTable)
|
|
301
|
+
|
|
302
|
+
db_robot = RobotTable(**values, revision=1, created_at=current_dt, updated_at=current_dt)
|
|
303
|
+
db.add(db_robot)
|
|
304
|
+
db.flush()
|
|
305
|
+
db_robot.record_sqn = db_robot.sqn
|
|
306
|
+
db.flush()
|
|
307
|
+
|
|
308
|
+
return RobotWithTitles.with_titles(db, [db_robot])[0]
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
class RobotTablesAndPagination(pdt.BaseModel):
|
|
312
|
+
robots: list[RobotWithTitles]
|
|
313
|
+
pagination: Pagination
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
@router.get("/robots/", response_model=RobotTablesAndPagination)
|
|
317
|
+
def list_robots(
|
|
318
|
+
match: str | None = Query(None,
|
|
319
|
+
max_length=256,
|
|
320
|
+
description="Case-insensitive literal substring of the robot alias"),
|
|
321
|
+
setup_spec: str | None = Query(None, max_length=128, description="Exact versioned robot type name"),
|
|
322
|
+
skip: int = Query(0, ge=0),
|
|
323
|
+
limit: int = Query(100, ge=1, le=1000),
|
|
324
|
+
db: sa_orm.Session = Depends(make_session),
|
|
325
|
+
) -> RobotTablesAndPagination:
|
|
326
|
+
query_stmt = db.query(RobotTable).filter(RobotTable.expired_at.is_(None))
|
|
327
|
+
if match is not None:
|
|
328
|
+
query_stmt = query_stmt.filter(RobotTable.alias.icontains(match, autoescape=True))
|
|
329
|
+
if setup_spec is not None:
|
|
330
|
+
query_stmt = query_stmt.filter(RobotTable.setup_spec == setup_spec)
|
|
331
|
+
|
|
332
|
+
total = query_stmt.count()
|
|
333
|
+
# Keep pagination stable across databases with different default collations.
|
|
334
|
+
db_robots = query_stmt.order_by(RobotTable.alias.collate("C"), RobotTable.sqn).offset(skip).limit(limit).all()
|
|
335
|
+
|
|
336
|
+
return RobotTablesAndPagination(
|
|
337
|
+
robots=RobotWithTitles.with_titles(db, db_robots),
|
|
338
|
+
pagination=Pagination(total=total, skip=skip, limit=limit),
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
@router.get("/robots/{robot_uuid}", response_model=RobotWithTitles)
|
|
343
|
+
def read_robot(robot_uuid: py_uuid.UUID, db: sa_orm.Session = Depends(make_session)) -> RobotWithTitles:
|
|
344
|
+
db_robot = (
|
|
345
|
+
db
|
|
346
|
+
.query(RobotTable)
|
|
347
|
+
.filter(RobotTable.uuid == robot_uuid, RobotTable.expired_at.is_(None))
|
|
348
|
+
.one_or_none()
|
|
349
|
+
)
|
|
350
|
+
if db_robot is None:
|
|
351
|
+
raise HTTPException(status_code=404, detail="robot not found")
|
|
352
|
+
return RobotWithTitles.with_titles(db, [db_robot])[0]
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
@router.put("/robots/{robot_uuid}", response_model=RobotWithTitles)
|
|
356
|
+
def modify_robot(
|
|
357
|
+
robot_uuid: py_uuid.UUID,
|
|
358
|
+
robot: Robot,
|
|
359
|
+
db: sa_orm.Session = Depends(make_session),
|
|
360
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
361
|
+
) -> RobotWithTitles:
|
|
362
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
363
|
+
db_robot = (
|
|
364
|
+
db
|
|
365
|
+
.query(RobotTable)
|
|
366
|
+
.filter(RobotTable.uuid == robot_uuid, RobotTable.expired_at.is_(None))
|
|
367
|
+
.with_for_update()
|
|
368
|
+
.one_or_none()
|
|
369
|
+
)
|
|
370
|
+
if db_robot is None:
|
|
371
|
+
raise HTTPException(status_code=404, detail="robot not found")
|
|
372
|
+
if robot.uuid != robot_uuid or robot.alias != db_robot.alias:
|
|
373
|
+
raise HTTPException(status_code=409, detail="Robot UUID and alias must remain stable")
|
|
374
|
+
|
|
375
|
+
db_spec = db.query(RobotSetupSpecTable).filter(RobotSetupSpecTable.name == robot.setup_spec).one_or_none()
|
|
376
|
+
if db_spec is None:
|
|
377
|
+
raise HTTPException(status_code=404, detail="robot_setup_spec not found")
|
|
378
|
+
|
|
379
|
+
_validate_components(db_spec.components_schema, robot.components)
|
|
380
|
+
|
|
381
|
+
current_dt = clock(db)
|
|
382
|
+
|
|
383
|
+
db_robot.expired_at = current_dt
|
|
384
|
+
db.flush()
|
|
385
|
+
|
|
386
|
+
db_new_robot = RobotTable(
|
|
387
|
+
**robot.model_dump(),
|
|
388
|
+
record_sqn=db_robot.record_sqn,
|
|
389
|
+
revision=db_robot.revision + 1,
|
|
390
|
+
created_at=db_robot.created_at,
|
|
391
|
+
updated_at=current_dt,
|
|
392
|
+
)
|
|
393
|
+
db.add(db_new_robot)
|
|
394
|
+
db.flush()
|
|
395
|
+
|
|
396
|
+
return RobotWithTitles.with_titles(db, [db_new_robot])[0]
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
@router.post("/bags/", response_model=BagWithTitles, status_code=201)
|
|
400
|
+
def register_bag(
|
|
401
|
+
bag: Bag,
|
|
402
|
+
db: sa_orm.Session = Depends(make_session),
|
|
403
|
+
generate_uuid: Callable[[type[SQLModel]], py_uuid.UUID] = Depends(get_uuid_generator),
|
|
404
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
405
|
+
) -> BagWithTitles:
|
|
406
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
407
|
+
if bag.flags & DELETED_FLAG:
|
|
408
|
+
raise HTTPException(status_code=422, detail="Use DELETE to mark a resource as deleted")
|
|
409
|
+
|
|
410
|
+
db_spec = db.query(BagFormatSpecTable).filter(BagFormatSpecTable.name == bag.format_spec).one_or_none()
|
|
411
|
+
if db_spec is None:
|
|
412
|
+
raise HTTPException(status_code=404, detail="bag_format_spec not found")
|
|
413
|
+
|
|
414
|
+
db_robot = (
|
|
415
|
+
db
|
|
416
|
+
.query(RobotTable)
|
|
417
|
+
.filter(RobotTable.alias == bag.robot_alias, RobotTable.expired_at.is_(None))
|
|
418
|
+
.one_or_none()
|
|
419
|
+
)
|
|
420
|
+
if db_robot is None:
|
|
421
|
+
raise HTTPException(status_code=404, detail="robot not found")
|
|
422
|
+
|
|
423
|
+
if db.query(PipelineTable).filter(PipelineTable.name == bag.pipeline_name).one_or_none() is None:
|
|
424
|
+
raise HTTPException(status_code=404, detail="pipeline not found")
|
|
425
|
+
|
|
426
|
+
current_dt = clock(db)
|
|
427
|
+
|
|
428
|
+
values = bag.model_dump()
|
|
429
|
+
if "uuid" not in bag.model_fields_set:
|
|
430
|
+
values["uuid"] = generate_uuid(BagTable)
|
|
431
|
+
|
|
432
|
+
db_bag = BagTable(**values, created_at=current_dt, updated_at=current_dt)
|
|
433
|
+
db.add(db_bag)
|
|
434
|
+
db.flush()
|
|
435
|
+
|
|
436
|
+
return BagWithTitles.with_titles(db, [db_bag])[0]
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
@router.get("/bags/{bag_uuid}", response_model=BagWithTitles)
|
|
440
|
+
def read_bag(bag_uuid: py_uuid.UUID, db: sa_orm.Session = Depends(make_session)) -> BagWithTitles:
|
|
441
|
+
db_bag = db.query(BagTable).filter(BagTable.uuid == bag_uuid).one_or_none()
|
|
442
|
+
if db_bag is None:
|
|
443
|
+
raise HTTPException(status_code=404, detail="bag not found")
|
|
444
|
+
return BagWithTitles.with_titles(db, [db_bag])[0]
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
@router.put("/bags/{bag_uuid}", response_model=BagWithTitles)
|
|
448
|
+
def modify_bag(
|
|
449
|
+
bag_uuid: py_uuid.UUID,
|
|
450
|
+
bag: Bag,
|
|
451
|
+
db: sa_orm.Session = Depends(make_session),
|
|
452
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
453
|
+
) -> BagWithTitles:
|
|
454
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
455
|
+
db_bag = db.query(BagTable).filter(BagTable.uuid == bag_uuid).with_for_update().one_or_none()
|
|
456
|
+
if db_bag is None:
|
|
457
|
+
raise HTTPException(status_code=404, detail="bag not found")
|
|
458
|
+
if bag.uuid != bag_uuid:
|
|
459
|
+
raise HTTPException(status_code=409, detail="Bag UUID must remain stable")
|
|
460
|
+
if db_bag.flags & DELETED_FLAG:
|
|
461
|
+
raise HTTPException(status_code=409, detail="Bag is deleted")
|
|
462
|
+
if bag.flags & DELETED_FLAG:
|
|
463
|
+
raise HTTPException(status_code=422, detail="Use DELETE to mark a resource as deleted")
|
|
464
|
+
|
|
465
|
+
db_spec = db.query(BagFormatSpecTable).filter(BagFormatSpecTable.name == bag.format_spec).one_or_none()
|
|
466
|
+
if db_spec is None:
|
|
467
|
+
raise HTTPException(status_code=404, detail="bag_format_spec not found")
|
|
468
|
+
|
|
469
|
+
db_robot = (
|
|
470
|
+
db
|
|
471
|
+
.query(RobotTable)
|
|
472
|
+
.filter(RobotTable.alias == bag.robot_alias, RobotTable.expired_at.is_(None))
|
|
473
|
+
.one_or_none()
|
|
474
|
+
)
|
|
475
|
+
if db_robot is None:
|
|
476
|
+
raise HTTPException(status_code=404, detail="robot not found")
|
|
477
|
+
|
|
478
|
+
if db.query(PipelineTable).filter(PipelineTable.name == bag.pipeline_name).one_or_none() is None:
|
|
479
|
+
raise HTTPException(status_code=404, detail="pipeline not found")
|
|
480
|
+
|
|
481
|
+
for key, value in bag.model_dump().items():
|
|
482
|
+
setattr(db_bag, key, value)
|
|
483
|
+
db_bag.updated_at = clock(db)
|
|
484
|
+
db.flush()
|
|
485
|
+
|
|
486
|
+
return BagWithTitles.with_titles(db, [db_bag])[0]
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
@router.delete("/bags/{bag_uuid}", response_model=BagWithTitles)
|
|
490
|
+
def delete_bag(
|
|
491
|
+
bag_uuid: py_uuid.UUID,
|
|
492
|
+
db: sa_orm.Session = Depends(make_session),
|
|
493
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
494
|
+
) -> BagWithTitles:
|
|
495
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
496
|
+
db_bag = db.query(BagTable).filter(BagTable.uuid == bag_uuid).with_for_update().one_or_none()
|
|
497
|
+
if db_bag is None:
|
|
498
|
+
raise HTTPException(status_code=404, detail="bag not found")
|
|
499
|
+
|
|
500
|
+
if not db_bag.flags & DELETED_FLAG:
|
|
501
|
+
db_bag.flags |= DELETED_FLAG
|
|
502
|
+
db_bag.updated_at = clock(db)
|
|
503
|
+
db.flush()
|
|
504
|
+
|
|
505
|
+
return BagWithTitles.with_titles(db, [db_bag])[0]
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
@router.post("/episodes/", response_model=EpisodeWithTitles, status_code=201)
|
|
509
|
+
def register_episode(
|
|
510
|
+
episode: Episode,
|
|
511
|
+
db: sa_orm.Session = Depends(make_session),
|
|
512
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
513
|
+
) -> EpisodeWithTitles:
|
|
514
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
515
|
+
if episode.flags & DELETED_FLAG:
|
|
516
|
+
raise HTTPException(status_code=422, detail="Use DELETE to mark a resource as deleted")
|
|
517
|
+
|
|
518
|
+
db_spec = (
|
|
519
|
+
db
|
|
520
|
+
.query(EpisodeFormatSpecTable)
|
|
521
|
+
.filter(EpisodeFormatSpecTable.name == episode.format_spec)
|
|
522
|
+
.one_or_none()
|
|
523
|
+
)
|
|
524
|
+
if db_spec is None:
|
|
525
|
+
raise HTTPException(status_code=404, detail="episode_format_spec not found")
|
|
526
|
+
|
|
527
|
+
db_bag = db.query(BagTable).filter(BagTable.uuid == episode.bag_uuid).one_or_none()
|
|
528
|
+
if db_bag is None:
|
|
529
|
+
raise HTTPException(status_code=404, detail="bag not found")
|
|
530
|
+
if db_bag.flags & DELETED_FLAG:
|
|
531
|
+
raise HTTPException(status_code=409, detail="Source bag is deleted")
|
|
532
|
+
|
|
533
|
+
current_dt = clock(db)
|
|
534
|
+
db_episode = EpisodeTable(**episode.model_dump(), created_at=current_dt, updated_at=current_dt)
|
|
535
|
+
db.add(db_episode)
|
|
536
|
+
db.flush()
|
|
537
|
+
|
|
538
|
+
return EpisodeWithTitles.with_titles(db, [db_episode])[0]
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
@router.get("/episodes/{bag_uuid}", response_model=EpisodeWithTitles)
|
|
542
|
+
def read_episode(bag_uuid: py_uuid.UUID, db: sa_orm.Session = Depends(make_session)) -> EpisodeWithTitles:
|
|
543
|
+
db_episode = db.query(EpisodeTable).filter(EpisodeTable.bag_uuid == bag_uuid).one_or_none()
|
|
544
|
+
if db_episode is None:
|
|
545
|
+
raise HTTPException(status_code=404, detail="episode not found")
|
|
546
|
+
return EpisodeWithTitles.with_titles(db, [db_episode])[0]
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
@router.put("/episodes/{bag_uuid}", response_model=EpisodeWithTitles)
|
|
550
|
+
def modify_episode(
|
|
551
|
+
bag_uuid: py_uuid.UUID,
|
|
552
|
+
episode: Episode,
|
|
553
|
+
db: sa_orm.Session = Depends(make_session),
|
|
554
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
555
|
+
) -> EpisodeWithTitles:
|
|
556
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
557
|
+
db_episode = db.query(EpisodeTable).filter(EpisodeTable.bag_uuid == bag_uuid).with_for_update().one_or_none()
|
|
558
|
+
if db_episode is None:
|
|
559
|
+
raise HTTPException(status_code=404, detail="episode not found")
|
|
560
|
+
if episode.bag_uuid != bag_uuid:
|
|
561
|
+
raise HTTPException(status_code=409, detail="Episode bag UUID must remain stable")
|
|
562
|
+
if db_episode.flags & DELETED_FLAG:
|
|
563
|
+
raise HTTPException(status_code=409, detail="Episode is deleted")
|
|
564
|
+
if episode.flags & DELETED_FLAG:
|
|
565
|
+
raise HTTPException(status_code=422, detail="Use DELETE to mark a resource as deleted")
|
|
566
|
+
|
|
567
|
+
db_spec = (
|
|
568
|
+
db
|
|
569
|
+
.query(EpisodeFormatSpecTable)
|
|
570
|
+
.filter(EpisodeFormatSpecTable.name == episode.format_spec)
|
|
571
|
+
.one_or_none()
|
|
572
|
+
)
|
|
573
|
+
if db_spec is None:
|
|
574
|
+
raise HTTPException(status_code=404, detail="episode_format_spec not found")
|
|
575
|
+
|
|
576
|
+
db_bag = db.query(BagTable).filter(BagTable.uuid == episode.bag_uuid).one_or_none()
|
|
577
|
+
if db_bag is None:
|
|
578
|
+
raise HTTPException(status_code=404, detail="bag not found")
|
|
579
|
+
if db_bag.flags & DELETED_FLAG:
|
|
580
|
+
raise HTTPException(status_code=409, detail="Source bag is deleted")
|
|
581
|
+
|
|
582
|
+
for key, value in episode.model_dump().items():
|
|
583
|
+
setattr(db_episode, key, value)
|
|
584
|
+
db_episode.updated_at = clock(db)
|
|
585
|
+
db.flush()
|
|
586
|
+
|
|
587
|
+
return EpisodeWithTitles.with_titles(db, [db_episode])[0]
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
@router.delete("/episodes/{bag_uuid}", response_model=EpisodeWithTitles)
|
|
591
|
+
def delete_episode(
|
|
592
|
+
bag_uuid: py_uuid.UUID,
|
|
593
|
+
db: sa_orm.Session = Depends(make_session),
|
|
594
|
+
clock: Callable[[sa_orm.Session], datetime.datetime] = Depends(get_clock),
|
|
595
|
+
) -> EpisodeWithTitles:
|
|
596
|
+
with database_transaction(db, redirect=exception_redirect):
|
|
597
|
+
db_episode = db.query(EpisodeTable).filter(EpisodeTable.bag_uuid == bag_uuid).with_for_update().one_or_none()
|
|
598
|
+
if db_episode is None:
|
|
599
|
+
raise HTTPException(status_code=404, detail="episode not found")
|
|
600
|
+
if not db_episode.flags & DELETED_FLAG:
|
|
601
|
+
db_episode.flags |= DELETED_FLAG
|
|
602
|
+
db_episode.updated_at = clock(db)
|
|
603
|
+
db.flush()
|
|
604
|
+
|
|
605
|
+
return EpisodeWithTitles.with_titles(db, [db_episode])[0]
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
class TagFilter(pdt.BaseModel):
|
|
609
|
+
model_config = pdt.ConfigDict(extra="forbid")
|
|
610
|
+
|
|
611
|
+
tagset_spec: str = pdt.Field(max_length=64)
|
|
612
|
+
tag: str = pdt.Field(max_length=128)
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
class ArtifactFilter(pdt.BaseModel):
|
|
616
|
+
model_config = pdt.ConfigDict(extra="forbid")
|
|
617
|
+
|
|
618
|
+
family: str | None = pdt.Field(None, max_length=32)
|
|
619
|
+
format_spec: str | None = pdt.Field(None, max_length=64)
|
|
620
|
+
acceptance: Acceptance
|
|
621
|
+
|
|
622
|
+
@pdt.model_validator(mode="after")
|
|
623
|
+
def validate_selector(self):
|
|
624
|
+
if (self.family is None) == (self.format_spec is None):
|
|
625
|
+
raise ValueError("Specify exactly one of family or format_spec")
|
|
626
|
+
return self
|
|
627
|
+
|
|
71
628
|
|
|
72
629
|
class EpisodeFilterSet(pdt.BaseModel):
|
|
73
630
|
model_config = pdt.ConfigDict(extra="forbid")
|
|
@@ -78,6 +635,8 @@ class EpisodeFilterSet(pdt.BaseModel):
|
|
|
78
635
|
robot_ids: list[str] | None = None
|
|
79
636
|
begin_frame_dt: pdt.AwareDatetime | None = None
|
|
80
637
|
end_frame_dt: pdt.AwareDatetime | None = None
|
|
638
|
+
tag_filters: list[TagFilter] = pdt.Field(default_factory=list)
|
|
639
|
+
artifact_filters: list[ArtifactFilter] = pdt.Field(default_factory=list)
|
|
81
640
|
|
|
82
641
|
@pdt.model_validator(mode="after")
|
|
83
642
|
def validate_interval(self):
|
|
@@ -108,6 +667,41 @@ def make_episode_filter_predicate_stmt(filter_set: EpisodeFilterSet):
|
|
|
108
667
|
RobotTable.expired_at.is_(None),
|
|
109
668
|
RobotTable.setup_spec.in_(filter_set.robot_types))
|
|
110
669
|
.correlate(BagTable))
|
|
670
|
+
for tag_filter in filter_set.tag_filters:
|
|
671
|
+
sub_predicate_stmts.append(sa
|
|
672
|
+
.exists()
|
|
673
|
+
.where(TagTable.bag_uuid == EpisodeTable.bag_uuid,
|
|
674
|
+
TagTable.tagset_spec == tag_filter.tagset_spec,
|
|
675
|
+
TagTable.tag == tag_filter.tag,
|
|
676
|
+
TagTable.flags.op("&")(DELETED_FLAG) == 0)
|
|
677
|
+
.correlate(EpisodeTable))
|
|
678
|
+
for artifact_filter in filter_set.artifact_filters:
|
|
679
|
+
artifact_stmt = (
|
|
680
|
+
sa
|
|
681
|
+
.select(ArtifactTable.acceptance)
|
|
682
|
+
.where(ArtifactTable.bag_uuid == EpisodeTable.bag_uuid, ArtifactTable.flags.op("&")(DELETED_FLAG) == 0)
|
|
683
|
+
)
|
|
684
|
+
if artifact_filter.family is not None:
|
|
685
|
+
artifact_stmt = (
|
|
686
|
+
artifact_stmt
|
|
687
|
+
.join(ArtifactFormatSpecTable, ArtifactFormatSpecTable.name == ArtifactTable.format_spec)
|
|
688
|
+
.where(ArtifactFormatSpecTable.family == artifact_filter.family)
|
|
689
|
+
)
|
|
690
|
+
else:
|
|
691
|
+
artifact_stmt = artifact_stmt.where(ArtifactTable.format_spec == artifact_filter.format_spec)
|
|
692
|
+
|
|
693
|
+
# Choose the latest artifact before checking acceptance.
|
|
694
|
+
latest_accepted_stmt = (
|
|
695
|
+
artifact_stmt
|
|
696
|
+
.correlate(EpisodeTable)
|
|
697
|
+
.order_by(ArtifactTable.sqn.desc())
|
|
698
|
+
.limit(1)
|
|
699
|
+
.scalar_subquery()
|
|
700
|
+
)
|
|
701
|
+
sub_predicate_stmts.append(sa
|
|
702
|
+
.func
|
|
703
|
+
.coalesce(latest_accepted_stmt == artifact_filter.acceptance, sa.false()))
|
|
704
|
+
|
|
111
705
|
return sa.and_(*sub_predicate_stmts)
|
|
112
706
|
|
|
113
707
|
|
|
@@ -210,32 +804,8 @@ def query_episodes(
|
|
|
210
804
|
)
|
|
211
805
|
|
|
212
806
|
db_tags = tag_query_stmt.order_by(TagTable.sqn).all()
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
db
|
|
216
|
-
.query(TagsetSpecTable.name, TagsetSpecTable.title)
|
|
217
|
-
.filter(TagsetSpecTable.name.in_({db_tag.tagset_spec for db_tag in db_tags}))
|
|
218
|
-
.all()
|
|
219
|
-
)
|
|
220
|
-
tag_titles = {
|
|
221
|
-
(tagset_spec, tag): title
|
|
222
|
-
for tagset_spec, tag, title in
|
|
223
|
-
db
|
|
224
|
-
.query(TagsetTable.tagset_spec, TagsetTable.tag, TagsetTable.title)
|
|
225
|
-
.filter(sa
|
|
226
|
-
.tuple_(TagsetTable.tagset_spec, TagsetTable.tag)
|
|
227
|
-
.in_({(db_tag.tagset_spec, db_tag.tag) for db_tag in db_tags}))
|
|
228
|
-
.all()
|
|
229
|
-
}
|
|
230
|
-
for db_tag in db_tags:
|
|
231
|
-
tags_by_bag[db_tag.bag_uuid].append(
|
|
232
|
-
TagWithTitles.model_validate(
|
|
233
|
-
db_tag.model_dump() | dict(
|
|
234
|
-
tagset_spec_title=tagset_spec_titles.get(db_tag.tagset_spec),
|
|
235
|
-
title=tag_titles.get((db_tag.tagset_spec, db_tag.tag)),
|
|
236
|
-
)
|
|
237
|
-
)
|
|
238
|
-
)
|
|
807
|
+
for db_tag in TagWithTitles.with_titles(db, db_tags):
|
|
808
|
+
tags_by_bag[db_tag.bag_uuid].append(db_tag)
|
|
239
809
|
|
|
240
810
|
artifacts_by_bag = {db_episode.bag_uuid: [] for db_episode, _, _ in db_episode_bag_selected}
|
|
241
811
|
if request.artifact_selectors is not False:
|
|
@@ -276,58 +846,56 @@ def query_episodes(
|
|
|
276
846
|
artifact_query_stmt = artifact_query_stmt.filter(sa.or_(sa.false(), *matching_predicate_stmts))
|
|
277
847
|
|
|
278
848
|
db_artifacts = artifact_query_stmt.order_by(ArtifactTable.sqn).all()
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
db
|
|
282
|
-
.query(ArtifactFormatSpecTable.name, ArtifactFormatSpecTable.title)
|
|
283
|
-
.filter(ArtifactFormatSpecTable.name.in_({db_artifact.format_spec for db_artifact in db_artifacts}))
|
|
284
|
-
.all()
|
|
285
|
-
)
|
|
286
|
-
for db_artifact in db_artifacts:
|
|
287
|
-
artifacts_by_bag[db_artifact.bag_uuid].append(
|
|
288
|
-
ArtifactWithTitles.model_validate(
|
|
289
|
-
db_artifact.model_dump() | dict(
|
|
290
|
-
format_spec_title=artifact_format_spec_titles.get(db_artifact.format_spec),
|
|
291
|
-
)
|
|
292
|
-
)
|
|
293
|
-
)
|
|
849
|
+
for db_artifact in ArtifactWithTitles.with_titles(db, db_artifacts):
|
|
850
|
+
artifacts_by_bag[db_artifact.bag_uuid].append(db_artifact)
|
|
294
851
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
.query(EpisodeFormatSpecTable.name, EpisodeFormatSpecTable.title)
|
|
298
|
-
.filter(EpisodeFormatSpecTable.name.in_({
|
|
299
|
-
db_episode.format_spec for db_episode, _, _ in db_episode_bag_selected
|
|
300
|
-
}))
|
|
301
|
-
.all()
|
|
302
|
-
)
|
|
303
|
-
|
|
304
|
-
bag_format_spec_titles = dict(
|
|
305
|
-
db
|
|
306
|
-
.query(BagFormatSpecTable.name, BagFormatSpecTable.title)
|
|
307
|
-
.filter(BagFormatSpecTable.name.in_({
|
|
308
|
-
db_bag.format_spec for _, db_bag, _ in db_episode_bag_selected
|
|
309
|
-
}))
|
|
310
|
-
.all()
|
|
311
|
-
)
|
|
852
|
+
db_episodes = EpisodeWithTitles.with_titles(db, (episode for episode, _, _ in db_episode_bag_selected))
|
|
853
|
+
db_bags = BagWithTitles.with_titles(db, (bag for _, bag, _ in db_episode_bag_selected))
|
|
312
854
|
|
|
313
855
|
return EnrichedEpisodesAndPagination(
|
|
314
856
|
episodes=[
|
|
315
857
|
EnrichedEpisode(
|
|
316
|
-
episode=
|
|
317
|
-
|
|
318
|
-
format_spec_title=episode_format_spec_titles.get(db_episode.format_spec),
|
|
319
|
-
)
|
|
320
|
-
),
|
|
321
|
-
bag=BagWithTitles.model_validate(
|
|
322
|
-
db_bag.model_dump() | dict(
|
|
323
|
-
format_spec_title=bag_format_spec_titles.get(db_bag.format_spec),
|
|
324
|
-
)
|
|
325
|
-
),
|
|
858
|
+
episode=db_episode,
|
|
859
|
+
bag=db_bag,
|
|
326
860
|
selected=selected,
|
|
327
861
|
artifacts=artifacts_by_bag[db_episode.bag_uuid],
|
|
328
862
|
tags=tags_by_bag[db_episode.bag_uuid],
|
|
329
863
|
)
|
|
330
|
-
for db_episode, db_bag,
|
|
864
|
+
for db_episode, db_bag, (_, _, selected)
|
|
865
|
+
in zip(db_episodes, db_bags, db_episode_bag_selected, strict=True)
|
|
331
866
|
],
|
|
332
867
|
pagination=pagination,
|
|
333
868
|
)
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
class StatEpisodeRequest(EpisodeSelection):
|
|
872
|
+
pass
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
class EpisodeStatistics(pdt.BaseModel):
|
|
876
|
+
total_count: int
|
|
877
|
+
total_duration_ns: int
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
@router.get("/episodes/stat/", response_model=EpisodeStatistics)
|
|
881
|
+
def stat_episodes(
|
|
882
|
+
request: StatEpisodeRequest,
|
|
883
|
+
db: sa_orm.Session = Depends(make_session),
|
|
884
|
+
) -> EpisodeStatistics:
|
|
885
|
+
# Aggregate only live episode/bag pairs, including explicitly selected UUIDs.
|
|
886
|
+
count, duration = (
|
|
887
|
+
db
|
|
888
|
+
.query(sa.func.count(EpisodeTable.sqn),
|
|
889
|
+
sa.func.coalesce(sa.func.sum(EpisodeTable.duration_ns), 0))
|
|
890
|
+
.select_from(EpisodeTable)
|
|
891
|
+
.join(BagTable, BagTable.uuid == EpisodeTable.bag_uuid)
|
|
892
|
+
.filter(BagTable.flags.op("&")(DELETED_FLAG) == 0,
|
|
893
|
+
EpisodeTable.flags.op("&")(DELETED_FLAG) == 0,
|
|
894
|
+
sa.and_(sa.or_(make_episode_filter_predicate_stmt(request.select_filter_set)
|
|
895
|
+
if request.select_filter_set else sa.false(),
|
|
896
|
+
EpisodeTable.bag_uuid.in_(request.inclusion_uuids)),
|
|
897
|
+
EpisodeTable.bag_uuid.not_in(request.exclusion_uuids)))
|
|
898
|
+
.one()
|
|
899
|
+
)
|
|
900
|
+
|
|
901
|
+
return EpisodeStatistics(total_count=count, total_duration_ns=duration)
|