Skip to content

Commit

Permalink
Added stats route for internal statistics and status
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrea committed Jul 10, 2024
1 parent 55a84cb commit 990fea7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/controllers/v4/internal/stats.js
Original file line number Diff line number Diff line change
@@ -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 };
16 changes: 16 additions & 0 deletions src/routes/v4/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions src/routes/v4/internal/stats.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 990fea7

Please sign in to comment.