analytics.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 startOfDay = new Date(from.getTime());
  63. startOfDay.setHours(0, 0, 0, 0);
  64. let endOfDay = new Date(from.getTime());
  65. endOfDay.setDate(endOfDay.getDate() + 1);
  66. endOfDay.setHours(0, 0, 0, 0);
  67. let transactionIndex = 0;
  68. while(startOfDay <= to){
  69. let currentTransactions = [];
  70. while(transactionIndex < response.length && new Date(response[transactionIndex].date) < endOfDay){
  71. currentTransactions.push(new Transaction(
  72. response[transactionIndex]._id,
  73. response[transactionIndex].date,
  74. response[transactionIndex].recipes,
  75. merchant
  76. ));
  77. transactionIndex++;
  78. }
  79. let thing = {
  80. date: new Date(startOfDay.getTime()),
  81. transactions: currentTransactions
  82. };
  83. this.transactionsByDate.push(thing);
  84. startOfDay.setDate(startOfDay.getDate() + 1);
  85. endOfDay.setDate(endOfDay.getDate() + 1);
  86. }
  87. }
  88. })
  89. .catch((err)=>{
  90. controller.createBanner("UNABLE TO UPDATE THE PAGE", "error");
  91. })
  92. .finally(()=>{
  93. loader.style.display = "none";
  94. });
  95. },
  96. displayIngredient: function(ingredient){
  97. if(this.ingredient === undefined || this.transactionsByDate.length === 0){
  98. return;
  99. }
  100. //break down data into dates and quantities
  101. let dates = [];
  102. let quantities = [];
  103. for(let i = 0; i < this.transactionsByDate.length; i++){
  104. dates.push(this.transactionsByDate[i].date);
  105. let sum = 0;
  106. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  107. let transactions = this.transactionsByDate[i].transactions[j];
  108. sum += transactions.getIngredientQuantity(this.ingredient);
  109. }
  110. quantities.push(sum);
  111. }
  112. //create and display the graph
  113. let trace = {
  114. x: dates,
  115. y: quantities,
  116. mode: "lines+markers",
  117. line: {
  118. color: "rgb(255, 99, 107)"
  119. }
  120. }
  121. const layout = {
  122. title: this.ingredient.name.toUpperCase(),
  123. xaxis: {title: "DATE"},
  124. yaxis: {title: `QUANTITY (${this.ingredient.unit.toUpperCase()})`}
  125. }
  126. Plotly.newPlot("itemUseGraph", [trace], layout);
  127. //Create min/max/avg
  128. //Current ingredient is stored on the "analMinUse" element
  129. let min = quantities[0];
  130. let max = quantities[0];
  131. let sum = 0;
  132. for(let i = 0; i < quantities.length; i++){
  133. if(quantities[i] < min){
  134. min = quantities[i];
  135. }
  136. if(quantities[i] > max){
  137. max = quantities[i];
  138. }
  139. sum += quantities[i];
  140. }
  141. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  142. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  143. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  144. //Create weekday averages
  145. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  146. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  147. for(let i = 0; i < quantities.length; i++){
  148. dayUse[dates[i].getDay()] += quantities[i];
  149. dayCount[dates[i].getDay()]++;
  150. }
  151. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  152. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  153. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  154. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  155. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  156. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  157. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  158. },
  159. displayRecipe: function(){
  160. if(this.recipe === undefined || this.transactionsByDate.length === 0){
  161. return;
  162. }
  163. //break down data into dates and quantities
  164. let dates = [];
  165. let quantities = [];
  166. for(let i = 0; i < this.transactionsByDate.length; i++){
  167. dates.push(this.transactionsByDate[i].date);
  168. let sum = 0;
  169. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  170. const transaction = this.transactionsByDate[i].transactions[j];
  171. for(let k = 0; k < transaction.recipes.length; k++){
  172. if(transaction.recipes[k].recipe === this.recipe){
  173. sum += transaction.recipes[k].quantity;
  174. }
  175. }
  176. }
  177. quantities.push(sum);
  178. }
  179. //create and display the graph
  180. const trace = {
  181. x: dates,
  182. y: quantities,
  183. mode: "lines+markers",
  184. line: {
  185. color: "rgb(255, 99, 107)"
  186. }
  187. }
  188. const layout = {
  189. title: this.recipe.name.toUpperCase(),
  190. xaxis: {title: "DATE"},
  191. yaxis: {title: "QUANTITY"}
  192. }
  193. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  194. //Display the boxes at the bottom
  195. //Current recipe is stored on the "recipeAvgUse" element
  196. let avg = 0;
  197. for(let i = 0; i < quantities.length; i++){
  198. avg += quantities[i];
  199. }
  200. avg = avg / quantities.length;
  201. document.getElementById("recipeAvgUse").innerText = avg.toFixed(2);
  202. document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`;
  203. },
  204. switchDisplay: function(){
  205. const checkbox = document.getElementById("analSlider");
  206. let ingredient = document.getElementById("analIngredientContent");
  207. let recipe = document.getElementById("analRecipeContent");
  208. if(checkbox.checked === true){
  209. ingredient.style.display = "none";
  210. recipe.style.display = "flex";
  211. this.displayRecipe();
  212. }else{
  213. ingredient.style.display = "flex";
  214. recipe.style.display = "none";
  215. this.displayIngredient();
  216. }
  217. },
  218. newDates: async function(Transaction){
  219. const from = document.getElementById("analStartDate").valueAsDate;
  220. const to = document.getElementById("analEndDate").valueAsDate;
  221. from.setHours(0, 0, 0, 0);
  222. to.setDate(to.getDate() + 1);
  223. to.setHours(0, 0, 0, 0);
  224. await this.getData(from, to, Transaction);
  225. if(document.getElementById("analSlider").checked === true){
  226. this.displayRecipe();
  227. }else{
  228. this.displayIngredient();
  229. }
  230. }
  231. }
  232. module.exports = analytics;