-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·109 lines (96 loc) · 3.93 KB
/
Copy pathserver.js
File metadata and controls
executable file
·109 lines (96 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env node
const http = require('http');
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const { loadDailyUsageData, loadMonthlyUsageData } = require('ccusage/data-loader');
const { calculateTotals, createTotalsObject, getTotalTokens } = require('ccusage/calculate-cost');
const PORT = 8150;
const pkg = require('./package.json');
const OPEN_CMD = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
// Format data to match CLI JSON output
const formatDailyData = (data) => data.map(d => ({
date: d.date,
inputTokens: d.inputTokens,
outputTokens: d.outputTokens,
cacheCreationTokens: d.cacheCreationTokens,
cacheReadTokens: d.cacheReadTokens,
totalTokens: getTotalTokens(d),
totalCost: d.totalCost,
modelsUsed: d.modelsUsed,
modelBreakdowns: d.modelBreakdowns,
}));
const formatMonthlyData = (data) => data.map(d => ({
month: d.month,
inputTokens: d.inputTokens,
outputTokens: d.outputTokens,
cacheCreationTokens: d.cacheCreationTokens,
cacheReadTokens: d.cacheReadTokens,
totalTokens: getTotalTokens(d),
totalCost: d.totalCost,
modelsUsed: d.modelsUsed,
modelBreakdowns: d.modelBreakdowns,
}));
const server = http.createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
const url = new URL(req.url, `http://${req.headers.host}`);
const force = url.searchParams.get('force') === 'true';
if (url.pathname === '/') {
fs.readFile(path.join(__dirname, 'public', 'index.html'), (err, content) => {
if (err) {
res.writeHead(500);
res.end('Error loading index.html');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content);
}
});
return;
}
// Cache Implementation
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
const handleCachedRequest = async (key, loader, formatter, dataKey) => {
if (!global.cache) global.cache = {};
const cached = global.cache[key];
const now = Date.now();
if (!force && cached && (now - cached.timestamp < CACHE_DURATION)) {
console.log(`Serving ${key} from cache (${Math.round((CACHE_DURATION - (now - cached.timestamp))/1000)}s left)`);
res.writeHead(200, { 'Content-Type': 'application/json', 'X-Cache': 'HIT' });
res.end(cached.data);
return;
}
try {
console.log(`Fetching ${key} data (Live)...`);
const rawData = await loader();
const totals = createTotalsObject(calculateTotals(rawData));
const result = JSON.stringify({ [dataKey]: formatter(rawData), totals });
global.cache[key] = { timestamp: now, data: result };
res.writeHead(200, { 'Content-Type': 'application/json', 'X-Cache': 'MISS' });
res.end(result);
} catch (error) {
console.error(`Error fetching ${key} data:`, error);
res.writeHead(500);
res.end(JSON.stringify({ error: `Failed to fetch ${key} data` }));
}
};
if (url.pathname === '/api/monthly') {
await handleCachedRequest('monthly', loadMonthlyUsageData, formatMonthlyData, 'monthly');
return;
}
if (url.pathname === '/api/daily') {
await handleCachedRequest('daily', loadDailyUsageData, formatDailyData, 'daily');
return;
}
if (url.pathname === '/api/version') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ version: pkg.version }));
return;
}
res.writeHead(404);
res.end('Not Found');
});
server.listen(PORT, () => {
console.log(`\n🚀 ccusage-ui server running at http://localhost:${PORT}`);
console.log('Fetching data and opening browser...\n');
exec(`${OPEN_CMD} http://localhost:${PORT}`);
});