mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Drop per-row step and throughput columns from the sessions by spend table
This commit is contained in:
1 parent
6d8be37cd9
commit
8163941ea2
4 files changed
+38
-228
No files matched your search
@@ -19,19 +19,6 @@ const zeroTokens = { input: 0, output: 0, reasoning: 0, cache: { read: 0, write:
|
||||
|
||||
const spentTokens = { input: 1000, output: 500, reasoning: 0, cache: { read: 0, write: 0 } }
|
||||
|
||||
function assistantStep(id: string) {
|
||||
return {
|
||||
id,
|
||||
type: 'assistant' as const,
|
||||
agent: 'build',
|
||||
model: { providerID: 'anthropic', id: 'claude' },
|
||||
time: { created: 1000, streamed: 2000 },
|
||||
content: [],
|
||||
cost: 0.1,
|
||||
tokens: { input: 100, output: 50, reasoning: 0, cache: { read: 0, write: 0 } },
|
||||
}
|
||||
}
|
||||
|
||||
function reposRoute(): FetchRoute {
|
||||
return {
|
||||
method: 'GET',
|
||||
@@ -67,39 +54,6 @@ function pagedSessionsRoute(pages: SessionInfo[][]): FetchRoute {
|
||||
}
|
||||
}
|
||||
|
||||
function messagesRoute(serverId = LOCAL_OPENCODE_SERVER_ID): FetchRoute {
|
||||
const path =
|
||||
serverId === LOCAL_OPENCODE_SERVER_ID
|
||||
? '/api/opencode/api/session/:sessionID/message'
|
||||
: `/api/opencode/servers/${serverId}/api/session/:sessionID/message`
|
||||
return {
|
||||
method: 'GET',
|
||||
path,
|
||||
respond: (_request, params) => ({
|
||||
json: { data: [assistantStep(`msg_${params.sessionID}`)], cursor: {} },
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
const localMessagesPath = '/api/opencode/api/session/:sessionID/message'
|
||||
|
||||
function pagedMessagesRoute(pageSizes: number[]): FetchRoute {
|
||||
return {
|
||||
method: 'GET',
|
||||
path: localMessagesPath,
|
||||
respond: (request, params) => {
|
||||
const cursor = request.searchParams.get('cursor')
|
||||
const index = cursor === null ? 0 : Number(cursor)
|
||||
const size = pageSizes[index] ?? 0
|
||||
const data = Array.from({ length: size }, (_, itemIndex) =>
|
||||
assistantStep(`msg_${params.sessionID}_${index}_${itemIndex}`),
|
||||
)
|
||||
const next = index + 1 <= pageSizes.length ? { next: String(index + 1) } : {}
|
||||
return { json: { data, cursor: next } }
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function rowTitles(): string[] {
|
||||
return screen
|
||||
.getAllByRole('row')
|
||||
@@ -107,12 +61,6 @@ function rowTitles(): string[] {
|
||||
.map((row) => within(row).getAllByRole('cell')[0]?.textContent ?? '')
|
||||
}
|
||||
|
||||
function cellsFor(title: string): HTMLElement[] {
|
||||
const row = screen.getByText(title).closest('tr')
|
||||
if (!row) throw new Error(`No row for ${title}`)
|
||||
return within(row).getAllByRole('cell')
|
||||
}
|
||||
|
||||
describe('UsageSessionsTable', () => {
|
||||
it('renders only sessions with spend or tokens, sorted by cost descending', async () => {
|
||||
const sessions = [
|
||||
@@ -146,37 +94,19 @@ describe('UsageSessionsTable', () => {
|
||||
}),
|
||||
]
|
||||
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
const result = renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions)],
|
||||
})
|
||||
|
||||
await screen.findByText('High spend')
|
||||
|
||||
expect(rowTitles()).toEqual(['High spend', 'Mid spend', 'Low spend'])
|
||||
expect(screen.queryByText('Free session')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders step counts and average token throughput per session', async () => {
|
||||
const sessions = [
|
||||
makeSession({
|
||||
id: 'ses_demo',
|
||||
title: 'Demo session',
|
||||
cost: 0.4,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 2000 },
|
||||
}),
|
||||
]
|
||||
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
})
|
||||
|
||||
await screen.findByText('Demo session')
|
||||
|
||||
await waitFor(() => expect(cellsFor('Demo session')[1]).toHaveTextContent('1'))
|
||||
expect(cellsFor('Demo session')[5]).toHaveTextContent('50.0 tok/s')
|
||||
expect(
|
||||
result.fetchFake.findRequests('GET', '/api/opencode/api/session/:sessionID/message'),
|
||||
).toHaveLength(0)
|
||||
expect(screen.queryByRole('columnheader', { name: 'Steps' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('links local sessions to a matching repo and leaves unmatched directories as plain text', async () => {
|
||||
@@ -201,7 +131,7 @@ describe('UsageSessionsTable', () => {
|
||||
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions)],
|
||||
})
|
||||
|
||||
const link = await screen.findByRole('link', { name: 'Demo session' })
|
||||
@@ -227,7 +157,7 @@ describe('UsageSessionsTable', () => {
|
||||
<UsageSessionsTable serverId={serverId} range={{ from: 0 }} />,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [sessionsRoute(sessions, serverId), messagesRoute(serverId)],
|
||||
fetchRoutes: [sessionsRoute(sessions, serverId)],
|
||||
},
|
||||
)
|
||||
|
||||
@@ -251,7 +181,7 @@ describe('UsageSessionsTable', () => {
|
||||
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions)],
|
||||
})
|
||||
|
||||
await screen.findByText('Session 1')
|
||||
@@ -297,7 +227,7 @@ describe('UsageSessionsTable', () => {
|
||||
/>,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions)],
|
||||
},
|
||||
)
|
||||
|
||||
@@ -323,7 +253,7 @@ describe('UsageSessionsTable', () => {
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
initialEntries: ['/?assistant=1&repoTab=x&usage=open'],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions)],
|
||||
})
|
||||
|
||||
const link = await screen.findByRole('link', { name: 'Demo session' })
|
||||
@@ -345,7 +275,7 @@ describe('UsageSessionsTable', () => {
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
initialEntries: ['/?panel=files&usage=open'],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions)],
|
||||
})
|
||||
|
||||
const link = await screen.findByRole('link', { name: 'Demo session' })
|
||||
@@ -377,7 +307,7 @@ describe('UsageSessionsTable', () => {
|
||||
/>,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions)],
|
||||
},
|
||||
)
|
||||
|
||||
@@ -411,7 +341,7 @@ describe('UsageSessionsTable', () => {
|
||||
/>,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions)],
|
||||
},
|
||||
)
|
||||
|
||||
@@ -431,7 +361,6 @@ describe('UsageSessionsTable', () => {
|
||||
path: localSessionsPath,
|
||||
respond: () => ({ status: 500, json: { error: 'boom' } }),
|
||||
},
|
||||
messagesRoute(),
|
||||
],
|
||||
})
|
||||
|
||||
@@ -466,7 +395,6 @@ describe('UsageSessionsTable', () => {
|
||||
? { json: { data: firstPage, cursor: { next: '1' } } }
|
||||
: { status: 500, json: { error: 'boom' } },
|
||||
},
|
||||
messagesRoute(),
|
||||
],
|
||||
})
|
||||
|
||||
@@ -501,11 +429,7 @@ describe('UsageSessionsTable', () => {
|
||||
<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [
|
||||
reposRoute(),
|
||||
pagedSessionsRoute([firstPage, secondPage]),
|
||||
messagesRoute(),
|
||||
],
|
||||
fetchRoutes: [reposRoute(), pagedSessionsRoute([firstPage, secondPage])],
|
||||
},
|
||||
)
|
||||
|
||||
@@ -540,18 +464,12 @@ describe('UsageSessionsTable', () => {
|
||||
<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 5000 }} />,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [
|
||||
reposRoute(),
|
||||
pagedSessionsRoute([firstPage, secondPage]),
|
||||
messagesRoute(),
|
||||
],
|
||||
fetchRoutes: [reposRoute(), pagedSessionsRoute([firstPage, secondPage])],
|
||||
},
|
||||
)
|
||||
|
||||
await screen.findByText('First page 0')
|
||||
await waitFor(() =>
|
||||
expect(result.fetchFake.findRequests('GET', localMessagesPath)).toHaveLength(10),
|
||||
)
|
||||
await waitFor(() => expect(screen.getAllByRole('row')).toHaveLength(11))
|
||||
|
||||
expect(result.fetchFake.findRequests('GET', localSessionsPath)).toHaveLength(1)
|
||||
expect(screen.queryByText('Beyond range session')).not.toBeInTheDocument()
|
||||
@@ -574,7 +492,7 @@ describe('UsageSessionsTable', () => {
|
||||
<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), pagedSessionsRoute(pages), messagesRoute()],
|
||||
fetchRoutes: [reposRoute(), pagedSessionsRoute(pages)],
|
||||
},
|
||||
)
|
||||
|
||||
@@ -586,34 +504,4 @@ describe('UsageSessionsTable', () => {
|
||||
screen.getByText('Ranked from the 500 most recently active sessions.'),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('marks a truncated step count with a plus', async () => {
|
||||
const sessions = [
|
||||
makeSession({
|
||||
id: 'ses_long',
|
||||
title: 'Long session',
|
||||
cost: 0.4,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 2000 },
|
||||
}),
|
||||
]
|
||||
|
||||
const result = renderWithProviders(
|
||||
<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [
|
||||
reposRoute(),
|
||||
sessionsRoute(sessions),
|
||||
pagedMessagesRoute([200, 200, 200, 200, 200]),
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
await screen.findByText('Long session')
|
||||
|
||||
await waitFor(() => expect(cellsFor('Long session')[1]).toHaveTextContent('1000+'))
|
||||
expect(cellsFor('Long session')[5]).toHaveTextContent('50.0 tok/s')
|
||||
expect(result.fetchFake.findRequests('GET', localMessagesPath)).toHaveLength(5)
|
||||
})
|
||||
})
|
||||
@@ -1,34 +1,21 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { Link, useLocation } from 'react-router-dom'
|
||||
import type { UseQueryResult } from '@tanstack/react-query'
|
||||
import type { SessionInfo } from '@opencode-manager/shared/opencode'
|
||||
import { LOCAL_OPENCODE_SERVER_ID } from '@opencode-manager/shared/schemas'
|
||||
import type { SessionUsageMessages } from '@/api/usage'
|
||||
import { findRepoForDirectory } from '@/components/navigation/sidebar-session-tree'
|
||||
import { ErrorBanner } from '@/components/ui/error-banner'
|
||||
import { useSessionsAcrossDirectories } from '@/hooks/useOpenCode'
|
||||
import { useNavigableRepos } from '@/hooks/useSidebarRepoGroups'
|
||||
import { useSessionUsageQueries } from '@/hooks/useUsage'
|
||||
import { formatCompactTokens } from '@/lib/contextUsage'
|
||||
import { getServerSessionPath, getSessionPath, withQueryParam } from '@/lib/navigation'
|
||||
import type { ResolvedUsageRange } from '@/lib/usageRange'
|
||||
import {
|
||||
formatTokensPerSecond,
|
||||
formatUsd,
|
||||
summarizeSessionSteps,
|
||||
totalTokens,
|
||||
} from '@/lib/usageStats'
|
||||
import { formatUsd, totalTokens } from '@/lib/usageStats'
|
||||
|
||||
interface UsageSessionsTableProps {
|
||||
serverId: string
|
||||
range: ResolvedUsageRange
|
||||
}
|
||||
|
||||
interface SessionMetrics {
|
||||
steps: string
|
||||
throughput: string
|
||||
}
|
||||
|
||||
const SESSION_PAGE_LIMIT = 100
|
||||
const MAX_SESSION_PAGES = 5
|
||||
const SESSION_CAP = SESSION_PAGE_LIMIT * MAX_SESSION_PAGES
|
||||
@@ -45,24 +32,6 @@ function isSessionActiveInRange(session: SessionInfo, range: ResolvedUsageRange)
|
||||
return true
|
||||
}
|
||||
|
||||
const sessionMetricsCache = new WeakMap<SessionUsageMessages, SessionMetrics>()
|
||||
|
||||
function sessionMetrics(
|
||||
query: UseQueryResult<SessionUsageMessages> | undefined,
|
||||
): SessionMetrics {
|
||||
if (!query || query.isPending) return { steps: '…', throughput: '…' }
|
||||
if (!query.data) return { steps: '—', throughput: '—' }
|
||||
const cached = sessionMetricsCache.get(query.data)
|
||||
if (cached) return cached
|
||||
const summary = summarizeSessionSteps(query.data.steps, { range: {}, models: [] })
|
||||
const metrics = {
|
||||
steps: query.data.truncated ? `${summary.steps}+` : String(summary.steps),
|
||||
throughput: formatTokensPerSecond(summary.averageTokensPerSecond),
|
||||
}
|
||||
sessionMetricsCache.set(query.data, metrics)
|
||||
return metrics
|
||||
}
|
||||
|
||||
export function UsageSessionsTable({ serverId, range }: UsageSessionsTableProps) {
|
||||
const {
|
||||
data: sessions,
|
||||
@@ -121,10 +90,6 @@ export function UsageSessionsTable({ serverId, range }: UsageSessionsTableProps)
|
||||
)
|
||||
|
||||
const visibleSessions = sortedSessions.slice(0, showCount)
|
||||
const usageQueries = useSessionUsageQueries(
|
||||
visibleSessions.map((session) => session.id),
|
||||
serverId,
|
||||
)
|
||||
|
||||
function sessionLinkPath(path: string): string {
|
||||
return panelId ? withQueryParam(path, 'panel', panelId) : path
|
||||
@@ -175,15 +140,12 @@ export function UsageSessionsTable({ serverId, range }: UsageSessionsTableProps)
|
||||
) : (
|
||||
<>
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full min-w-[40rem] text-sm">
|
||||
<table className="w-full min-w-[32rem] text-sm">
|
||||
<thead className="border-b border-border/60 text-xs uppercase text-muted-foreground">
|
||||
<tr>
|
||||
<th scope="col" className="px-3 py-2.5 text-left font-medium">
|
||||
Session
|
||||
</th>
|
||||
<th scope="col" className="px-3 py-2.5 text-right font-medium">
|
||||
Steps
|
||||
</th>
|
||||
<th scope="col" className="px-3 py-2.5 text-right font-medium">
|
||||
Input
|
||||
</th>
|
||||
@@ -193,37 +155,29 @@ export function UsageSessionsTable({ serverId, range }: UsageSessionsTableProps)
|
||||
<th scope="col" className="px-3 py-2.5 text-right font-medium">
|
||||
Cache read
|
||||
</th>
|
||||
<th scope="col" className="px-3 py-2.5 text-right font-medium">
|
||||
Avg tokens/s
|
||||
</th>
|
||||
<th scope="col" className="px-3 py-2.5 text-right font-medium">
|
||||
Cost
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-border/60">
|
||||
{visibleSessions.map((session, index) => {
|
||||
const metrics = sessionMetrics(usageQueries[index])
|
||||
return (
|
||||
<tr key={session.id}>
|
||||
<td className="px-3 py-2.5">{renderTitle(session)}</td>
|
||||
<td className="px-3 py-2.5 text-right tabular-nums">{metrics.steps}</td>
|
||||
<td className="px-3 py-2.5 text-right tabular-nums">
|
||||
{formatCompactTokens(session.tokens.input)}
|
||||
</td>
|
||||
<td className="px-3 py-2.5 text-right tabular-nums">
|
||||
{formatCompactTokens(session.tokens.output)}
|
||||
</td>
|
||||
<td className="px-3 py-2.5 text-right tabular-nums">
|
||||
{formatCompactTokens(session.tokens.cache.read)}
|
||||
</td>
|
||||
<td className="px-3 py-2.5 text-right tabular-nums">{metrics.throughput}</td>
|
||||
<td className="px-3 py-2.5 text-right tabular-nums">
|
||||
{formatUsd(session.cost)}
|
||||
</td>
|
||||
</tr>
|
||||
)
|
||||
})}
|
||||
{visibleSessions.map((session) => (
|
||||
<tr key={session.id}>
|
||||
<td className="px-3 py-2.5">{renderTitle(session)}</td>
|
||||
<td className="px-3 py-2.5 text-right tabular-nums">
|
||||
{formatCompactTokens(session.tokens.input)}
|
||||
</td>
|
||||
<td className="px-3 py-2.5 text-right tabular-nums">
|
||||
{formatCompactTokens(session.tokens.output)}
|
||||
</td>
|
||||
<td className="px-3 py-2.5 text-right tabular-nums">
|
||||
{formatCompactTokens(session.tokens.cache.read)}
|
||||
</td>
|
||||
<td className="px-3 py-2.5 text-right tabular-nums">
|
||||
{formatUsd(session.cost)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
sessionUsageQueryKey,
|
||||
usageStatsQueryKey,
|
||||
useSessionUsage,
|
||||
useSessionUsageQueries,
|
||||
useUsageStats,
|
||||
} from './useUsage'
|
||||
import { renderHookWithProviders } from '@/test/render'
|
||||
@@ -176,28 +175,3 @@ describe('useSessionUsage', () => {
|
||||
expect(request?.searchParams.toString()).toBe('limit=200&order=desc&type=assistant')
|
||||
})
|
||||
})
|
||||
|
||||
describe('useSessionUsageQueries', () => {
|
||||
it('loads usage for every requested session', async () => {
|
||||
const { result, fetchFake } = renderHookWithProviders(
|
||||
() => useSessionUsageQueries(['ses_1', 'ses_2'], LOCAL_OPENCODE_SERVER_ID),
|
||||
{
|
||||
...hookProviders,
|
||||
fetchRoutes: [
|
||||
messagesRoute('ses_1', [assistantMessage('msg_1', 1000)]),
|
||||
messagesRoute('ses_2', [assistantMessage('msg_2', 2000)]),
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
await waitFor(() =>
|
||||
expect(result.current.map((query) => query.data?.steps.map((step) => step.id))).toEqual([
|
||||
['msg_1'],
|
||||
['msg_2'],
|
||||
]),
|
||||
)
|
||||
|
||||
expect(fetchFake.findRequests('GET', '/api/opencode/api/session/ses_1/message')).toHaveLength(1)
|
||||
expect(fetchFake.findRequests('GET', '/api/opencode/api/session/ses_2/message')).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useQueries, useQuery } from '@tanstack/react-query'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { getSessionStats, loadSessionUsageMessages } from '@/api/usage'
|
||||
import { serverScopedKey } from '@/lib/serverScope'
|
||||
import { resolveUsageRange, type UsageRangeSelection } from '@/lib/usageRange'
|
||||
@@ -47,9 +47,3 @@ function buildSessionUsageQueryOptions(sessionID: string | undefined, serverId:
|
||||
export function useSessionUsage(sessionID: string | undefined, serverId: string) {
|
||||
return useQuery(buildSessionUsageQueryOptions(sessionID, serverId))
|
||||
}
|
||||
|
||||
export function useSessionUsageQueries(sessionIDs: readonly string[], serverId: string) {
|
||||
return useQueries({
|
||||
queries: sessionIDs.map((sessionID) => buildSessionUsageQueryOptions(sessionID, serverId)),
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user