<commit_before>'use strict';

var _ = require('lodash');

function init() {

  var TIME_10_MINS_MS = 10 * 60 * 1000;

  function treatmentnotify() {
    return treatmentnotify;
  }

  treatmentnotify.label = 'Treatment Notifications';
  treatmentnotify.pluginType = 'notification';

  treatmentnotify.checkNotifications = function checkNotifications (sbx) {

    var now = Date.now();

    var lastMBG = _.last(sbx.data.mbgs);
    var lastTreatment = _.last(sbx.data.treatments);

    //TODO: figure out why date is x here #CleanUpDataModel
    var lastMBGTime = lastMBG ? lastMBG.x : 0;
    var mbgAgo = (lastMBGTime && lastMBGTime < now) ? now - lastMBGTime : 0;
    var mbgCurrent = mbgAgo != 0 && mbgAgo < TIME_10_MINS_MS;

    var lastTreatmentTime = lastTreatment ? new Date(lastTreatment.created_at).getTime() : 0;
    var treatmentAgo = (lastTreatmentTime && lastTreatmentTime < now) ? now - lastTreatmentTime : 0;
    var treatmentCurrent = treatmentAgo != 0 && treatmentAgo < TIME_10_MINS_MS;

    if (mbgCurrent || treatmentCurrent) {
      autoSnoozeAlarms(lastMBGTime, lastTreatmentTime, sbx);
      //and add some info notifications
      //the notification providers (push, websockets, etc) are responsible to not sending the same notifications repeatedly
      if (mbgCurrent) requestMBGNotify(lastMBG, sbx);
      if (treatmentCurrent) requestTreatmentNotify(lastTreatment, sbx);
    }
  };

  /**
   * auto snooze any alarms by max(treatment, mbg) time + 10m
   */
  function autoSnoozeAlarms(lastMBGTime, lastTreatmentTime, sbx) {
    sbx.notifications.requestSnooze({
      level: sbx.notifications.levels.URGENT
      , mills: Math.max(lastMBGTime, lastTreatmentTime) + TIME_10_MINS_MS
      //, debug: results
    });
  }

  function requestMBGNotify (lastMBG, sbx) {
    sbx.notifications.requestNotify({
      level: sbx.notifications.levels.INFO
      , title: 'Calibration' //assume all MGBs are calibrations for now
      //TODO: figure out why mbg is y here #CleanUpDataModel
      , message: '\nMeter BG: ' + sbx.scaleBg(lastMBG.y) + ' ' + sbx.unitsLabel
      , pushoverSound: 'magic'
      //, debug: results
    });
  }

  function requestTreatmentNotify (lastTreatment, sbx) {<commit_msg>just use the snooze time, don't add it to the last treatment time<commit_after>'use strict';

var _ = require('lodash');

function init() {

  var TIME_10_MINS_MS = 10 * 60 * 1000;

  function treatmentnotify() {
    return treatmentnotify;
  }

  treatmentnotify.label = 'Treatment Notifications';
  treatmentnotify.pluginType = 'notification';

  treatmentnotify.checkNotifications = function checkNotifications (sbx) {

    var now = Date.now();

    var lastMBG = _.last(sbx.data.mbgs);
    var lastTreatment = _.last(sbx.data.treatments);

    //TODO: figure out why date is x here #CleanUpDataModel
    var lastMBGTime = lastMBG ? lastMBG.x : 0;
    var mbgAgo = (lastMBGTime && lastMBGTime < now) ? now - lastMBGTime : 0;
    var mbgCurrent = mbgAgo != 0 && mbgAgo < TIME_10_MINS_MS;

    var lastTreatmentTime = lastTreatment ? new Date(lastTreatment.created_at).getTime() : 0;
    var treatmentAgo = (lastTreatmentTime && lastTreatmentTime < now) ? now - lastTreatmentTime : 0;
    var treatmentCurrent = treatmentAgo != 0 && treatmentAgo < TIME_10_MINS_MS;

    if (mbgCurrent || treatmentCurrent) {
      autoSnoozeAlarms(sbx);
      //and add some info notifications
      //the notification providers (push, websockets, etc) are responsible to not sending the same notifications repeatedly
      if (mbgCurrent) requestMBGNotify(lastMBG, sbx);
      if (treatmentCurrent) requestTreatmentNotify(lastTreatment, sbx);
    }
  };

  function autoSnoozeAlarms(sbx) {
    sbx.notifications.requestSnooze({
      level: sbx.notifications.levels.URGENT
      , mills: TIME_10_MINS_MS
      //, debug: results
    });
  }

  function requestMBGNotify (lastMBG, sbx) {
    sbx.notifications.requestNotify({
      level: sbx.notifications.levels.INFO
      , title: 'Calibration' //assume all MGBs are calibrations for now
      //TODO: figure out why mbg is y here #CleanUpDataModel
      , message: '\nMeter BG: ' + sbx.scaleBg(lastMBG.y) + ' ' + sbx.unitsLabel
      , pushoverSound: 'magic'
      //, debug: results
    });
  }

  function requestTreatmentNotify (lastTreatment, sbx) {
{'fraction_non_alphanumeric': 0.10549501507071644, 'fraction_numerical': 0.010665430095061441, 'mean_word_length': 3.51255230125523, 'pattern_counts': {'":': 0, '<': 11, '<?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': '2337824', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1357, 'n_words': 347}