Skip to content

Commit

Permalink
[Synthetics] Fixes project monitor edit code path (#146416)
Browse files Browse the repository at this point in the history
## Summary

Fixed project monitor editor code path, it was called un-necessarily on
each request.
  • Loading branch information
shahzad31 authored Dec 2, 2022
1 parent 2a34e0e commit dc2b29a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 = [
{
Expand Down Expand Up @@ -86,7 +93,7 @@ const privateLocations = times(1).map((n) => {
agentPolicyId: `loc-${n}`,
concurrentMonitors: 1,
};
});
}) as PrivateLocation[];

describe('ProjectMonitorFormatter', () => {
const mockEsClient = {
Expand Down Expand Up @@ -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}`,
Expand All @@ -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,
Expand Down Expand Up @@ -194,8 +207,6 @@ describe('ProjectMonitorFormatter', () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,
Expand Down Expand Up @@ -244,8 +255,6 @@ describe('ProjectMonitorFormatter', () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,
Expand Down Expand Up @@ -289,8 +298,6 @@ describe('ProjectMonitorFormatter', () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,
Expand Down Expand Up @@ -335,8 +342,6 @@ describe('ProjectMonitorFormatter', () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,
Expand Down Expand Up @@ -387,8 +392,6 @@ describe('ProjectMonitorFormatter', () => {
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default-space',
locations,
privateLocations,
encryptedSavedObjectsClient,
savedObjectsClient: soClient,
monitors: testMonitors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -87,8 +88,6 @@ export class ProjectMonitorFormatter {
private writeIntegrationPoliciesPermissions?: boolean;

constructor({
locations,
privateLocations,
savedObjectsClient,
encryptedSavedObjectsClient,
projectId,
Expand All @@ -98,8 +97,6 @@ export class ProjectMonitorFormatter {
syntheticsMonitorClient,
request,
}: {
locations: Locations;
privateLocations: PrivateLocation[];
savedObjectsClient: SavedObjectsClientContract;
encryptedSavedObjectsClient: EncryptedSavedObjectsClient;
projectId: string;
Expand All @@ -111,19 +108,40 @@ export class ProjectMonitorFormatter {
}) {
this.projectId = projectId;
this.spaceId = spaceId;
this.locations = locations;
this.privateLocations = privateLocations;
this.savedObjectsClient = savedObjectsClient;
this.encryptedSavedObjectsClient = encryptedSavedObjectsClient;
this.syntheticsMonitorClient = syntheticsMonitorClient;
this.monitors = monitors;
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<{
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -258,7 +275,8 @@ export class ProjectMonitorFormatter {
);
}

await finder.close();
// no need to wait for it
finder.close();

return hits;
};
Expand Down Expand Up @@ -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)
);
Expand Down

0 comments on commit dc2b29a

Please sign in to comment.