Skip to content

Commit

Permalink
[Synthetics] Remove fields from API query interface (#158449)
Browse files Browse the repository at this point in the history
## Summary

Remove fields from API query interface !!

Fixes #153472
  • Loading branch information
shahzad31 authored May 25, 2023
1 parent c783e6a commit 97a93b3
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ export type UMRouteHandler = ({
subject,
}: UptimeRouteContext) => IKibanaResponse<any> | Promise<IKibanaResponse<any>>;

export interface RouteContext {
export interface RouteContext<Query = Record<string, any>> {
uptimeEsClient: UptimeEsClient;
context: UptimeRequestHandlerContext;
request: SyntheticsRequest;
request: KibanaRequest<Record<string, any>, Query, Record<string, any>>;
response: KibanaResponseFactory;
savedObjectsClient: SavedObjectsClientContract;
server: UptimeServerSetup;
Expand Down
37 changes: 20 additions & 17 deletions x-pack/plugins/synthetics/server/routes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ import { EncryptedSyntheticsMonitor, ServiceLocations } from '../../common/runti
import { monitorAttributes, syntheticsMonitorType } from '../../common/types/saved_objects';
import { RouteContext } from '../legacy_uptime/routes';

const StringOrArraySchema = schema.maybe(
schema.oneOf([schema.string(), schema.arrayOf(schema.string())])
);

export const QuerySchema = schema.object({
page: schema.maybe(schema.number()),
perPage: schema.maybe(schema.number()),
sortField: schema.maybe(schema.string()),
sortOrder: schema.maybe(schema.oneOf([schema.literal('desc'), schema.literal('asc')])),
query: schema.maybe(schema.string()),
filter: schema.maybe(schema.string()),
tags: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
monitorTypes: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
locations: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
projects: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
schedules: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
status: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
fields: schema.maybe(schema.arrayOf(schema.string())),
tags: StringOrArraySchema,
monitorTypes: StringOrArraySchema,
locations: StringOrArraySchema,
projects: StringOrArraySchema,
schedules: StringOrArraySchema,
status: StringOrArraySchema,
searchAfter: schema.maybe(schema.arrayOf(schema.string())),
});

Expand All @@ -34,12 +37,12 @@ export type MonitorsQuery = TypeOf<typeof QuerySchema>;
export const OverviewStatusSchema = schema.object({
query: schema.maybe(schema.string()),
filter: schema.maybe(schema.string()),
tags: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
monitorTypes: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
locations: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
projects: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
schedules: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
status: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
tags: StringOrArraySchema,
monitorTypes: StringOrArraySchema,
locations: StringOrArraySchema,
projects: StringOrArraySchema,
schedules: StringOrArraySchema,
status: StringOrArraySchema,
scopeStatusByLocation: schema.maybe(schema.boolean()),
});

Expand All @@ -56,7 +59,8 @@ export const SEARCH_FIELDS = [
];

export const getMonitors = async (
context: RouteContext
context: RouteContext<MonitorsQuery>,
{ fields }: { fields?: string[] } = {}
): Promise<SavedObjectsFindResponse<EncryptedSyntheticsMonitor>> => {
const {
perPage = 50,
Expand All @@ -68,11 +72,10 @@ export const getMonitors = async (
monitorTypes,
locations,
filter = '',
fields,
searchAfter,
projects,
schedules,
} = context.request.query as MonitorsQuery;
} = context.request.query;

const filterStr = await getMonitorFilters({
filter,
Expand All @@ -93,8 +96,8 @@ export const getMonitors = async (
searchFields: SEARCH_FIELDS,
search: query ? `${query}*` : undefined,
filter: filterStr,
fields,
searchAfter,
fields,
});
};

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/synthetics/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
getSyntheticsEnablementRoute,
} from './synthetics_service/enablement';
import {
getAllSyntheticsMonitorRoute,
getSyntheticsMonitorOverviewRoute,
getSyntheticsMonitorRoute,
} from './monitor_cruds/get_monitor';
Expand Down Expand Up @@ -53,6 +52,7 @@ import { addPrivateLocationRoute } from './settings/private_locations/add_privat
import { deletePrivateLocationRoute } from './settings/private_locations/delete_private_location';
import { getPrivateLocationsRoute } from './settings/private_locations/get_private_locations';
import { getSyntheticsFilters } from './filters/filters';
import { getAllSyntheticsMonitorRoute } from './monitor_cruds/get_monitors_list';
import { getLocationMonitors } from './settings/private_locations/get_location_monitors';

export const syntheticsAppRestApiRoutes: SyntheticsRestApiRouteFactory[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ export const deleteSyntheticsMonitorProjectRoute: SyntheticsRestApiRouteFactory
values: monitorsToDelete.map((id: string) => `${id}`),
})}`;

const { saved_objects: monitors } = await getMonitors({
...routeContext,
request: {
...request,
query: { ...request.query, filter: deleteFilter, fields: [], perPage: 500 },
const { saved_objects: monitors } = await getMonitors(
{
...routeContext,
request: {
...request,
query: { ...request.query, filter: deleteFilter, perPage: 500 },
},
},
});
{ fields: [] }
);

const {
integrations: { writeIntegrationPolicies },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ import { UMServerLibs } from '../../legacy_uptime/lib/lib';
import { SyntheticsRestApiRouteFactory } from '../../legacy_uptime/routes/types';
import { API_URLS, SYNTHETICS_API_URLS } from '../../../common/constants';
import { getMonitorNotFoundResponse } from '../synthetics_service/service_errors';
import {
getMonitorFilters,
getMonitors,
isMonitorsQueryFiltered,
MonitorsQuery,
QuerySchema,
SEARCH_FIELDS,
} from '../common';
import { getMonitorFilters, MonitorsQuery, QuerySchema, SEARCH_FIELDS } from '../common';

export const getSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = (libs: UMServerLibs) => ({
method: 'GET',
Expand Down Expand Up @@ -78,43 +71,6 @@ export const getSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = (libs: U
},
});

export const getAllSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = () => ({
method: 'GET',
path: API_URLS.SYNTHETICS_MONITORS,
validate: {
query: QuerySchema,
},
handler: async (routeContext): Promise<any> => {
const { request, savedObjectsClient, syntheticsMonitorClient } = routeContext;
const totalCountQuery = async () => {
if (isMonitorsQueryFiltered(request.query)) {
return savedObjectsClient.find({
type: syntheticsMonitorType,
perPage: 0,
page: 1,
});
}
};

const [queryResult, totalCount] = await Promise.all([
getMonitors(routeContext),
totalCountQuery(),
]);

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

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

return {
...rest,
monitors,
absoluteTotal,
perPage: perPageT,
syncErrors: syntheticsMonitorClient.syntheticsService.syncErrors,
};
},
});

export const getSyntheticsMonitorOverviewRoute: SyntheticsRestApiRouteFactory = () => ({
method: 'GET',
path: SYNTHETICS_API_URLS.SYNTHETICS_OVERVIEW,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,25 @@ export const getSyntheticsProjectMonitorsRoute: SyntheticsRestApiRouteFactory =
const decodedSearchAfter = searchAfter ? decodeURI(searchAfter) : undefined;

try {
const { saved_objects: monitors, total } = await getMonitors({
...routeContext,
request: {
...request,
query: {
...request.query,
filter: `${syntheticsMonitorType}.attributes.${ConfigKey.PROJECT_ID}: "${decodedProjectName}"`,
fields: [ConfigKey.JOURNEY_ID, ConfigKey.CONFIG_HASH],
perPage,
sortField: ConfigKey.JOURNEY_ID,
sortOrder: 'asc',
searchAfter: decodedSearchAfter ? [...decodedSearchAfter.split(',')] : undefined,
const { saved_objects: monitors, total } = await getMonitors(
{
...routeContext,
request: {
...request,
query: {
...request.query,
filter: `${syntheticsMonitorType}.attributes.${ConfigKey.PROJECT_ID}: "${decodedProjectName}"`,
perPage,
sortField: ConfigKey.JOURNEY_ID,
sortOrder: 'asc',
searchAfter: decodedSearchAfter ? [...decodedSearchAfter.split(',')] : undefined,
},
},
},
});
{
fields: [ConfigKey.JOURNEY_ID, ConfigKey.CONFIG_HASH],
}
);
const projectMonitors = monitors.map((monitor) => ({
journey_id: monitor.attributes[ConfigKey.JOURNEY_ID],
hash: monitor.attributes[ConfigKey.CONFIG_HASH] || '',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 { SyntheticsRestApiRouteFactory } from '../../legacy_uptime/routes';
import { API_URLS } from '../../../common/constants';
import { getMonitors, isMonitorsQueryFiltered, QuerySchema } from '../common';
import { syntheticsMonitorType } from '../../../common/types/saved_objects';

export const getAllSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = () => ({
method: 'GET',
path: API_URLS.SYNTHETICS_MONITORS,
validate: {
query: QuerySchema,
},
handler: async (routeContext): Promise<any> => {
const { request, savedObjectsClient, syntheticsMonitorClient } = routeContext;
const totalCountQuery = async () => {
if (isMonitorsQueryFiltered(request.query)) {
return savedObjectsClient.find({
type: syntheticsMonitorType,
perPage: 0,
page: 1,
});
}
};

const [queryResult, totalCount] = await Promise.all([
getMonitors(routeContext),
totalCountQuery(),
]);

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

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

return {
...rest,
monitors,
absoluteTotal,
perPage: perPageT,
syncErrors: syntheticsMonitorClient.syntheticsService.syncErrors,
};
},
});

0 comments on commit 97a93b3

Please sign in to comment.