Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move to production #4081

Merged
merged 10 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion k8s/analytics/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ images:
celeryWorker: eu.gcr.io/airqo-250220/airqo-analytics-celery-worker
reportJob: eu.gcr.io/airqo-250220/airqo-analytics-report-job
devicesSummaryJob: eu.gcr.io/airqo-250220/airqo-analytics-devices-summary-job
tag: prod-8513ca91-1734151901
tag: prod-1f1d50ac-1734167898
api:
name: airqo-analytics-api
label: analytics-api
Expand Down
2 changes: 1 addition & 1 deletion k8s/auth-service/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ app:
replicaCount: 3
image:
repository: eu.gcr.io/airqo-250220/airqo-auth-api
tag: prod-8513ca91-1734151901
tag: prod-1f1d50ac-1734167898
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
2 changes: 1 addition & 1 deletion k8s/auth-service/values-stage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ app:
replicaCount: 2
image:
repository: eu.gcr.io/airqo-250220/airqo-stage-auth-api
tag: stage-ff5c18e5-1734151846
tag: stage-331742e7-1734167859
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
2 changes: 1 addition & 1 deletion k8s/exceedance/values-prod-airqo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ app:
configmap: env-exceedance-production
image:
repository: eu.gcr.io/airqo-250220/airqo-exceedance-job
tag: prod-8513ca91-1734151901
tag: prod-1f1d50ac-1734167898
nameOverride: ''
fullnameOverride: ''
2 changes: 1 addition & 1 deletion k8s/exceedance/values-prod-kcca.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ app:
configmap: env-exceedance-production
image:
repository: eu.gcr.io/airqo-250220/kcca-exceedance-job
tag: prod-8513ca91-1734151901
tag: prod-1f1d50ac-1734167898
nameOverride: ''
fullnameOverride: ''
2 changes: 1 addition & 1 deletion k8s/predict/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ images:
predictJob: eu.gcr.io/airqo-250220/airqo-predict-job
trainJob: eu.gcr.io/airqo-250220/airqo-train-job
predictPlaces: eu.gcr.io/airqo-250220/airqo-predict-places-air-quality
tag: prod-8513ca91-1734151901
tag: prod-1f1d50ac-1734167898
api:
name: airqo-prediction-api
label: prediction-api
Expand Down
2 changes: 1 addition & 1 deletion k8s/spatial/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ app:
replicaCount: 3
image:
repository: eu.gcr.io/airqo-250220/airqo-spatial-api
tag: prod-8513ca91-1734151901
tag: prod-1f1d50ac-1734167898
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
2 changes: 1 addition & 1 deletion k8s/website/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ app:
replicaCount: 3
image:
repository: eu.gcr.io/airqo-250220/airqo-website-api
tag: prod-8513ca91-1734151901
tag: prod-1f1d50ac-1734167898
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
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 };
Loading