feat: auto-refresh dashboard, show 'home' for home dir sessions

This commit is contained in:
AgentSeal 2026-04-15 01:31:07 -07:00
parent e75655e283
commit d597d458f9
2 changed files with 17 additions and 9 deletions

View file

@ -67,8 +67,9 @@ program
.description('Interactive usage dashboard')
.option('-p, --period <period>', 'Starting period: today, week, month, 30days', 'week')
.option('--provider <provider>', 'Filter by provider: all, claude, codex', 'all')
.option('--refresh <seconds>', 'Auto-refresh interval in seconds', parseInt)
.action(async (opts) => {
await renderDashboard(toPeriod(opts.period), opts.provider)
await renderDashboard(toPeriod(opts.period), opts.provider, opts.refresh)
})
function buildPeriodData(label: string, projects: ProjectSummary[]): PeriodData {
@ -153,16 +154,18 @@ program
.command('today')
.description('Today\'s usage dashboard')
.option('--provider <provider>', 'Filter by provider: all, claude, codex', 'all')
.option('--refresh <seconds>', 'Auto-refresh interval in seconds', parseInt)
.action(async (opts) => {
await renderDashboard('today', opts.provider)
await renderDashboard('today', opts.provider, opts.refresh)
})
program
.command('month')
.description('This month\'s usage dashboard')
.option('--provider <provider>', 'Filter by provider: all, claude, codex', 'all')
.option('--refresh <seconds>', 'Auto-refresh interval in seconds', parseInt)
.action(async (opts) => {
await renderDashboard('month', opts.provider)
await renderDashboard('month', opts.provider, opts.refresh)
})
program

View file

@ -194,18 +194,16 @@ const _homeEncoded = homedir().replace(/\//g, '-')
function shortProject(encoded: string): string {
let path = encoded.replace(/^-/, '')
// Strip home dir prefix (e.g. "Users-torukmakto-" → "")
if (path.startsWith(_homeEncoded.replace(/^-/, ''))) {
path = path.slice(_homeEncoded.replace(/^-/, '').length).replace(/^-/, '')
}
// Strip common system prefixes
path = path
.replace(/^private-tmp-[^-]+-[^-]+-/, '') // /private/tmp/<org>/<env>/
.replace(/^private-tmp-/, '')
.replace(/^tmp-/, '')
if (!path) return '~'
if (!path) return 'home'
const parts = path.split('-').filter(Boolean)
if (parts.length <= 3) return parts.join('/')
@ -485,10 +483,11 @@ function DashboardContent({ projects, period, columns }: { projects: ProjectSumm
)
}
function InteractiveDashboard({ initialProjects, initialPeriod, initialProvider }: {
function InteractiveDashboard({ initialProjects, initialPeriod, initialProvider, refreshSeconds }: {
initialProjects: ProjectSummary[]
initialPeriod: Period
initialProvider: string
refreshSeconds?: number
}) {
const { exit } = useApp()
const [period, setPeriod] = useState<Period>(initialPeriod)
@ -528,6 +527,12 @@ function InteractiveDashboard({ initialProjects, initialPeriod, initialProvider
setLoading(false)
}, [])
useEffect(() => {
if (!refreshSeconds || refreshSeconds <= 0) return
const id = setInterval(() => { reloadData(period, activeProvider) }, refreshSeconds * 1000)
return () => clearInterval(id)
}, [refreshSeconds, period, activeProvider, reloadData])
const switchPeriod = useCallback(async (newPeriod: Period) => {
if (newPeriod === period) return
setPeriod(newPeriod)
@ -592,7 +597,7 @@ function StaticDashboard({ projects, period }: { projects: ProjectSummary[]; per
)
}
export async function renderDashboard(period: Period = 'week', provider: string = 'all'): Promise<void> {
export async function renderDashboard(period: Period = 'week', provider: string = 'all', refreshSeconds?: number): Promise<void> {
await loadPricing()
const range = getDateRange(period)
const projects = await parseAllSessions(range, provider)
@ -601,7 +606,7 @@ export async function renderDashboard(period: Period = 'week', provider: string
if (isTTY) {
const { waitUntilExit } = render(
<InteractiveDashboard initialProjects={projects} initialPeriod={period} initialProvider={provider} />
<InteractiveDashboard initialProjects={projects} initialPeriod={period} initialProvider={provider} refreshSeconds={refreshSeconds} />
)
await waitUntilExit()
} else {