Skip to content

Commit

Permalink
Add request flyouts to JSON watch form and Threshold Watch edit form. (
Browse files Browse the repository at this point in the history
…elastic#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.
  • Loading branch information
cjcenizal authored Sep 25, 2019
1 parent bf00b3b commit 178d47c
Show file tree
Hide file tree
Showing 48 changed files with 1,486 additions and 1,434 deletions.
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

0 comments on commit 178d47c

Please sign in to comment.