analytics.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. const Transaction = require("./Transaction");
  2. let analytics = {
  3. transactions: {},
  4. ingredient: {},
  5. display: function(){
  6. console.time("display");
  7. let startDate = new Date();
  8. startDate.setMonth(startDate.getMonth() - 1);
  9. const dateIndices = controller.transactionIndices(merchant.transactions, startDate);
  10. this.transactions = merchant.transactions.slice(dateIndices[0], dateIndices[1]);
  11. const itemsList = document.getElementById("itemsList");
  12. let now = new Date();
  13. let lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate(), now.getHours(), now.getMinutes());
  14. document.getElementById("analStartDate").valueAsDate = lastMonth;
  15. document.getElementById("analEndDate").valueAsDate = now;
  16. document.getElementById("analDateBtn").onclick = ()=>{this.changeDates()};
  17. while(itemsList.children.length > 0){
  18. itemsList.removeChild(itemsList.firstChild);
  19. }
  20. for(let i = 0; i < merchant.ingredients.length; i++){
  21. let li = document.createElement("li");
  22. li.classList.add("itemButton");
  23. li.item = merchant.ingredients[i];
  24. li.innerText = merchant.ingredients[i].ingredient.name;
  25. li.onclick = ()=>{
  26. const itemsList = document.getElementById("itemsList");
  27. for(let i = 0; i < itemsList.children.length; i++){
  28. itemsList.children[i].classList.remove("analItemActive");
  29. }
  30. li.classList.add("analItemActive");
  31. this.ingredient = merchant.ingredients[i];
  32. this.ingredientDisplay();
  33. };
  34. itemsList.appendChild(li);
  35. }
  36. console.timeEnd("display");
  37. },
  38. ingredientDisplay: function(){
  39. console.time("load");
  40. //Get list of recipes that contain the ingredient
  41. let containingRecipes = [];
  42. for(let i = 0; i < merchant.recipes.length; i++){
  43. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  44. if(merchant.recipes[i].ingredients[j].ingredient === this.ingredient.ingredient){
  45. containingRecipes.push({
  46. recipe: merchant.recipes[i],
  47. quantity: merchant.recipes[i].ingredients[j].quantity
  48. });
  49. break;
  50. }
  51. }
  52. }
  53. //Create Graph
  54. let quantities = [];
  55. let dates = [];
  56. let currentDate = this.transactions[0].date;
  57. let currentQuantity = 0;
  58. for(let i = 0; i < this.transactions.length; i++){
  59. if(currentDate.getDate() !== this.transactions[i].date.getDate()){
  60. quantities.push(this.ingredient.ingredient.convert(currentQuantity));
  61. dates.push(currentDate);
  62. currentQuantity = 0;
  63. currentDate = this.transactions[i].date;
  64. }
  65. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  66. for(let k = 0; k < containingRecipes.length; k++){
  67. if(this.transactions[i].recipes[j].recipe === containingRecipes[k].recipe){
  68. for(let l = 0; l < this.transactions[i].recipes[j].recipe.ingredients.length; l++){
  69. const transIngredient = this.transactions[i].recipes[j].recipe.ingredients[l];
  70. if(transIngredient.ingredient === this.ingredient.ingredient){
  71. currentQuantity += transIngredient.quantity * this.transactions[i].recipes[j].quantity;
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. let trace = {
  80. x: dates,
  81. y: quantities,
  82. mode: "lines+markers"
  83. }
  84. const layout = {
  85. title: this.ingredient.ingredient.name
  86. }
  87. Plotly.newPlot("itemUseGraph", [trace], layout);
  88. //Create use cards
  89. let sum = 0;
  90. let max = 0;
  91. let min = quantities[0];
  92. for(let i = 0; i < quantities.length; i++){
  93. sum += quantities[i];
  94. if(quantities[i] > max){
  95. max = quantities[i];
  96. }else if(quantities[i] < min){
  97. min = quantities[i];
  98. }
  99. }
  100. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  101. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  102. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  103. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  104. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  105. for(let i = 0; i < quantities.length; i++){
  106. dayUse[dates[i].getDay()] += quantities[i];
  107. dayCount[dates[i].getDay()]++;
  108. }
  109. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  110. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  111. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  112. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  113. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  114. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  115. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  116. console.timeEnd("load");
  117. },
  118. changeDates: function(){
  119. let dates = {
  120. from: document.getElementById("analStartDate").valueAsDate,
  121. to: document.getElementById("analEndDate").valueAsDate
  122. }
  123. if(dates.from > dates.to || dates.from === "" || dates.to === "" || dates.to > new Date()){
  124. banner.createError("INVALID DATE");
  125. return;
  126. }
  127. let loader = document.getElementById("loaderContainer");
  128. loader.style.display = "flex";
  129. fetch("/transaction/retrieve", {
  130. method: "post",
  131. headers: {
  132. "Content-Type": "application/json;charset=utf-8"
  133. },
  134. body: JSON.stringify(dates)
  135. })
  136. .then((response)=>response.json())
  137. .then((response)=>{
  138. if(typeof(response) === "string"){
  139. banner.createError(response.data);
  140. }else{
  141. this.transactions = [];
  142. for(let i = 0; i < response.length; i++){
  143. this.transactions.push(new Transaction(
  144. response[i]._id,
  145. new Date(response[i].date),
  146. response[i].recipes,
  147. merchant
  148. ));
  149. }
  150. this.ingredientDisplay();
  151. }
  152. })
  153. .catch((err)=>{
  154. banner.createError("ERROR: UNABLE TO DISPLAY THE DATA");
  155. })
  156. .finally(()=>{
  157. loader.style.display = "none";
  158. });
  159. }
  160. }
  161. module.exports = analytics;