total.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. function iobTotal(opts, time) {
  2. var now = time.getTime();
  3. var iobCalc = opts.calculate;
  4. var treatments = opts.treatments;
  5. var profile_data = opts.profile;
  6. var dia = profile_data.dia;
  7. var peak = 0;
  8. var iob = 0;
  9. var basaliob = 0;
  10. var bolusiob = 0;
  11. var netbasalinsulin = 0;
  12. var bolusinsulin = 0;
  13. //var bolussnooze = 0;
  14. var activity = 0;
  15. if (!treatments) return {};
  16. //if (typeof time === 'undefined') {
  17. //var time = new Date();
  18. //}
  19. // force minimum DIA of 3h
  20. if (dia < 3) {
  21. //console.error("Warning; adjusting DIA from",dia,"to minimum of 3 hours");
  22. dia = 3;
  23. }
  24. var curveDefaults = {
  25. 'bilinear': {
  26. requireLongDia: false,
  27. peak: 75 // not really used, but prevents having to check later
  28. },
  29. 'rapid-acting': {
  30. requireLongDia: true,
  31. peak: 75,
  32. tdMin: 300
  33. },
  34. 'ultra-rapid': {
  35. requireLongDia: true,
  36. peak: 55,
  37. tdMin: 300
  38. },
  39. };
  40. var curve = 'bilinear';
  41. if (profile_data.curve !== undefined) {
  42. curve = profile_data.curve.toLowerCase();
  43. }
  44. if (!(curve in curveDefaults)) {
  45. console.error('Unsupported curve function: "' + curve + '". Supported curves: "bilinear", "rapid-acting" (Novolog, Novorapid, Humalog, Apidra) and "ultra-rapid" (Fiasp). Defaulting to "rapid-acting".');
  46. curve = 'rapid-acting';
  47. }
  48. var defaults = curveDefaults[curve];
  49. // Force minimum of 5 hour DIA when default requires a Long DIA.
  50. if (defaults.requireLongDia && dia < 5) {
  51. //console.error('Pump DIA must be set to 5 hours or more with the new curves, please adjust your pump. Defaulting to 5 hour DIA.');
  52. dia = 5;
  53. }
  54. peak = defaults.peak;
  55. treatments.forEach(function(treatment) {
  56. if( treatment.date <= now ) {
  57. var dia_ago = now - dia*60*60*1000;
  58. if( treatment.date > dia_ago ) {
  59. // tIOB = total IOB
  60. var tIOB = iobCalc(treatment, time, curve, dia, peak, profile_data);
  61. if (tIOB && tIOB.iobContrib) { iob += tIOB.iobContrib; }
  62. if (tIOB && tIOB.activityContrib) { activity += tIOB.activityContrib; }
  63. // basals look like either of these:
  64. // {"insulin":-0.05,"date":1507265512363.6365,"created_at":"2017-10-06T04:51:52.363Z"}
  65. // {"insulin":0.05,"date":1507266530000,"created_at":"2017-10-06T05:08:50.000Z"}
  66. // boluses look like:
  67. // {"timestamp":"2017-10-05T22:06:31-07:00","started_at":"2017-10-06T05:06:31.000Z","date":1507266391000,"insulin":0.5}
  68. if (treatment.insulin && tIOB && tIOB.iobContrib) {
  69. if (treatment.insulin < 0.1) {
  70. basaliob += tIOB.iobContrib;
  71. netbasalinsulin += treatment.insulin;
  72. } else {
  73. bolusiob += tIOB.iobContrib;
  74. bolusinsulin += treatment.insulin;
  75. }
  76. }
  77. //console.error(JSON.stringify(treatment));
  78. }
  79. } // else { console.error("ignoring future treatment:",treatment); }
  80. });
  81. return {
  82. iob: Math.round(iob * 1000) / 1000,
  83. activity: Math.round(activity * 10000) / 10000,
  84. basaliob: Math.round(basaliob * 1000) / 1000,
  85. bolusiob: Math.round(bolusiob * 1000) / 1000,
  86. netbasalinsulin: Math.round(netbasalinsulin * 1000) / 1000,
  87. bolusinsulin: Math.round(bolusinsulin * 1000) / 1000,
  88. time: time
  89. };
  90. }
  91. exports = module.exports = iobTotal;