{description}
+ +{stat}
+Loading...
; + } + + //total orders across all users in restaurant history + const calculateTotalOrders = () => { + if (!restaurantData || !restaurantData.history) { + return 0; + } + + return restaurantData.history.reduce( + (total, history) => total + (history.userHistory ? history.userHistory.length : 0), + 0 + ); + }; + + // get total revenue of restaurant based on user order history + const calculateTotalSales = () => { + if (!restaurantData || !restaurantData.history) { + return 0; + } + + return restaurantData.history.reduce((totalSales, history) => { + if (history.userHistory) { + history.userHistory.forEach(order => { + totalSales += order.price || 0; + }); + } + return totalSales; + }, 0); + + } + + + + //get the count of each menu item to display on pie chart to visualize most popular items + const calculateMenuItemCounts = () => { + if (!restaurantData || !restaurantData.history) { + return []; + } + + //keep count in dictionary here of all items + const itemCounts = {}; + + //go through restaurant history and each user's history + restaurantData.history.forEach((history) => { + //check if user history exists + if (history.userHistory) { + //go through each order in history + history.userHistory.forEach((order) => { + // get the item id + const menuItemId = order.menuItemId; + // find the item in list that matches id + const menuItem = restaurantData.restaurantMenu.menuList.find((item) => item.menuId === menuItemId); + + //if it exists, update count of item to keep track of total num orders of each item + if (menuItem) { + const menuItemName = menuItem.name; + itemCounts[menuItemId] = { + count: (itemCounts[menuItemId]?.count || 0) + 1, + name: menuItemName, + }; + } + }); + } + }); + + // Turn the itemCounts object to an array, this is for the pie chart data visualization + return Object.keys(itemCounts).map((menuItemId) => ({ + menuItemId, + name: itemCounts[menuItemId].name, + count: itemCounts[menuItemId].count, + })); + }; + + + //get the most popular times based on when users come to place order + const calculatePopularTimes = () => { + if (!restaurantData || !restaurantData.history) { + return []; + } + + //get count of users at a time + const userCounts = {}; + + // go through history + restaurantData.history.forEach((history) => { + if (history.userHistory) { + //go through user history + history.userHistory.forEach((order) => { + //check when order was created + if (order.createdAt) { + //get the hour of the order time + const hour = new Date(order.createdAt).getHours(); + // + const timeKey = `${hour}:00`; + + // if there is no existing user at that time, create set + if (!userCounts[timeKey]) { + userCounts[timeKey] = new Set(); + } + + //update the list of users at that time + userCounts[timeKey].add(history.userId); + } + }); + } + }); + + // Convert the userCounts object to an array of objects, used for the bar chart + return Object.keys(userCounts).map((timeKey) => ({ + hour: timeKey, + count: userCounts[timeKey].size, + })); + + }; + + //Get total unique users + const calculateTotalCustomers = () => { + if (!restaurantData || !restaurantData.history) { + return 0; + } + + //set for keeping unique users id + const uniqueUserIds = new Set(); + + //go through history + restaurantData.history.forEach((history) => { + // check if user exists, then add to set + if (history.userId) { + uniqueUserIds.add(history.userId); + } + }); + + //return the size to show how many users there are + return uniqueUserIds.size; + }; + + + //get the min and max range of menu item price + const calculatePriceRange = (menuList) => { + if (!menuList || menuList.length === 0) { + return null; + } + + //map each item in the menuList to a price + const prices = menuList.map(item => item.price); + + // Find the minimum and maximum prices + const minPrice = Math.min(...prices); + const maxPrice = Math.max(...prices); + + + return { minPrice, maxPrice }; + }; + + //get the price range based on restaurants menu list + const priceRange = calculatePriceRange(restaurantData.restaurantMenu.menuList); + + + //for popular items + const menuItemCounts = calculateMenuItemCounts(); + + //count the orders + const totalOrders = calculateTotalOrders(); + + //total revenue from items only + const totalSales = calculateTotalSales().toLocaleString(); + + //count users from history + const totalCustomers = calculateTotalCustomers(); + + //get avg spending of custoemr total + const avgCustomerSpending = totalCustomers > 0 ? (totalSales / totalCustomers).toFixed(2) : 0; + + //get times that people come in on based on order time + const popularTimesData = calculatePopularTimes(); + const sortedPopularTimesData = popularTimesData.sort((a, b) => b.count - a.count); + + return ( +{restaurantData.restaurantName}
+ +Popular Menu Items
+{`Menu Item: ${payload[0]?.payload.name}`}
+{`Total Orders: ${payload[0]?.payload.count}`}
+Most to Least Popular Times
+