glucose-get-last.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var getLastGlucose = function (data) {
  2. data = data.map(function prepGlucose (obj) {
  3. //Support the NS sgv field to avoid having to convert in a custom way
  4. obj.glucose = obj.glucose || obj.sgv;
  5. return obj;
  6. });
  7. var now = data[0];
  8. var now_date = now.date || Date.parse(now.display_time) || Date.parse(then.dateString);
  9. var change;
  10. var last_deltas = [];
  11. var short_deltas = [];
  12. var long_deltas = [];
  13. var last_cal = 0;
  14. //console.error(now.glucose);
  15. for (var i=1; i < data.length; i++) {
  16. // if we come across a cal record, don't process any older SGVs
  17. if (typeof data[i] !== 'undefined' && data[i].type === "cal") {
  18. last_cal = i;
  19. break;
  20. }
  21. // only use data from the same device as the most recent BG data point
  22. if (typeof data[i] !== 'undefined' && data[i].glucose > 38 && data[i].device === now.device) {
  23. var then = data[i];
  24. var then_date = then.date || Date.parse(then.display_time) || Date.parse(then.dateString);
  25. var avgdelta = 0;
  26. var minutesago;
  27. if (typeof then_date !== 'undefined' && typeof now_date !== 'undefined') {
  28. minutesago = Math.round( (now_date - then_date) / (1000 * 60) );
  29. // multiply by 5 to get the same units as delta, i.e. mg/dL/5m
  30. change = now.glucose - then.glucose;
  31. avgdelta = change/minutesago * 5;
  32. } else { console.error("Error: date field not found: cannot calculate avgdelta"); }
  33. //if (i < 5) {
  34. //console.error(then.glucose, minutesago, avgdelta);
  35. //}
  36. // use the average of all data points in the last 2.5m for all further "now" calculations
  37. if (-2 < minutesago && minutesago < 2.5) {
  38. now.glucose = ( now.glucose + then.glucose ) / 2;
  39. now_date = ( now_date + then_date ) / 2;
  40. //console.error(then.glucose, now.glucose);
  41. // short_deltas are calculated from everything ~5-15 minutes ago
  42. } else if (2.5 < minutesago && minutesago < 17.5) {
  43. //console.error(minutesago, avgdelta);
  44. short_deltas.push(avgdelta);
  45. // last_deltas are calculated from everything ~5 minutes ago
  46. if (2.5 < minutesago && minutesago < 7.5) {
  47. last_deltas.push(avgdelta);
  48. }
  49. //console.error(then.glucose, minutesago, avgdelta, last_deltas, short_deltas);
  50. // long_deltas are calculated from everything ~20-40 minutes ago
  51. } else if (17.5 < minutesago && minutesago < 42.5) {
  52. long_deltas.push(avgdelta);
  53. }
  54. }
  55. }
  56. var last_delta = 0;
  57. var short_avgdelta = 0;
  58. var long_avgdelta = 0;
  59. if (last_deltas.length > 0) {
  60. last_delta = last_deltas.reduce(function(a, b) { return a + b; }) / last_deltas.length;
  61. }
  62. if (short_deltas.length > 0) {
  63. short_avgdelta = short_deltas.reduce(function(a, b) { return a + b; }) / short_deltas.length;
  64. }
  65. if (long_deltas.length > 0) {
  66. long_avgdelta = long_deltas.reduce(function(a, b) { return a + b; }) / long_deltas.length;
  67. }
  68. return {
  69. delta: Math.round( last_delta * 100 ) / 100
  70. , glucose: Math.round( now.glucose * 100 ) / 100
  71. , noise: Math.round(now.noise)
  72. , short_avgdelta: Math.round( short_avgdelta * 100 ) / 100
  73. , long_avgdelta: Math.round( long_avgdelta * 100 ) / 100
  74. , date: now_date
  75. , last_cal: last_cal
  76. };
  77. };
  78. module.exports = getLastGlucose;