statistics.js 2.7 KB

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