refresh_features.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const { db } = require('../app/src/infrastructure/mongojs')
  2. const minimist = require('minimist')
  3. const _ = require('lodash')
  4. const async = require('async')
  5. const FeaturesUpdater = require('../app/src/Features/Subscription/FeaturesUpdater')
  6. const UserFeaturesUpdater = require('../app/src/Features/Subscription/UserFeaturesUpdater')
  7. const ScriptLogger = {
  8. checkedUsersCount: 0,
  9. mismatchUsersCount: 0,
  10. allDaysSinceLastLoggedIn: [],
  11. allMismatchReasons: {},
  12. recordMismatch: (user, mismatchReasons) => {
  13. const mismatchReasonsString = JSON.stringify(mismatchReasons)
  14. if (ScriptLogger.allMismatchReasons[mismatchReasonsString]) {
  15. ScriptLogger.allMismatchReasons[mismatchReasonsString].push(user._id)
  16. } else {
  17. ScriptLogger.allMismatchReasons[mismatchReasonsString] = [user._id]
  18. }
  19. ScriptLogger.mismatchUsersCount += 1
  20. if (user.lastLoggedIn) {
  21. let daysSinceLastLoggedIn =
  22. (new Date() - user.lastLoggedIn) / 1000 / 3600 / 24
  23. ScriptLogger.allDaysSinceLastLoggedIn.push(daysSinceLastLoggedIn)
  24. }
  25. },
  26. printProgress: () => {
  27. console.warn(
  28. `Users checked: ${ScriptLogger.checkedUsersCount}. Mismatches: ${
  29. ScriptLogger.mismatchUsersCount
  30. }`
  31. )
  32. },
  33. printSummary: () => {
  34. console.log('All Mismatch Reasons:', ScriptLogger.allMismatchReasons)
  35. console.log('Mismatch Users Count', ScriptLogger.mismatchUsersCount)
  36. console.log(
  37. 'Average Last Logged In (Days):',
  38. _.sum(ScriptLogger.allDaysSinceLastLoggedIn) /
  39. ScriptLogger.allDaysSinceLastLoggedIn.length
  40. )
  41. console.log(
  42. 'Recent Logged In (Last 7 Days):',
  43. _.filter(ScriptLogger.allDaysSinceLastLoggedIn, a => a < 7).length
  44. )
  45. console.log(
  46. 'Recent Logged In (Last 30 Days):',
  47. _.filter(ScriptLogger.allDaysSinceLastLoggedIn, a => a < 30).length
  48. )
  49. }
  50. }
  51. const checkAndUpdateUser = (user, callback) =>
  52. FeaturesUpdater._computeFeatures(user._id, (error, freshFeatures) => {
  53. if (error) {
  54. return callback(error)
  55. }
  56. let mismatchReasons = FeaturesUpdater.compareFeatures(
  57. user.features,
  58. freshFeatures
  59. )
  60. if (Object.keys(mismatchReasons).length === 0) {
  61. // features are matching; nothing else to do
  62. return callback()
  63. }
  64. ScriptLogger.recordMismatch(user, mismatchReasons)
  65. if (!COMMIT) {
  66. // not saving features; nothing else to do
  67. return callback()
  68. }
  69. UserFeaturesUpdater.overrideFeatures(user._id, freshFeatures, callback)
  70. })
  71. const checkAndUpdateUsers = (users, callback) =>
  72. async.eachLimit(users, ASYNC_LIMIT, checkAndUpdateUser, callback)
  73. const loopForUsers = (skip, callback) => {
  74. db.users
  75. .find({}, { features: 1, lastLoggedIn: 1 })
  76. .sort('_id')
  77. .skip(skip)
  78. .limit(FETCH_LIMIT, (error, users) => {
  79. if (error) {
  80. return callback(error)
  81. }
  82. if (users.length === 0) {
  83. console.warn('DONE')
  84. return callback()
  85. }
  86. checkAndUpdateUsers(users, error => {
  87. if (error) {
  88. return callback(error)
  89. }
  90. ScriptLogger.checkedUsersCount += users.length
  91. retryCounter = 0
  92. ScriptLogger.printProgress()
  93. ScriptLogger.printSummary()
  94. loopForUsers(MONGO_SKIP + ScriptLogger.checkedUsersCount, callback)
  95. })
  96. })
  97. }
  98. let retryCounter = 0
  99. const run = () =>
  100. loopForUsers(MONGO_SKIP + ScriptLogger.checkedUsersCount, error => {
  101. if (error) {
  102. if (retryCounter < 3) {
  103. console.error(error)
  104. retryCounter += 1
  105. console.warn(`RETRYING IN 60 SECONDS. (${retryCounter}/3)`)
  106. return setTimeout(run, 6000)
  107. }
  108. throw error
  109. }
  110. process.exit()
  111. })
  112. let FETCH_LIMIT, ASYNC_LIMIT, COMMIT, MONGO_SKIP
  113. const setup = () => {
  114. const argv = minimist(process.argv.slice(2))
  115. FETCH_LIMIT = argv.fetch ? argv.fetch : 100
  116. ASYNC_LIMIT = argv.async ? argv.async : 10
  117. MONGO_SKIP = argv.skip ? argv.skip : 0
  118. COMMIT = argv.commit !== undefined
  119. if (!COMMIT) {
  120. console.warn('Doing dry run without --commit')
  121. }
  122. if (MONGO_SKIP) {
  123. console.warn(`Skipping first ${MONGO_SKIP} records`)
  124. }
  125. }
  126. setup()
  127. run()