mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Bound sessions by spend to the range end and stop overwriting the saved model filter
This commit is contained in:
1 parent
97b94c1d07
commit
6d8be37cd9
4 files changed
+59
-19
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
|
||||
import { screen, waitFor, within } from '@testing-library/react'
|
||||
import { screen, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import type { SessionStatsInfo } from '@opencode-manager/shared/opencode'
|
||||
import { LOCAL_OPENCODE_SERVER_ID } from '@opencode-manager/shared/schemas'
|
||||
@@ -302,7 +302,7 @@ describe('UsageDashboard', () => {
|
||||
expect(window.localStorage.getItem('oc:usage:models')).toBe('[]')
|
||||
})
|
||||
|
||||
it('prunes stored models that are missing from the loaded stats', async () => {
|
||||
it('keeps stored models that are missing from the loaded stats', async () => {
|
||||
window.localStorage.setItem(
|
||||
'oc:usage:models',
|
||||
JSON.stringify(['anthropic/claude', 'ghost/model']),
|
||||
@@ -314,11 +314,15 @@ describe('UsageDashboard', () => {
|
||||
fetchFallback: sessionMessageFallback,
|
||||
})
|
||||
|
||||
await waitFor(() =>
|
||||
expect(JSON.parse(window.localStorage.getItem('oc:usage:models') ?? '[]')).toEqual([
|
||||
'anthropic/claude',
|
||||
]),
|
||||
await screen.findByText(hiddenSectionsNote)
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Filter by model' })).toHaveTextContent(
|
||||
'anthropic/claude',
|
||||
)
|
||||
expect(JSON.parse(window.localStorage.getItem('oc:usage:models') ?? 'null')).toEqual([
|
||||
'anthropic/claude',
|
||||
'ghost/model',
|
||||
])
|
||||
})
|
||||
|
||||
it('renders an error banner when the stats request fails', async () => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useMemo } from 'react'
|
||||
import { useMemo } from 'react'
|
||||
import { format } from 'date-fns'
|
||||
import type { SessionStatsInfo } from '@opencode-manager/shared/opencode'
|
||||
import { ActivitySpinner } from '@/components/ui/activity-spinner'
|
||||
@@ -58,18 +58,17 @@ export function UsageDashboard({ serverId }: UsageDashboardProps) {
|
||||
)
|
||||
const data = stats.data
|
||||
|
||||
useEffect(() => {
|
||||
if (!data) return
|
||||
const selectedModels = useMemo(() => {
|
||||
if (!data) return models
|
||||
const available = new Set(data.models.map((entry) => modelUsageKey(entry.model)))
|
||||
const pruned = models.filter((key) => available.has(key))
|
||||
if (pruned.length !== models.length) setModels(pruned)
|
||||
}, [data, models, setModels])
|
||||
return models.filter((key) => available.has(key))
|
||||
}, [data, models])
|
||||
|
||||
const isFiltered = models.length > 0
|
||||
const isFiltered = selectedModels.length > 0
|
||||
const showReset = isFiltered || !isDefaultUsageRange(range)
|
||||
|
||||
function renderStats(statsData: SessionStatsInfo, activeRange: ResolvedUsageRange) {
|
||||
const totals = filterModelUsage(statsData.models, models)
|
||||
const totals = filterModelUsage(statsData.models, selectedModels)
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<p className="text-xs text-muted-foreground">
|
||||
@@ -81,7 +80,7 @@ export function UsageDashboard({ serverId }: UsageDashboardProps) {
|
||||
<UsageTokenComposition tokens={totals.tokens} />
|
||||
<UsageModelsTable
|
||||
models={statsData.models.filter((entry) =>
|
||||
models.includes(modelUsageKey(entry.model)),
|
||||
selectedModels.includes(modelUsageKey(entry.model)),
|
||||
)}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
@@ -145,7 +144,7 @@ export function UsageDashboard({ serverId }: UsageDashboardProps) {
|
||||
<UsageRangePicker value={range} onChange={setSelection} />
|
||||
<UsageModelFilter
|
||||
models={data?.models.map((entry) => modelUsageKey(entry.model)) ?? []}
|
||||
selected={models}
|
||||
selected={selectedModels}
|
||||
onChange={setModels}
|
||||
/>
|
||||
{showReset && (
|
||||
|
||||
@@ -386,6 +386,41 @@ describe('UsageSessionsTable', () => {
|
||||
expect(rowTitles()).toEqual(['Updated later', 'Active in range'])
|
||||
})
|
||||
|
||||
it('excludes sessions created after the range end', async () => {
|
||||
const sessions = [
|
||||
makeSession({
|
||||
id: 'ses_future',
|
||||
title: 'Created later',
|
||||
cost: 0.4,
|
||||
tokens: spentTokens,
|
||||
time: { created: 5000, updated: 6000 },
|
||||
}),
|
||||
makeSession({
|
||||
id: 'ses_inside',
|
||||
title: 'Created in range',
|
||||
cost: 0.2,
|
||||
tokens: spentTokens,
|
||||
time: { created: 1000, updated: 2000 },
|
||||
}),
|
||||
]
|
||||
|
||||
renderWithProviders(
|
||||
<UsageSessionsTable
|
||||
serverId={LOCAL_OPENCODE_SERVER_ID}
|
||||
range={{ from: 1000, to: 3000 }}
|
||||
/>,
|
||||
{
|
||||
...localOptions,
|
||||
fetchRoutes: [reposRoute(), sessionsRoute(sessions), messagesRoute()],
|
||||
},
|
||||
)
|
||||
|
||||
await screen.findByText('Created in range')
|
||||
|
||||
expect(rowTitles()).toEqual(['Created in range'])
|
||||
expect(screen.queryByText('Created later')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders an error banner when the session list request fails', async () => {
|
||||
renderWithProviders(<UsageSessionsTable serverId={LOCAL_OPENCODE_SERVER_ID} range={{ from: 0 }} />, {
|
||||
...localOptions,
|
||||
|
||||
@@ -39,8 +39,10 @@ function hasSpendOrTokens(session: SessionInfo): boolean {
|
||||
return session.cost > 0 || totalTokens(session.tokens) > 0
|
||||
}
|
||||
|
||||
function isSessionActiveFrom(session: SessionInfo, range: ResolvedUsageRange): boolean {
|
||||
return range.from === undefined || session.time.updated >= range.from
|
||||
function isSessionActiveInRange(session: SessionInfo, range: ResolvedUsageRange): boolean {
|
||||
if (range.from !== undefined && session.time.updated < range.from) return false
|
||||
if (range.to !== undefined && session.time.created > range.to) return false
|
||||
return true
|
||||
}
|
||||
|
||||
const sessionMetricsCache = new WeakMap<SessionUsageMessages, SessionMetrics>()
|
||||
@@ -113,7 +115,7 @@ export function UsageSessionsTable({ serverId, range }: UsageSessionsTableProps)
|
||||
const sortedSessions = useMemo(
|
||||
() =>
|
||||
sessions
|
||||
.filter((session) => isSessionActiveFrom(session, range) && hasSpendOrTokens(session))
|
||||
.filter((session) => isSessionActiveInRange(session, range) && hasSpendOrTokens(session))
|
||||
.sort((a, b) => b.cost - a.cost || totalTokens(b.tokens) - totalTokens(a.tokens)),
|
||||
[sessions, range],
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user