Skip to content

Commit

Permalink
ensure that logging is consistent with the new group-specific prefere…
Browse files Browse the repository at this point in the history
…nce management
  • Loading branch information
Baalmart committed Dec 14, 2024
1 parent 331742e commit b907f64
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/auth-service/bin/jobs/preferences-log-job.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const cron = require("node-cron");
const mongoose = require("mongoose");
const UserModel = require("@models/User");
const PreferenceModel = require("@models/Preference");
const constants = require("@config/constants");
Expand All @@ -13,25 +14,41 @@ const logUserPreferences = async () => {
try {
const batchSize = 100;
let skip = 0;
let totalCountWithoutSelectedSites = 0; // To keep track of total count
let totalUsersProcessed = 0; // To keep track of total users processed
let totalCountWithoutSelectedSites = 0;
let totalUsersProcessed = 0;

// Use the default group ID
const defaultGroupId = mongoose.Types.ObjectId(constants.DEFAULT_GROUP);

while (true) {
// Fetch users with their group_roles
const users = await UserModel("airqo")
.find()
.limit(batchSize)
.skip(skip)
.select("_id email")
.select("_id email group_roles")
.lean();

if (users.length === 0) {
break;
}

// Fetch existing preferences for users in batch
const userIds = users.map((user) => user._id);
// Filter users who are members of the default group
const validUsers = users.filter(
(user) =>
user.group_roles &&
user.group_roles.some(
(role) => role.group.toString() === defaultGroupId.toString()
)
);

// Fetch existing preferences for valid users in the default group
const userIds = validUsers.map((user) => user._id);
const preferences = await PreferenceModel("airqo")
.find({ user_id: { $in: userIds } })
.find({
user_id: { $in: userIds },
group_id: defaultGroupId,
})
.select("_id user_id selected_sites")
.lean();

Expand All @@ -40,31 +57,33 @@ const logUserPreferences = async () => {
preferencesMap.set(pref.user_id.toString(), pref);
});

// Collect IDs of users without selected_sites
const usersWithoutSelectedSites = users.filter((user) => {
// Collect IDs of users without selected_sites in the default group
const usersWithoutSelectedSites = validUsers.filter((user) => {
const preference = preferencesMap.get(user._id.toString());
return !preference || isEmpty(preference.selected_sites);
});

// Aggregate results
totalCountWithoutSelectedSites += usersWithoutSelectedSites.length;
totalUsersProcessed += users.length; // Increment total processed users
totalUsersProcessed += validUsers.length;

skip += batchSize;
}

// Log the aggregated results once after processing all users
if (totalUsersProcessed > 0 && totalCountWithoutSelectedSites > 0) {
if (totalUsersProcessed > 0) {
const percentageWithoutSelectedSites = (
(totalCountWithoutSelectedSites / totalUsersProcessed) *
100
).toFixed(2);

logger.info(
`💔💔 Total count of users without any Customised Locations: ${totalCountWithoutSelectedSites}, which is ${percentageWithoutSelectedSites}% of all Analytics users.`
`💔💔 Total count of users without Customised Locations in the default group: ${totalCountWithoutSelectedSites}, which is ${percentageWithoutSelectedSites}% of processed users.`
);
} else {
logger.info(`😎🎉✅ All Analytics users have Customised Locations.`);
logger.info(
`😎🎉✅ No users processed or all users have Customised Locations.`
);
}
} catch (error) {
logger.error(`🐛🐛 Error in logUserPreferences: ${stringify(error)}`);
Expand All @@ -76,3 +95,5 @@ cron.schedule(schedule, logUserPreferences, {
scheduled: true,
timezone: "Africa/Nairobi",
});

module.exports = { logUserPreferences };

0 comments on commit b907f64

Please sign in to comment.