mcp-product-manager
Version:
MCP Orchestrator for task and project management with web interface
102 lines • 4.3 kB
JavaScript
// Current usage endpoint - provides real-time usage data with model breakdown
import { Router } from 'express';
import { success, error } from '../../utils/response.js';
const router = Router();
// GET /api/usage/current - Get current block usage with analysis
router.get('/', async (req, res, next) => {
try {
const usageService = req.app.locals.usageService;
if (!usageService || !usageService.isInitialized) {
return error(res, 'Usage tracking service not initialized', 503);
}
const currentUsage = await usageService.getCurrentUsage();
if (currentUsage.error) {
return error(res, currentUsage.error, 500);
}
// Format response with key metrics
const response = {
block: {
id: currentUsage.block.blockId,
startTime: currentUsage.block.startTime,
elapsedMinutes: currentUsage.block.elapsedMinutes,
isActive: currentUsage.block.isActive
},
usage: {
opus: {
tokens: currentUsage.usage.opus.tokens,
cost: currentUsage.usage.opus.cost,
breakdown: {
input: currentUsage.usage.opus.inputTokens,
output: currentUsage.usage.opus.outputTokens,
cache: currentUsage.usage.opus.cacheTokens
}
},
sonnet: {
tokens: currentUsage.usage.sonnet.tokens,
cost: currentUsage.usage.sonnet.cost,
breakdown: {
input: currentUsage.usage.sonnet.inputTokens,
output: currentUsage.usage.sonnet.outputTokens,
cache: currentUsage.usage.sonnet.cacheTokens
}
},
total: {
tokens: currentUsage.usage.total.tokens,
cost: currentUsage.usage.total.cost
}
},
analysis: {
burnRate: {
perHour: currentUsage.analysis.burnRate,
perMinute: currentUsage.analysis.burnRate / 60
},
budget: {
remaining: currentUsage.analysis.budgetRemaining,
hoursUntilExhausted: currentUsage.analysis.hoursUntilBudgetExhausted,
percentUsed: ((500 - currentUsage.analysis.budgetRemaining) / 500) * 100
},
modelRatio: {
opus: currentUsage.analysis.opusRatio * 100,
sonnet: currentUsage.analysis.sonnetRatio * 100
},
warnings: currentUsage.analysis.warnings,
recommendations: currentUsage.analysis.recommendations
},
timestamp: currentUsage.timestamp
};
success(res, response);
}
catch (err) {
console.error('Error in /api/usage/current:', err);
next(err);
}
});
// GET /api/usage/current/summary - Simplified summary for dashboard widgets
router.get('/summary', async (req, res, next) => {
try {
const usageService = req.app.locals.usageService;
if (!usageService || !usageService.isInitialized) {
return error(res, 'Usage tracking service not initialized', 503);
}
const currentUsage = await usageService.getCurrentUsage();
if (currentUsage.error) {
return error(res, currentUsage.error, 500);
}
// Simplified summary for quick display
const summary = {
cost: currentUsage.usage.total.cost,
burnRate: currentUsage.analysis.burnRate,
budgetRemaining: currentUsage.analysis.budgetRemaining,
timeRemaining: currentUsage.analysis.hoursUntilBudgetExhausted,
criticalWarnings: currentUsage.analysis.warnings.filter(w => w.severity === 'critical').length,
timestamp: currentUsage.timestamp
};
success(res, summary);
}
catch (err) {
console.error('Error in /api/usage/current/summary:', err);
next(err);
}
});
export default router;
//# sourceMappingURL=current.js.map