@@ -4,8 +4,8 @@ import { RequestOptions, request } from 'https'
4
4
import { URL } from 'url'
5
5
import { getMultiClusterHub } from '../lib/multi-cluster-hub'
6
6
import { getNamespace , getServiceAccountToken , getServiceCACertificate } from '../lib/serviceAccountToken'
7
- import { IResource } from '../resources/resource'
8
7
import { logger } from './logger'
8
+ import { IQuery } from '../routes/aggregators/applications'
9
9
10
10
export type ISearchResult = {
11
11
data : {
@@ -57,74 +57,13 @@ export async function getSearchOptions(headers: OutgoingHttpHeaders): Promise<Re
57
57
return options
58
58
}
59
59
60
- // search api does not provide paging but we want to break up our searches so as not to overtax the search api by querying for all apps at once
61
- // this is pseudo-paging: we grab all apps that begin with a letter, that way we don't have overlapping results
62
- // we don't want to do this letter by letter because that would take 36 searches
63
- // so we create 6 groupings of letters and we try to make each group search return about the same number of apps
64
- export const pagedSearchQueries : string [ ] [ ] = [
65
- [ 'a*' , 'i*' , 'n*' ] ,
66
- [ 'e*' , 'r*' , 'o*' ] ,
67
- [ 's*' , 't*' , 'u*' , 'l*' , 'm*' , 'c*' ] ,
68
- [ 'd*' , 'b*' , 'g*' , '0*' , '1*' , '2*' , '3*' , '4*' ] ,
69
- [ 'h*' , 'p*' , 'k*' , 'y*' , 'v*' , 'z*' , 'w*' , 'f*' ] ,
70
- [ 'j*' , 'q*' , 'x*' , '5*' , '6*' , '7*' , '8*' , '9*' ] ,
71
- ]
72
-
73
- export async function getPagedSearchResources (
74
- query : {
75
- operationName : string
76
- variables : { input : { filters : { property : string ; values : string [ ] } [ ] ; limit : number } [ ] }
77
- query : string
78
- } ,
79
- usePagedQuery : boolean ,
80
- kind : string ,
81
- pass : number
82
- ) {
60
+ export async function getSearchResults ( query : IQuery ) {
83
61
const options = await getServiceAccountOptions ( )
84
- let resources : IResource [ ] = [ ]
85
- for ( let i = 0 ; i < pagedSearchQueries . length ; ) {
86
- const _query = structuredClone ( query )
87
- // should we limit the results by groupings of apps that
88
- // begin with certain letters?
89
- if ( usePagedQuery ) {
90
- const values = pagedSearchQueries [ i ]
91
- _query . variables . input [ 0 ] . filters . push ( {
92
- property : 'name' ,
93
- values,
94
- } )
95
- }
96
- let results : ISearchResult
97
- try {
98
- results = await getSearchResults ( options , JSON . stringify ( _query ) , kind , pass )
99
- } catch ( e ) {
100
- logger . error ( `getPagedSearchResources ${ kind } ${ e } ` )
101
- continue
102
- }
103
- const items = ( results . data ?. searchResult ?. [ 0 ] ?. items || [ ] ) as IResource [ ]
104
- resources = resources . concat ( items )
105
- if ( process . env . NODE_ENV !== 'test' ) {
106
- let timeout = 10000
107
- if ( items . length < 1000 ) timeout = 2000
108
- await new Promise ( ( r ) => setTimeout ( r , timeout ) )
109
- }
110
- if ( ! usePagedQuery ) break
111
- i ++
112
- }
113
- return resources
114
- }
115
-
116
- export function getSearchResults (
117
- options : string | RequestOptions | URL ,
118
- variables : string ,
119
- kind : string ,
120
- pass : number
121
- ) {
122
- // if acm/mce are starting up, increase the timeout in case search hasn't started yet
123
- const requestTimeout = ( pass <= 2 ? 10 : 2 ) * 60 * 1000
62
+ const requestTimeout = 2 * 60 * 1000
124
63
return new Promise < ISearchResult > ( ( resolve , reject ) => {
125
64
let body = ''
126
65
const id = setTimeout ( ( ) => {
127
- logger . error ( `getSearchResults ${ kind } request timeout` )
66
+ logger . error ( `getSearchResults request timeout` )
128
67
reject ( Error ( 'request timeout' ) )
129
68
} , requestTimeout )
130
69
const req = request ( options , ( res ) => {
@@ -136,14 +75,14 @@ export function getSearchResults(
136
75
const result = JSON . parse ( body ) as ISearchResult
137
76
const message = typeof result === 'string' ? result : result . message
138
77
if ( message ) {
139
- logger . error ( `getSearchResults ${ kind } return error ${ message } ` )
78
+ logger . error ( `getSearchResults return error ${ message } ` )
140
79
reject ( Error ( result . message ) )
141
80
}
142
81
resolve ( result )
143
82
} catch ( e ) {
144
83
// search might be overwhelmed
145
84
// pause before next request
146
- logger . error ( `getSearchResults ${ kind } parse error ${ e } ${ body } ` )
85
+ logger . error ( `getSearchResults parse error ${ e } ${ body } ` )
147
86
setTimeout ( ( ) => {
148
87
reject ( Error ( body ) )
149
88
} , requestTimeout )
@@ -152,10 +91,10 @@ export function getSearchResults(
152
91
} )
153
92
} )
154
93
req . on ( 'error' , ( e ) => {
155
- logger . error ( `getSearchResults ${ kind } request error ${ e . message } ` )
94
+ logger . error ( `getSearchResults request error ${ e . message } ` )
156
95
reject ( e )
157
96
} )
158
- req . write ( variables )
97
+ req . write ( JSON . stringify ( query ) )
159
98
req . end ( )
160
99
} )
161
100
}
@@ -206,7 +145,8 @@ export async function pingSearchAPI() {
206
145
reject ( new Error ( 'no data' ) )
207
146
}
208
147
} catch ( e ) {
209
- reject ( new Error ( new String ( e ) . valueOf ( ) ) )
148
+ logger . error ( `pingSearchAPI parse error ${ e } ${ body } ` )
149
+ reject ( new Error ( String ( e ) . valueOf ( ) ) )
210
150
}
211
151
clearTimeout ( id )
212
152
} )
0 commit comments