2021-11-12 13:15:51 +01:00
|
|
|
import { Request, Response } from 'express';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../../types/option';
|
|
|
|
import { IUnleashServices } from '../../types/services';
|
|
|
|
import EventService from '../../services/event-service';
|
2021-05-04 22:11:30 +02:00
|
|
|
import { ADMIN } from '../../types/permissions';
|
2021-11-12 13:15:51 +01:00
|
|
|
import { IEvent } from '../../types/events';
|
|
|
|
import Controller from '../controller';
|
2022-05-30 16:44:37 +02:00
|
|
|
import { anonymise } from '../../util/anonymise';
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2016-09-05 16:50:52 +02:00
|
|
|
const version = 1;
|
2021-04-22 10:07:10 +02:00
|
|
|
export default class EventController extends Controller {
|
|
|
|
private eventService: EventService;
|
|
|
|
|
2022-05-30 16:44:37 +02:00
|
|
|
private anonymise: boolean = false;
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
{ eventService }: Pick<IUnleashServices, 'eventService'>,
|
|
|
|
) {
|
2018-12-19 14:50:01 +01:00
|
|
|
super(config);
|
2021-04-22 10:07:10 +02:00
|
|
|
this.eventService = eventService;
|
2022-05-30 16:44:37 +02:00
|
|
|
this.anonymise = config.experimental?.anonymiseEventLog;
|
2021-05-04 22:11:30 +02:00
|
|
|
this.get('/', this.getEvents, ADMIN);
|
2018-11-30 10:11:36 +01:00
|
|
|
this.get('/:name', this.getEventsForToggle);
|
|
|
|
}
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2022-05-30 16:44:37 +02:00
|
|
|
fixEvents(events: IEvent[]): IEvent[] {
|
|
|
|
if (this.anonymise) {
|
|
|
|
return events.map((e: IEvent) => ({
|
|
|
|
...e,
|
|
|
|
createdBy: anonymise(e.createdBy),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return events;
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:15:51 +01:00
|
|
|
async getEvents(
|
|
|
|
req: Request<any, any, any, { project?: string }>,
|
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
|
|
|
const { project } = req.query;
|
|
|
|
let events: IEvent[];
|
|
|
|
if (project) {
|
|
|
|
events = await this.eventService.getEventsForProject(project);
|
2021-09-20 12:13:38 +02:00
|
|
|
} else {
|
|
|
|
events = await this.eventService.getEvents();
|
|
|
|
}
|
2022-05-30 16:44:37 +02:00
|
|
|
res.json({
|
|
|
|
version,
|
|
|
|
events: this.fixEvents(events),
|
|
|
|
});
|
2018-11-30 10:11:36 +01:00
|
|
|
}
|
2014-11-14 07:13:08 +01:00
|
|
|
|
2021-11-12 13:15:51 +01:00
|
|
|
async getEventsForToggle(
|
|
|
|
req: Request<{ name: string }>,
|
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
2016-12-05 17:58:29 +01:00
|
|
|
const toggleName = req.params.name;
|
2021-08-13 10:36:19 +02:00
|
|
|
const events = await this.eventService.getEventsForToggle(toggleName);
|
|
|
|
|
2022-05-30 16:44:37 +02:00
|
|
|
res.json({
|
|
|
|
version,
|
|
|
|
toggleName,
|
|
|
|
events: this.fixEvents(events),
|
|
|
|
});
|
2018-11-30 10:11:36 +01:00
|
|
|
}
|
|
|
|
}
|