history.js 6.2 KB

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