From 62221522497ab9bb7890dc76de59af31f04ba6b5 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 20 Dec 2024 15:44:17 -0300 Subject: [PATCH 1/2] Nick: credit usage endpoint --- apps/api/src/controllers/v1/credit-usage.ts | 45 +++++++++++++++++++++ apps/api/src/routes/v1.ts | 8 ++++ apps/api/src/services/rate-limiter.ts | 2 +- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 apps/api/src/controllers/v1/credit-usage.ts diff --git a/apps/api/src/controllers/v1/credit-usage.ts b/apps/api/src/controllers/v1/credit-usage.ts new file mode 100644 index 000000000..da522c138 --- /dev/null +++ b/apps/api/src/controllers/v1/credit-usage.ts @@ -0,0 +1,45 @@ +import { Request, Response } from "express"; +import { RequestWithAuth } from "./types"; +import { getACUC } from "../auth"; +import { logger } from "../../lib/logger"; + +export async function creditUsageController( + req: RequestWithAuth, + res: Response, +): Promise { + try { + // If we already have the credit usage info from auth, use it + if (req.acuc) { + res.json({ + success: true, + data: { + remaining_credits: req.acuc.remaining_credits, + }, + }); + return; + } + + // Otherwise fetch fresh data + const chunk = await getACUC(req.auth.team_id); + if (!chunk) { + res.status(404).json({ + success: false, + error: "Could not find credit usage information", + }); + return; + } + + res.json({ + success: true, + data: { + remaining_credits: chunk.remaining_credits, + }, + }); + } catch (error) { + logger.error("Error in credit usage controller:", error); + res.status(500).json({ + success: false, + error: "Internal server error while fetching credit usage", + }); + } +} diff --git a/apps/api/src/routes/v1.ts b/apps/api/src/routes/v1.ts index f09573d98..764271145 100644 --- a/apps/api/src/routes/v1.ts +++ b/apps/api/src/routes/v1.ts @@ -31,6 +31,7 @@ import { extractController } from "../controllers/v1/extract"; // import { keyAuthController } from "../../src/controllers/v1/keyAuth"; // import { livenessController } from "../controllers/v1/liveness"; // import { readinessController } from "../controllers/v1/readiness"; +import { creditUsageController } from "../controllers/v1/credit-usage"; function checkCreditsMiddleware( minimum?: number, @@ -224,3 +225,10 @@ v1Router.delete( // Health/Probe routes // v1Router.get("/health/liveness", livenessController); // v1Router.get("/health/readiness", readinessController); + +v1Router.get( + "/team/credit-usage", + authMiddleware(RateLimiterMode.CrawlStatus), + wrap(creditUsageController), +); + diff --git a/apps/api/src/services/rate-limiter.ts b/apps/api/src/services/rate-limiter.ts index 21025589d..d88ff7ad0 100644 --- a/apps/api/src/services/rate-limiter.ts +++ b/apps/api/src/services/rate-limiter.ts @@ -86,7 +86,7 @@ const RATE_LIMITS = { testSuite: { free: 10000, default: 10000, - }, + } }; export const redisRateLimitClient = new Redis( From ba95df96b1fdcfdf1530d1cdd6415058f86b38a3 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 20 Dec 2024 15:45:44 -0300 Subject: [PATCH 2/2] Update rate-limiter.ts --- apps/api/src/services/rate-limiter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/api/src/services/rate-limiter.ts b/apps/api/src/services/rate-limiter.ts index d88ff7ad0..21025589d 100644 --- a/apps/api/src/services/rate-limiter.ts +++ b/apps/api/src/services/rate-limiter.ts @@ -86,7 +86,7 @@ const RATE_LIMITS = { testSuite: { free: 10000, default: 10000, - } + }, }; export const redisRateLimitClient = new Redis(