analytics.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.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. console.timeEnd("load");
  123. },
  124. changeDates: function(){
  125. let dates = {
  126. from: document.getElementById("analStartDate").valueAsDate,
  127. to: document.getElementById("analEndDate").valueAsDate
  128. }
  129. if(dates.from > dates.to || dates.from === "" || dates.to === "" || dates.to > new Date()){
  130. banner.createError("INVALID DATE");
  131. return;
  132. }
  133. let loader = document.getElementById("loaderContainer");
  134. loader.style.display = "flex";
  135. fetch("/transaction/retrieve", {
  136. method: "post",
  137. headers: {
  138. "Content-Type": "application/json;charset=utf-8"
  139. },
  140. body: JSON.stringify(dates)
  141. })
  142. .then((response)=>response.json())
  143. .then((response)=>{
  144. if(typeof(response) === "string"){
  145. banner.createError(response.data);
  146. }else{
  147. this.transactions = [];
  148. for(let i = 0; i < response.length; i++){
  149. this.transactions.push(new Transaction(
  150. response[i]._id,
  151. new Date(response[i].date),
  152. response[i].recipes,
  153. merchant
  154. ));
  155. }
  156. this.ingredientDisplay();
  157. }
  158. })
  159. .catch((err)=>{
  160. banner.createError("ERROR: UNABLE TO DISPLAY THE DATA");
  161. })
  162. .finally(()=>{
  163. loader.style.display = "none";
  164. });
  165. }
  166. }
  167. module.exports = analytics;