(fix/search) Search logs fix (#1491)

* Update search.ts

* Update search.ts
This commit is contained in:
Nicolas 2025-04-22 17:12:10 -04:00 committed by GitHub
parent e10d4c7b0c
commit e532a96b0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -95,9 +95,10 @@ async function scrapeSearchResult(
mode: "single_urls" as Mode, mode: "single_urls" as Mode,
team_id: options.teamId, team_id: options.teamId,
scrapeOptions: options.scrapeOptions, scrapeOptions: options.scrapeOptions,
internalOptions: { teamId: options.teamId }, internalOptions: { teamId: options.teamId, useCache: true },
origin: options.origin, origin: options.origin,
is_scrape: true, is_scrape: true,
}, },
{}, {},
jobId, jobId,
@ -157,6 +158,13 @@ export async function searchController(
method: "searchController", method: "searchController",
}); });
let responseData: SearchResponse = {
success: true,
data: [],
};
const startTime = new Date().getTime();
const costTracking = new CostTracking();
try { try {
req.body = searchRequestSchema.parse(req.body); req.body = searchRequestSchema.parse(req.body);
@ -165,8 +173,6 @@ export async function searchController(
origin: req.body.origin, origin: req.body.origin,
}); });
const startTime = new Date().getTime();
let limit = req.body.limit; let limit = req.body.limit;
// Buffer results by 50% to account for filtered URLs // Buffer results by 50% to account for filtered URLs
@ -196,37 +202,17 @@ export async function searchController(
if (searchResults.length === 0) { if (searchResults.length === 0) {
logger.info("No search results found"); logger.info("No search results found");
return res.status(200).json({ responseData.warning = "No search results found";
success: true, } else if (
data: [],
warning: "No search results found",
});
}
if (
!req.body.scrapeOptions.formats || !req.body.scrapeOptions.formats ||
req.body.scrapeOptions.formats.length === 0 req.body.scrapeOptions.formats.length === 0
) { ) {
billTeam(req.auth.team_id, req.acuc?.sub_id, searchResults.length).catch( responseData.data = searchResults.map((r) => ({
(error) => {
logger.error(
`Failed to bill team ${req.auth.team_id} for ${searchResults.length} credits: ${error}`,
);
},
);
return res.status(200).json({
success: true,
data: searchResults.map((r) => ({
url: r.url, url: r.url,
title: r.title, title: r.title,
description: r.description, description: r.description,
})) as Document[], })) as Document[];
}); } else {
}
const costTracking = new CostTracking();
// Scrape each non-blocked result, handling timeouts individually
logger.info("Scraping search results"); logger.info("Scraping search results");
const scrapePromises = searchResults.map((result) => const scrapePromises = searchResults.map((result) =>
scrapeSearchResult(result, { scrapeSearchResult(result, {
@ -242,14 +228,6 @@ export async function searchController(
num_docs: docs.length, num_docs: docs.length,
}); });
// Bill for successful scrapes only
billTeam(req.auth.team_id, req.acuc?.sub_id, docs.length).catch((error) => {
logger.error(
`Failed to bill team ${req.auth.team_id} for ${docs.length} credits: ${error}`,
);
});
// Filter out empty content but keep docs with SERP results
const filteredDocs = docs.filter( const filteredDocs = docs.filter(
(doc) => (doc) =>
doc.serpResults || (doc.markdown && doc.markdown.trim().length > 0), doc.serpResults || (doc.markdown && doc.markdown.trim().length > 0),
@ -260,26 +238,33 @@ export async function searchController(
}); });
if (filteredDocs.length === 0) { if (filteredDocs.length === 0) {
return res.status(200).json({ responseData.data = docs;
success: true, responseData.warning = "No content found in search results";
data: docs, } else {
warning: "No content found in search results", responseData.data = filteredDocs;
});
} }
}
// Bill team once for all successful results
billTeam(req.auth.team_id, req.acuc?.sub_id, responseData.data.length).catch((error) => {
logger.error(
`Failed to bill team ${req.auth.team_id} for ${responseData.data.length} credits: ${error}`,
);
});
const endTime = new Date().getTime(); const endTime = new Date().getTime();
const timeTakenInSeconds = (endTime - startTime) / 1000; const timeTakenInSeconds = (endTime - startTime) / 1000;
logger.info("Logging job", { logger.info("Logging job", {
num_docs: filteredDocs.length, num_docs: responseData.data.length,
time_taken: timeTakenInSeconds, time_taken: timeTakenInSeconds,
}); });
logJob({ logJob({
job_id: jobId, job_id: jobId,
success: true, success: true,
num_docs: filteredDocs.length, num_docs: responseData.data.length,
docs: filteredDocs, docs: responseData.data,
time_taken: timeTakenInSeconds, time_taken: timeTakenInSeconds,
team_id: req.auth.team_id, team_id: req.auth.team_id,
mode: "search", mode: "search",
@ -288,10 +273,8 @@ export async function searchController(
cost_tracking: costTracking, cost_tracking: costTracking,
}); });
return res.status(200).json({ return res.status(200).json(responseData);
success: true,
data: filteredDocs,
});
} catch (error) { } catch (error) {
if ( if (
error instanceof Error && error instanceof Error &&