2021-04-22 10:07:10 +02:00
|
|
|
import { Request, Response } from 'express';
|
|
|
|
import Controller from '../controller';
|
|
|
|
import { handleErrors } from './util';
|
|
|
|
import { UPDATE_APPLICATION } from '../../permissions';
|
|
|
|
import { IUnleashConfig } from '../../types/option';
|
|
|
|
import { IUnleashServices } from '../../types/services';
|
|
|
|
import { Logger } from '../../logger';
|
|
|
|
import ClientMetricsService from '../../services/client-metrics';
|
2018-01-17 15:31:53 +01:00
|
|
|
|
2018-12-04 08:50:45 +01:00
|
|
|
class MetricsController extends Controller {
|
2021-04-22 10:07:10 +02:00
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private metrics: ClientMetricsService;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
{
|
|
|
|
clientMetricsService,
|
|
|
|
}: Pick<IUnleashServices, 'clientMetricsService'>,
|
|
|
|
) {
|
2018-12-19 14:50:01 +01:00
|
|
|
super(config);
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger = config.getLogger('/admin-api/metrics.js');
|
2018-12-19 14:50:01 +01:00
|
|
|
|
2020-12-17 19:22:30 +01:00
|
|
|
this.metrics = clientMetricsService;
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-12-04 08:50:45 +01:00
|
|
|
this.get('/seen-toggles', this.getSeenToggles);
|
|
|
|
this.get('/seen-apps', this.getSeenApps);
|
|
|
|
this.get('/feature-toggles', this.getFeatureToggles);
|
|
|
|
this.get('/feature-toggles/:name', this.getFeatureToggle);
|
2018-12-19 10:36:56 +01:00
|
|
|
this.post(
|
|
|
|
'/applications/:appName',
|
|
|
|
this.createApplication,
|
2020-04-14 22:29:11 +02:00
|
|
|
UPDATE_APPLICATION,
|
2018-12-19 10:36:56 +01:00
|
|
|
);
|
2020-09-25 09:39:12 +02:00
|
|
|
this.delete(
|
|
|
|
'/applications/:appName',
|
|
|
|
this.deleteApplication,
|
|
|
|
UPDATE_APPLICATION,
|
|
|
|
);
|
2018-12-04 08:50:45 +01:00
|
|
|
this.get('/applications/', this.getApplications);
|
|
|
|
this.get('/applications/:appName', this.getApplication);
|
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async getSeenToggles(req: Request, res: Response): Promise<void> {
|
2021-02-17 15:24:43 +01:00
|
|
|
try {
|
|
|
|
const seenAppToggles = await this.metrics.getAppsWithToggles();
|
|
|
|
res.json(seenAppToggles);
|
|
|
|
} catch (e) {
|
|
|
|
handleErrors(res, this.logger, e);
|
|
|
|
}
|
2018-12-04 08:50:45 +01:00
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async getSeenApps(req: Request, res: Response): Promise<void> {
|
2021-02-17 15:24:43 +01:00
|
|
|
try {
|
|
|
|
const seenApps = await this.metrics.getSeenApps();
|
|
|
|
res.json(seenApps);
|
|
|
|
} catch (e) {
|
|
|
|
handleErrors(res, this.logger, e);
|
|
|
|
}
|
2018-12-04 08:50:45 +01:00
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async getFeatureToggles(req: Request, res: Response): Promise<void> {
|
2021-02-17 15:24:43 +01:00
|
|
|
try {
|
|
|
|
const toggles = await this.metrics.getTogglesMetrics();
|
|
|
|
res.json(toggles);
|
|
|
|
} catch (e) {
|
|
|
|
handleErrors(res, this.logger, e);
|
|
|
|
}
|
2018-12-04 08:50:45 +01:00
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async getFeatureToggle(req: Request, res: Response): Promise<void> {
|
2021-02-17 15:24:43 +01:00
|
|
|
try {
|
|
|
|
const { name } = req.params;
|
|
|
|
const data = await this.metrics.getTogglesMetrics();
|
|
|
|
const lastHour = data.lastHour[name] || {};
|
|
|
|
const lastMinute = data.lastMinute[name] || {};
|
|
|
|
res.json({
|
|
|
|
lastHour,
|
|
|
|
lastMinute,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
handleErrors(res, this.logger, e);
|
|
|
|
}
|
2018-12-04 08:50:45 +01:00
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async deleteApplication(req: Request, res: Response): Promise<void> {
|
2020-09-25 09:39:12 +02:00
|
|
|
const { appName } = req.params;
|
|
|
|
|
|
|
|
try {
|
2021-01-18 12:32:19 +01:00
|
|
|
await this.metrics.deleteApplication(appName);
|
2020-09-25 09:39:12 +02:00
|
|
|
res.status(200).end();
|
|
|
|
} catch (e) {
|
2021-01-18 12:32:19 +01:00
|
|
|
handleErrors(res, this.logger, e);
|
2020-09-25 09:39:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async createApplication(req: Request, res: Response): Promise<void> {
|
2020-04-14 22:29:11 +02:00
|
|
|
const input = { ...req.body, appName: req.params.appName };
|
2018-12-04 08:50:45 +01:00
|
|
|
try {
|
2021-01-18 12:32:19 +01:00
|
|
|
await this.metrics.createApplication(input);
|
|
|
|
res.status(202).end();
|
2018-12-04 08:50:45 +01:00
|
|
|
} catch (err) {
|
2021-01-18 12:32:19 +01:00
|
|
|
handleErrors(res, this.logger, err);
|
2018-12-04 08:50:45 +01:00
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async getApplications(req: Request, res: Response): Promise<void> {
|
2018-12-04 08:50:45 +01:00
|
|
|
try {
|
2021-01-18 12:32:19 +01:00
|
|
|
const applications = await this.metrics.getApplications(req.query);
|
2018-12-04 08:50:45 +01:00
|
|
|
res.json({ applications });
|
|
|
|
} catch (err) {
|
2021-01-18 12:32:19 +01:00
|
|
|
handleErrors(res, this.logger, err);
|
2018-12-04 08:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async getApplication(req: Request, res: Response): Promise<void> {
|
2020-04-14 22:29:11 +02:00
|
|
|
const { appName } = req.params;
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-12-04 08:50:45 +01:00
|
|
|
try {
|
2021-01-18 12:32:19 +01:00
|
|
|
const appDetails = await this.metrics.getApplication(appName);
|
2018-12-04 08:50:45 +01:00
|
|
|
res.json(appDetails);
|
|
|
|
} catch (err) {
|
2021-01-18 12:32:19 +01:00
|
|
|
handleErrors(res, this.logger, err);
|
2018-12-04 08:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 10:07:10 +02:00
|
|
|
export default MetricsController;
|
2018-12-04 08:50:45 +01:00
|
|
|
module.exports = MetricsController;
|