-
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
16 changed files
with
289 additions
and
19 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
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,34 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export class SearchError extends Error { | ||
constructor({ status, title, message, path }) { | ||
super(message); | ||
this.name = 'SearchError'; | ||
this.status = status; | ||
this.title = title; | ||
this.message = message; | ||
this.path = path; | ||
Error.captureStackTrace(this, SearchError); | ||
|
||
// Babel doesn't support traditional `extends` syntax for built-in classes. | ||
// https://babeljs.io/docs/en/caveats/#classes | ||
Object.setPrototypeOf(this, SearchError.prototype); | ||
} | ||
} |
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
Oops, something went wrong.