Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sessions): add last start time to UI table for sessions #5481

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,7 @@ type ProjectSession implements Node {
numTracesWithError: Int!
firstInput: SpanIOValue
lastOutput: SpanIOValue
lastTraceStartTime: DateTime
tokenUsage: TokenUsage!
traces(first: Int = 50, last: Int, after: String, before: String): TraceConnection!
traceLatencyMsQuantile(probability: Float!): Float
Expand Down
7 changes: 7 additions & 0 deletions app/src/pages/project/SessionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export function SessionsTable(props: SessionsTableProps) {
lastOutput {
value
}
lastTraceStartTime
tokenUsage {
prompt
completion
Expand Down Expand Up @@ -141,6 +142,12 @@ export function SessionsTable(props: SessionsTableProps) {
enableSorting: true,
cell: TimestampCell,
},
{
header: "last trace start time",
accessorKey: "lastTraceStartTime",
enableSorting: false,
cell: TimestampCell,
},
{
header: "p50 latency",
accessorKey: "traceLatencyMsP50",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions app/src/pages/project/__generated__/SessionsTableQuery.graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/phoenix/server/api/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ProjectByNameDataLoader,
RecordCountDataLoader,
SessionIODataLoader,
SessionLastTraceStartTimeDataLoader,
SessionNumTracesDataLoader,
SessionNumTracesWithErrorDataLoader,
SessionTokenUsagesDataLoader,
Expand Down Expand Up @@ -76,6 +77,7 @@ class DataLoaders:
record_counts: RecordCountDataLoader
session_first_inputs: SessionIODataLoader
session_last_outputs: SessionIODataLoader
session_last_trace_start_times: SessionLastTraceStartTimeDataLoader
session_num_traces: SessionNumTracesDataLoader
session_num_traces_with_error: SessionNumTracesWithErrorDataLoader
session_token_usages: SessionTokenUsagesDataLoader
Expand Down
2 changes: 2 additions & 0 deletions src/phoenix/server/api/dataloaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .project_by_name import ProjectByNameDataLoader
from .record_counts import RecordCountCache, RecordCountDataLoader
from .session_io import SessionIODataLoader
from .session_last_start_times import SessionLastTraceStartTimeDataLoader
from .session_num_traces import SessionNumTracesDataLoader
from .session_num_traces_with_error import SessionNumTracesWithErrorDataLoader
from .session_token_usages import SessionTokenUsagesDataLoader
Expand Down Expand Up @@ -52,6 +53,7 @@
"MinStartOrMaxEndTimeDataLoader",
"RecordCountDataLoader",
"SessionIODataLoader",
"SessionLastTraceStartTimeDataLoader",
"SessionNumTracesDataLoader",
"SessionNumTracesWithErrorDataLoader",
"SessionTokenUsagesDataLoader",
Expand Down
42 changes: 42 additions & 0 deletions src/phoenix/server/api/dataloaders/session_last_start_times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from datetime import datetime
from typing import Optional

from openinference.semconv.trace import SpanAttributes
from sqlalchemy import func, select
from strawberry.dataloader import DataLoader
from typing_extensions import TypeAlias

from phoenix.db import models
from phoenix.server.types import DbSessionFactory

Key: TypeAlias = int
Result: TypeAlias = Optional[datetime]


class SessionLastTraceStartTimeDataLoader(DataLoader[Key, Result]):
def __init__(self, db: DbSessionFactory) -> None:
super().__init__(load_fn=self._load_fn)
self._db = db

async def _load_fn(self, keys: list[Key]) -> list[Result]:
stmt = (
select(
models.Trace.project_session_rowid,
func.max(models.Trace.start_time).label("last_start_time"),
)
.where(models.Trace.project_session_rowid.in_(set(keys)))
.group_by(models.Trace.project_session_rowid)
)
async with self._db() as session:
result: dict[Key, Result] = {
id_: last_start_time
async for id_, last_start_time in await session.stream(stmt)
if id_ is not None
}
return [result.get(key) for key in keys]


INPUT_VALUE = SpanAttributes.INPUT_VALUE.split(".")
INPUT_MIME_TYPE = SpanAttributes.INPUT_MIME_TYPE.split(".")
OUTPUT_VALUE = SpanAttributes.OUTPUT_VALUE.split(".")
OUTPUT_MIME_TYPE = SpanAttributes.OUTPUT_MIME_TYPE.split(".")
7 changes: 7 additions & 0 deletions src/phoenix/server/api/types/ProjectSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ async def last_output(
value=record.value,
)

@strawberry.field
async def last_trace_start_time(
self,
info: Info[Context, None],
) -> Optional[datetime]:
return await info.context.data_loaders.session_last_trace_start_times.load(self.id_attr)

@strawberry.field
async def token_usage(
self,
Expand Down
2 changes: 2 additions & 0 deletions src/phoenix/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
ProjectByNameDataLoader,
RecordCountDataLoader,
SessionIODataLoader,
SessionLastTraceStartTimeDataLoader,
SessionNumTracesDataLoader,
SessionNumTracesWithErrorDataLoader,
SessionTokenUsagesDataLoader,
Expand Down Expand Up @@ -617,6 +618,7 @@ def get_context() -> Context:
),
session_first_inputs=SessionIODataLoader(db, "first_input"),
session_last_outputs=SessionIODataLoader(db, "last_output"),
session_last_trace_start_times=SessionLastTraceStartTimeDataLoader(db),
session_num_traces=SessionNumTracesDataLoader(db),
session_num_traces_with_error=SessionNumTracesWithErrorDataLoader(db),
session_token_usages=SessionTokenUsagesDataLoader(db),
Expand Down