analytics2.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. //display the boxes at the bottom
  102. },
  103. displayRecipe: function(recipe){
  104. //break down data into dates and quantities
  105. let dates = [];
  106. let quantities = [];
  107. for(let i = 0; i < this.transactionsByDate.length; i++){
  108. dates.push(this.transactionsByDate[i].date);
  109. let sum = 0;
  110. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  111. const transaction = this.transactionsByDate[i].transactions[j];
  112. for(let k = 0; k < transaction.recipes.length; k++){
  113. if(transaction.recipes[k].recipe === recipe){
  114. sum += transaction.recipes[k].quantity;
  115. }
  116. }
  117. }
  118. quantities.push(sum);
  119. }
  120. //create and display the graph
  121. const trace = {
  122. x: dates,
  123. y: quantities,
  124. mode: "lines+markers",
  125. line: {
  126. color: "rgb(255, 99, 107)"
  127. }
  128. }
  129. const layout = {
  130. title: recipe.name.toUpperCase(),
  131. xaxis: {title: "DATE"},
  132. yaxis: {title: "QUANTITY"}
  133. }
  134. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  135. //display the boxes at the bottom
  136. },
  137. switchDisplay: function(){
  138. const checkbox = document.getElementById("analSlider");
  139. let ingredient = document.getElementById("analIngredientContent");
  140. let recipe = document.getElementById("analRecipeContent");
  141. if(checkbox.checked === true){
  142. ingredient.style.display = "none";
  143. recipe.style.display = "flex";
  144. if(this.recipesDisplay === false){
  145. this.displayRecipe(merchant.recipes[0]);
  146. }
  147. }else{
  148. ingredient.style.display = "flex";
  149. recipe.style.display = "none";
  150. }
  151. }
  152. }
  153. module.exports = analytics;