Nick: fixed credits issue

This commit is contained in:
Nicolas
2024-08-28 18:32:45 -03:00
parent c3158b0f98
commit 67229c6b3a
5 changed files with 51 additions and 47 deletions
+9 -11
View File
@@ -63,6 +63,7 @@ export async function scrapeHelper(
pageOptions, pageOptions,
extractorOptions, extractorOptions,
origin: req.body.origin ?? defaultOrigin, origin: req.body.origin ?? defaultOrigin,
is_scrape: true,
}, },
{}, {},
jobId, jobId,
@@ -179,9 +180,7 @@ export async function scrapeController(req: Request, res: Response) {
typeof extractorOptions.extractionSchema !== "object" || typeof extractorOptions.extractionSchema !== "object" ||
extractorOptions.extractionSchema === null extractorOptions.extractionSchema === null
) { ) {
return res return res.status(400).json({
.status(400)
.json({
error: error:
"extractorOptions.extractionSchema must be an object if llm-extraction mode is specified", "extractorOptions.extractionSchema must be an object if llm-extraction mode is specified",
}); });
@@ -202,9 +201,7 @@ export async function scrapeController(req: Request, res: Response) {
} catch (error) { } catch (error) {
Logger.error(error); Logger.error(error);
earlyReturn = true; earlyReturn = true;
return res return res.status(500).json({
.status(500)
.json({
error: error:
"Error checking team credits. Please contact hello@firecrawl.com for help.", "Error checking team credits. Please contact hello@firecrawl.com for help.",
}); });
@@ -231,8 +228,8 @@ export async function scrapeController(req: Request, res: Response) {
: 0; : 0;
if (result.success) { if (result.success) {
let creditsToBeBilled = 0; // billing for doc done on queue end let creditsToBeBilled = 1;
const creditsPerLLMExtract = 50; const creditsPerLLMExtract = 49;
if (extractorOptions.mode.includes("llm-extraction")) { if (extractorOptions.mode.includes("llm-extraction")) {
// creditsToBeBilled = creditsToBeBilled + (creditsPerLLMExtract * filteredDocs.length); // creditsToBeBilled = creditsToBeBilled + (creditsPerLLMExtract * filteredDocs.length);
@@ -245,6 +242,8 @@ export async function scrapeController(req: Request, res: Response) {
// Don't bill if we're early returning // Don't bill if we're early returning
return; return;
} }
if (creditsToBeBilled > 0) {
// billing for doc done on queue end, bill only for llm extraction
const billingResult = await billTeam(team_id, creditsToBeBilled); const billingResult = await billTeam(team_id, creditsToBeBilled);
if (!billingResult.success) { if (!billingResult.success) {
return res.status(402).json({ return res.status(402).json({
@@ -254,6 +253,7 @@ export async function scrapeController(req: Request, res: Response) {
}); });
} }
} }
}
logJob({ logJob({
job_id: jobId, job_id: jobId,
@@ -276,9 +276,7 @@ export async function scrapeController(req: Request, res: Response) {
} catch (error) { } catch (error) {
Sentry.captureException(error); Sentry.captureException(error);
Logger.error(error); Logger.error(error);
return res return res.status(500).json({
.status(500)
.json({
error: error:
typeof error === "string" typeof error === "string"
? error ? error
+1
View File
@@ -29,6 +29,7 @@ export async function scrapeController(req: RequestWithAuth<{}, ScrapeResponse,
pageOptions, pageOptions,
extractorOptions: {}, extractorOptions: {},
origin: req.body.origin, origin: req.body.origin,
is_scrape: true,
}, {}, jobId, jobPriority); }, {}, jobId, jobPriority);
let doc: any | undefined; let doc: any | undefined;
+6 -1
View File
@@ -57,6 +57,7 @@ export async function startWebScraperPipeline({
team_id: job.data.team_id, team_id: job.data.team_id,
bull_job_id: job.id.toString(), bull_job_id: job.id.toString(),
priority: job.opts.priority, priority: job.opts.priority,
is_scrape: job.data.is_scrape ?? false,
})) as { success: boolean; message: string; docs: Document[] }; })) as { success: boolean; message: string; docs: Document[] };
} }
export async function runWebScraper({ export async function runWebScraper({
@@ -71,6 +72,7 @@ export async function runWebScraper({
team_id, team_id,
bull_job_id, bull_job_id,
priority, priority,
is_scrape=false,
}: RunWebScraperParams): Promise<RunWebScraperResult> { }: RunWebScraperParams): Promise<RunWebScraperResult> {
try { try {
const provider = new WebScraperDataProvider(); const provider = new WebScraperDataProvider();
@@ -118,8 +120,8 @@ export async function runWebScraper({
}) })
: docs; : docs;
if(is_scrape === false) {
const billingResult = await billTeam(team_id, filteredDocs.length); const billingResult = await billTeam(team_id, filteredDocs.length);
if (!billingResult.success) { if (!billingResult.success) {
// throw new Error("Failed to bill team, no subscription was found"); // throw new Error("Failed to bill team, no subscription was found");
return { return {
@@ -128,6 +130,9 @@ export async function runWebScraper({
docs: [], docs: [],
}; };
} }
}
// This is where the returnvalue from the job is set // This is where the returnvalue from the job is set
onSuccess(filteredDocs, mode); onSuccess(filteredDocs, mode);
+2
View File
@@ -32,6 +32,7 @@ export interface WebScraperOptions {
sitemapped?: boolean; sitemapped?: boolean;
webhook?: string; webhook?: string;
v1?: boolean; v1?: boolean;
is_scrape?: boolean;
} }
export interface RunWebScraperParams { export interface RunWebScraperParams {
@@ -46,6 +47,7 @@ export interface RunWebScraperParams {
team_id: string; team_id: string;
bull_job_id: string; bull_job_id: string;
priority?: number; priority?: number;
is_scrape?: boolean;
} }
export interface RunWebScraperResult { export interface RunWebScraperResult {
+1 -3
View File
@@ -153,8 +153,6 @@ export interface ScrapeResponseV0 {
* Includes options for both scraping and mapping during a crawl. * Includes options for both scraping and mapping during a crawl.
*/ */
export interface CrawlParams { export interface CrawlParams {
scrapeOptions?: ScrapeParams;
crawlerOptions?: {
includePaths?: string[]; includePaths?: string[];
excludePaths?: string[]; excludePaths?: string[];
maxDepth?: number; maxDepth?: number;
@@ -162,7 +160,7 @@ export interface CrawlParams {
allowBackwardLinks?: boolean; allowBackwardLinks?: boolean;
allowExternalLinks?: boolean; allowExternalLinks?: boolean;
ignoreSitemap?: boolean; ignoreSitemap?: boolean;
}; scrapeOptions?: ScrapeParams;
} }
/** /**
* Parameters for crawling operations on v0. * Parameters for crawling operations on v0.