Skip to content

Commit

Permalink
[Synthetics] Fixes monitors search by location/hosts (#146529)
Browse files Browse the repository at this point in the history
Fixes #146077
Fixes #146078
  • Loading branch information
shahzad31 authored Dec 1, 2022
1 parent 5e65ebb commit 6f7c6ad
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"siem-ui-timeline-pinned-event": "e2697b38751506c7fce6e8b7207a830483dc4283",
"space": "c4a0acce1bd4b9cce85154f2a350624a53111c59",
"spaces-usage-stats": "922d3235bbf519e3fb3b260e27248b1df8249b79",
"synthetics-monitor": "d784b64a3def47d3f3d1f367df71ae41ef33cb3c",
"synthetics-monitor": "7c1e5a78fb3b88cc03b441d3bf3714d9967ab214",
"synthetics-privates-locations": "dd00385f4a27ef062c3e57312eeb3799872fa4af",
"tag": "39413f4578cc2128c9a0fda97d0acd1c8862c47a",
"task": "ef53d0f070bd54957b8fe22fae3b1ff208913f76",
Expand Down
17 changes: 17 additions & 0 deletions x-pack/plugins/synthetics/common/utils/location_formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { PrivateLocation, ServiceLocation } from '../runtime_types';

export const formatLocation = (location: ServiceLocation | PrivateLocation) => {
return {
id: location.id,
label: location.label,
geo: location.geo,
isServiceManaged: location.isServiceManaged,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
EuiLink,
EuiTextArea,
} from '@elastic/eui';
import { formatLocation } from '../../../../../../common/utils/location_formatter';
import { getDocLinks } from '../../../../../kibana_services';
import { useMonitorName } from '../hooks/use_monitor_name';
import { MonitorTypeRadioGroup } from '../fields/monitor_type_radio_group';
Expand Down Expand Up @@ -407,10 +408,7 @@ export const FIELD: Record<string, FieldMeta> = {
onChange: (updatedValues: ServiceLocations) => {
setValue(
ConfigKey.LOCATIONS,
updatedValues.map((location) => ({
id: location.id,
isServiceManaged: location.isServiceManaged,
})) as MonitorServiceLocations,
updatedValues.map((location) => formatLocation(location)) as MonitorServiceLocations,
{ shouldValidate: Boolean(formState.submitCount > 0) }
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { i18n } from '@kbn/i18n';
import { EuiCheckboxGroup, EuiFormRow, EuiText, EuiBadge, EuiIconTip } from '@elastic/eui';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { useRouteMatch } from 'react-router-dom';
import { formatLocation } from '../../../../../common/utils/location_formatter';
import { monitorManagementListSelector } from '../../../state/selectors';
import { MonitorServiceLocations, LocationStatus } from '../../../../../common/runtime_types';
import { ClientPluginsStart } from '../../../../plugin';
Expand Down Expand Up @@ -44,9 +45,7 @@ export const ServiceLocations = ({
const location = locations.find((loc) => loc.id === optionId);
if (isSelected) {
setLocations((prevLocations) =>
location
? [...prevLocations, { id: location.id, isServiceManaged: location.isServiceManaged }]
: prevLocations
location ? [...prevLocations, formatLocation(location)] : prevLocations
);
} else {
setLocations((prevLocations) => [...prevLocations].filter((loc) => loc.id !== optionId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export const getSyntheticsMonitorSavedObjectType = (
},
},
},
label: {
type: 'text',
},
},
},
custom_heartbeat_id: {
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/synthetics/server/routes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const SEARCH_FIELDS = [
'name',
'tags.text',
'locations.id.text',
'locations.label',
'urls',
'hosts',
'project_id.text',
Expand Down Expand Up @@ -74,7 +75,7 @@ export const getMonitors = (
page,
sortField: sortField === 'schedule.keyword' ? 'schedule.number' : sortField,
sortOrder,
searchFields: ['name', 'tags.text', 'locations.id.text', 'urls', 'project_id.text'],
searchFields: SEARCH_FIELDS,
search: query ? `${query}*` : undefined,
filter: filterStr,
fields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,30 @@ export const getAllSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = () =>
query: QuerySchema,
},
handler: async ({ request, savedObjectsClient, syntheticsMonitorClient }): Promise<any> => {
const queryResult = await getMonitors(
request.query,
syntheticsMonitorClient.syntheticsService,
savedObjectsClient
);

const countResult = isMonitorsQueryFiltered(request.query)
? await savedObjectsClient.find({
const totalCountQuery = async () => {
if (isMonitorsQueryFiltered(request.query)) {
return savedObjectsClient.find({
type: syntheticsMonitorType,
perPage: 0,
page: 1,
})
: queryResult;
});
}
};

const [queryResult, totalCount] = await Promise.all([
getMonitors(request.query, syntheticsMonitorClient.syntheticsService, savedObjectsClient),
totalCountQuery(),
]);

const absoluteTotal = totalCount?.total ?? queryResult.total;

const { saved_objects: monitors, per_page: perPageT, ...rest } = queryResult;

return {
...rest,
monitors,
absoluteTotal,
perPage: perPageT,
absoluteTotal: countResult.total,
syncErrors: syntheticsMonitorClient.syntheticsService.syncErrors,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ describe('browser normalizers', () => {
id: 'us_central',
isServiceManaged: true,
label: 'Test Location',
url: 'test-url',
status: 'ga',
},
],
name: 'test-name-1',
Expand Down Expand Up @@ -182,8 +180,6 @@ describe('browser normalizers', () => {
id: 'us_central',
isServiceManaged: true,
label: 'Test Location',
url: 'test-url',
status: 'ga',
},
{
geo: {
Expand All @@ -193,8 +189,6 @@ describe('browser normalizers', () => {
id: 'us_east',
isServiceManaged: true,
label: 'Test Location',
url: 'test-url',
status: 'ga',
},
],
name: 'test-name-2',
Expand Down Expand Up @@ -240,8 +234,6 @@ describe('browser normalizers', () => {
id: 'us_central',
isServiceManaged: true,
label: 'Test Location',
url: 'test-url',
status: 'ga',
},
{
geo: {
Expand All @@ -251,15 +243,11 @@ describe('browser normalizers', () => {
id: 'us_east',
isServiceManaged: true,
label: 'Test Location',
url: 'test-url',
status: 'ga',
},
{
id: 'germany',
isServiceManaged: false,
label: 'Germany',
agentPolicyId: 'germany',
concurrentMonitors: 1,
},
],
name: 'test-name-3',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { omit } from 'lodash';
import { formatLocation } from '../../../../common/utils/location_formatter';
import { formatKibanaNamespace } from '../../../../common/formatters';
import {
BrowserFields,
Expand Down Expand Up @@ -108,9 +109,9 @@ export const getMonitorLocations = ({
);
}) || [];

return [...publicLocs, ...privateLocs].filter(
(location) => location !== undefined
) as BrowserFields[ConfigKey.LOCATIONS];
return [...publicLocs, ...privateLocs]
.filter((location) => location !== undefined)
.map((loc) => formatLocation(loc!)) as BrowserFields[ConfigKey.LOCATIONS];
};

export const getUnsupportedKeysError = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ describe('icmp normalizers', () => {
id: 'us_central',
isServiceManaged: true,
label: 'Test Location',
status: 'ga',
url: 'test-url',
},
],
name: 'Cloudflare DNS',
Expand Down Expand Up @@ -162,8 +160,6 @@ describe('icmp normalizers', () => {
id: 'us_central',
isServiceManaged: true,
label: 'Test Location',
status: 'ga',
url: 'test-url',
},
],
name: 'Cloudflare DNS 2',
Expand Down Expand Up @@ -217,8 +213,6 @@ describe('icmp normalizers', () => {
id: 'us_central',
isServiceManaged: true,
label: 'Test Location',
status: 'ga',
url: 'test-url',
},
],
name: 'Cloudflare DNS 3',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ describe('tcp normalizers', () => {
id: 'us_central',
isServiceManaged: true,
label: 'Test Location',
status: 'ga',
url: 'test-url',
},
],
name: 'GMail SMTP',
Expand Down Expand Up @@ -184,8 +182,6 @@ describe('tcp normalizers', () => {
id: 'us_central',
isServiceManaged: true,
label: 'Test Location',
status: 'ga',
url: 'test-url',
},
],
name: 'Always Down',
Expand Down Expand Up @@ -253,8 +249,6 @@ describe('tcp normalizers', () => {
id: 'us_central',
isServiceManaged: true,
label: 'Test Location',
status: 'ga',
url: 'test-url',
},
],
name: 'Always Down',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { httpServerMock } from '@kbn/core-http-server-mocks';
import { formatSecrets } from '../utils';

import * as telemetryHooks from '../../routes/telemetry/monitor_upgrade_sender';
import { formatLocation } from '../../../common/utils/location_formatter';

const testMonitors = [
{
Expand Down Expand Up @@ -453,7 +454,7 @@ const payloadData = [
form_monitor_type: 'multistep',
ignore_https_errors: false,
journey_id: 'check if title is present 10 0',
locations: privateLocations,
locations: privateLocations.map((l) => formatLocation(l)),
name: 'check if title is present 10 0',
namespace: 'default_space',
origin: 'project',
Expand Down Expand Up @@ -515,7 +516,7 @@ const payloadData = [
form_monitor_type: 'multistep',
ignore_https_errors: false,
journey_id: 'check if title is present 10 1',
locations: privateLocations,
locations: privateLocations.map((l) => formatLocation(l)),
name: 'check if title is present 10 1',
namespace: 'default_space',
origin: 'project',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Subject } from 'rxjs';
import { formatSecrets } from '../utils';

import * as telemetryHooks from '../../routes/telemetry/monitor_upgrade_sender';
import { formatLocation } from '../../../common/utils/location_formatter';

const testMonitors = [
{
Expand Down Expand Up @@ -525,7 +526,7 @@ const payloadData = [
form_monitor_type: 'multistep',
ignore_https_errors: false,
journey_id: 'check if title is present 10 0',
locations: privateLocations,
locations: privateLocations.map((l) => formatLocation(l)),
name: 'check if title is present 10 0',
namespace: 'default_space',
origin: 'project',
Expand Down Expand Up @@ -587,7 +588,7 @@ const payloadData = [
form_monitor_type: 'multistep',
ignore_https_errors: false,
journey_id: 'check if title is present 10 1',
locations: privateLocations,
locations: privateLocations.map((l) => formatLocation(l)),
name: 'check if title is present 10 1',
namespace: 'default_space',
origin: 'project',
Expand Down
21 changes: 0 additions & 21 deletions x-pack/test/api_integration/apis/synthetics/add_monitor_project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,8 @@ export default function ({ getService }: FtrProviderContext) {
lon: 0,
},
id: 'localhost',
isInvalid: false,
isServiceManaged: true,
label: 'Local Synthetics Service',
status: 'experimental',
url: 'mockDevUrl',
},
],
name: 'check if title is present',
Expand Down Expand Up @@ -282,11 +279,8 @@ export default function ({ getService }: FtrProviderContext) {
lon: 0,
},
id: 'localhost',
isInvalid: false,
isServiceManaged: true,
label: 'Local Synthetics Service',
status: 'experimental',
url: 'mockDevUrl',
},
],
max_redirects: '0',
Expand Down Expand Up @@ -390,11 +384,8 @@ export default function ({ getService }: FtrProviderContext) {
lon: 0,
},
id: 'localhost',
isInvalid: false,
isServiceManaged: true,
label: 'Local Synthetics Service',
status: 'experimental',
url: 'mockDevUrl',
},
],
name: monitor.name,
Expand Down Expand Up @@ -489,21 +480,15 @@ export default function ({ getService }: FtrProviderContext) {
lon: 0,
},
id: 'localhost',
isInvalid: false,
isServiceManaged: true,
label: 'Local Synthetics Service',
status: 'experimental',
url: 'mockDevUrl',
},
{
agentPolicyId: testPolicyId,
concurrentMonitors: 1,
geo: {
lat: '',
lon: '',
},
id: testPolicyId,
isInvalid: false,
isServiceManaged: false,
label: 'Test private location 0',
},
Expand Down Expand Up @@ -1317,22 +1302,16 @@ export default function ({ getService }: FtrProviderContext) {
id: 'localhost',
label: 'Local Synthetics Service',
geo: { lat: 0, lon: 0 },
url: 'mockDevUrl',
isServiceManaged: true,
status: 'experimental',
isInvalid: false,
},
{
label: 'Test private location 0',
isServiceManaged: false,
isInvalid: false,
agentPolicyId: testPolicyId,
id: testPolicyId,
geo: {
lat: '',
lon: '',
},
concurrentMonitors: 1,
},
]);
});
Expand Down
Loading

0 comments on commit 6f7c6ad

Please sign in to comment.