77import { kfetchAbortable } from 'ui/kfetch' ;
88import { SearchError } from 'ui/courier' ;
99
10- export const rollupSearchStrategy = {
11- id : 'rollup' ,
10+ function getAllFetchParams ( searchRequests , Promise ) {
11+ return Promise . map ( searchRequests , ( searchRequest ) => {
12+ return Promise . try ( searchRequest . getFetchParams , void 0 , searchRequest )
13+ . then ( ( fetchParams ) => {
14+ return ( searchRequest . fetchParams = fetchParams ) ;
15+ } )
16+ . then ( value => ( { resolved : value } ) )
17+ . catch ( error => ( { rejected : error } ) ) ;
18+ } ) ;
19+ }
1220
13- search : async ( { searchRequests, Promise } ) => {
14- // TODO: Batch together requests to hit a bulk rollup search endpoint.
15- const searchRequest = searchRequests [ 0 ] ;
16- const searchParams = await searchRequest . getFetchParams ( ) ;
17- const indexPattern = searchParams . index . title || searchParams . index ;
21+ async function serializeAllFetchParams ( fetchParams , searchRequests ) {
22+ const searchRequestsWithFetchParams = [ ] ;
23+ const failedSearchRequests = [ ] ;
24+
25+ // Gather the fetch param responses from all the successful requests.
26+ fetchParams . forEach ( ( result , index ) => {
27+ if ( result . resolved ) {
28+ searchRequestsWithFetchParams . push ( result . resolved ) ;
29+ } else {
30+ const searchRequest = searchRequests [ index ] ;
31+
32+ searchRequest . handleFailure ( result . rejected ) ;
33+ failedSearchRequests . push ( searchRequest ) ;
34+ }
35+ } ) ;
36+
37+ const serializedFetchParams = serializeFetchParams ( searchRequestsWithFetchParams ) ;
38+
39+ return {
40+ serializedFetchParams,
41+ failedSearchRequests,
42+ } ;
43+ }
44+
45+ function serializeFetchParams ( searchRequestsWithFetchParams ) {
46+ return JSON . stringify ( searchRequestsWithFetchParams . map ( searchRequestWithFetchParams => {
47+ const indexPattern = searchRequestWithFetchParams . index . title || searchRequestWithFetchParams . index ;
1848 const {
1949 body : {
2050 size,
2151 aggs,
2252 query : _query ,
2353 } ,
24- } = searchParams ;
54+ index,
55+ } = searchRequestWithFetchParams ;
2556
2657 // TODO: Temporarily automatically assign same timezone and interval as what's defined by
2758 // the rollup job. This should be done by the visualization itself.
@@ -30,7 +61,7 @@ export const rollupSearchStrategy = {
3061
3162 Object . keys ( subAggs ) . forEach ( subAggName => {
3263 if ( subAggName === 'date_histogram' ) {
33- const dateHistogramAgg = searchRequest . source . getField ( ' index' ) . typeMeta . aggs . date_histogram ;
64+ const dateHistogramAgg = index . typeMeta . aggs . date_histogram ;
3465 const subAgg = subAggs [ subAggName ] ;
3566 const field = subAgg . field ;
3667 subAgg . time_zone = dateHistogramAgg [ field ] . time_zone ;
@@ -39,30 +70,43 @@ export const rollupSearchStrategy = {
3970 } ) ;
4071 } ) ;
4172
42- const index = indexPattern ;
4373 const query = {
4474 'size' : size ,
4575 'aggregations' : aggs ,
4676 'query' : _query ,
4777 } ;
4878
79+ return { index : indexPattern , query } ;
80+ } ) ) ;
81+ }
82+
83+ export const rollupSearchStrategy = {
84+ id : 'rollup' ,
85+
86+ search : async ( { searchRequests, Promise } ) => {
87+ // Flatten the searchSource within each searchRequest to get the fetch params,
88+ // e.g. body, filters, index pattern, query.
89+ const allFetchParams = await getAllFetchParams ( searchRequests , Promise ) ;
90+
91+ // Serialize the fetch params into a format suitable for the body of an ES query.
92+ const {
93+ serializedFetchParams,
94+ failedSearchRequests,
95+ } = await serializeAllFetchParams ( allFetchParams , searchRequests ) ;
96+
4997 const {
5098 fetching,
5199 abort,
52100 } = kfetchAbortable ( {
53101 pathname : '../api/rollup/search' ,
54102 method : 'POST' ,
55- body : JSON . stringify ( { index , query } ) ,
103+ body : serializedFetchParams ,
56104 } ) ;
57105
58- // TODO: Implement this. Search requests which can't be sent.
59- const failedSearchRequests = [ ] ;
60-
61106 return {
62- // Munge data into shape expected by consumer.
63107 searching : new Promise ( ( resolve , reject ) => {
64108 fetching . then ( result => {
65- resolve ( [ result ] ) ;
109+ resolve ( result ) ;
66110 } ) . catch ( error => {
67111 const {
68112 body : { statusText, error : title , message } ,
0 commit comments