statistics.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:healthcheck'); // eslint-disable-line no-unused-vars
  3. const express = require('express');
  4. const router = express.Router();
  5. const helmet = require('helmet');
  6. const util = require('util');
  7. const USER_STATUS_MASTER = {
  8. 1: 'registered',
  9. 2: 'active',
  10. 3: 'suspended',
  11. 4: 'deleted',
  12. 5: 'invited',
  13. };
  14. /**
  15. * @swagger
  16. * tags:
  17. * name: Statistics
  18. */
  19. module.exports = (crowi) => {
  20. const models = crowi.models;
  21. const User = models.User;
  22. const getUserStatistics = async() => {
  23. const userCountGroupByStatus = await User.aggregate().group({
  24. _id: '$status',
  25. totalCount: { $sum: 1 },
  26. });
  27. // Initialize userCountResults with 0
  28. const userCountResults = {};
  29. Object.values(USER_STATUS_MASTER).forEach((status) => {
  30. userCountResults[status] = 0;
  31. });
  32. userCountGroupByStatus.forEach((userCount) => {
  33. const key = USER_STATUS_MASTER[userCount._id];
  34. userCountResults[key] = userCount.totalCount;
  35. });
  36. const activeUserCount = userCountResults.active;
  37. // Use userCountResults for inactive users, so delete unnecessary active
  38. delete userCountResults.active;
  39. // Calculate the total number of inactive users
  40. const inactiveUserTotal = userCountResults.invited + userCountResults.deleted + userCountResults.suspended + userCountResults.registered;
  41. // Get admin users
  42. const findAdmins = util.promisify(User.findAdmins).bind(User);
  43. const adminUsers = await findAdmins();
  44. return {
  45. total: activeUserCount + inactiveUserTotal,
  46. active: {
  47. total: activeUserCount,
  48. admin: adminUsers.length,
  49. },
  50. inactive: {
  51. total: inactiveUserTotal,
  52. ...userCountResults,
  53. },
  54. };
  55. };
  56. const getUserStatisticsForNotLoggedIn = async() => {
  57. const data = await getUserStatistics();
  58. delete data.active.admin;
  59. delete data.inactive.invited;
  60. delete data.inactive.deleted;
  61. delete data.inactive.suspended;
  62. delete data.inactive.registered;
  63. return data;
  64. };
  65. /**
  66. * @swagger
  67. *
  68. * /_api/v3/statistics/user:
  69. * get:
  70. * tags: [Statistics, apiv3]
  71. * operationId: getStatisticsUser
  72. * summary: /_api/v3/statistics/user
  73. * description: Get statistics for user
  74. * responses:
  75. * 200:
  76. * description: Statistics for user
  77. * content:
  78. * application/json:
  79. * schema:
  80. * properties:
  81. * data:
  82. * type: object
  83. * description: Statistics for all user
  84. */
  85. router.get('/user', helmet.noCache(), async(req, res) => {
  86. const data = req.user == null ? await getUserStatisticsForNotLoggedIn() : await getUserStatistics();
  87. res.status(200).send({ data });
  88. });
  89. return router;
  90. };