Skip to content

Commit 6f30432

Browse files
author
Marco
committed
latest
1 parent 0573aab commit 6f30432

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

components/webui/server/src/routes/api/presto-search/index.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
4343
tags: ["Presto Search"],
4444
},
4545
},
46-
46+
// eslint-disable-next-line max-lines-per-function
4747
async (request, reply) => {
4848
const {queryString} = request.body;
4949

@@ -61,13 +61,29 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
6161
`Received ${data.length} rows from Presto query`
6262
);
6363

64+
if (false === isResolved) {
65+
request.log.error(
66+
"Presto data received before searchJobId was resolved; " +
67+
"skipping insert."
68+
);
69+
70+
return;
71+
}
72+
73+
if (0 === data.length) {
74+
return;
75+
}
76+
6477
insertPrestoRowsToMongo({
65-
columns: columns,
66-
data: data,
67-
isResolved: isResolved,
68-
log: request.log,
69-
mongoDb: mongoDb,
70-
searchJobId: searchJobId,
78+
columns,
79+
data,
80+
mongoDb,
81+
searchJobId,
82+
}).catch((err: unknown) => {
83+
request.log.error(
84+
err,
85+
"Failed to insert Presto results into MongoDB"
86+
);
7187
});
7288
},
7389
error: (error) => {

components/webui/server/src/routes/api/presto-search/utils.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type {InsertManyResult} from "mongodb";
2+
13
import type {InsertPrestoRowsToMongoProps} from "./typings.js";
24

35

@@ -28,31 +30,18 @@ const prestoRowToObject = (
2830
* @param props.data
2931
* @param props.columns
3032
* @param props.searchJobId
31-
* @param props.isResolved
3233
* @param props.mongoDb
33-
* @param props.log
34+
* @return Promise that resolves when the insertion is complete
3435
*/
3536
const insertPrestoRowsToMongo = ({
3637
columns,
3738
data,
38-
isResolved,
39-
log,
4039
mongoDb,
4140
searchJobId,
42-
}: InsertPrestoRowsToMongoProps): void => {
43-
if (false === isResolved) {
44-
log.error("Presto data received before searchJobId was resolved; skipping insert.");
45-
46-
return;
47-
}
48-
49-
if (0 < data.length && searchJobId) {
50-
const collection = mongoDb.collection(searchJobId);
51-
const resultDocs = data.map((row) => prestoRowToObject(row, columns));
52-
collection.insertMany(resultDocs).catch((err: unknown) => {
53-
log.error(err, "Failed to insert Presto results into MongoDB");
54-
});
55-
}
41+
}: InsertPrestoRowsToMongoProps): Promise<InsertManyResult<Document>> => {
42+
const collection = mongoDb.collection(searchJobId);
43+
const resultDocs = data.map((row) => prestoRowToObject(row, columns));
44+
return collection.insertMany(resultDocs);
5645
};
5746

5847
export {insertPrestoRowsToMongo};

0 commit comments

Comments
 (0)