<commit_before>'use strict';

var _ = require('lodash');
var moment = require('moment');

function cage() {
  return cage;
}

function init() {
  return cage();
}

module.exports = init;

cage.label = 'Cannula Age';
cage.pluginType = 'pill-minor';

cage.getPrefs = function getPrefs(sbx) {
  // CAGE_INFO = 44 CAGE_WARN=48 CAGE_URGENT=70
  return {
    'info' : sbx.extendedSettings.info ? sbx.extendedSettings.info : 44,
    'warn' : sbx.extendedSettings.warn ? sbx.extendedSettings.warn : 48,
    'urgent' : sbx.extendedSettings.urgent ? sbx.extendedSettings.urgent : 72,
    'enableAlerts' : sbx.extendedSettings.enablealerts
  };
};

cage.checkNotifications = function checkNotifications(sbx) {

  var cannulaInfo = cage.findLatestTimeChange(sbx);
  var prefs = cage.getPrefs(sbx);

  if (!prefs.enableAlerts || !cannulaInfo.checkForAlert) { return; }
  
  var sendNotification = false;
  var title = 'Cannula age ' + cannulaInfo.age +' hours';  
  var sound = 'incoming';
  var msg = ', change due soon';
  var level = 1;

  if (cannulaInfo.age === prefs.info) { sendNotification = true; }
  if (cannulaInfo.age === prefs.warn) { sendNotification = true; msg = ', time to change'; }
  if (cannulaInfo.age === prefs.urgent) { sendNotification = true; msg = ', change overdue!'; sound = 'persistent'; level = 2;}

  if (sendNotification) {
    sbx.notifications.requestNotify({
      level: level
      , title: title
      , message: title + msg
      , pushoverSound: sound
      , plugin: cage
      , debug: {
        cannulaInfo: cannulaInfo
      }
    });
  }
};

cage.findLatestTimeChange = function findLatestTimeChange(sbx) {

  var returnValue = {'message':'', 'found': false, 'age': 0, 'treatmentDate': null, 'checkForAlert': false};

  var prevDate = 0;

  _.forEach(sbx.data.treatments, function eachTreatment (treatment) {
    var treatmentDate = treatment.mills;
    if (treatment.eventType === 'Site Change' && treatmentDate > prevDate && treatmentDate < sbx.time) {
    
      prevDate = treatmentDate;
      returnValue.treatmentDate = treatmentDate;

      //allow for 10 minute period after a full hour during which we'll alert the user
      var a = moment(sbx.time);
      var b = moment(returnValue.treatmentDate);
      var hours = a.diff(b,'hours');
      var minFractions = a.diff(b,'minutes') - hours * 60;
      
      returnValue.checkForAlert = minFractions <= 10;
      
      if (!returnValue.found) {
        returnValue.found = true;
        returnValue.age = hours;
      } else {
        if (hours > 0 && hours < returnValue.age) {
          returnValue.age = hours;
          if (treatment.notes) {
            returnValue.message = treatment.notes;
          }
        }
      }
    }
  });

  return returnValue;
};

cage.updateVisualisation = function updateVisualisation (sbx) {<commit_msg>Fixes old CAGE notes being displayed on newer site change events, as reported on https://github.com/nightscout/cgm-remote-monitor/issues/699<commit_after>'use strict';

var _ = require('lodash');
var moment = require('moment');

function cage() {
  return cage;
}

function init() {
  return cage();
}

module.exports = init;

cage.label = 'Cannula Age';
cage.pluginType = 'pill-minor';

cage.getPrefs = function getPrefs(sbx) {
  // CAGE_INFO = 44 CAGE_WARN=48 CAGE_URGENT=70
  return {
    'info' : sbx.extendedSettings.info ? sbx.extendedSettings.info : 44,
    'warn' : sbx.extendedSettings.warn ? sbx.extendedSettings.warn : 48,
    'urgent' : sbx.extendedSettings.urgent ? sbx.extendedSettings.urgent : 72,
    'enableAlerts' : sbx.extendedSettings.enablealerts
  };
};

cage.checkNotifications = function checkNotifications(sbx) {

  var cannulaInfo = cage.findLatestTimeChange(sbx);
  var prefs = cage.getPrefs(sbx);

  if (!prefs.enableAlerts || !cannulaInfo.checkForAlert) { return; }
  
  var sendNotification = false;
  var title = 'Cannula age ' + cannulaInfo.age +' hours';  
  var sound = 'incoming';
  var msg = ', change due soon';
  var level = 1;

  if (cannulaInfo.age === prefs.info) { sendNotification = true; }
  if (cannulaInfo.age === prefs.warn) { sendNotification = true; msg = ', time to change'; }
  if (cannulaInfo.age === prefs.urgent) { sendNotification = true; msg = ', change overdue!'; sound = 'persistent'; level = 2;}

  if (sendNotification) {
    sbx.notifications.requestNotify({
      level: level
      , title: title
      , message: title + msg
      , pushoverSound: sound
      , plugin: cage
      , debug: {
        cannulaInfo: cannulaInfo
      }
    });
  }
};

cage.findLatestTimeChange = function findLatestTimeChange(sbx) {

  var returnValue = {'message':'', 'found': false, 'age': 0, 'treatmentDate': null, 'checkForAlert': false};

  var prevDate = 0;

  _.forEach(sbx.data.treatments, function eachTreatment (treatment) {
    var treatmentDate = treatment.mills;
    if (treatment.eventType === 'Site Change' && treatmentDate > prevDate && treatmentDate < sbx.time) {
    
      prevDate = treatmentDate;
      returnValue.treatmentDate = treatmentDate;

      //allow for 10 minute period after a full hour during which we'll alert the user
      var a = moment(sbx.time);
      var b = moment(returnValue.treatmentDate);
      var hours = a.diff(b,'hours');
      var minFractions = a.diff(b,'minutes') - hours * 60;
      
      returnValue.checkForAlert = minFractions <= 10;
      
      if (!returnValue.found) {
        returnValue.found = true;
        returnValue.age = hours;
      } else {
        if (hours > 0 && hours < returnValue.age) {
          returnValue.age = hours;
          if (treatment.notes) {
            returnValue.message = treatment.notes;
          } else {
            returnValue.message = '';
          }
        }
      }
    }
  });

  return returnValue;
};

cage.updateVisualisation = function updateVisualisation (sbx) {
{'fraction_non_alphanumeric': 0.1284591732148958, 'fraction_numerical': 0.008370345063204646, 'mean_word_length': 3.0351481736733286, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '781905', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1845, 'n_words': 440}