UNPKG

@limitrate/cli

Version:

CLI dashboard for LimitRate rate limiting inspection

87 lines (84 loc) 2.73 kB
import { getStorage } from "./chunk-ZWXM33YD.mjs"; // src/commands/inspect.ts import Table from "cli-table3"; function inspect() { const storage = getStorage(); console.log("\n\u{1F4CA} LimitRate Dashboard (last 48 hours)\n"); console.log("=" + "=".repeat(60)); console.log(""); const eventCount = storage.getEventCount(); console.log(`Total events tracked: ${eventCount.toLocaleString()} `); const endpointStats = storage.getEndpointStats(); if (endpointStats.length > 0) { const endpointTable = new Table({ head: ["Endpoint", "Total Hits", "Blocked", "Slowdowns"], colWidths: [35, 15, 12, 12] }); for (const stat of endpointStats) { endpointTable.push([ stat.endpoint, stat.totalHits.toLocaleString(), stat.blocked.toLocaleString(), stat.slowdowns.toLocaleString() ]); } console.log("\u{1F4C8} Endpoint Statistics:\n"); console.log(endpointTable.toString()); console.log(""); } else { console.log("\u{1F4C8} No endpoint data yet. Start making requests!\n"); } const topOffenders = storage.getTopOffenders(10); if (topOffenders.length > 0) { const offenderTable = new Table({ head: ["User", "Plan", "Blocks (last hour)"], colWidths: [30, 12, 20] }); for (const offender of topOffenders) { offenderTable.push([ offender.user.length > 28 ? offender.user.substring(0, 25) + "..." : offender.user, offender.plan, offender.blocks.toLocaleString() ]); } console.log("\u{1F6A8} Top Offenders (last hour):\n"); console.log(offenderTable.toString()); console.log(""); } const recentEvents = storage.getRecentEvents(10); if (recentEvents.length > 0) { console.log("\u{1F4CB} Recent Events:\n"); for (const event of recentEvents) { const timestamp = new Date(event.timestamp).toLocaleTimeString(); const emoji = getEventEmoji(event.type); const userDisplay = event.user.length > 20 ? event.user.substring(0, 17) + "..." : event.user; console.log(` ${emoji} [${timestamp}] ${userDisplay} (${event.plan}) ${event.type} on ${event.endpoint}`); } console.log(""); } console.log("=" + "=".repeat(60)); console.log(""); console.log("\u{1F4A1} Tip: Run this command again to see updated stats"); console.log("\u{1F4D6} Docs: https://github.com/limitrate/limitrate\n"); storage.close(); } function getEventEmoji(type) { switch (type) { case "rate_exceeded": case "cost_exceeded": case "blocked": return "\u{1F6AB}"; case "slowdown_applied": return "\u{1F40C}"; case "allowed": return "\u2705"; default: return "\u{1F4CC}"; } } export { inspect };