Skip to content

Commit

Permalink
[ML] add time range capping for fields_service.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Mar 31, 2020
1 parent f5a00e4 commit f3b1e7b
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions x-pack/plugins/ml/server/models/fields_service/fields_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import Boom from 'boom';
import { APICaller } from 'kibana/server';
import { duration } from 'moment';
import { parseInterval } from '../../../common/util/parse_interval';
import { initCardinalityFieldsCache } from './fields_aggs_cache';

Expand All @@ -16,6 +17,19 @@ import { initCardinalityFieldsCache } from './fields_aggs_cache';
export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
const fieldsAggsCache = initCardinalityFieldsCache();

/**
* Caps the time range to the last 90 days if necessary
*/
function getSafeTimeRange(earliestMs: number, latestMs: number): { start: number; end: number } {
const capOffsetMs = duration(3, 'months').asMilliseconds();
const capRangeStart = latestMs - capOffsetMs;

return {
start: Math.max(earliestMs, capRangeStart),
end: latestMs,
};
}

/**
* Gets aggregatable fields.
*/
Expand Down Expand Up @@ -61,12 +75,14 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
return {};
}

const { start, end } = getSafeTimeRange(earliestMs, latestMs);

const cachedValues =
fieldsAggsCache.getValues(
index,
timeFieldName,
earliestMs,
latestMs,
start,
end,
'overallCardinality',
fieldNames
) ?? {};
Expand All @@ -84,8 +100,8 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
{
range: {
[timeFieldName]: {
gte: earliestMs,
lte: latestMs,
gte: start,
lte: end,
format: 'epoch_millis',
},
},
Expand Down Expand Up @@ -130,7 +146,7 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
return obj;
}, {} as { [field: string]: number });

fieldsAggsCache.updateValues(index, timeFieldName, earliestMs, latestMs, {
fieldsAggsCache.updateValues(index, timeFieldName, start, end, {
overallCardinality: aggResult,
});

Expand Down Expand Up @@ -185,15 +201,16 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
}

/**
* Caps provided time boundaries based on the interval.
* @param earliestMs
* @param latestMs
* @param interval
* Caps provided time boundaries based on the interval
*/
function getSafeTimeRange(
function getSafeTimeRangeForInterval(
interval: string,
...timeRange: number[]
): { start: number; end: number };
function getSafeTimeRangeForInterval(
interval: string,
earliestMs: number,
latestMs: number,
interval: string
latestMs: number
): { start: number; end: number } {
const maxNumberOfBuckets = 1000;
const end = latestMs;
Expand Down Expand Up @@ -234,7 +251,7 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
interval: string | undefined
): Promise<{ [key: string]: number }> {
if (!interval) {
throw new Error('Interval is required to retrieve max bucket cardinalities.');
throw Boom.badRequest('Interval is required to retrieve max bucket cardinalities.');
}

const aggregatableFields = await getAggregatableFields(index, fieldNames);
Expand All @@ -243,12 +260,17 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
return {};
}

const { start, end } = getSafeTimeRangeForInterval(
interval,
...Object.values(getSafeTimeRange(earliestMs, latestMs))
);

const cachedValues =
fieldsAggsCache.getValues(
index,
timeFieldName,
earliestMs,
latestMs,
start,
end,
'maxBucketCardinality',
fieldNames
) ?? {};
Expand All @@ -260,8 +282,6 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
return cachedValues;
}

const { start, end } = getSafeTimeRange(earliestMs, latestMs, interval);

const mustCriteria = [
{
range: {
Expand Down Expand Up @@ -334,6 +354,10 @@ export function fieldsServiceProvider(callAsCurrentUser: APICaller) {
return obj;
}, {} as { [field: string]: number });

fieldsAggsCache.updateValues(index, timeFieldName, start, end, {
maxBucketCardinality: aggResult,
});

return {
...cachedValues,
...aggResult,
Expand Down

0 comments on commit f3b1e7b

Please sign in to comment.