UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature toggles service. It provides different strategies for handling feature toggles.

155 lines 5.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const controller_1 = __importDefault(require("../controller")); const permissions_1 = require("../../types/permissions"); const create_request_schema_1 = require("../../openapi/util/create-request-schema"); const create_response_schema_1 = require("../../openapi/util/create-response-schema"); const standard_responses_1 = require("../../openapi/util/standard-responses"); class MetricsController extends controller_1.default { constructor(config, { clientInstanceService, openApiService, }) { super(config); this.logger = config.getLogger('/admin-api/metrics.ts'); this.flagResolver = config.flagResolver; 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); this.route({ method: 'post', path: '/applications/:appName', handler: this.createApplication, permission: permissions_1.UPDATE_APPLICATION, middleware: [ openApiService.validPath({ tags: ['Metrics'], operationId: 'createApplication', responses: { 202: standard_responses_1.emptyResponse, }, requestBody: (0, create_request_schema_1.createRequestSchema)('applicationSchema'), }), ], }); this.route({ method: 'delete', path: '/applications/:appName', handler: this.deleteApplication, permission: permissions_1.UPDATE_APPLICATION, acceptAnyContentType: true, middleware: [ openApiService.validPath({ tags: ['Metrics'], operationId: 'deleteApplication', responses: { 200: standard_responses_1.emptyResponse, }, }), ], }); this.route({ method: 'get', path: '/applications', handler: this.getApplications, permission: permissions_1.NONE, middleware: [ openApiService.validPath({ tags: ['Metrics'], operationId: 'getApplications', responses: { 200: (0, create_response_schema_1.createResponseSchema)('applicationsSchema'), }, }), ], }); this.route({ method: 'get', path: '/applications/:appName', handler: this.getApplication, permission: permissions_1.NONE, middleware: [ openApiService.validPath({ tags: ['Metrics'], operationId: 'getApplication', responses: { 200: (0, create_response_schema_1.createResponseSchema)('applicationSchema'), }, }), ], }); this.route({ method: 'get', path: '/rps', handler: this.getRps, permission: permissions_1.NONE, middleware: [ openApiService.validPath({ tags: ['Metrics'], operationId: 'getRequestsPerSecond', responses: { 200: (0, create_response_schema_1.createResponseSchema)('requestsPerSecondSegmentedSchema'), }, }), ], }); } async deprecated(req, res) { res.status(410).json({ lastHour: {}, lastMinute: {}, maturity: 'deprecated', }); } async deleteApplication(req, res) { const { appName } = req.params; await this.clientInstanceService.deleteApplication(appName); res.status(200).end(); } async createApplication(req, res) { const input = { ...req.body, appName: req.params.appName, }; await this.clientInstanceService.createApplication(input); res.status(202).end(); } async getApplications(req, res) { const query = req.query.strategyName ? { strategyName: req.query.strategyName } : {}; const applications = await this.clientInstanceService.getApplications(query); res.json({ applications }); } async getApplication(req, res) { const { appName } = req.params; const appDetails = await this.clientInstanceService.getApplication(appName); res.json(appDetails); } async getRps(req, res) { if (!this.flagResolver.isEnabled('networkView')) { res.status(404).send('Not enabled'); return; } try { const hoursToQuery = 6; const [clientMetrics, adminMetrics] = await Promise.all([ this.clientInstanceService.getRPSForPath('/api/client/.*', hoursToQuery), this.clientInstanceService.getRPSForPath('/api/admin/.*', hoursToQuery), ]); res.json({ clientMetrics, adminMetrics, }); } catch (err) { this.logger.error('Failed to fetch RPS metrics', err); res.status(500).send('Error fetching RPS metrics'); } } } exports.default = MetricsController; //# sourceMappingURL=metrics.js.map