@@ -6,21 +6,28 @@ import {
6
6
PrestoQueryJobCreationSchema ,
7
7
PrestoQueryJobSchema ,
8
8
} from "../../../schemas/presto-search.js" ;
9
+ import { insertPrestoRowsToMongo } from "./utils.js" ;
9
10
10
11
11
12
/**
12
13
* Presto search API routes.
13
14
*
14
15
* @param fastify
15
16
*/
17
+ // eslint-disable-next-line max-lines-per-function
16
18
const plugin : FastifyPluginAsyncTypebox = async ( fastify ) => {
17
- const { Presto} = fastify ;
19
+ const { Presto, mongo} = fastify ;
20
+ const mongoDb = mongo . db ;
18
21
19
22
if ( "undefined" === typeof Presto ) {
20
23
// If Presto client is not available, skip the plugin registration.
21
24
return ;
22
25
}
23
26
27
+ if ( "undefined" === typeof mongoDb ) {
28
+ throw new Error ( "MongoDB database not found" ) ;
29
+ }
30
+
24
31
/**
25
32
* Submits a search query.
26
33
*/
@@ -36,7 +43,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
36
43
tags : [ "Presto Search" ] ,
37
44
} ,
38
45
} ,
39
-
46
+ // eslint-disable-next-line max-lines-per-function
40
47
async ( request , reply ) => {
41
48
const { queryString} = request . body ;
42
49
@@ -45,13 +52,39 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
45
52
try {
46
53
searchJobId = await new Promise < string > ( ( resolve , reject ) => {
47
54
let isResolved = false ;
48
-
49
55
Presto . client . execute ( {
50
56
// eslint-disable-next-line no-warning-comments
51
- // TODO: Data, error , and success handlers are dummy implementations
57
+ // TODO: Error , and success handlers are dummy implementations
52
58
// and will be replaced with proper implementations.
53
59
data : ( _ , data , columns ) => {
54
- request . log . info ( { columns, data} , "Presto data" ) ;
60
+ request . log . info (
61
+ `Received ${ data . length } rows from Presto query`
62
+ ) ;
63
+
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
+
77
+ insertPrestoRowsToMongo (
78
+ data ,
79
+ columns ,
80
+ searchJobId ,
81
+ mongoDb
82
+ ) . catch ( ( err : unknown ) => {
83
+ request . log . error (
84
+ err ,
85
+ "Failed to insert Presto results into MongoDB"
86
+ ) ;
87
+ } ) ;
55
88
} ,
56
89
error : ( error ) => {
57
90
request . log . info ( error , "Presto search failed" ) ;
0 commit comments