Skip to content

Commit 626ea40

Browse files
committed
Add EventTarget.project_name
Return the project name for all targets in `/api/events/list`. Required for displaying event targets and building entity URLs in the UI. Implemented by joining event targets with projects. The performance effect of the join are tolerable — with a dataset of 1.8 million events, requests with various filters take about 0.05s with hot Postgres cache, and up to ~0.75s with cold cache, which is similar to previous measurements.
1 parent 376be2a commit 626ea40

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/dstack/_internal/core/models/events.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ class EventTarget(CoreModel):
3737
)
3838
),
3939
]
40+
project_name: Annotated[
41+
Optional[str],
42+
Field(
43+
description=(
44+
"Name of the project the target entity belongs to,"
45+
" or `null` for target types not bound to a project (e.g., users)"
46+
)
47+
),
48+
]
4049
id: Annotated[uuid.UUID, Field(description="ID of the target entity")]
4150
name: Annotated[str, Field(description="Name of the target entity")]
4251

src/dstack/_internal/server/services/events.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,12 @@ async def list_events(
370370
.order_by(*order_by)
371371
.limit(limit)
372372
.options(
373-
joinedload(EventModel.targets),
373+
(
374+
joinedload(EventModel.targets)
375+
.joinedload(EventTargetModel.entity_project)
376+
.load_only(ProjectModel.name)
377+
.noload(ProjectModel.owner)
378+
),
374379
joinedload(EventModel.actor_user).load_only(UserModel.name),
375380
)
376381
)
@@ -395,6 +400,7 @@ def event_model_to_event(event_model: EventModel) -> Event:
395400
EventTarget(
396401
type=target.entity_type,
397402
project_id=target.entity_project_id,
403+
project_name=target.entity_project.name if target.entity_project else None,
398404
id=target.entity_id,
399405
name=target.entity_name,
400406
)

src/tests/_internal/server/routers/test_events.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ async def test_response_format(self, session: AsyncSession, client: AsyncClient)
7979
{
8080
"type": "project",
8181
"project_id": str(project.id),
82+
"project_name": "test_project",
8283
"id": str(project.id),
8384
"name": "test_project",
8485
},
@@ -94,12 +95,14 @@ async def test_response_format(self, session: AsyncSession, client: AsyncClient)
9495
{
9596
"type": "project",
9697
"project_id": str(project.id),
98+
"project_name": "test_project",
9799
"id": str(project.id),
98100
"name": "test_project",
99101
},
100102
{
101103
"type": "user",
102104
"project_id": None,
105+
"project_name": None,
103106
"id": str(user.id),
104107
"name": "test_user",
105108
},

0 commit comments

Comments
 (0)