From 30b9ba4ac778d165486b34fd836918a674b67caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Mon, 16 Dec 2019 16:25:10 +0530 Subject: [PATCH] Merge pull request #2452 from overleaf/ta-refresh-features-script-extra Improve Script To Refresh All Users' Features GitOrigin-RevId: f13fe0b01a7c26eca950ff8a008c937232769417 --- .../Subscription/UserFeaturesUpdater.js | 8 +++ services/web/scripts/refresh_features.js | 70 ++++++++++--------- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/services/web/app/src/Features/Subscription/UserFeaturesUpdater.js b/services/web/app/src/Features/Subscription/UserFeaturesUpdater.js index 8751bdc52e..6f1e41f750 100644 --- a/services/web/app/src/Features/Subscription/UserFeaturesUpdater.js +++ b/services/web/app/src/Features/Subscription/UserFeaturesUpdater.js @@ -31,5 +31,13 @@ module.exports = { (result != null ? result.nModified : undefined) === 1 ) ) + }, + + overrideFeatures(user_id, features, callback) { + const conditions = { _id: user_id } + const update = { features } + return User.update(conditions, update, (err, result) => + callback(err, (result != null ? result.nModified : undefined) === 1) + ) } } diff --git a/services/web/scripts/refresh_features.js b/services/web/scripts/refresh_features.js index dcf7f82e4e..d7f70591d4 100644 --- a/services/web/scripts/refresh_features.js +++ b/services/web/scripts/refresh_features.js @@ -6,6 +6,8 @@ const FeaturesUpdater = require('../app/src/Features/Subscription/FeaturesUpdate const UserFeaturesUpdater = require('../app/src/Features/Subscription/UserFeaturesUpdater') const getMismatchReasons = (currentFeatures, expectedFeatures) => { + currentFeatures = _.clone(currentFeatures) + expectedFeatures = _.clone(expectedFeatures) if (_.isEqual(currentFeatures, expectedFeatures)) { return {} } @@ -15,35 +17,33 @@ const getMismatchReasons = (currentFeatures, expectedFeatures) => { .sort() .forEach(key => { if (expectedFeatures[key] !== currentFeatures[key]) { - mismatchReasons[key] = currentFeatures[key] + mismatchReasons[key] = expectedFeatures[key] } }) - return mismatchReasons -} - -const normalizeMismatchReasons = mismatchReasons => { - if ( - mismatchReasons.collaborators > 1 && - mismatchReasons.collaborators < 10 && - mismatchReasons.collaborators !== 6 - ) { - mismatchReasons.collaborators = 10 - } - - if (mismatchReasons.collaborators > 10) { - mismatchReasons.collaborators = -1 - } - if (mismatchReasons.compileTimeout) { - mismatchReasons.compileTimeout = 240 + // store the compile timeout difference instead of the new compile timeout + mismatchReasons.compileTimeout = + expectedFeatures.compileTimeout - currentFeatures.compileTimeout + } + + if (mismatchReasons.collaborators) { + // store the collaborators difference instead of the new number only + // replace -1 by 100 to make it clearer + if (expectedFeatures.collaborators === -1) { + expectedFeatures.collaborators = 100 + } + if (currentFeatures.collaborators === -1) { + currentFeatures.collaborators = 100 + } + mismatchReasons.collaborators = + expectedFeatures.collaborators - currentFeatures.collaborators } return mismatchReasons } const recordMismatch = (user, mismatchReasons) => { - mismatchReasons = normalizeMismatchReasons(mismatchReasons) const mismatchReasonsString = JSON.stringify(mismatchReasons) if (allMismatchReasons[mismatchReasonsString]) { allMismatchReasons[mismatchReasonsString] += 1 @@ -79,7 +79,7 @@ const checkAndUpdateUser = (user, callback) => return callback() } - UserFeaturesUpdater.updateFeatures(user._id, freshFeatures, callback) + UserFeaturesUpdater.overrideFeatures(user._id, freshFeatures, callback) }) const updateUsers = (users, callback) => @@ -122,6 +122,22 @@ const loopForUsers = (lastUserId, callback) => { }) } +const printSummary = () => { + console.log({ allMismatchReasons }) + console.log( + 'Average Last Logged In (Days):', + _.sum(allDaysSinceLastLoggedIn) / allDaysSinceLastLoggedIn.length + ) + console.log( + 'Recent Logged In (Last 7 Days):', + _.filter(allDaysSinceLastLoggedIn, a => a < 7).length + ) + console.log( + 'Recent Logged In (Last 30 Days):', + _.filter(allDaysSinceLastLoggedIn, a => a < 30).length + ) +} + let checkedUsersCount = 0 let mismatchUsersCount = 0 let allDaysSinceLastLoggedIn = [] @@ -131,19 +147,7 @@ const run = () => if (error) { throw error } - console.log({ allMismatchReasons }) - console.log( - 'Average Last Logged In (Days):', - _.sum(allDaysSinceLastLoggedIn) / allDaysSinceLastLoggedIn.length - ) - console.log( - 'Recent Logged In (Last 7 Days):', - _.filter(allDaysSinceLastLoggedIn, a => a < 7).length - ) - console.log( - 'Recent Logged In (Last 30 Days):', - _.filter(allDaysSinceLastLoggedIn, a => a < 30).length - ) + printSummary() process.exit() })