Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions components/webui/server/src/routes/api/presto-search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
PrestoQueryJobCreationSchema,
PrestoQueryJobSchema,
} from "../../../schemas/presto-search.js";
import {MAX_PRESTO_SEARCH_RESULTS} from "./typings.js";
import {insertPrestoRowsToMongo} from "./utils.js";


Expand Down Expand Up @@ -61,18 +62,20 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
const {queryString} = request.body;

let searchJobId: string;
let totalResultsCount = 0;
let storedResultsCount = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove storedResultsCount


try {
// eslint-disable-next-line max-lines-per-function
searchJobId = await new Promise<string>((resolve, reject) => {
let isResolved = false;
Presto.client.execute({
// eslint-disable-next-line no-warning-comments
// TODO: Error, and success handlers are dummy implementations
// and will be replaced with proper implementations.
data: (_, data, columns) => {
totalResultsCount += data.length;
Copy link
Contributor

@hoophalab hoophalab Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this after the insertion of data and before the update of metadata


request.log.info(
`Received ${data.length} rows from Presto query`
`Received ${data.length} rows from Presto query ` +
`(total: ${totalResultsCount})`
);

if (false === isResolved) {
Expand All @@ -88,15 +91,35 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
return;
}

insertPrestoRowsToMongo(
data,
columns,
searchJobId,
mongoDb
if (storedResultsCount < MAX_PRESTO_SEARCH_RESULTS) {
const remainingSlots =
MAX_PRESTO_SEARCH_RESULTS - storedResultsCount;
const dataToInsert = data.slice(0, remainingSlots);

Comment on lines +94 to +98
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (storedResultsCount < MAX_PRESTO_SEARCH_RESULTS) {
const remainingSlots =
MAX_PRESTO_SEARCH_RESULTS - storedResultsCount;
const dataToInsert = data.slice(0, remainingSlots);
if (totalResultsCount < MAX_PRESTO_SEARCH_RESULTS) {
const remainingSlots =
MAX_PRESTO_SEARCH_RESULTS - totalResultsCount;
const dataToInsert = data.slice(0, remainingSlots);

if (0 < dataToInsert.length) {
storedResultsCount += dataToInsert.length;
insertPrestoRowsToMongo(
dataToInsert,
columns,
searchJobId,
mongoDb
).catch((err: unknown) => {
request.log.error(
err,
"Failed to insert Presto results into MongoDB"
);
});
}
}

// Always update metadata with total count
searchResultsMetadataCollection.updateOne(
{_id: searchJobId},
{$set: {numTotalResults: totalResultsCount}}
).catch((err: unknown) => {
request.log.error(
err,
"Failed to insert Presto results into MongoDB"
"Failed to update total results count in metadata"
);
});
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Maximum number of Presto search results to store in MongoDB.
*/
export const MAX_PRESTO_SEARCH_RESULTS = 1000;