From ce20f4d3f2b86f20f4ced2797163886793504a29 Mon Sep 17 00:00:00 2001 From: Tushar Bansal Date: Wed, 10 Apr 2024 07:00:37 +0530 Subject: [PATCH] chore: parallelize recommendations (#17) * chore: parallelize * ci: fix * feat: v2 --- .github/workflows/commit_rules.yml | 2 +- controllers/userOrderControl.js | 71 ++++++++++++++++++++++++++++++ routes/orderRoutes.js | 2 + 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/.github/workflows/commit_rules.yml b/.github/workflows/commit_rules.yml index 5ccdbcc..ecc4933 100644 --- a/.github/workflows/commit_rules.yml +++ b/.github/workflows/commit_rules.yml @@ -14,4 +14,4 @@ jobs: - uses: webiny/action-conventional-commits@v1.3.0 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. \ No newline at end of file + allowed-commit-types: "feat,fix,chore,ci" # Optional, set if you want a subset of commit types to be allowed. \ No newline at end of file diff --git a/controllers/userOrderControl.js b/controllers/userOrderControl.js index 71b457e..b07dfc2 100644 --- a/controllers/userOrderControl.js +++ b/controllers/userOrderControl.js @@ -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(); diff --git a/routes/orderRoutes.js b/routes/orderRoutes.js index 7fd2cc4..0e69c19 100644 --- a/routes/orderRoutes.js +++ b/routes/orderRoutes.js @@ -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,