analytics.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. let analytics = {
  2. isPopulated: false,
  3. ingredient: undefined,
  4. recipe: undefined,
  5. transactionsByDate: [],
  6. display: function(Transaction){
  7. if(!this.isPopulated){
  8. document.getElementById("analRecipeContent").style.display = "none";
  9. let to = new Date()
  10. let from = new Date(to.getFullYear(), to.getMonth() - 1, to.getDate());
  11. document.getElementById("analStartDate").valueAsDate = from;
  12. document.getElementById("analEndDate").valueAsDate = to;
  13. let analSlider = document.getElementById("analSlider");
  14. analSlider.onclick = ()=>{this.switchDisplay()};
  15. analSlider.checked = false;
  16. document.getElementById("analDateBtn").onclick = ()=>{this.newDates(Transaction)};
  17. this.populateButtons();
  18. if(merchant.ingredients.length > 0){
  19. this.ingredient = merchant.ingredients[0].ingredient;
  20. }
  21. if(merchant.recipes.length > 0){
  22. this.recipe = merchant.recipes[0];
  23. }
  24. this.newDates(Transaction);
  25. this.isPopulated = true;
  26. }
  27. },
  28. populateButtons: function(){
  29. let ingredientButtons = document.getElementById("analIngredientList");
  30. let recipeButtons = document.getElementById("analRecipeList");
  31. for(let i = 0; i < merchant.ingredients.length; i++){
  32. let button = document.createElement("button");
  33. button.innerText = merchant.ingredients[i].ingredient.name;
  34. button.classList.add("choosable");
  35. button.onclick = ()=>{
  36. this.ingredient = merchant.ingredients[i].ingredient;
  37. this.displayIngredient()
  38. };
  39. ingredientButtons.appendChild(button);
  40. }
  41. for(let i = 0; i < merchant.recipes.length; i++){
  42. let button = document.createElement("button");
  43. button.innerText = merchant.recipes[i].name;
  44. button.classList.add("choosable");
  45. button.onclick = ()=>{
  46. this.recipe = merchant.recipes[i];
  47. this.displayRecipe();
  48. };
  49. recipeButtons.appendChild(button);
  50. }
  51. },
  52. getData: function(from, to, Transaction){
  53. let loader = document.getElementById("loaderContainer");
  54. loader.style.display = "flex";
  55. return fetch(`/transactions/${from.toISOString()}/${to.toISOString()}`)
  56. .then(response => response.json())
  57. .then((response)=>{
  58. if(typeof(response) === "string"){
  59. controller.createBanner(response, "error");
  60. }else{
  61. this.transactionsByDate = [];
  62. let nextDay = new Date(from.getTime());
  63. nextDay.setDate(nextDay.getDate() + 1);
  64. let currentTransactions = [];
  65. for(let i = 0; i < response.length; i++){
  66. response[i].date = new Date(response[i].date);
  67. if(response[i].date >= from && response[i].date < nextDay){
  68. currentTransactions.push(new Transaction(
  69. response[i]._id,
  70. response[i].date,
  71. response[i].recipes,
  72. merchant
  73. ));
  74. }else{
  75. this.transactionsByDate.push({
  76. date: new Date(from.getTime()),
  77. transactions: currentTransactions
  78. });
  79. from.setDate(from.getDate() + 1);
  80. nextDay.setDate(nextDay.getDate() + 1);
  81. currentTransactions = [];
  82. }
  83. }
  84. }
  85. })
  86. .catch((err)=>{
  87. controller.createBanner("UNABLE TO UPDATE THE PAGE", "error");
  88. })
  89. .finally(()=>{
  90. loader.style.display = "none";
  91. });
  92. },
  93. displayIngredient: function(ingredient){
  94. if(this.ingredient === undefined || this.transactionsByDate.length === 0){
  95. return;
  96. }
  97. //break down data into dates and quantities
  98. let dates = [];
  99. let quantities = [];
  100. for(let i = 0; i < this.transactionsByDate.length; i++){
  101. dates.push(this.transactionsByDate[i].date);
  102. let sum = 0;
  103. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  104. let transactions = this.transactionsByDate[i].transactions[j];
  105. sum += transactions.getIngredientQuantity(this.ingredient);
  106. }
  107. quantities.push(sum);
  108. }
  109. //create and display the graph
  110. let trace = {
  111. x: dates,
  112. y: quantities,
  113. mode: "lines+markers",
  114. line: {
  115. color: "rgb(255, 99, 107)"
  116. }
  117. }
  118. const layout = {
  119. title: this.ingredient.name.toUpperCase(),
  120. xaxis: {title: "DATE"},
  121. yaxis: {title: `QUANTITY (${this.ingredient.unit.toUpperCase()})`}
  122. }
  123. Plotly.newPlot("itemUseGraph", [trace], layout);
  124. //Create min/max/avg
  125. //Current ingredient is stored on the "analMinUse" element
  126. let min = quantities[0];
  127. let max = quantities[0];
  128. let sum = 0;
  129. for(let i = 0; i < quantities.length; i++){
  130. if(quantities[i] < min){
  131. min = quantities[i];
  132. }
  133. if(quantities[i] > max){
  134. max = quantities[i];
  135. }
  136. sum += quantities[i];
  137. }
  138. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  139. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  140. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  141. //Create weekday averages
  142. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  143. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  144. for(let i = 0; i < quantities.length; i++){
  145. dayUse[dates[i].getDay()] += quantities[i];
  146. dayCount[dates[i].getDay()]++;
  147. }
  148. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  149. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  150. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  151. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  152. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  153. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  154. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  155. },
  156. displayRecipe: function(){
  157. if(this.recipe === undefined || this.transactionsByDate.length === 0){
  158. return;
  159. }
  160. //break down data into dates and quantities
  161. let dates = [];
  162. let quantities = [];
  163. for(let i = 0; i < this.transactionsByDate.length; i++){
  164. dates.push(this.transactionsByDate[i].date);
  165. let sum = 0;
  166. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  167. const transaction = this.transactionsByDate[i].transactions[j];
  168. for(let k = 0; k < transaction.recipes.length; k++){
  169. if(transaction.recipes[k].recipe === this.recipe){
  170. sum += transaction.recipes[k].quantity;
  171. }
  172. }
  173. }
  174. quantities.push(sum);
  175. }
  176. //create and display the graph
  177. const trace = {
  178. x: dates,
  179. y: quantities,
  180. mode: "lines+markers",
  181. line: {
  182. color: "rgb(255, 99, 107)"
  183. }
  184. }
  185. const layout = {
  186. title: this.recipe.name.toUpperCase(),
  187. xaxis: {title: "DATE"},
  188. yaxis: {title: "QUANTITY"}
  189. }
  190. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  191. //Display the boxes at the bottom
  192. //Current recipe is stored on the "recipeAvgUse" element
  193. let avg = 0;
  194. for(let i = 0; i < quantities.length; i++){
  195. avg += quantities[i];
  196. }
  197. avg = avg / quantities.length;
  198. document.getElementById("recipeAvgUse").innerText = avg.toFixed(2);
  199. document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`;
  200. },
  201. switchDisplay: function(){
  202. const checkbox = document.getElementById("analSlider");
  203. let ingredient = document.getElementById("analIngredientContent");
  204. let recipe = document.getElementById("analRecipeContent");
  205. if(checkbox.checked === true){
  206. ingredient.style.display = "none";
  207. recipe.style.display = "flex";
  208. this.displayRecipe();
  209. }else{
  210. ingredient.style.display = "flex";
  211. recipe.style.display = "none";
  212. this.displayIngredient();
  213. }
  214. },
  215. newDates: async function(Transaction){
  216. const from = document.getElementById("analStartDate").valueAsDate;
  217. const to = document.getElementById("analEndDate").valueAsDate;
  218. from.setHours(0, 0, 0, 0);
  219. to.setDate(to.getDate() + 1);
  220. to.setHours(0, 0, 0, 0);
  221. await this.getData(from, to, Transaction);
  222. if(document.getElementById("analSlider").checked === true){
  223. this.displayRecipe();
  224. }else{
  225. this.displayIngredient();
  226. }
  227. }
  228. }
  229. module.exports = analytics;