Skip to content

Commit 178d47c

Browse files
authored
Add request flyouts to JSON watch form and Threshold Watch edit form. (#43232)
* Refactor watch serialization logic into common serialization functions. - Refactor build helpers to accept specific arguments instead of the entire watch object, to make dependencies more obvious. - Move action models into common directory. * Remove boom error reporting from action models because this is an unnecessary level of defensiveness since we control the UI that consumes this API. * Convert tests from Mocha to Jest. - Remove mocks and fix assertions that depended upon mocked dependencies. These assertions were low-value because they tested implementation details. - Remove other assertions based upon implementation details. * Remove serializeMonitoringWatch logic, since Monitoring doesn't use the create endpoint.
1 parent bf00b3b commit 178d47c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1486
-1434
lines changed

x-pack/legacy/plugins/watcher/common/constants/watch_types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
export const WATCH_TYPES: { [key: string]: string } = {
88
JSON: 'json',
9-
109
THRESHOLD: 'threshold',
11-
1210
MONITORING: 'monitoring',
1311
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
export declare function serializeJsonWatch(name: string, json: any): any;
8+
export declare function serializeThresholdWatch(config: any): any;
9+
export declare function buildInput(config: any): any;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
export { singleLineScript } from './single_line_script';
7+
export { serializeJsonWatch } from './serialize_json_watch';
8+
export { serializeThresholdWatch } from './serialize_threshold_watch';
9+
export { buildInput } from './serialization_helpers';

x-pack/legacy/plugins/watcher/server/models/watch/threshold_watch/build_actions.js renamed to x-pack/legacy/plugins/watcher/common/lib/serialization/serialization_helpers/build_actions.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
*/
66

77
import { forEach } from 'lodash';
8+
import { Action } from '../../../models/action';
89

910
/*
1011
watch.actions
1112
*/
12-
export function buildActions({ actions }) {
13+
export function buildActions(actions) {
1314
const result = {};
1415

1516
forEach(actions, (action) => {
16-
Object.assign(result, action.upstreamJson);
17+
const actionModel = Action.fromDownstreamJson(action);
18+
Object.assign(result, actionModel.upstreamJson);
1719
});
1820

1921
return result;

x-pack/legacy/plugins/watcher/server/models/watch/threshold_watch/build_condition.js renamed to x-pack/legacy/plugins/watcher/common/lib/serialization/serialization_helpers/build_condition.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { singleLineScript } from '../lib/single_line_script';
7+
import { singleLineScript } from './single_line_script';
88
import { COMPARATORS } from '../../../../common/constants';
99
const { BETWEEN } = COMPARATORS;
1010
/*
1111
watch.condition.script.inline
1212
*/
13-
function buildInline({ aggType, thresholdComparator, hasTermsAgg }) {
13+
function buildInline(aggType, thresholdComparator, hasTermsAgg) {
1414
let script = '';
1515

1616
if (aggType === 'count' && !hasTermsAgg) {
@@ -113,7 +113,7 @@ function buildInline({ aggType, thresholdComparator, hasTermsAgg }) {
113113
/*
114114
watch.condition.script.params
115115
*/
116-
function buildParams({ threshold }) {
116+
function buildParams(threshold) {
117117
return {
118118
threshold
119119
};
@@ -122,11 +122,11 @@ function buildParams({ threshold }) {
122122
/*
123123
watch.condition
124124
*/
125-
export function buildCondition(watch) {
125+
export function buildCondition({ aggType, thresholdComparator, hasTermsAgg, threshold }) {
126126
return {
127127
script: {
128-
source: buildInline(watch),
129-
params: buildParams(watch)
128+
source: buildInline(aggType, thresholdComparator, hasTermsAgg),
129+
params: buildParams(threshold)
130130
}
131131
};
132132
}

x-pack/legacy/plugins/watcher/server/models/watch/threshold_watch/build_input.js renamed to x-pack/legacy/plugins/watcher/common/lib/serialization/serialization_helpers/build_input.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { set } from 'lodash';
99
/*
1010
watch.input.search.request.indices
1111
*/
12-
function buildIndices({ index }) {
12+
function buildIndices(index) {
1313
if (Array.isArray(index)) {
1414
return index;
1515
}
@@ -22,7 +22,7 @@ function buildIndices({ index }) {
2222
/*
2323
watch.input.search.request.body.query.bool.filter.range
2424
*/
25-
function buildRange({ timeWindowSize, timeWindowUnit, timeField }) {
25+
function buildRange(timeWindowSize, timeWindowUnit, timeField) {
2626
return {
2727
[timeField]: {
2828
gte: `{{ctx.trigger.scheduled_time}}||-${timeWindowSize}${timeWindowUnit}`,
@@ -35,12 +35,12 @@ function buildRange({ timeWindowSize, timeWindowUnit, timeField }) {
3535
/*
3636
watch.input.search.request.body.query
3737
*/
38-
function buildQuery(watch) {
38+
function buildQuery(timeWindowSize, timeWindowUnit, timeField) {
3939
//TODO: This is where a saved search would be applied
4040
return {
4141
bool: {
4242
filter: {
43-
range: buildRange(watch)
43+
range: buildRange(timeWindowSize, timeWindowUnit, timeField)
4444
}
4545
}
4646
};
@@ -49,7 +49,7 @@ function buildQuery(watch) {
4949
/*
5050
watch.input.search.request.body.aggs
5151
*/
52-
function buildAggs({ aggType, aggField, termField, termSize, termOrder }) {
52+
function buildAggs(aggType, aggField, termField, termSize, termOrder) {
5353
if (aggType === 'count' && !termField) {
5454
return null;
5555
}
@@ -107,13 +107,13 @@ function buildAggs({ aggType, aggField, termField, termSize, termOrder }) {
107107
/*
108108
watch.input.search.request.body
109109
*/
110-
function buildBody(watch) {
110+
function buildBody(timeWindowSize, timeWindowUnit, timeField, aggType, aggField, termField, termSize, termOrder) {
111111
const result = {
112112
size: 0,
113-
query: buildQuery(watch)
113+
query: buildQuery(timeWindowSize, timeWindowUnit, timeField)
114114
};
115115

116-
const aggs = buildAggs(watch);
116+
const aggs = buildAggs(aggType, aggField, termField, termSize, termOrder);
117117
if (Boolean(aggs)) {
118118
result.aggs = aggs;
119119
}
@@ -124,12 +124,12 @@ function buildBody(watch) {
124124
/*
125125
watch.input
126126
*/
127-
export function buildInput(watch) {
127+
export function buildInput({ index, timeWindowSize, timeWindowUnit, timeField, aggType, aggField, termField, termSize, termOrder }) {
128128
return {
129129
search: {
130130
request: {
131-
body: buildBody(watch),
132-
indices: buildIndices(watch)
131+
body: buildBody(timeWindowSize, timeWindowUnit, timeField, aggType, aggField, termField, termSize, termOrder),
132+
indices: buildIndices(index)
133133
}
134134
}
135135
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
/*
8+
watch.metadata
9+
*/
10+
11+
export function buildMetadata({
12+
index,
13+
timeField,
14+
triggerIntervalSize,
15+
triggerIntervalUnit,
16+
aggType,
17+
aggField,
18+
termSize,
19+
termField,
20+
thresholdComparator,
21+
timeWindowSize,
22+
timeWindowUnit,
23+
threshold,
24+
}) {
25+
return {
26+
watcherui: {
27+
index,
28+
time_field: timeField,
29+
trigger_interval_size: triggerIntervalSize,
30+
trigger_interval_unit: triggerIntervalUnit,
31+
agg_type: aggType,
32+
agg_field: aggField,
33+
term_size: termSize,
34+
term_field: termField,
35+
threshold_comparator: thresholdComparator,
36+
time_window_size: timeWindowSize,
37+
time_window_unit: timeWindowUnit,
38+
threshold,
39+
}
40+
};
41+
}

x-pack/legacy/plugins/watcher/server/models/watch/threshold_watch/build_transform.js renamed to x-pack/legacy/plugins/watcher/common/lib/serialization/serialization_helpers/build_transform.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { singleLineScript } from '../lib/single_line_script';
7+
import { singleLineScript } from './single_line_script';
88
import { COMPARATORS } from '../../../../common/constants';
99
const { BETWEEN } = COMPARATORS;
1010
/*
1111
watch.transform.script.inline
1212
*/
13-
function buildInline({ aggType, hasTermsAgg, thresholdComparator }) {
13+
function buildInline(aggType, thresholdComparator, hasTermsAgg) {
1414
let script = '';
1515

1616
if (aggType === 'count' && !hasTermsAgg) {
@@ -119,7 +119,7 @@ function buildInline({ aggType, hasTermsAgg, thresholdComparator }) {
119119
/*
120120
watch.transform.script.params
121121
*/
122-
function buildParams({ threshold }) {
122+
function buildParams(threshold) {
123123
return {
124124
threshold
125125
};
@@ -128,11 +128,11 @@ function buildParams({ threshold }) {
128128
/*
129129
watch.transform
130130
*/
131-
export function buildTransform(watch) {
131+
export function buildTransform({ aggType, thresholdComparator, hasTermsAgg, threshold }) {
132132
return {
133133
script: {
134-
source: buildInline(watch),
135-
params: buildParams(watch)
134+
source: buildInline(aggType, thresholdComparator, hasTermsAgg),
135+
params: buildParams(threshold)
136136
}
137137
};
138138
}

x-pack/legacy/plugins/watcher/server/models/watch/threshold_watch/build_trigger.js renamed to x-pack/legacy/plugins/watcher/common/lib/serialization/serialization_helpers/build_trigger.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77
/*
88
watch.trigger.schedule
99
*/
10-
function buildSchedule({ triggerIntervalSize, triggerIntervalUnit }) {
10+
export function buildTrigger(triggerIntervalSize, triggerIntervalUnit) {
1111
return {
12-
interval: `${triggerIntervalSize}${triggerIntervalUnit}`
13-
};
14-
}
15-
16-
export function buildTrigger(watch) {
17-
return {
18-
schedule: buildSchedule(watch)
12+
schedule: {
13+
interval: `${triggerIntervalSize}${triggerIntervalUnit}`
14+
},
1915
};
2016
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
export { buildActions } from './build_actions';
8+
export { buildCondition } from './build_condition';
9+
export { buildInput } from './build_input';
10+
export { buildMetadata } from './build_metadata';
11+
export { buildTransform } from './build_transform';
12+
export { buildTrigger } from './build_trigger';

0 commit comments

Comments
 (0)