Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staging #61

Merged
merged 8 commits into from
Aug 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added stats route for internal statistics and status
kyrea committed Jul 10, 2024
commit 990fea7bd0da5fbb4c296bda378851da3b973d97
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
@@ -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.
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;