-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rollupSearchStrategy and SearchError (#20505)
* Add /api/rollup/search endpoint and rollupSearchStrategy. * Add SearchError for surfacing courier search errors. - Use toastNotifications to surface errors in visualization editor. - Add call-out react directive and use it to surface rollup errors in the visualization editor sidebar. - Temporarily assign timezone and interval values from rollup job to the search to avoid errors.
- Loading branch information
Showing
11 changed files
with
225 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.visEditorSidebarError { | ||
padding: 8px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import './register'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { addSearchStrategy } from 'ui/courier'; | ||
import { rollupSearchStrategy } from './rollup_search_strategy'; | ||
|
||
addSearchStrategy(rollupSearchStrategy); |
114 changes: 114 additions & 0 deletions
114
x-pack/plugins/rollup/public/search/rollup_search_strategy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { kfetchAbortable } from 'ui/kfetch'; | ||
import { SearchError } from 'ui/courier'; | ||
|
||
export const rollupSearchStrategy = { | ||
id: 'rollup', | ||
|
||
search: async ({ searchRequests, Promise }) => { | ||
// TODO: Batch together requests to hit a bulk rollup search endpoint. | ||
const searchRequest = searchRequests[0]; | ||
const { | ||
index: { title: indexPattern }, | ||
body: { | ||
size, | ||
aggs, | ||
}, | ||
} = await searchRequest.getFetchParams(); | ||
|
||
function findDateHistogram(aggs) { | ||
if (Array.isArray(aggs)) { | ||
for (let i = 0; i < aggs.length; i++) { | ||
const dateHistogram = findDateHistogram(aggs[i]); | ||
|
||
if (dateHistogram) { | ||
return dateHistogram; | ||
} | ||
} | ||
} else if (typeof aggs === 'object') { | ||
const aggNames = Object.keys(aggs); | ||
const aggsList = aggNames.map(aggName => aggs[aggName]); | ||
|
||
if (aggsList.includes('date_histogram')) { | ||
return aggs; | ||
} | ||
|
||
return findDateHistogram(aggsList); | ||
} | ||
} | ||
|
||
// TODO: Temporarily automatically assign same timezone and interval as what's defined by | ||
// the rollup job. This should be done by the visualization itself. | ||
const searchableAggs = JSON.parse(searchRequest.source.getField('index').originalBody.typeMeta); | ||
const { time_zone: timeZone, interval } = findDateHistogram(searchableAggs); | ||
|
||
Object.keys(aggs).forEach(aggName => { | ||
const subAggs = aggs[aggName]; | ||
|
||
Object.keys(subAggs).forEach(subAggName => { | ||
if (subAggName === 'date_histogram') { | ||
const subAgg = subAggs[subAggName]; | ||
subAgg.time_zone = timeZone; | ||
subAgg.interval = interval; | ||
} | ||
}); | ||
}); | ||
|
||
const index = indexPattern; | ||
const query = { | ||
'size': size, | ||
'aggregations': aggs, | ||
}; | ||
|
||
const { | ||
fetching, | ||
abort, | ||
} = kfetchAbortable({ | ||
pathname: '../api/rollup/search', | ||
method: 'POST', | ||
body: JSON.stringify({ index, query }), | ||
}); | ||
|
||
// TODO: Implement this. Search requests which can't be sent. | ||
const failedSearchRequests = []; | ||
|
||
return { | ||
// Munge data into shape expected by consumer. | ||
searching: new Promise((resolve, reject) => { | ||
fetching.then(result => { | ||
resolve([ result ]); | ||
}).catch(error => { | ||
const { | ||
body: { statusText, error: title, message }, | ||
res: { url }, | ||
} = error; | ||
|
||
// Format kfetch error as a SearchError. | ||
const searchError = new SearchError({ | ||
status: statusText, | ||
title, | ||
message, | ||
path: url, | ||
}); | ||
|
||
reject(searchError); | ||
}); | ||
}), | ||
abort, | ||
failedSearchRequests, | ||
}; | ||
}, | ||
|
||
isViable: (indexPattern) => { | ||
if (!indexPattern) { | ||
return false; | ||
} | ||
|
||
return indexPattern.type === 'rollup'; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { callWithRequestFactory } from '../../lib/call_with_request_factory'; | ||
import { isEsErrorFactory } from '../../lib/is_es_error_factory'; | ||
import { wrapEsError, wrapUnknownError } from '../../lib/error_wrappers'; | ||
|
||
export function registerSearchRoute(server) { | ||
const isEsError = isEsErrorFactory(server); | ||
|
||
server.route({ | ||
path: '/api/rollup/search', | ||
method: 'POST', | ||
handler: async (request, reply) => { | ||
const { index, query } = request.payload; | ||
const callWithRequest = callWithRequestFactory(server, request); | ||
|
||
try { | ||
const results = await callWithRequest('rollup.search', { | ||
index, | ||
body: query, | ||
}); | ||
|
||
reply(results); | ||
} catch(err) { | ||
if (isEsError(err)) { | ||
return reply(wrapEsError(err)); | ||
} | ||
|
||
reply(wrapUnknownError(err)); | ||
} | ||
}, | ||
}); | ||
} |