kibana/x-pack/plugins/monitoring/server/lib/create_query.js
Chris Roberson 97e5be671a
[6.x] [Monitoring] Support for unlinked deployments (#28278) (#29340)
* [Monitoring] Support for unlinked deployments (#28278)

* Unlinked deployment working for beats

* Use better constant

* Show N/A for license

* Rename to Unlinked Cluster

* Use callout to mention unlinked cluster

* PR feedback

* Use fragment

* Speed up the query by using terminate_after

* Handle failures more defensively

* Remove unnecessary msearch

* PR feedback

* PR feedback and a bit of light refactor

* Updated text

* Add api integration tests

* Localize call out

* Update loc pattern

* Fix improper i18n.translate usage

* Revert "Fix improper i18n.translate usage"

This reverts commit 0e2e7608c3.

* Revert "Update loc pattern"

This reverts commit cc99fe8a8a.

* Ensure the unlinked deployment cluster counts as a valid cluster

* Sometimes, you miss the smallest things

* Ensure the unlinked cluster is supported, in that users can click the link and load it

* Update tests

* PR feedback. Simplifying the flag supported code and adding more tests

* Update naming

* Rename to Standalone Cluster

* Remove unnecessary file

* Move logic for setting isSupported to exclusively in flag supported clusters code, update tests

* Fix up the data for these tests
2019-01-28 21:12:14 -05:00

107 lines
3.5 KiB
JavaScript

/*
* 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 { defaults, get } from 'lodash';
import { MissingRequiredError } from './error_missing_required';
import moment from 'moment';
import { standaloneClusterFilter } from './standalone_clusters';
import { STANDALONE_CLUSTER_CLUSTER_UUID } from '../../common/constants';
/*
* Builds a type filter syntax that supports backwards compatibility to read
* from indices before and after `_type` is removed from Elasticsearch
*
* TODO: this backwards compatibility helper will only be supported for 5.x-6. This
* function should be removed in 7.0
*/
export const createTypeFilter = (type) => {
return {
bool: {
should: [
{ term: { _type: type } },
{ term: { type } }
]
}
};
};
/*
* Creates the boilerplace for querying monitoring data, including filling in
* document UUIDs, start time and end time, and injecting additional filters.
*
* Create a bool for types that will query for documents using `_type` (true type) and `type` (as a field)
* Makes backwards compatibility for types being deprecated in Elasticsearch
*
* Options object:
* @param {String} options.type - `type` field value of the documents
* @param {Array} options.filters - additional filters to add to the `bool` section of the query. Default: []
* @param {string} options.clusterUuid - a UUID of the cluster. Required.
* @param {string} options.uuid - a UUID of the metric to filter for, or `null` if UUID should not be part of the query
* @param {Date} options.start - numeric timestamp (optional)
* @param {Date} options.end - numeric timestamp (optional)
* @param {Metric} options.metric - Metric instance or metric fields object @see ElasticsearchMetric.getMetricFields
*/
export function createQuery(options) {
options = defaults(options, { filters: [] });
const { type, clusterUuid, uuid, start, end, filters } = options;
const isFromStandaloneCluster = clusterUuid === STANDALONE_CLUSTER_CLUSTER_UUID;
let typeFilter;
if (type) {
typeFilter = createTypeFilter(type);
}
let clusterUuidFilter;
if (clusterUuid && !isFromStandaloneCluster) {
clusterUuidFilter = { term: { 'cluster_uuid': clusterUuid } };
}
let uuidFilter;
// options.uuid can be null, for example getting all the clusters
if (uuid) {
const uuidField = get(options, 'metric.uuidField');
if (!uuidField) {
throw new MissingRequiredError('options.uuid given but options.metric.uuidField is false');
}
uuidFilter = { term: { [uuidField]: uuid } };
}
const timestampField = get(options, 'metric.timestampField');
if (!timestampField) {
throw new MissingRequiredError('metric.timestampField');
}
const timeRangeFilter = {
range: {
[timestampField]: {
format: 'epoch_millis'
}
}
};
if (start) {
timeRangeFilter.range[timestampField].gte = moment.utc(start).valueOf();
}
if (end) {
timeRangeFilter.range[timestampField].lte = moment.utc(end).valueOf();
}
const combinedFilters = [typeFilter, clusterUuidFilter, uuidFilter, ...filters];
if (end || start) {
combinedFilters.push(timeRangeFilter);
}
if (isFromStandaloneCluster) {
combinedFilters.push(standaloneClusterFilter);
}
const query = {
bool: {
filter: combinedFilters.filter(Boolean)
}
};
return query;
}