orderCalculator.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. let orderCalculator = {
  2. display: function(){
  3. let calculatorItems = document.getElementById("calculatorItemsBody");
  4. let template = document.getElementById("calculatorItem").content.children[0];
  5. let calculations = this.mlPredict();
  6. // while(calculatorItems.children.length > 0){
  7. // calculatorItems.removeChild(calculatorItems.firstChild);
  8. // }
  9. // for(let i = 0; i < calculations.length; i++){
  10. // let outputString = `${calculations[i].output.toFixed(2)} ${calculations[i].ingredient.unit.toUpperCase()}`;
  11. // let item = template.cloneNode(true);
  12. // item.children[0].innerText = calculations[i].ingredient.name,
  13. // item.children[1].innerText = outputString;
  14. // calculatorItems.appendChild(item);
  15. // }
  16. },
  17. predict: function(){
  18. let now = new Date();
  19. let yesterday = new Date();
  20. yesterday.setHours(0, 0, 0, 0);
  21. let monthAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 30);
  22. let weekAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7);
  23. let calculations = [];
  24. let month = merchant.getIngredientsSold(monthAgo, yesterday);
  25. let week = merchant.getIngredientsSold(weekAgo, yesterday);
  26. let weights = {
  27. month: 0.33,
  28. week: 0.67
  29. }
  30. for(let i = 0; i < month.length; i++){
  31. for(let j = 0; j < week.length; j++){
  32. if(month[i].ingredient.id === week[j].ingredient.id){
  33. let monthAverage = (month[i].quantity / 30) * weights.month;
  34. let weekAverage = (week[i].quantity / 7) * weights.week;
  35. let calc = {
  36. ingredient: month[i].ingredient,
  37. output: monthAverage + weekAverage
  38. };
  39. calculations.push(calc);
  40. }
  41. }
  42. }
  43. return calculations;
  44. },
  45. mlPredict: function(){
  46. let data = {
  47. to: new Date(),
  48. recipes: []
  49. }
  50. data.from = new Date(data.to.getFullYear() - 2, data.to.getMonth(), data.to.getDate());
  51. fetch("/transaction", {
  52. method: "post",
  53. headers: {
  54. "Content-Type": "application/json;charset=utf-8"
  55. },
  56. body: JSON.stringify(data)
  57. })
  58. .then(response => response.json())
  59. .then((response)=>{
  60. if(typeof(response) === "string"){
  61. controller.createBanner(response, "error");
  62. }else{
  63. const options = {
  64. task: "regression",
  65. debug: false
  66. }
  67. const nn = ml5.neuralNetwork(options);
  68. this.createData(nn, response);
  69. nn.normalizeData();
  70. nn.train(()=>{
  71. nn.predict({
  72. month: 0,
  73. day: 0
  74. }, (err, results)=>{
  75. console.log(results[0].value);
  76. });
  77. });
  78. }
  79. })
  80. .catch((err)=>{
  81. controller.createBanner("SOMETHING'S FUCKY...", "error");
  82. });
  83. },
  84. createData: function(nn, transactions){
  85. let today = new Date(transactions[transactions.length-1].date);
  86. today.setHours(0, 0, 0, 0);
  87. let tomorrow = new Date(transactions[transactions.length-1].date);
  88. tomorrow.setDate(tomorrow.getDate() + 1);
  89. tomorrow.setHours(0, 0, 0, 0);
  90. let dailySum = 0;
  91. for(let i = transactions.length - 1; i >= 0; i--){
  92. // for(let i = 100; i >= 0; i--){
  93. transactions[i].date = new Date(transactions[i].date);
  94. if(transactions[i].date >= tomorrow){
  95. let inputs = {
  96. month: today.getMonth(),
  97. day: today.getDay()
  98. };
  99. let output = {quantity: dailySum};
  100. nn.addData(inputs, output);
  101. dailySum = 0;
  102. today.setDate(today.getDate() + 1);
  103. tomorrow.setDate(tomorrow.getDate() + 1);
  104. }
  105. for(let j = 0; j < transactions[i].recipes.length; j++){
  106. for(let k = 0; k < merchant.recipes.length; k++){
  107. if(merchant.recipes[k].id === transactions[i].recipes[j].recipe){
  108. for(let l = 0; l < merchant.recipes[k].ingredients.length; l++){
  109. if(merchant.recipes[k].ingredients[l].ingredient === merchant.ingredients[5].ingredient){
  110. dailySum += merchant.recipes[k].ingredients[l].quantity * transactions[i].recipes[j].quantity;
  111. }
  112. }
  113. break;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. module.exports = orderCalculator;