Nick: credit usage endpoint

This commit is contained in:
Nicolas 2024-12-20 15:44:17 -03:00
parent 58b8064958
commit 6222152249
3 changed files with 54 additions and 1 deletions

View File

@ -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<void> {
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",
});
}
}

View File

@ -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),
);

View File

@ -86,7 +86,7 @@ const RATE_LIMITS = {
testSuite: {
free: 10000,
default: 10000,
},
}
};
export const redisRateLimitClient = new Redis(