calc-glucose-stats.js 979 B

12345678910111213141516171819202122232425262728293031
  1. const moment = require('moment');
  2. const _ = require('lodash');
  3. const stats = require('./glucose-stats');
  4. module.exports = {};
  5. const calcStatsExports = module.exports;
  6. calcStatsExports.updateGlucoseStats = (options) => {
  7. var hist = _.map(_.sortBy(options.glucose_hist, 'dateString'), function readDate(value) {
  8. value.readDateMills = moment(value.dateString).valueOf();
  9. return value;
  10. });
  11. if (hist && hist.length > 0) {
  12. var noise_val = stats.calcSensorNoise(null, hist, null, null);
  13. var ns_noise_val = stats.calcNSNoise(noise_val, hist);
  14. if ('noise' in options.glucose_hist[0]) {
  15. console.error("Glucose noise CGM reported level: ", options.glucose_hist[0].noise);
  16. ns_noise_val = Math.max(ns_noise_val, options.glucose_hist[0].noise);
  17. }
  18. console.error("Glucose noise calculated: ", noise_val, " setting noise level to ", ns_noise_val);
  19. options.glucose_hist[0].noise = ns_noise_val;
  20. }
  21. return options.glucose_hist;
  22. };