-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sessions): add trace latency p50 to session details (#5236)
* feat(sessions): add trace latency p50 to session details * fix type for latency in ui * update test, fix where clause * add is not none check for db filter * revert latency dataloader changes * revert dataloader changes for project span and trace latency * refactor to use its own data loader * remove extra param from test * add unit tests * ruff * fix import * fix name filter in other dataloader tests * update unit test fixture naming, add unit test for project session graphql trace_latency_ms_quantile field * clean up imports * pin aiohttp * fix unit test deps
- Loading branch information
1 parent
c400b52
commit cb6d27e
Showing
16 changed files
with
253 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 18 additions & 4 deletions
22
app/src/pages/trace/__generated__/SessionDetailsQuery.graphql.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/phoenix/server/api/dataloaders/session_trace_latency_ms_quantile.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from collections import defaultdict | ||
from typing import Optional | ||
|
||
import numpy as np | ||
from aioitertools.itertools import groupby | ||
from sqlalchemy import select | ||
from strawberry.dataloader import DataLoader | ||
from typing_extensions import TypeAlias | ||
|
||
from phoenix.db import models | ||
from phoenix.server.types import DbSessionFactory | ||
|
||
SessionId: TypeAlias = int | ||
Probability: TypeAlias = float | ||
QuantileValue: TypeAlias = float | ||
|
||
Key: TypeAlias = tuple[SessionId, Probability] | ||
Result: TypeAlias = Optional[QuantileValue] | ||
ResultPosition: TypeAlias = int | ||
|
||
DEFAULT_VALUE: Result = None | ||
|
||
|
||
class SessionTraceLatencyMsQuantileDataLoader(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]: | ||
results: list[Result] = [DEFAULT_VALUE] * len(keys) | ||
argument_position_map: defaultdict[ | ||
SessionId, defaultdict[Probability, list[ResultPosition]] | ||
] = defaultdict(lambda: defaultdict(list)) | ||
session_rowids = {session_id for session_id, _ in keys} | ||
for position, (session_id, probability) in enumerate(keys): | ||
argument_position_map[session_id][probability].append(position) | ||
stmt = ( | ||
select( | ||
models.Trace.project_session_rowid, | ||
models.Trace.latency_ms, | ||
) | ||
.where(models.Trace.project_session_rowid.in_(session_rowids)) | ||
.order_by(models.Trace.project_session_rowid) | ||
) | ||
async with self._db() as session: | ||
data = await session.stream(stmt) | ||
async for project_session_rowid, group in groupby( | ||
data, lambda row: row.project_session_rowid | ||
): | ||
session_latencies = [row.latency_ms for row in group] | ||
for probability, positions in argument_position_map[project_session_rowid].items(): | ||
quantile_value = np.quantile(session_latencies, probability) | ||
for position in positions: | ||
results[position] = quantile_value | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.