history.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. function arrayHasElementWithSameTimestampAndProperty(array,t,propname) {
  2. for (var j=0; j < array.length; j++) {
  3. var element = array[j];
  4. if (element.timestamp === t && element[propname] !== undefined) return true;
  5. if ( element[propname] !== undefined ) {
  6. var eDate = new Date(element.timestamp);
  7. var tDate = new Date(t);
  8. var tMin = new Date(tDate.getTime() - 2000);
  9. var tMax = new Date(tDate.getTime() + 2000);
  10. //console.error(tDate, tMin, tMax);
  11. if (eDate > tMin && eDate < tMax) return true;
  12. }
  13. }
  14. return false;
  15. }
  16. function findMealInputs (inputs) {
  17. var pumpHistory = inputs.history;
  18. var carbHistory = inputs.carbs;
  19. var profile_data = inputs.profile;
  20. var mealInputs = [];
  21. var bolusWizardInputs = [];
  22. var duplicates = 0;
  23. for (var i=0; i < carbHistory.length; i++) {
  24. var current = carbHistory[i];
  25. if (current.carbs && current.created_at) {
  26. var temp = {};
  27. temp.timestamp = current.created_at;
  28. temp.carbs = current.carbs;
  29. temp.nsCarbs = current.carbs;
  30. if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.created_at,"carbs")) {
  31. mealInputs.push(temp);
  32. } else {
  33. duplicates += 1;
  34. }
  35. }
  36. }
  37. for (i=0; i < pumpHistory.length; i++) {
  38. current = pumpHistory[i];
  39. if (current._type === "Bolus" && current.timestamp) {
  40. //console.log(pumpHistory[i]);
  41. temp = {};
  42. temp.timestamp = current.timestamp;
  43. temp.bolus = current.amount;
  44. if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.timestamp,"bolus")) {
  45. mealInputs.push(temp);
  46. } else {
  47. duplicates += 1;
  48. }
  49. } else if (current._type === "BolusWizard" && current.timestamp) {
  50. // Delay process the BolusWizard entries to make sure we've seen all possible that correspond to the bolus wizard.
  51. // More specifically, we need to make sure we process the corresponding bolus entry first.
  52. bolusWizardInputs.push(current);
  53. } else if ((current._type === "Meal Bolus" || current._type === "Correction Bolus" || current._type === "Snack Bolus" || current._type === "Bolus Wizard" || current._type === "Carb Correction") && current.created_at) {
  54. //imports carbs entered through Nightscout Care Portal
  55. //"Bolus Wizard" refers to the Nightscout Bolus Wizard, not the Medtronic Bolus Wizard
  56. temp = {};
  57. temp.timestamp = current.created_at;
  58. temp.carbs = current.carbs;
  59. temp.nsCarbs = current.carbs;
  60. // don't enter the treatment if there's another treatment with the same exact timestamp
  61. // to prevent duped carb entries from multiple sources
  62. if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.created_at,"carbs")) {
  63. mealInputs.push(temp);
  64. } else {
  65. duplicates += 1;
  66. }
  67. } else if (current.enteredBy === "xdrip") {
  68. temp = {};
  69. temp.timestamp = current.created_at;
  70. temp.carbs = current.carbs;
  71. temp.nsCarbs = current.carbs;
  72. temp.bolus = current.insulin;
  73. if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.timestamp,"carbs")) {
  74. mealInputs.push(temp);
  75. } else {
  76. duplicates += 1;
  77. }
  78. } else if (current.carbs > 0) {
  79. temp = {};
  80. temp.carbs = current.carbs;
  81. temp.nsCarbs = current.carbs;
  82. temp.timestamp = current.created_at;
  83. temp.bolus = current.insulin;
  84. if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.timestamp,"carbs")) {
  85. mealInputs.push(temp);
  86. } else {
  87. duplicates += 1;
  88. }
  89. } else if (current._type === "JournalEntryMealMarker" && current.carb_input > 0 && current.timestamp) {
  90. temp = {};
  91. temp.timestamp = current.timestamp;
  92. temp.carbs = current.carb_input;
  93. temp.journalCarbs = current.carb_input;
  94. if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.timestamp,"carbs")) {
  95. mealInputs.push(temp);
  96. } else {
  97. duplicates += 1;
  98. }
  99. }
  100. }
  101. for(i=0; i < bolusWizardInputs.length; i++) {
  102. current = bolusWizardInputs[i];
  103. //console.log(bolusWizardInputs[i]);
  104. temp = {};
  105. temp.timestamp = current.timestamp;
  106. temp.carbs = current.carb_input;
  107. temp.bwCarbs = current.carb_input;
  108. // don't enter the treatment if there's another treatment with the same exact timestamp
  109. // to prevent duped carb entries from multiple sources
  110. if (!arrayHasElementWithSameTimestampAndProperty(mealInputs,current.timestamp,"carbs")) {
  111. if (arrayHasElementWithSameTimestampAndProperty(mealInputs,current.timestamp,"bolus")) {
  112. mealInputs.push(temp);
  113. //bwCarbs += temp.carbs;
  114. } else {
  115. console.error("Skipping bolus wizard entry", i, "in the pump history with",current.carb_input,"g carbs and no insulin.");
  116. if (current.carb_input === 0) {
  117. console.error("This is caused by a BolusWizard without carbs. If you specified insulin, it will be noted as a seperate Bolus");
  118. }
  119. if (current.timestamp) {
  120. console.error("Timestamp of bolus wizard:", current.timestamp);
  121. }
  122. }
  123. } else {
  124. duplicates += 1;
  125. }
  126. }
  127. //if (duplicates > 0) console.error("Removed duplicate bolus/carb entries:" + duplicates);
  128. return mealInputs;
  129. }
  130. exports = module.exports = findMealInputs;