Skip to content

Commit

Permalink
chore: parallelize recommendations (#17)
Browse files Browse the repository at this point in the history
* chore: parallelize

* ci: fix

* feat: v2
  • Loading branch information
tusharbansal22 authored Apr 10, 2024
1 parent 8431d21 commit ce20f4d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/commit_rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
- uses: webiny/[email protected]
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Optional, for private repositories.
allowed-commit-types: "feat,fix" # Optional, set if you want a subset of commit types to be allowed.
allowed-commit-types: "feat,fix,chore,ci" # Optional, set if you want a subset of commit types to be allowed.
71 changes: 71 additions & 0 deletions controllers/userOrderControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,77 @@ exports.getRecommendation = async (req, res, next) => {
}
};

exports.getRecommendationv2 = async (req, res, next) => {
try {
const userID = req.params.user_id;
let orders = await Order.find({ user_id: userID })
.sort({ createdAt: -1 })
.limit(5);
let recommendedItemsSet = new Set();


const defaultItems = ["Samosa", "Pav Bhaji"];
const defaultRecommendationsPromises = defaultItems.map(async (itemName) => {
let foodItemTitleCase = toTitleCase(itemName);
try {
const response = await axios.get(
`https://food-recommendation-yqpc.onrender.com/recommend/${foodItemTitleCase}`
);
console.log(response);
const recommendedRecipes = response.data.recommended_recipes;


for (let recipe of recommendedRecipes) {
const dbItem = await MenuItem.findOne({ name: recipe });
if (dbItem) {
recommendedItemsSet.add(JSON.stringify(dbItem));
}
}
} catch (e) {
console.log(e);
}
});


const ordersRecommendationsPromises = orders.map(async (order) => {
const item = order.items[0];
const itemName = item.name;
let foodItemTitleCase = toTitleCase(itemName);


try {
const response = await axios.get(
`https://food-recommendation-yqpc.onrender.com/recommend/${foodItemTitleCase}`
);
const recommendedRecipes = response.data.recommended_recipes;

for (let recipe of recommendedRecipes) {
const dbItem = await MenuItem.findOne({ name: recipe });
if (dbItem) {
recommendedItemsSet.add(JSON.stringify(dbItem));
}
}
} catch (e) {
console.log(e);
}
});


await Promise.all(ordersRecommendationsPromises);


await Promise.all(defaultRecommendationsPromises);

const recommendedItems = Array.from(recommendedItemsSet).map((item) =>
JSON.parse(item)
);

res.json({ recommendedItems });
} catch (error) {
next(error);
}
};

function toTitleCase(str) {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
Expand Down
2 changes: 2 additions & 0 deletions routes/orderRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ router.get("/events/:vendorId", listenForNewOrders);

router.get("/user/recommend/:user_id", checkAuth, getRecommendation);

router.get("/user/recommendv2/:user_id", checkAuth, getRecommendation);

router.get(
"/delivery_boy/orders/:delivery_boy_id",
checkDeliveryPartner,
Expand Down

0 comments on commit ce20f4d

Please sign in to comment.