analytics.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. line: {
  84. color: "rgb(255, 99, 107)"
  85. }
  86. }
  87. const layout = {
  88. title: this.ingredient.ingredient.name.toUpperCase(),
  89. xaxis: {
  90. title: "DATE"
  91. },
  92. yaxis: {
  93. title: `QUANTITY (${this.ingredient.ingredient.unit.toUpperCase()})`,
  94. }
  95. }
  96. Plotly.newPlot("itemUseGraph", [trace], layout);
  97. //Create use cards
  98. let sum = 0;
  99. let max = 0;
  100. let min = quantities[0];
  101. for(let i = 0; i < quantities.length; i++){
  102. sum += quantities[i];
  103. if(quantities[i] > max){
  104. max = quantities[i];
  105. }else if(quantities[i] < min){
  106. min = quantities[i];
  107. }
  108. }
  109. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  110. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  111. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.ingredient.unit}`;
  112. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  113. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  114. for(let i = 0; i < quantities.length; i++){
  115. dayUse[dates[i].getDay()] += quantities[i];
  116. dayCount[dates[i].getDay()]++;
  117. }
  118. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  119. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  120. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  121. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  122. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  123. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  124. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.ingredient.unit}`;
  125. console.timeEnd("load");
  126. },
  127. changeDates: function(){
  128. let dates = {
  129. from: document.getElementById("analStartDate").valueAsDate,
  130. to: document.getElementById("analEndDate").valueAsDate
  131. }
  132. if(dates.from > dates.to || dates.from === "" || dates.to === "" || dates.to > new Date()){
  133. banner.createError("INVALID DATE");
  134. return;
  135. }
  136. let loader = document.getElementById("loaderContainer");
  137. loader.style.display = "flex";
  138. fetch("/transaction/retrieve", {
  139. method: "post",
  140. headers: {
  141. "Content-Type": "application/json;charset=utf-8"
  142. },
  143. body: JSON.stringify(dates)
  144. })
  145. .then((response)=>response.json())
  146. .then((response)=>{
  147. if(typeof(response) === "string"){
  148. banner.createError(response.data);
  149. }else{
  150. this.transactions = [];
  151. for(let i = 0; i < response.length; i++){
  152. this.transactions.push(new Transaction(
  153. response[i]._id,
  154. new Date(response[i].date),
  155. response[i].recipes,
  156. merchant
  157. ));
  158. }
  159. this.ingredientDisplay();
  160. }
  161. })
  162. .catch((err)=>{
  163. banner.createError("ERROR: UNABLE TO DISPLAY THE DATA");
  164. })
  165. .finally(()=>{
  166. loader.style.display = "none";
  167. });
  168. }
  169. }
  170. module.exports = analytics;