<commit_before>'use strict';

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

function init() {

  function cage() {
    return cage;
  }

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

  cage.checkNotifications = function checkNotifications(sbx) {
  
    // Just return if no notification times are configured
  
  	if (!sbx.extendedSettings.urgent && !sbx.extendedSettings.warn) {
  	  return;
  	}
  
    var cannulaInfo = cage.findLatestTimeChange(sbx);

	if (!cannulaInfo.checkForAlert) {
	  return;
	}
    
    // CAGE_WARN=48 CAGE_URGENT=70

    if (cannulaInfo.age === sbx.extendedSettings.urgent || cannulaInfo.age === sbx.extendedSettings.warn )
    {
      var title = 'Cannula age ' + cannulaInfo.age +' hours';
      sbx.notifications.requestNotify({
          level: 2
          , title: title
          , message: title + ', time to change?'
          , pushoverSound: 'persistent'
          , plugin: cage
          , debug: {
            cannulaInfo: cannulaInfo
        }
      });
      
    }
  }

  function frac(f) {
    return f % 1;
  }

  cage.findLatestTimeChange = function findLatestTimeChange(sbx) {
  
  var returnValue = {'message':'', 'found': false, 'age': 0, 'treatmentDate': null, 'checkForAlert': false};
  
      _.forEach(sbx.data.treatments, function eachTreatment (treatment) {
      if (treatment.eventType === 'Site Change') {
        returnValue.treatmentDate = new Date(treatment.created_at);
        var hours = Math.round(Math.abs(sbx.time - returnValue.treatmentDate) / 36e5);

        //allow for around 10 minute period after a full hour during which we'll alert the user
        returnValue.checkForAlert = frac(Math.abs(sbx.time - returnValue.treatmentDate) / 36e5) < 0.15;

        if (!returnValue.found) {
          returnValue.found = true;
          returnValue.age = hours;
        } else {
          if (hours < returnValue.age) {
            returnValue.age = hours;
            if (treatment.notes) {
              returnValue.message = treatment.notes;
            }
          }
        }
      }
    });
    
    return returnValue;
  }
  
  cage.updateVisualisation = function updateVisualisation (sbx) {

    var cannulaInfo = cage.findLatestTimeChange(sbx);

    var info = [{label: 'Inserted:', value: moment(cannulaInfo.treatmentDate).format('lll')}];
    if (cannulaInfo.message !== '') info.push({label: 'Notes:', value: cannulaInfo.message});

    var shownAge = cannulaInfo.found ? cannulaInfo.age : 'n/a ';

    sbx.pluginBase.updatePillText(cage, {
      value: shownAge + 'h'
      , label: 'CAGE'
      , info: info
    });
    
  };

  return cage();
}


module.exports = init;<commit_msg>Formatting fixes to make Codacy happy<commit_after>'use strict';

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

function init() {
  return cage();
}

module.exports = init;

function cage() {
  return cage;
}

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

cage.checkNotifications = function checkNotifications(sbx) {

  // Just return if no notification times are configured

  if (!sbx.extendedSettings.urgent && !sbx.extendedSettings.warn) {
    return;
  }

  var cannulaInfo = cage.findLatestTimeChange(sbx);

  if (!cannulaInfo.checkForAlert) {
    return;
  }

  // CAGE_WARN=48 CAGE_URGENT=70

  if (cannulaInfo.age === sbx.extendedSettings.urgent || cannulaInfo.age === sbx.extendedSettings.warn )
  {
    var title = 'Cannula age ' + cannulaInfo.age +' hours';
    sbx.notifications.requestNotify({
      level: 2
      , title: title
      , message: title + ', time to change?'
      , pushoverSound: 'persistent'
      , plugin: cage
      , debug: {
        cannulaInfo: cannulaInfo
      }
    });

  }
}

function frac(f) {
  return f % 1;
}

cage.findLatestTimeChange = function findLatestTimeChange(sbx) {

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

  _.forEach(sbx.data.treatments, function eachTreatment (treatment) {
    if (treatment.eventType === 'Site Change') {
      returnValue.treatmentDate = new Date(treatment.created_at);
      var hours = Math.round(Math.abs(sbx.time - returnValue.treatmentDate) / 36e5);

      //allow for around 10 minute period after a full hour during which we'll alert the user
      returnValue.checkForAlert = frac(Math.abs(sbx.time - returnValue.treatmentDate) / 36e5) < 0.15;

      if (!returnValue.found) {
        returnValue.found = true;
        returnValue.age = hours;
      } else {
        if (hours < returnValue.age) {
          returnValue.age = hours;
          if (treatment.notes) {
            returnValue.message = treatment.notes;
          }
        }
      }
    }
  });

  return returnValue;
}

cage.updateVisualisation = function updateVisualisation (sbx) {

  var cannulaInfo = cage.findLatestTimeChange(sbx);

  var info = [{label: 'Inserted:', value: moment(cannulaInfo.treatmentDate).format('lll')}];
  if (cannulaInfo.message !== '') info.push({label: 'Notes:', value: cannulaInfo.message});

  var shownAge = cannulaInfo.found ? cannulaInfo.age : 'n/a ';

  sbx.pluginBase.updatePillText(cage, {
    value: shownAge + 'h'
    , label: 'CAGE'
    , info: info
  });
};
{'fraction_non_alphanumeric': 0.1304099142040038, 'fraction_numerical': 0.006863679694947569, 'mean_word_length': 2.755189692197566, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '3068915', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1668, 'n_words': 362}