<commit_before>'use strict';

var _ = require('lodash');

var sandbox = require('./sandbox')();
var units = require('./units')();
var iob = require('./plugins/iob')();
var delta = require('./plugins/delta')();
var bwp = require('./plugins/boluswizardpreview')();

var DIRECTIONS = {
  NONE: 0
  , DoubleUp: 1
  , SingleUp: 2
  , FortyFiveUp: 3
  , Flat: 4
  , FortyFiveDown: 5
  , SingleDown: 6
  , DoubleDown: 7
  , 'NOT COMPUTABLE': 8
  , 'RATE OUT OF RANGE': 9
};

function directionToTrend (direction) {
  var trend = 8;
  if (direction in DIRECTIONS) {
    trend = DIRECTIONS[direction];
  }
  return trend;
}

function reverseAndSlice (entries, req) {
  var reversed = entries.slice(0);
  reversed.reverse();
  return reversed.slice(0, req.count);
}


function mapSGVs(req, sbx) {
  function scaleMgdlAPebbleLegacyHackThatWillNotGoAway (bg) {
    if (req.mmol) {
      return units.mgdlToMMOL(bg);
    } else {
      return bg.toString();
    }
  }

  var cal = sbx.lastEntry(sbx.data.cals);

  return _.map(reverseAndSlice(sbx.data.sgvs, req), function transformSGV(sgv) {
    var transformed = {
      sgv: scaleMgdlAPebbleLegacyHackThatWillNotGoAway(sgv.mgdl), trend: directionToTrend(sgv.direction), direction: sgv.direction, datetime: sgv.mills
    };

    if (req.rawbg && cal) {
      transformed.filtered = sgv.filtered;
      transformed.unfiltered = sgv.unfiltered;
      transformed.noise = sgv.noise;
    }

    return transformed;
  });

}

function addExtraData (first, req, sbx) {
  //for compatibility we're keeping battery and iob on the first bg, but they would be better somewhere else

  var data = sbx.data;

  function addDelta() {
    var prev = data.sgvs.length >= 2 ? data.sgvs[data.sgvs.length - 2] : null;
    var current = sbx.lastSGVEntry();

    //for legacy reasons we need to return a 0 for delta if it can't be calculated
    var deltaResult = delta.calc(prev, current, sbx);
    first.bgdelta = deltaResult && deltaResult.scaled || 0;
    if (req.mmol) {
      first.bgdelta = first.bgdelta.toFixed(1);
    }
  }

  function addBattery() {
    if (data.devicestatus && data.devicestatus.uploaderBattery && data.devicestatus.uploaderBattery >= 0) {
      first.battery = data.devicestatus.uploaderBattery.toString();
    }
  }

  function addIOB() {
    if (req.iob) {
      var iobResult = iob.calcTotal(data.treatments, data.profile, Date.now());
      if (iobResult) {
        first.iob = iobResult.display;
      }

      sbx.properties.iob = iobResult;
      var bwpResult = bwp.calc(sbx);
      if (bwpResult) {
        bgs[0].bwp = bwpResult.bolusEstimateDisplay;
        bgs[0].bwpo = bwpResult.outcomeDisplay;
      }
    }
  }

  addDelta();
  addBattery();
  addIOB();
}

function prepareBGs (req, sbx) {
  if (sbx.data.sgvs.length === 0) {
    return [];
  }

  var bgs = mapSGVs(req, sbx);
  addExtraData(bgs[0], req, sbx);

  return bgs;
}
<commit_msg>Fixed to work with latest dev<commit_after>'use strict';

var _ = require('lodash');

var sandbox = require('./sandbox')();
var units = require('./units')();
var iob = require('./plugins/iob')();
var bwp = require('./plugins/boluswizardpreview')();
var delta = require('./plugins/delta')();

var DIRECTIONS = {
  NONE: 0
  , DoubleUp: 1
  , SingleUp: 2
  , FortyFiveUp: 3
  , Flat: 4
  , FortyFiveDown: 5
  , SingleDown: 6
  , DoubleDown: 7
  , 'NOT COMPUTABLE': 8
  , 'RATE OUT OF RANGE': 9
};

function directionToTrend (direction) {
  var trend = 8;
  if (direction in DIRECTIONS) {
    trend = DIRECTIONS[direction];
  }
  return trend;
}

function reverseAndSlice (entries, req) {
  var reversed = entries.slice(0);
  reversed.reverse();
  return reversed.slice(0, req.count);
}


function mapSGVs(req, sbx) {
  function scaleMgdlAPebbleLegacyHackThatWillNotGoAway (bg) {
    if (req.mmol) {
      return units.mgdlToMMOL(bg);
    } else {
      return bg.toString();
    }
  }

  var cal = sbx.lastEntry(sbx.data.cals);

  return _.map(reverseAndSlice(sbx.data.sgvs, req), function transformSGV(sgv) {
    var transformed = {
      sgv: scaleMgdlAPebbleLegacyHackThatWillNotGoAway(sgv.mgdl), trend: directionToTrend(sgv.direction), direction: sgv.direction, datetime: sgv.mills
    };

    if (req.rawbg && cal) {
      transformed.filtered = sgv.filtered;
      transformed.unfiltered = sgv.unfiltered;
      transformed.noise = sgv.noise;
    }

    return transformed;
  });

}

function addExtraData (first, req, sbx) {
  //for compatibility we're keeping battery and iob on the first bg, but they would be better somewhere else

  var data = sbx.data;

  function addDelta() {
    var prev = data.sgvs.length >= 2 ? data.sgvs[data.sgvs.length - 2] : null;
    var current = sbx.lastSGVEntry();

    //for legacy reasons we need to return a 0 for delta if it can't be calculated
    var deltaResult = delta.calc(prev, current, sbx);
    first.bgdelta = deltaResult && deltaResult.scaled || 0;
    if (req.mmol) {
      first.bgdelta = first.bgdelta.toFixed(1);
    }
  }

  function addBattery() {
    if (data.devicestatus && data.devicestatus.uploaderBattery && data.devicestatus.uploaderBattery >= 0) {
      first.battery = data.devicestatus.uploaderBattery.toString();
    }
  }

  function addIOB() {
    if (req.iob) {
      var iobResult = iob.calcTotal(data.treatments, data.profile, Date.now());
      if (iobResult) {
        first.iob = iobResult.display;
      }
      
      sbx.properties.iob = iobResult;
      var bwpResult = bwp.calc(sbx);

      if (bwpResult) {
        first.bwp = bwpResult.bolusEstimateDisplay;
        first.bwpo = bwpResult.outcomeDisplay;
      }
      
    }
  }

  addDelta();
  addBattery();
  addIOB();
}

function prepareBGs (req, sbx) {
  if (sbx.data.sgvs.length === 0) {
    return [];
  }

  var bgs = mapSGVs(req, sbx);
  addExtraData(bgs[0], req, sbx);

  return bgs;
}

{'fraction_non_alphanumeric': 0.13566289825282632, 'fraction_numerical': 0.0075368276807125725, 'mean_word_length': 3.077513966480447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3983495', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2045, 'n_words': 469}