1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/src/lib/routes/admin-api/event.ts

183 lines
5.7 KiB
TypeScript
Raw Normal View History

import { Request, Response } from 'express';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import EventService from '../../services/event-service';
import { ADMIN, NONE } from '../../types/permissions';
import { IEvent } from '../../types/events';
import Controller from '../controller';
2022-05-30 16:44:37 +02:00
import { anonymise } from '../../util/anonymise';
import { OpenApiService } from '../../services/openapi-service';
import { createResponseSchema } from '../../openapi/util/create-response-schema';
import { endpointDescriptions } from '../../openapi/endpoint-descriptions';
import {
eventsSchema,
EventsSchema,
} from '../../../lib/openapi/spec/events-schema';
import { serializeDates } from '../../../lib/types/serialize-dates';
import {
featureEventsSchema,
FeatureEventsSchema,
} from '../../../lib/openapi/spec/feature-events-schema';
import { getStandardResponses } from '../../../lib/openapi/util/standard-responses';
import { createRequestSchema } from '../../openapi/util/create-request-schema';
import { SearchEventsSchema } from '../../openapi/spec/search-events-schema';
const version = 1;
export default class EventController extends Controller {
private eventService: EventService;
2022-05-30 16:44:37 +02:00
private anonymise: boolean = false;
private openApiService: OpenApiService;
constructor(
config: IUnleashConfig,
{
eventService,
openApiService,
}: Pick<IUnleashServices, 'eventService' | 'openApiService'>,
) {
super(config);
this.eventService = eventService;
2022-05-30 16:44:37 +02:00
this.anonymise = config.experimental?.anonymiseEventLog;
this.openApiService = openApiService;
this.route({
method: 'get',
path: '',
handler: this.getEvents,
permission: ADMIN,
middleware: [
openApiService.validPath({
operationId: 'getEvents',
tags: ['Events'],
responses: {
...getStandardResponses(401),
200: createResponseSchema('eventsSchema'),
},
parameters: [
{
name: 'project',
description:
'The name of the project whose events you want to retrieve',
schema: { type: 'string' },
in: 'query',
},
],
...endpointDescriptions.admin.events,
}),
],
});
this.route({
method: 'get',
path: '/:featureName',
handler: this.getEventsForToggle,
permission: NONE,
middleware: [
openApiService.validPath({
operationId: 'getEventsForToggle',
tags: ['Events'],
responses: {
...getStandardResponses(401),
200: createResponseSchema('featureEventsSchema'),
},
...endpointDescriptions.admin.eventsPerFeature,
}),
],
});
this.route({
method: 'post',
path: '/search',
handler: this.searchEvents,
permission: NONE,
middleware: [
openApiService.validPath({
operationId: 'searchEvents',
tags: ['Events'],
requestBody: createRequestSchema('searchEventsSchema'),
responses: { 200: createResponseSchema('eventsSchema') },
}),
],
});
}
maybeAnonymiseEvents(events: IEvent[]): IEvent[] {
2022-05-30 16:44:37 +02:00
if (this.anonymise) {
return events.map((e: IEvent) => ({
...e,
createdBy: anonymise(e.createdBy),
}));
}
return events;
}
async getEvents(
req: Request<any, any, any, { project?: string }>,
res: Response<EventsSchema>,
): Promise<void> {
const { project } = req.query;
let events: IEvent[];
if (project) {
events = await this.eventService.searchEvents({ project });
} else {
events = await this.eventService.getEvents();
}
const response: EventsSchema = {
2022-05-30 16:44:37 +02:00
version,
events: serializeDates(this.maybeAnonymiseEvents(events)),
};
this.openApiService.respondWithValidation(
200,
res,
eventsSchema.$id,
response,
);
}
2014-11-14 07:13:08 +01:00
async getEventsForToggle(
req: Request<{ featureName: string }>,
res: Response<FeatureEventsSchema>,
): Promise<void> {
const feature = req.params.featureName;
const events = await this.eventService.searchEvents({ feature });
const response = {
version,
toggleName: feature,
events: serializeDates(this.maybeAnonymiseEvents(events)),
};
this.openApiService.respondWithValidation(
200,
res,
featureEventsSchema.$id,
response,
);
}
async searchEvents(
req: Request<unknown, unknown, SearchEventsSchema>,
res: Response<EventsSchema>,
): Promise<void> {
const events = await this.eventService.searchEvents(req.body);
const response = {
2022-05-30 16:44:37 +02:00
version,
events: serializeDates(this.maybeAnonymiseEvents(events)),
};
this.openApiService.respondWithValidation(
200,
res,
featureEventsSchema.$id,
response,
);
}
}