Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ function buildToolRows(projects: ProjectSummary[]): Row[] {
}))
}

function buildMcpRows(projects: ProjectSummary[]): Row[] {
const mcpTotals: Record<string, number> = {}
for (const project of projects) {
for (const session of project.sessions) {
for (const [server, d] of Object.entries(session.mcpBreakdown)) {
mcpTotals[server] = (mcpTotals[server] ?? 0) + d.calls
}
}
}
const total = Object.values(mcpTotals).reduce((s, n) => s + n, 0)
return Object.entries(mcpTotals)
.sort(([, a], [, b]) => b - a)
.map(([server, calls]) => ({
Server: server,
Calls: calls,
'Share (%)': pct(calls, total),
}))
}

function buildBashRows(projects: ProjectSummary[]): Row[] {
const bashTotals: Record<string, number> = {}
for (const project of projects) {
Expand Down Expand Up @@ -270,6 +289,7 @@ function buildReadme(periods: PeriodExport[]): string {
' projects.csv Spend per project folder for the selected detail period.',
' sessions.csv One row per session for the selected detail period.',
' tools.csv Tool invocations and share for the selected detail period.',
' mcp.csv MCP server invocations and share for the selected detail period.',
' shell-commands.csv Shell commands executed via Bash tool for the selected detail period.',
'',
'Notes',
Expand Down Expand Up @@ -338,6 +358,7 @@ export async function exportCsv(periods: PeriodExport[], outputPath: string): Pr
await writeFile(join(folder, 'projects.csv'), rowsToCsv(buildProjectRows(thirtyDayProjects)), 'utf-8')
await writeFile(join(folder, 'sessions.csv'), rowsToCsv(buildSessionRows(thirtyDayProjects)), 'utf-8')
await writeFile(join(folder, 'tools.csv'), rowsToCsv(buildToolRows(thirtyDayProjects)), 'utf-8')
await writeFile(join(folder, 'mcp.csv'), rowsToCsv(buildMcpRows(thirtyDayProjects)), 'utf-8')
await writeFile(join(folder, 'shell-commands.csv'), rowsToCsv(buildBashRows(thirtyDayProjects)), 'utf-8')

return folder
Expand All @@ -362,6 +383,7 @@ export async function exportJson(periods: PeriodExport[], outputPath: string): P
projects: buildProjectRows(thirtyDayProjects),
sessions: buildSessionRows(thirtyDayProjects),
tools: buildToolRows(thirtyDayProjects),
mcp: buildMcpRows(thirtyDayProjects),
shellCommands: buildBashRows(thirtyDayProjects),
}

Expand Down
32 changes: 31 additions & 1 deletion tests/export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mkdtemp, readFile, readdir, rm } from 'fs/promises'
import { join } from 'path'
import { tmpdir } from 'os'

import { exportCsv, type PeriodExport } from '../src/export.js'
import { exportCsv, exportJson, type PeriodExport } from '../src/export.js'
import type { ProjectSummary } from '../src/types.js'

let tmpDir: string
Expand Down Expand Up @@ -193,4 +193,34 @@ describe('exportCsv', () => {
expect(readme).toContain('selected detail period')
expect(readme).not.toContain('30-day window')
})

it('writes MCP server usage to mcp.csv', async () => {
const project = makeProject('app')
project.sessions[0]!.mcpBreakdown = { node_repl: { calls: 5 } }
const periods: PeriodExport[] = [{ label: '30 Days', projects: [project] }]

const folder = await exportCsv(periods, join(tmpDir, 'mcp.csv'))
const mcp = await readFile(join(folder, 'mcp.csv'), 'utf-8')

expect(mcp).toContain('Server,Calls,Share (%)')
expect(mcp).toContain('node_repl,5,100')
})
})

describe('exportJson', () => {
it('includes an mcp section with per-server usage', async () => {
const project = makeProject('app')
project.sessions[0]!.mcpBreakdown = { node_repl: { calls: 3 }, github: { calls: 1 } }
const periods: PeriodExport[] = [{ label: '30 Days', projects: [project] }]

const outputPath = join(tmpDir, 'export.json')
const saved = await exportJson(periods, outputPath)
const data = JSON.parse(await readFile(saved, 'utf-8'))

expect(Array.isArray(data.mcp)).toBe(true)
expect(data.mcp).toEqual([
{ Server: 'node_repl', Calls: 3, 'Share (%)': 75 },
{ Server: 'github', Calls: 1, 'Share (%)': 25 },
])
})
})