Skip to content

Commit

Permalink
feat: add last start time to ui table
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerHYang committed Nov 21, 2024
1 parent fadfab7 commit 2da7952
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 9 deletions.
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
lastStartTime: 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 @@ -88,6 +88,7 @@ export function SessionsTable(props: SessionsTableProps) {
lastOutput {
value
}
lastStartTime
tokenUsage {
prompt
completion
Expand Down Expand Up @@ -138,6 +139,12 @@ export function SessionsTable(props: SessionsTableProps) {
enableSorting: true,
cell: TimestampCell,
},
{
header: "last start time",
accessorKey: "lastStartTime",
enableSorting: false,
cell: TimestampCell,
},
{
header: "total tokens",
accessorKey: "tokenCountTotal",
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,
SessionLastStartTimeDataLoader,
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_start_times: SessionLastStartTimeDataLoader
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 SessionLastStartTimeDataLoader
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",
"SessionLastStartTimeDataLoader",
"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 SessionLastStartTimeDataLoader(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_start_time(
self,
info: Info[Context, None],
) -> Optional[datetime]:
return await info.context.data_loaders.session_last_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,
SessionLastStartTimeDataLoader,
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_start_times=SessionLastStartTimeDataLoader(db),
session_num_traces=SessionNumTracesDataLoader(db),
session_num_traces_with_error=SessionNumTracesWithErrorDataLoader(db),
session_token_usages=SessionTokenUsagesDataLoader(db),
Expand Down

0 comments on commit 2da7952

Please sign in to comment.