Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/watcher/common/constants/watch_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

export const WATCH_TYPES: { [key: string]: string } = {
JSON: 'json',

THRESHOLD: 'threshold',

MONITORING: 'monitoring',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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.
*/

export declare function serializeJsonWatch(name: string, json: any): any;
export declare function serializeThresholdWatch(config: any): any;
export declare function buildInput(config: any): any;
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { singleLineScript } from './single_line_script';
export { serializeJsonWatch } from './serialize_json_watch';
export { serializeThresholdWatch } from './serialize_threshold_watch';
export { buildInput } from './serialization_helpers';
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
*/

import { forEach } from 'lodash';
import { Action } from '../../../models/action';

/*
watch.actions
*/
export function buildActions({ actions }) {
export function buildActions(actions) {
const result = {};

forEach(actions, (action) => {
Object.assign(result, action.upstreamJson);
const actionModel = Action.fromDownstreamJson(action);
Object.assign(result, actionModel.upstreamJson);
});

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { singleLineScript } from '../lib/single_line_script';
import { singleLineScript } from './single_line_script';
import { COMPARATORS } from '../../../../common/constants';
const { BETWEEN } = COMPARATORS;
/*
watch.condition.script.inline
*/
function buildInline({ aggType, thresholdComparator, hasTermsAgg }) {
function buildInline(aggType, thresholdComparator, hasTermsAgg) {
let script = '';

if (aggType === 'count' && !hasTermsAgg) {
Expand Down Expand Up @@ -113,7 +113,7 @@ function buildInline({ aggType, thresholdComparator, hasTermsAgg }) {
/*
watch.condition.script.params
*/
function buildParams({ threshold }) {
function buildParams(threshold) {
return {
threshold
};
Expand All @@ -122,11 +122,11 @@ function buildParams({ threshold }) {
/*
watch.condition
*/
export function buildCondition(watch) {
export function buildCondition({ aggType, thresholdComparator, hasTermsAgg, threshold }) {
return {
script: {
source: buildInline(watch),
params: buildParams(watch)
source: buildInline(aggType, thresholdComparator, hasTermsAgg),
params: buildParams(threshold)
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { set } from 'lodash';
/*
watch.input.search.request.indices
*/
function buildIndices({ index }) {
function buildIndices(index) {
if (Array.isArray(index)) {
return index;
}
Expand All @@ -22,7 +22,7 @@ function buildIndices({ index }) {
/*
watch.input.search.request.body.query.bool.filter.range
*/
function buildRange({ timeWindowSize, timeWindowUnit, timeField }) {
function buildRange(timeWindowSize, timeWindowUnit, timeField) {
return {
[timeField]: {
gte: `{{ctx.trigger.scheduled_time}}||-${timeWindowSize}${timeWindowUnit}`,
Expand All @@ -35,12 +35,12 @@ function buildRange({ timeWindowSize, timeWindowUnit, timeField }) {
/*
watch.input.search.request.body.query
*/
function buildQuery(watch) {
function buildQuery(timeWindowSize, timeWindowUnit, timeField) {
//TODO: This is where a saved search would be applied
return {
bool: {
filter: {
range: buildRange(watch)
range: buildRange(timeWindowSize, timeWindowUnit, timeField)
}
}
};
Expand All @@ -49,7 +49,7 @@ function buildQuery(watch) {
/*
watch.input.search.request.body.aggs
*/
function buildAggs({ aggType, aggField, termField, termSize, termOrder }) {
function buildAggs(aggType, aggField, termField, termSize, termOrder) {
if (aggType === 'count' && !termField) {
return null;
}
Expand Down Expand Up @@ -107,13 +107,13 @@ function buildAggs({ aggType, aggField, termField, termSize, termOrder }) {
/*
watch.input.search.request.body
*/
function buildBody(watch) {
function buildBody(timeWindowSize, timeWindowUnit, timeField, aggType, aggField, termField, termSize, termOrder) {
const result = {
size: 0,
query: buildQuery(watch)
query: buildQuery(timeWindowSize, timeWindowUnit, timeField)
};

const aggs = buildAggs(watch);
const aggs = buildAggs(aggType, aggField, termField, termSize, termOrder);
if (Boolean(aggs)) {
result.aggs = aggs;
}
Expand All @@ -124,12 +124,12 @@ function buildBody(watch) {
/*
watch.input
*/
export function buildInput(watch) {
export function buildInput({ index, timeWindowSize, timeWindowUnit, timeField, aggType, aggField, termField, termSize, termOrder }) {
return {
search: {
request: {
body: buildBody(watch),
indices: buildIndices(watch)
body: buildBody(timeWindowSize, timeWindowUnit, timeField, aggType, aggField, termField, termSize, termOrder),
indices: buildIndices(index)
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.
*/

/*
watch.metadata
*/

export function buildMetadata({
index,
timeField,
triggerIntervalSize,
triggerIntervalUnit,
aggType,
aggField,
termSize,
termField,
thresholdComparator,
timeWindowSize,
timeWindowUnit,
threshold,
}) {
return {
watcherui: {
index,
time_field: timeField,
trigger_interval_size: triggerIntervalSize,
trigger_interval_unit: triggerIntervalUnit,
agg_type: aggType,
agg_field: aggField,
term_size: termSize,
term_field: termField,
threshold_comparator: thresholdComparator,
time_window_size: timeWindowSize,
time_window_unit: timeWindowUnit,
threshold,
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { singleLineScript } from '../lib/single_line_script';
import { singleLineScript } from './single_line_script';
import { COMPARATORS } from '../../../../common/constants';
const { BETWEEN } = COMPARATORS;
/*
watch.transform.script.inline
*/
function buildInline({ aggType, hasTermsAgg, thresholdComparator }) {
function buildInline(aggType, thresholdComparator, hasTermsAgg) {
let script = '';

if (aggType === 'count' && !hasTermsAgg) {
Expand Down Expand Up @@ -119,7 +119,7 @@ function buildInline({ aggType, hasTermsAgg, thresholdComparator }) {
/*
watch.transform.script.params
*/
function buildParams({ threshold }) {
function buildParams(threshold) {
return {
threshold
};
Expand All @@ -128,11 +128,11 @@ function buildParams({ threshold }) {
/*
watch.transform
*/
export function buildTransform(watch) {
export function buildTransform({ aggType, thresholdComparator, hasTermsAgg, threshold }) {
return {
script: {
source: buildInline(watch),
params: buildParams(watch)
source: buildInline(aggType, thresholdComparator, hasTermsAgg),
params: buildParams(threshold)
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
/*
watch.trigger.schedule
*/
function buildSchedule({ triggerIntervalSize, triggerIntervalUnit }) {
export function buildTrigger(triggerIntervalSize, triggerIntervalUnit) {
return {
interval: `${triggerIntervalSize}${triggerIntervalUnit}`
};
}

export function buildTrigger(watch) {
return {
schedule: buildSchedule(watch)
schedule: {
interval: `${triggerIntervalSize}${triggerIntervalUnit}`
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.
*/

export { buildActions } from './build_actions';
export { buildCondition } from './build_condition';
export { buildInput } from './build_input';
export { buildMetadata } from './build_metadata';
export { buildTransform } from './build_transform';
export { buildTrigger } from './build_trigger';
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 { set } from 'lodash';
import { WATCH_TYPES } from '../../constants';

export function serializeJsonWatch(name, json) {
// We don't want to overwrite any metadata provided by the consumer.
const { metadata = {} } = json;
set(metadata, 'xpack.type', WATCH_TYPES.JSON);

const serializedWatch = {
...json,
metadata,
};

if (name) {
serializedWatch.metadata.name = name;
}

return serializedWatch;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 { serializeJsonWatch } from './serialize_json_watch';

describe('serializeJsonWatch', () => {
it('serializes with name', () => {
expect(serializeJsonWatch('test', { foo: 'bar' })).toEqual({
foo: 'bar',
metadata: {
name: 'test',
xpack: {
type: 'json',
},
},
});
});

it('serializes without name', () => {
expect(serializeJsonWatch(undefined, { foo: 'bar' })).toEqual({
foo: 'bar',
metadata: {
xpack: {
type: 'json',
},
},
});
});

it('respects provided metadata', () => {
expect(serializeJsonWatch(undefined, { metadata: { foo: 'bar' } })).toEqual({
metadata: {
foo: 'bar',
xpack: {
type: 'json',
},
},
});
});
});
Loading