diff --git a/src/controllers/v4/internal/stats.js b/src/controllers/v4/internal/stats.js new file mode 100644 index 0000000..0397357 --- /dev/null +++ b/src/controllers/v4/internal/stats.js @@ -0,0 +1,33 @@ +import createError from 'http-errors'; +import Stats from '../../../models/schemas/Stat.js'; + +// Get Internal Status or statistics +const getStats = async (req, res, next) => { + const key = req.headers.key; + // Check for valid access key in headers + if (!key || key !== process.env.ACCESS_KEY) { + return res.status(401).json({ + message: 'Unauthorized', + }); + } + try { + const [result] = await Stats.aggregate([ + // Select a random document from the results + { $sample: { size: 1 } }, + { $project: { __v: 0, _id: 0 } }, + ]); + + if (!result) { + return next(createError(404, 'Could not find any Stats')); + } + + res.status(200).json(result); + + await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { stats: 1 } }); + } catch (error) { + await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { failed_requests: 1 } }); + return next(error); + } +}; + +export { getStats }; diff --git a/src/routes/v4/index.js b/src/routes/v4/index.js index f79c929..d67da16 100644 --- a/src/routes/v4/index.js +++ b/src/routes/v4/index.js @@ -1192,6 +1192,22 @@ import yesRoutes from './interactions/yes.js'; */ router.use('/yes', yesRoutes); +import statsRoutes from './internal/stats.js'; + +/** + * @api {use} v4/stats Use Stats Routes + * @apiDescription Mount the stats-related routes for handling interactions. + * @apiName UseStatsRoutes + * @apiGroup Routes + * + * @apiSuccess {Object} routes Stats-related routes mounted on the parent router. + * + * @function createStatsRoutes + * @description Creates and returns a set of routes for handling interactions related to Stats. + * @returns {Object} Stats-related routes. + */ +router.use('/stats', statsRoutes); + /** * Exporting the router for use in other parts of the application. * @exports {Router} router - Express Router instance with mounted routes. diff --git a/src/routes/v4/internal/stats.js b/src/routes/v4/internal/stats.js new file mode 100644 index 0000000..025f9f6 --- /dev/null +++ b/src/routes/v4/internal/stats.js @@ -0,0 +1,28 @@ +import { Router } from 'express'; +import { getStats } from '../../../controllers/v4/internal/stats.js'; +import createRateLimiter from '../../../middlewares/rateLimit.js'; + +const router = Router(); + +router + .route('/') + /** + * @api {post} v4/stats Get Statistics + * @apiDescription Get statistics about the system usage. + * @apiName getStats + * @apiGroup Statistics + * @apiPermission user + * + * @apiHeader {String} Authorization System access token. + * + * @apiSuccess {Object} stats System statistics or status. + * + * @apiError (Unauthorized 401) Unauthorized Only authenticated users can access the data. + * @apiError (Forbidden 403) Forbidden Only authorized users can access the data. + * @apiError (Too Many Requests 429) TooManyRequests The client has exceeded the allowed number of requests within the time window. + * @apiError (Internal Server Error 500) InternalServerError An error occurred while processing the request. + */ + .get(createRateLimiter(), getStats); + +// Export the router +export default router;