analytics.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. //Get list of recipes that contain the ingredient
  38. let containingRecipes = [];
  39. for(let i = 0; i < merchant.recipes.length; i++){
  40. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  41. if(merchant.recipes[i].ingredients[j].ingredient === this.ingredient.ingredient){
  42. containingRecipes.push({
  43. recipe: merchant.recipes[i],
  44. quantity: merchant.recipes[i].ingredients[j].quantity
  45. });
  46. break;
  47. }
  48. }
  49. }
  50. //Create Graph
  51. let quantities = [];
  52. let dates = [];
  53. let currentDate = this.transactions[0].date;
  54. let currentQuantity = 0;
  55. for(let i = 0; i < this.transactions.length; i++){
  56. if(currentDate.getDate() !== this.transactions[i].date.getDate()){
  57. quantities.push(this.ingredient.ingredient.convert(currentQuantity));
  58. dates.push(currentDate);
  59. currentQuantity = 0;
  60. currentDate = this.transactions[i].date;
  61. }
  62. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  63. for(let k = 0; k < containingRecipes.length; k++){
  64. if(this.transactions[i].recipes[j].recipe === containingRecipes[k].recipe){
  65. for(let l = 0; l < this.transactions[i].recipes[j].recipe.ingredients.length; l++){
  66. const transIngredient = this.transactions[i].recipes[j].recipe.ingredients[l];
  67. if(transIngredient.ingredient === this.ingredient.ingredient){
  68. currentQuantity += transIngredient.quantity * this.transactions[i].recipes[j].quantity;
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. let trace = {
  77. x: dates,
  78. y: quantities,
  79. mode: "lines+markers",
  80. line: {
  81. color: "rgb(255, 99, 107)"
  82. }
  83. }
  84. const layout = {
  85. title: this.ingredient.ingredient.name.toUpperCase(),
  86. xaxis: {
  87. title: "DATE"
  88. },
  89. yaxis: {
  90. title: `QUANTITY (${this.ingredient.ingredient.unit.toUpperCase()})`,
  91. }
  92. }
  93. Plotly.newPlot("itemUseGraph", [trace], layout);
  94. //Create use cards
  95. let sum = 0;
  96. let max = 0;
  97. let min = quantities[0];
  98. for(let i = 0; i < quantities.length; i++){
  99. sum += quantities[i];
  100. if(quantities[i] > max){
  101. max = quantities[i];
  102. }else if(quantities[i] < min){
  103. min = quantities[i];
  104. }
  105. }
  106. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  107. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  108. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  109. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  110. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  111. for(let i = 0; i < quantities.length; i++){
  112. dayUse[dates[i].getDay()] += quantities[i];
  113. dayCount[dates[i].getDay()]++;
  114. }
  115. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  116. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  117. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  118. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  119. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  120. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  121. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  122. },
  123. changeDates: function(){
  124. let dates = {
  125. from: document.getElementById("analStartDate").valueAsDate,
  126. to: document.getElementById("analEndDate").valueAsDate
  127. }
  128. if(dates.from > dates.to || dates.from === "" || dates.to === "" || dates.to > new Date()){
  129. banner.createError("INVALID DATE");
  130. return;
  131. }
  132. let loader = document.getElementById("loaderContainer");
  133. loader.style.display = "flex";
  134. fetch("/transaction/retrieve", {
  135. method: "post",
  136. headers: {
  137. "Content-Type": "application/json;charset=utf-8"
  138. },
  139. body: JSON.stringify(dates)
  140. })
  141. .then((response)=>response.json())
  142. .then((response)=>{
  143. if(typeof(response) === "string"){
  144. banner.createError(response.data);
  145. }else{
  146. this.transactions = [];
  147. for(let i = 0; i < response.length; i++){
  148. this.transactions.push(new Transaction(
  149. response[i]._id,
  150. new Date(response[i].date),
  151. response[i].recipes,
  152. merchant
  153. ));
  154. }
  155. this.ingredientDisplay();
  156. }
  157. })
  158. .catch((err)=>{
  159. banner.createError("ERROR: UNABLE TO DISPLAY THE DATA");
  160. })
  161. .finally(()=>{
  162. loader.style.display = "none";
  163. });
  164. }
  165. }
  166. module.exports = analytics;