2024-10-09 04:50:59 -04:00
|
|
|
import { db, waitForDb } from '../app/src/infrastructure/mongodb.js'
|
|
|
|
import minimist from 'minimist'
|
|
|
|
import _ from 'lodash'
|
|
|
|
import async from 'async'
|
|
|
|
import FeaturesUpdater from '../app/src/Features/Subscription/FeaturesUpdater.js'
|
|
|
|
import FeaturesHelper from '../app/src/Features/Subscription/FeaturesHelper.js'
|
|
|
|
import UserFeaturesUpdater from '../app/src/Features/Subscription/UserFeaturesUpdater.js'
|
|
|
|
import AnalyticsManager from '../app/src/Features/Analytics/AnalyticsManager.js'
|
2024-10-18 07:04:57 -04:00
|
|
|
import DropboxHandler from '../modules/dropbox/app/src/DropboxHandler.mjs'
|
2024-10-09 04:50:59 -04:00
|
|
|
import { OError } from '../app/src/Features/Errors/Errors.js'
|
2019-12-05 08:57:58 -05:00
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
const ScriptLogger = {
|
|
|
|
checkedUsersCount: 0,
|
|
|
|
mismatchUsersCount: 0,
|
|
|
|
allDaysSinceLastLoggedIn: [],
|
|
|
|
allMismatchReasons: {},
|
|
|
|
|
|
|
|
recordMismatch: (user, mismatchReasons) => {
|
|
|
|
const mismatchReasonsString = JSON.stringify(mismatchReasons)
|
|
|
|
if (ScriptLogger.allMismatchReasons[mismatchReasonsString]) {
|
|
|
|
ScriptLogger.allMismatchReasons[mismatchReasonsString].push(user._id)
|
|
|
|
} else {
|
|
|
|
ScriptLogger.allMismatchReasons[mismatchReasonsString] = [user._id]
|
2019-12-16 05:55:10 -05:00
|
|
|
}
|
2019-12-05 08:57:58 -05:00
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
ScriptLogger.mismatchUsersCount += 1
|
2019-12-05 08:57:58 -05:00
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
if (user.lastLoggedIn) {
|
2021-05-05 09:05:04 -04:00
|
|
|
const daysSinceLastLoggedIn =
|
2020-01-07 06:03:14 -05:00
|
|
|
(new Date() - user.lastLoggedIn) / 1000 / 3600 / 24
|
|
|
|
ScriptLogger.allDaysSinceLastLoggedIn.push(daysSinceLastLoggedIn)
|
|
|
|
}
|
|
|
|
},
|
2019-12-05 08:57:58 -05:00
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
printProgress: () => {
|
|
|
|
console.warn(
|
2020-12-15 05:23:54 -05:00
|
|
|
`Users checked: ${ScriptLogger.checkedUsersCount}. Mismatches: ${ScriptLogger.mismatchUsersCount}`
|
2020-01-07 06:03:14 -05:00
|
|
|
)
|
|
|
|
},
|
2019-12-05 08:57:58 -05:00
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
printSummary: () => {
|
|
|
|
console.log('All Mismatch Reasons:', ScriptLogger.allMismatchReasons)
|
|
|
|
console.log('Mismatch Users Count', ScriptLogger.mismatchUsersCount)
|
|
|
|
console.log(
|
|
|
|
'Average Last Logged In (Days):',
|
|
|
|
_.sum(ScriptLogger.allDaysSinceLastLoggedIn) /
|
|
|
|
ScriptLogger.allDaysSinceLastLoggedIn.length
|
|
|
|
)
|
|
|
|
console.log(
|
|
|
|
'Recent Logged In (Last 7 Days):',
|
|
|
|
_.filter(ScriptLogger.allDaysSinceLastLoggedIn, a => a < 7).length
|
|
|
|
)
|
|
|
|
console.log(
|
|
|
|
'Recent Logged In (Last 30 Days):',
|
|
|
|
_.filter(ScriptLogger.allDaysSinceLastLoggedIn, a => a < 30).length
|
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-12-05 08:57:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const checkAndUpdateUser = (user, callback) =>
|
2021-09-20 08:20:14 -04:00
|
|
|
FeaturesUpdater.computeFeatures(user._id, (error, freshFeatures) => {
|
2019-12-05 08:57:58 -05:00
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
2021-09-20 08:20:14 -04:00
|
|
|
const mismatchReasons = FeaturesHelper.compareFeatures(
|
2020-01-07 06:03:14 -05:00
|
|
|
user.features,
|
|
|
|
freshFeatures
|
|
|
|
)
|
2019-12-10 03:10:14 -05:00
|
|
|
if (Object.keys(mismatchReasons).length === 0) {
|
2019-12-05 08:57:58 -05:00
|
|
|
// features are matching; nothing else to do
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
ScriptLogger.recordMismatch(user, mismatchReasons)
|
2019-12-05 08:57:58 -05:00
|
|
|
|
|
|
|
if (!COMMIT) {
|
|
|
|
// not saving features; nothing else to do
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2022-05-26 05:31:06 -04:00
|
|
|
const matchedFeatureSet = FeaturesHelper.getMatchedFeatureSet(freshFeatures)
|
2024-05-21 15:07:24 -04:00
|
|
|
AnalyticsManager.setUserPropertyForUserInBackground(
|
2022-05-26 05:31:06 -04:00
|
|
|
user._id,
|
|
|
|
'feature-set',
|
|
|
|
matchedFeatureSet
|
|
|
|
)
|
|
|
|
|
|
|
|
UserFeaturesUpdater.overrideFeatures(
|
|
|
|
user._id,
|
|
|
|
freshFeatures,
|
|
|
|
(error, featuresChanged) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
mismatchReasons.dropbox !== undefined &&
|
|
|
|
freshFeatures.dropbox === false
|
|
|
|
) {
|
|
|
|
DropboxHandler.unlinkAccount(
|
|
|
|
user._id,
|
|
|
|
{ sendEmail: false },
|
|
|
|
error => {
|
|
|
|
if (error) {
|
|
|
|
return callback(
|
|
|
|
OError.tag(error, 'error unlinking dropbox', {
|
|
|
|
userId: user._id,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2024-10-09 04:50:59 -04:00
|
|
|
console.log({ userId: user._id }, 'Unlinked dropbox')
|
2022-05-26 05:31:06 -04:00
|
|
|
callback(null, featuresChanged)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
callback(null, featuresChanged)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2019-12-05 08:57:58 -05:00
|
|
|
})
|
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
const checkAndUpdateUsers = (users, callback) =>
|
|
|
|
async.eachLimit(users, ASYNC_LIMIT, checkAndUpdateUser, callback)
|
2019-12-05 08:57:58 -05:00
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
const loopForUsers = (skip, callback) => {
|
2019-12-05 08:57:58 -05:00
|
|
|
db.users
|
2020-10-07 09:17:49 -04:00
|
|
|
.find({})
|
|
|
|
.project({ features: 1, lastLoggedIn: 1 })
|
2021-05-13 06:25:29 -04:00
|
|
|
.sort({ _id: 1 })
|
2020-01-07 06:03:14 -05:00
|
|
|
.skip(skip)
|
2020-10-07 09:17:49 -04:00
|
|
|
.limit(FETCH_LIMIT)
|
|
|
|
.toArray((error, users) => {
|
2019-12-05 08:57:58 -05:00
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (users.length === 0) {
|
2020-01-07 06:03:14 -05:00
|
|
|
console.warn('DONE')
|
2019-12-05 08:57:58 -05:00
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
checkAndUpdateUsers(users, error => {
|
2019-12-05 08:57:58 -05:00
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2020-01-07 06:03:14 -05:00
|
|
|
ScriptLogger.checkedUsersCount += users.length
|
|
|
|
retryCounter = 0
|
|
|
|
ScriptLogger.printProgress()
|
|
|
|
ScriptLogger.printSummary()
|
|
|
|
loopForUsers(MONGO_SKIP + ScriptLogger.checkedUsersCount, callback)
|
2019-12-05 08:57:58 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
let retryCounter = 0
|
2019-12-05 08:57:58 -05:00
|
|
|
const run = () =>
|
2020-01-07 06:03:14 -05:00
|
|
|
loopForUsers(MONGO_SKIP + ScriptLogger.checkedUsersCount, error => {
|
2019-12-05 08:57:58 -05:00
|
|
|
if (error) {
|
2020-01-07 06:03:14 -05:00
|
|
|
if (retryCounter < 3) {
|
|
|
|
console.error(error)
|
|
|
|
retryCounter += 1
|
|
|
|
console.warn(`RETRYING IN 60 SECONDS. (${retryCounter}/3)`)
|
|
|
|
return setTimeout(run, 6000)
|
|
|
|
}
|
2019-12-05 08:57:58 -05:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
let FETCH_LIMIT, ASYNC_LIMIT, COMMIT, MONGO_SKIP
|
2019-12-05 08:57:58 -05:00
|
|
|
const setup = () => {
|
|
|
|
const argv = minimist(process.argv.slice(2))
|
|
|
|
FETCH_LIMIT = argv.fetch ? argv.fetch : 100
|
|
|
|
ASYNC_LIMIT = argv.async ? argv.async : 10
|
2020-01-07 06:03:14 -05:00
|
|
|
MONGO_SKIP = argv.skip ? argv.skip : 0
|
2019-12-05 08:57:58 -05:00
|
|
|
COMMIT = argv.commit !== undefined
|
2022-03-16 05:24:01 -04:00
|
|
|
const FORCE = argv.force !== undefined
|
|
|
|
if (!FORCE) {
|
|
|
|
console.log(
|
|
|
|
'NOTE: features can be automatically refreshed on login (using `featuresEpoch`)\n' +
|
|
|
|
'Consider incrementing settings.featuresEpoch instead of running this script.\n' +
|
2024-10-09 04:50:59 -04:00
|
|
|
'If you really need to run this script, use refresh_features.mjs --force.'
|
2022-03-16 05:24:01 -04:00
|
|
|
)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2019-12-05 08:57:58 -05:00
|
|
|
if (!COMMIT) {
|
2020-01-07 06:03:14 -05:00
|
|
|
console.warn('Doing dry run without --commit')
|
|
|
|
}
|
|
|
|
if (MONGO_SKIP) {
|
|
|
|
console.warn(`Skipping first ${MONGO_SKIP} records`)
|
2019-12-05 08:57:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-09 04:50:59 -04:00
|
|
|
await waitForDb()
|
|
|
|
setup()
|
|
|
|
run()
|