analytics.js 5.2 KB

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