analytics2.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. let analytics = {
  2. isPopulated: false,
  3. recipesDisplay: false,
  4. transactionsByDate: [],
  5. display: async function(Transaction){
  6. if(!this.isPopulated){
  7. let to = new Date()
  8. let from = new Date(to.getFullYear(), to.getMonth() - 1, to.getDate());
  9. await this.getData(from, to, Transaction);
  10. let analSlider = document.getElementById("analSlider");
  11. analSlider.onclick = ()=>{this.switchDisplay()};
  12. analSlider.checked = false;
  13. document.getElementById("analRecipeContent").style.display = "none";
  14. this.populateButtons();
  15. this.displayIngredient(merchant.ingredients[0].ingredient);
  16. this.displayRecipe(merchant.recipes[0]);
  17. this.isPopulated = true;
  18. }
  19. },
  20. populateButtons: function(){
  21. let ingredientButtons = document.getElementById("analIngredientList");
  22. let recipeButtons = document.getElementById("analRecipeList");
  23. for(let i = 0; i < merchant.ingredients.length; i++){
  24. let button = document.createElement("button");
  25. button.innerText = merchant.ingredients[i].ingredient.name;
  26. button.classList.add("choosable");
  27. button.onclick = ()=>{this.displayIngredient(merchant.ingredients[i].ingredient)};
  28. ingredientButtons.appendChild(button);
  29. }
  30. for(let i = 0; i < merchant.recipes.length; i++){
  31. let button = document.createElement("button");
  32. button.innerText = merchant.recipes[i].name;
  33. button.classList.add("choosable");
  34. button.onclick = ()=>{this.displayRecipe(merchant.recipes[i])};
  35. recipeButtons.appendChild(button);
  36. }
  37. },
  38. getData: function(from, to, Transaction){
  39. let loader = document.getElementById("loaderContainer");
  40. loader.style.display = "flex";
  41. return fetch(`/transactions/${from.toISOString()}/${to.toISOString()}`)
  42. .then(response => response.json())
  43. .then((response)=>{
  44. if(typeof(response) === "string"){
  45. banner.createError(response);
  46. }else{
  47. this.transactionsByDate = [];
  48. for(let i = 0; i < response.length; i++){
  49. const date = new Date(response[i].date);
  50. let newDate = {
  51. date: date,
  52. transactions: []
  53. };
  54. for(let j = 0; j < response[i].transactions.length; j++){
  55. newDate.transactions.push(new Transaction(
  56. response[i].transactions[j]._id,
  57. date,
  58. response[i].transactions[j].recipes,
  59. merchant
  60. ));
  61. }
  62. this.transactionsByDate.push(newDate);
  63. }
  64. }
  65. })
  66. .catch((err)=>{
  67. banner.createError("UNABLE TO UPDATE THE PAGE");
  68. })
  69. .finally(()=>{
  70. loader.style.display = "none";
  71. });
  72. },
  73. displayIngredient: function(ingredient){
  74. //break down data into dates and quantities
  75. let dates = [];
  76. let quantities = [];
  77. for(let i = 0; i < this.transactionsByDate.length; i++){
  78. dates.push(this.transactionsByDate[i].date);
  79. let sum = 0;
  80. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  81. let transactions = this.transactionsByDate[i].transactions[j];
  82. sum += transactions.getIngredientQuantity(ingredient);
  83. }
  84. quantities.push(sum);
  85. }
  86. //create and display the graph
  87. let trace = {
  88. x: dates,
  89. y: quantities,
  90. mode: "lines+markers",
  91. line: {
  92. color: "rgb(255, 99, 107)"
  93. }
  94. }
  95. const layout = {
  96. title: ingredient.name.toUpperCase(),
  97. xaxis: {title: "DATE"},
  98. yaxis: {title: `QUANTITY (${ingredient.unit.toUpperCase()})`}
  99. }
  100. Plotly.newPlot("itemUseGraph", [trace], layout);
  101. //Create min/max/avg
  102. let min = quantities[0];
  103. let max = quantities[0];
  104. let sum = 0;
  105. for(let i = 0; i < quantities.length; i++){
  106. if(quantities[i] < min){
  107. min = quantities[i];
  108. }
  109. if(quantities[i] > max){
  110. max = quantities[i];
  111. }
  112. sum += quantities[i];
  113. }
  114. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${ingredient.unit.toUpperCase()}`;
  115. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${ingredient.unit.toUpperCase()}`;
  116. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${ingredient.unit.toUpperCase()}`;
  117. //Create weekday averages
  118. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  119. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  120. for(let i = 0; i < quantities.length; i++){
  121. dayUse[dates[i].getDay()] += quantities[i];
  122. dayCount[dates[i].getDay()]++;
  123. }
  124. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${ingredient.unit.toUpperCase()}`;
  125. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${ingredient.unit.toUpperCase()}`;
  126. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${ingredient.unit.toUpperCase()}`;
  127. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${ingredient.unit.toUpperCase()}`;
  128. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${ingredient.unit.toUpperCase()}`;
  129. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${ingredient.unit.toUpperCase()}`;
  130. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${ingredient.unit.toUpperCase()}`;
  131. },
  132. displayRecipe: function(recipe){
  133. //break down data into dates and quantities
  134. let dates = [];
  135. let quantities = [];
  136. for(let i = 0; i < this.transactionsByDate.length; i++){
  137. dates.push(this.transactionsByDate[i].date);
  138. let sum = 0;
  139. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  140. const transaction = this.transactionsByDate[i].transactions[j];
  141. for(let k = 0; k < transaction.recipes.length; k++){
  142. if(transaction.recipes[k].recipe === recipe){
  143. sum += transaction.recipes[k].quantity;
  144. }
  145. }
  146. }
  147. quantities.push(sum);
  148. }
  149. //create and display the graph
  150. const trace = {
  151. x: dates,
  152. y: quantities,
  153. mode: "lines+markers",
  154. line: {
  155. color: "rgb(255, 99, 107)"
  156. }
  157. }
  158. const layout = {
  159. title: recipe.name.toUpperCase(),
  160. xaxis: {title: "DATE"},
  161. yaxis: {title: "QUANTITY"}
  162. }
  163. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  164. //Display the boxes at the bottom
  165. let avg = 0;
  166. for(let i = 0; i < quantities.length; i++){
  167. avg += quantities[i];
  168. }
  169. avg = avg / quantities.length;
  170. document.getElementById("recipeAvgUse").innerText = avg.toFixed(2);
  171. document.getElementById("recipeAvgRevenue").innerText = `$${(avg * recipe.price).toFixed(2)}`
  172. },
  173. switchDisplay: function(){
  174. const checkbox = document.getElementById("analSlider");
  175. let ingredient = document.getElementById("analIngredientContent");
  176. let recipe = document.getElementById("analRecipeContent");
  177. if(checkbox.checked === true){
  178. ingredient.style.display = "none";
  179. recipe.style.display = "flex";
  180. if(this.recipesDisplay === false){
  181. this.displayRecipe(merchant.recipes[0]);
  182. }
  183. }else{
  184. ingredient.style.display = "flex";
  185. recipe.style.display = "none";
  186. }
  187. }
  188. }
  189. module.exports = analytics;