-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added stats route for internal statistics and status
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |