analytics.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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(){
  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 transaction = this.transactionsByDate[i].transactions[j];
  108. sum += transaction.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. let yaxis = `QUANTITY (${this.ingredient.unit.toUpperCase()})`;
  122. if(this.ingredient.specialUnit === "bottle"){
  123. yaxis = `QUANTITY (${this.ingredient.specialUnit.toUpperCase()})`
  124. }
  125. const layout = {
  126. title: this.ingredient.name.toUpperCase(),
  127. xaxis: {title: "DATE"},
  128. yaxis: {title: yaxis}
  129. }
  130. Plotly.newPlot("itemUseGraph", [trace], layout);
  131. //Create min/max/avg
  132. //Current ingredient is stored on the "analMinUse" element
  133. let min = quantities[0];
  134. let max = quantities[0];
  135. let sum = 0;
  136. for(let i = 0; i < quantities.length; i++){
  137. if(quantities[i] < min){
  138. min = quantities[i];
  139. }
  140. if(quantities[i] > max){
  141. max = quantities[i];
  142. }
  143. sum += quantities[i];
  144. }
  145. document.getElementById("analMinUse").innerText = `${min.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  146. document.getElementById("analAvgUse").innerText = `${(sum / quantities.length).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  147. document.getElementById("analMaxUse").innerText = `${max.toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  148. //Create weekday averages
  149. let dayUse = [0, 0, 0, 0, 0, 0, 0];
  150. let dayCount = [0, 0, 0, 0, 0, 0, 0];
  151. for(let i = 0; i < quantities.length; i++){
  152. dayUse[dates[i].getDay()] += quantities[i];
  153. dayCount[dates[i].getDay()]++;
  154. }
  155. document.getElementById("analDayOne").innerText = `${(dayUse[0] / dayCount[0]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  156. document.getElementById("analDayTwo").innerText = `${(dayUse[1] / dayCount[1]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  157. document.getElementById("analDayThree").innerText = `${(dayUse[2] / dayCount[2]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  158. document.getElementById("analDayFour").innerText = `${(dayUse[3] / dayCount[3]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  159. document.getElementById("analDayFive").innerText = `${(dayUse[4] / dayCount[4]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  160. document.getElementById("analDaySix").innerText = `${(dayUse[5] / dayCount[5]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  161. document.getElementById("analDaySeven").innerText = `${(dayUse[6] / dayCount[6]).toFixed(2)} ${this.ingredient.unit.toUpperCase()}`;
  162. },
  163. displayRecipe: function(){
  164. if(this.recipe === undefined || this.transactionsByDate.length === 0){
  165. return;
  166. }
  167. //break down data into dates and quantities
  168. let dates = [];
  169. let quantities = [];
  170. for(let i = 0; i < this.transactionsByDate.length; i++){
  171. dates.push(this.transactionsByDate[i].date);
  172. let sum = 0;
  173. for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
  174. const transaction = this.transactionsByDate[i].transactions[j];
  175. for(let k = 0; k < transaction.recipes.length; k++){
  176. if(transaction.recipes[k].recipe === this.recipe){
  177. sum += transaction.recipes[k].quantity;
  178. }
  179. }
  180. }
  181. quantities.push(sum);
  182. }
  183. //create and display the graph
  184. const trace = {
  185. x: dates,
  186. y: quantities,
  187. mode: "lines+markers",
  188. line: {
  189. color: "rgb(255, 99, 107)"
  190. }
  191. }
  192. const layout = {
  193. title: this.recipe.name.toUpperCase(),
  194. xaxis: {title: "DATE"},
  195. yaxis: {title: "QUANTITY"}
  196. }
  197. Plotly.newPlot("recipeSalesGraph", [trace], layout);
  198. //Display the boxes at the bottom
  199. //Current recipe is stored on the "recipeAvgUse" element
  200. let avg = 0;
  201. for(let i = 0; i < quantities.length; i++){
  202. avg += quantities[i];
  203. }
  204. avg = avg / quantities.length;
  205. document.getElementById("recipeAvgUse").innerText = avg.toFixed(2);
  206. document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`;
  207. },
  208. switchDisplay: function(){
  209. const checkbox = document.getElementById("analSlider");
  210. let ingredient = document.getElementById("analIngredientContent");
  211. let recipe = document.getElementById("analRecipeContent");
  212. if(checkbox.checked === true){
  213. ingredient.style.display = "none";
  214. recipe.style.display = "flex";
  215. this.displayRecipe();
  216. }else{
  217. ingredient.style.display = "flex";
  218. recipe.style.display = "none";
  219. this.displayIngredient();
  220. }
  221. },
  222. newDates: async function(Transaction){
  223. const from = document.getElementById("analStartDate").valueAsDate;
  224. const to = document.getElementById("analEndDate").valueAsDate;
  225. from.setHours(0, 0, 0, 0);
  226. to.setDate(to.getDate() + 1);
  227. to.setHours(0, 0, 0, 0);
  228. await this.getData(from, to, Transaction);
  229. if(document.getElementById("analSlider").checked === true){
  230. this.displayRecipe();
  231. }else{
  232. this.displayIngredient();
  233. }
  234. }
  235. }
  236. module.exports = analytics;