mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
[Synthetics] Fixes project monitor edit code path (#146416)
## Summary Fixed project monitor editor code path, it was called un-necessarily on each request.
This commit is contained in:
parent
2a34e0e783
commit
dc2b29add9
3 changed files with 55 additions and 35 deletions
|
@ -10,7 +10,6 @@ import { ProjectMonitor } from '../../../common/runtime_types';
|
|||
|
||||
import { SyntheticsRestApiRouteFactory } from '../../legacy_uptime/routes/types';
|
||||
import { API_URLS } from '../../../common/constants';
|
||||
import { getAllLocations } from '../../synthetics_service/get_all_locations';
|
||||
import { ProjectMonitorFormatter } from '../../synthetics_service/project_monitor/project_monitor_formatter';
|
||||
|
||||
const MAX_PAYLOAD_SIZE = 1048576 * 20; // 20MiB
|
||||
|
@ -52,18 +51,11 @@ export const addSyntheticsProjectMonitorRoute: SyntheticsRestApiRouteFactory = (
|
|||
}
|
||||
|
||||
try {
|
||||
const { publicLocations, privateLocations } = await getAllLocations(
|
||||
server,
|
||||
syntheticsMonitorClient,
|
||||
savedObjectsClient
|
||||
);
|
||||
const encryptedSavedObjectsClient = server.encryptedSavedObjects.getClient();
|
||||
|
||||
const pushMonitorFormatter = new ProjectMonitorFormatter({
|
||||
projectId: decodedProjectName,
|
||||
spaceId,
|
||||
locations: publicLocations,
|
||||
privateLocations,
|
||||
encryptedSavedObjectsClient,
|
||||
savedObjectsClient,
|
||||
monitors,
|
||||
|
|
|
@ -10,7 +10,13 @@ import {
|
|||
INSUFFICIENT_FLEET_PERMISSIONS,
|
||||
ProjectMonitorFormatter,
|
||||
} from './project_monitor_formatter';
|
||||
import { ConfigKey, DataStream, LocationStatus } from '../../../common/runtime_types';
|
||||
import {
|
||||
ConfigKey,
|
||||
DataStream,
|
||||
Locations,
|
||||
LocationStatus,
|
||||
PrivateLocation,
|
||||
} from '../../../common/runtime_types';
|
||||
import { DEFAULT_FIELDS } from '../../../common/constants/monitor_defaults';
|
||||
import { times } from 'lodash';
|
||||
import { SyntheticsService } from '../synthetics_service';
|
||||
|
@ -22,6 +28,7 @@ import { formatSecrets } from '../utils';
|
|||
|
||||
import * as telemetryHooks from '../../routes/telemetry/monitor_upgrade_sender';
|
||||
import { formatLocation } from '../../../common/utils/location_formatter';
|
||||
import * as locationsUtil from '../get_all_locations';
|
||||
|
||||
const testMonitors = [
|
||||
{
|
||||
|
@ -86,7 +93,7 @@ const privateLocations = times(1).map((n) => {
|
|||
agentPolicyId: `loc-${n}`,
|
||||
concurrentMonitors: 1,
|
||||
};
|
||||
});
|
||||
}) as PrivateLocation[];
|
||||
|
||||
describe('ProjectMonitorFormatter', () => {
|
||||
const mockEsClient = {
|
||||
|
@ -124,7 +131,7 @@ describe('ProjectMonitorFormatter', () => {
|
|||
|
||||
const encryptedSavedObjectsClient = encryptedSavedObjectsMock.createStart().getClient();
|
||||
|
||||
const locations = times(3).map((n) => {
|
||||
const publicLocations = times(3).map((n) => {
|
||||
return {
|
||||
id: `loc-${n}`,
|
||||
label: `Location ${n}`,
|
||||
|
@ -136,16 +143,22 @@ describe('ProjectMonitorFormatter', () => {
|
|||
isServiceManaged: true,
|
||||
status: LocationStatus.GA,
|
||||
};
|
||||
});
|
||||
}) as Locations;
|
||||
|
||||
const monitorClient = new SyntheticsMonitorClient(syntheticsService, serverMock);
|
||||
|
||||
jest.spyOn(locationsUtil, 'getAllLocations').mockImplementation(
|
||||
async () =>
|
||||
({
|
||||
publicLocations,
|
||||
privateLocations,
|
||||
} as any)
|
||||
);
|
||||
|
||||
it('should return errors', async () => {
|
||||
const pushMonitorFormatter = new ProjectMonitorFormatter({
|
||||
projectId: 'test-project',
|
||||
spaceId: 'default-space',
|
||||
locations,
|
||||
privateLocations,
|
||||
encryptedSavedObjectsClient,
|
||||
savedObjectsClient: soClient,
|
||||
monitors: testMonitors,
|
||||
|
@ -194,8 +207,6 @@ describe('ProjectMonitorFormatter', () => {
|
|||
const pushMonitorFormatter = new ProjectMonitorFormatter({
|
||||
projectId: 'test-project',
|
||||
spaceId: 'default-space',
|
||||
locations,
|
||||
privateLocations,
|
||||
encryptedSavedObjectsClient,
|
||||
savedObjectsClient: soClient,
|
||||
monitors: testMonitors,
|
||||
|
@ -244,8 +255,6 @@ describe('ProjectMonitorFormatter', () => {
|
|||
const pushMonitorFormatter = new ProjectMonitorFormatter({
|
||||
projectId: 'test-project',
|
||||
spaceId: 'default-space',
|
||||
locations,
|
||||
privateLocations,
|
||||
encryptedSavedObjectsClient,
|
||||
savedObjectsClient: soClient,
|
||||
monitors: testMonitors,
|
||||
|
@ -289,8 +298,6 @@ describe('ProjectMonitorFormatter', () => {
|
|||
const pushMonitorFormatter = new ProjectMonitorFormatter({
|
||||
projectId: 'test-project',
|
||||
spaceId: 'default-space',
|
||||
locations,
|
||||
privateLocations,
|
||||
encryptedSavedObjectsClient,
|
||||
savedObjectsClient: soClient,
|
||||
monitors: testMonitors,
|
||||
|
@ -335,8 +342,6 @@ describe('ProjectMonitorFormatter', () => {
|
|||
const pushMonitorFormatter = new ProjectMonitorFormatter({
|
||||
projectId: 'test-project',
|
||||
spaceId: 'default-space',
|
||||
locations,
|
||||
privateLocations,
|
||||
encryptedSavedObjectsClient,
|
||||
savedObjectsClient: soClient,
|
||||
monitors: testMonitors,
|
||||
|
@ -387,8 +392,6 @@ describe('ProjectMonitorFormatter', () => {
|
|||
const pushMonitorFormatter = new ProjectMonitorFormatter({
|
||||
projectId: 'test-project',
|
||||
spaceId: 'default-space',
|
||||
locations,
|
||||
privateLocations,
|
||||
encryptedSavedObjectsClient,
|
||||
savedObjectsClient: soClient,
|
||||
monitors: testMonitors,
|
||||
|
|
|
@ -13,6 +13,7 @@ import {
|
|||
} from '@kbn/core/server';
|
||||
import { i18n } from '@kbn/i18n';
|
||||
import { EncryptedSavedObjectsClient } from '@kbn/encrypted-saved-objects-plugin/server';
|
||||
import { getAllLocations } from '../get_all_locations';
|
||||
import { syncNewMonitorBulk } from '../../routes/monitor_cruds/bulk_cruds/add_monitor_bulk';
|
||||
import { SyntheticsMonitorClient } from '../synthetics_monitor/synthetics_monitor_client';
|
||||
import { syncEditedMonitorBulk } from '../../routes/monitor_cruds/bulk_cruds/edit_monitor_bulk';
|
||||
|
@ -71,7 +72,7 @@ export const FAILED_TO_UPDATE_MONITORS = i18n.translate(
|
|||
export class ProjectMonitorFormatter {
|
||||
private projectId: string;
|
||||
private spaceId: string;
|
||||
private locations: Locations;
|
||||
private publicLocations: Locations;
|
||||
private privateLocations: PrivateLocation[];
|
||||
private savedObjectsClient: SavedObjectsClientContract;
|
||||
private encryptedSavedObjectsClient: EncryptedSavedObjectsClient;
|
||||
|
@ -87,8 +88,6 @@ export class ProjectMonitorFormatter {
|
|||
private writeIntegrationPoliciesPermissions?: boolean;
|
||||
|
||||
constructor({
|
||||
locations,
|
||||
privateLocations,
|
||||
savedObjectsClient,
|
||||
encryptedSavedObjectsClient,
|
||||
projectId,
|
||||
|
@ -98,8 +97,6 @@ export class ProjectMonitorFormatter {
|
|||
syntheticsMonitorClient,
|
||||
request,
|
||||
}: {
|
||||
locations: Locations;
|
||||
privateLocations: PrivateLocation[];
|
||||
savedObjectsClient: SavedObjectsClientContract;
|
||||
encryptedSavedObjectsClient: EncryptedSavedObjectsClient;
|
||||
projectId: string;
|
||||
|
@ -111,8 +108,6 @@ export class ProjectMonitorFormatter {
|
|||
}) {
|
||||
this.projectId = projectId;
|
||||
this.spaceId = spaceId;
|
||||
this.locations = locations;
|
||||
this.privateLocations = privateLocations;
|
||||
this.savedObjectsClient = savedObjectsClient;
|
||||
this.encryptedSavedObjectsClient = encryptedSavedObjectsClient;
|
||||
this.syntheticsMonitorClient = syntheticsMonitorClient;
|
||||
|
@ -120,10 +115,33 @@ export class ProjectMonitorFormatter {
|
|||
this.server = server;
|
||||
this.projectFilter = `${syntheticsMonitorType}.attributes.${ConfigKey.PROJECT_ID}: "${this.projectId}"`;
|
||||
this.request = request;
|
||||
this.publicLocations = [];
|
||||
this.privateLocations = [];
|
||||
}
|
||||
|
||||
init = async () => {
|
||||
const locationsPromise = getAllLocations(
|
||||
this.server,
|
||||
this.syntheticsMonitorClient,
|
||||
this.savedObjectsClient
|
||||
);
|
||||
const existingMonitorsPromise = this.getProjectMonitorsForProject();
|
||||
|
||||
const [locations, existingMonitors] = await Promise.all([
|
||||
locationsPromise,
|
||||
existingMonitorsPromise,
|
||||
]);
|
||||
|
||||
const { publicLocations, privateLocations } = locations;
|
||||
|
||||
this.publicLocations = publicLocations;
|
||||
this.privateLocations = privateLocations;
|
||||
|
||||
return existingMonitors;
|
||||
};
|
||||
|
||||
public configureAllProjectMonitors = async () => {
|
||||
const existingMonitors = await this.getProjectMonitorsForProject();
|
||||
const existingMonitors = await this.init();
|
||||
|
||||
const normalizedNewMonitors: SyntheticsMonitor[] = [];
|
||||
const normalizedUpdateMonitors: Array<{
|
||||
|
@ -161,7 +179,6 @@ export class ProjectMonitorFormatter {
|
|||
),
|
||||
payload: monitor,
|
||||
});
|
||||
continue;
|
||||
} else if (previousMonitor) {
|
||||
this.updatedMonitors.push(monitor.id);
|
||||
normalizedUpdateMonitors.push({ monitor: normM as MonitorFields, previousMonitor });
|
||||
|
@ -197,7 +214,7 @@ export class ProjectMonitorFormatter {
|
|||
|
||||
const { normalizedFields: normalizedMonitor, errors } = normalizeProjectMonitor({
|
||||
monitor,
|
||||
locations: this.locations,
|
||||
locations: this.publicLocations,
|
||||
privateLocations: this.privateLocations,
|
||||
projectId: this.projectId,
|
||||
namespace: this.spaceId,
|
||||
|
@ -258,7 +275,8 @@ export class ProjectMonitorFormatter {
|
|||
);
|
||||
}
|
||||
|
||||
await finder.close();
|
||||
// no need to wait for it
|
||||
finder.close();
|
||||
|
||||
return hits;
|
||||
};
|
||||
|
@ -336,6 +354,13 @@ export class ProjectMonitorFormatter {
|
|||
errors: ServiceLocationErrors;
|
||||
updatedCount: number;
|
||||
}> => {
|
||||
if (monitors.length === 0) {
|
||||
return {
|
||||
editedMonitors: [],
|
||||
errors: [],
|
||||
updatedCount: 0,
|
||||
};
|
||||
}
|
||||
const decryptedPreviousMonitors = await this.getDecryptedMonitors(
|
||||
monitors.map((m) => m.previousMonitor)
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue