mirror of
https://github.com/elastic/kibana.git
synced 2025-06-27 18:51:07 -04:00
[Synthetics] Remove fields from API query interface (#158449)
## Summary Remove fields from API query interface !! Fixes https://github.com/elastic/kibana/issues/153472
This commit is contained in:
parent
c783e6a33f
commit
97a93b34ee
7 changed files with 97 additions and 84 deletions
|
@ -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;
|
||||
|
|
|
@ -12,6 +12,10 @@ 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()),
|
||||
|
@ -19,13 +23,12 @@ export const QuerySchema = schema.object({
|
|||
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())),
|
||||
});
|
||||
|
||||
|
@ -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()),
|
||||
});
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -93,8 +96,8 @@ export const getMonitors = async (
|
|||
searchFields: SEARCH_FIELDS,
|
||||
search: query ? `${query}*` : undefined,
|
||||
filter: filterStr,
|
||||
fields,
|
||||
searchAfter,
|
||||
fields,
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -23,7 +23,6 @@ import {
|
|||
getSyntheticsEnablementRoute,
|
||||
} from './synthetics_service/enablement';
|
||||
import {
|
||||
getAllSyntheticsMonitorRoute,
|
||||
getSyntheticsMonitorOverviewRoute,
|
||||
getSyntheticsMonitorRoute,
|
||||
} from './monitor_cruds/get_monitor';
|
||||
|
@ -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[] = [
|
||||
|
|
|
@ -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 },
|
||||
|
|
|
@ -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',
|
||||
|
@ -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,
|
||||
|
|
|
@ -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] || '',
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue