analytics.js 5.9 KB

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