All files / src/lib/routes/admin-api metrics.ts

100% Statements 27/27
50% Branches 1/2
100% Functions 6/6
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87  59x 59x                                 141x 141x   141x     141x 141x 141x 141x     141x         141x         141x 141x       2x               4x   4x 4x       2x 2x 2x       4x     4x     4x       1x   1x     1x     59x  
import { Request, Response } from 'express';
import Controller from '../controller';
import { UPDATE_APPLICATION } from '../../types/permissions';
import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import { Logger } from '../../logger';
import ClientInstanceService from '../../services/client-metrics/instance-service';
 
class MetricsController extends Controller {
    private logger: Logger;
 
    private clientInstanceService: ClientInstanceService;
 
    constructor(
        config: IUnleashConfig,
        {
            clientInstanceService,
        }: Pick<IUnleashServices, 'clientInstanceService'>,
    ) {
        super(config);
        this.logger = config.getLogger('/admin-api/metrics.ts');
 
        this.clientInstanceService = clientInstanceService;
 
        // deprecated routes
        this.get('/seen-toggles', this.deprecated);
        this.get('/seen-apps', this.deprecated);
        this.get('/feature-toggles', this.deprecated);
        this.get('/feature-toggles/:name', this.deprecated);
 
        // in use
        this.post(
            '/applications/:appName',
            this.createApplication,
            UPDATE_APPLICATION,
        );
        this.delete(
            '/applications/:appName',
            this.deleteApplication,
            UPDATE_APPLICATION,
        );
        this.get('/applications/', this.getApplications);
        this.get('/applications/:appName', this.getApplication);
    }
 
    async deprecated(req: Request, res: Response): Promise<void> {
        res.status(410).json({
            lastHour: {},
            lastMinute: {},
            maturity: 'deprecated',
        });
    }
 
    async deleteApplication(req: Request, res: Response): Promise<void> {
        const { appName } = req.params;
 
        await this.clientInstanceService.deleteApplication(appName);
        res.status(200).end();
    }
 
    async createApplication(req: Request, res: Response): Promise<void> {
        const input = { ...req.body, appName: req.params.appName };
        await this.clientInstanceService.createApplication(input);
        res.status(202).end();
    }
 
    async getApplications(req: Request, res: Response): Promise<void> {
        const query = req.query.strategyName
            ? { strategyName: req.query.strategyName as string }
            : {};
        const applications = await this.clientInstanceService.getApplications(
            query,
        );
        res.json({ applications });
    }
 
    async getApplication(req: Request, res: Response): Promise<void> {
        const { appName } = req.params;
 
        const appDetails = await this.clientInstanceService.getApplication(
            appName,
        );
        res.json(appDetails);
    }
}
export default MetricsController;