<commit_before>var config = require('./config.js');
phantom.casperPath = './vendor/casperjs';
phantom.injectJs('./vendor/casperjs/bin/bootstrap.js');

////////////////////////////////////////////////////////////////
// transmit data from carelink to nightscout
// data looks like this: https://gist.github.com/mddub/b033ec0c800deec02471

var Rusha = require('./vendor/rusha.js');

var NIGHTSCOUT_ENTRY_TYPE = 'reported_active_bolus';

var isNewData = (function() {
  var lastUpdateTime;

  return function(data) {
    var oldLastUpdateTime = lastUpdateTime;
    lastUpdateTime = data['lastMedicalDeviceDataUpdateServerTime'];
    return lastUpdateTime !== oldLastUpdateTime;
  };
})();

function addTimeToEntry(pumpTimeString, entry) {
  var timeUTC = Date.parse(pumpTimeString + ' ' + config.PUMP_TIMEZONE);
  entry['date'] = timeUTC;
  entry['dateString'] = new Date(timeUTC).toISOString();
  return entry;
}

function transformForNightscout(data) {
  var entries = [];

  if(data['activeInsulin']) {
    entries.push(
      addTimeToEntry(
        data['activeInsulin']['datetime'],
        {
          'type': NIGHTSCOUT_ENTRY_TYPE,
          'insulin': data['activeInsulin']['amount']
        }
      )
    );
  }

  if(data['sgs'] && data['sgs'].length) {
    var sgvs = data['sgs'].filter(function(entry) {
      return entry['kind'] === 'SG' && entry['sg'] !== 0;
    });
    // TODO: don't assume minimed will continue giving sensor glucose values ordered by date ascending
    for(var i = Math.max(0, sgvs.length - config.NUM_RECORDS_TO_SUBMIT); i < sgvs.length; i++) {
      var sgv = sgvs[i];
      entries.push(
        addTimeToEntry(
          sgv['datetime'],
          {
            'type': 'sgv',
            'sgv': sgv['sg'],
          }
        )
      );
    }
  }

  entries.forEach(function(entry) {
    entry['device'] = 'MiniMed Connect ' + data['medicalDeviceFamily'] + ' ' + data['medicalDeviceSerialNumber'];
  });

  var activeEntry = entries.filter(function(e) { return e['type'] === NIGHTSCOUT_ENTRY_TYPE; })[0];
  var activeIns = activeEntry ? activeEntry['activeInsulin'] + ' at ' + activeEntry['dateString'] : 'unknown';
  var sgvEntries = entries.filter(function(e) { return e['type'] === 'sgv'; });
  var recentSgv = sgvEntries.length ? sgvEntries[sgvEntries.length - 1]['sgv'] + ' at ' + activeEntry['dateString'] : 'unknown';
  casper.log('active insulin ' + activeIns, 'info');
  casper.log('sensor glucose ' + recentSgv, 'info');

  return entries;
}

function sendToNightscout(data, callback) {
  var endpoint = config.NIGHTSCOUT_HOST + '/api/v1/entries.json';
  var entries = transformForNightscout(data);
  if(entries.length === 0) {
    casper.log('No data found in CareLink JSON: ' + JSON.stringify(data), 'error');
    return callback.apply(casper);
  }

  casper.log('POST ' + endpoint + ' ' + JSON.stringify(entries), 'info');
  casper.open(endpoint, {
    method: 'post',
    data: JSON.stringify(entries),
    headers: {<commit_msg>Fix logging<commit_after>var config = require('./config.js');
phantom.casperPath = './vendor/casperjs';
phantom.injectJs('./vendor/casperjs/bin/bootstrap.js');

////////////////////////////////////////////////////////////////
// transmit data from carelink to nightscout
// data looks like this: https://gist.github.com/mddub/b033ec0c800deec02471

var Rusha = require('./vendor/rusha.js');

var ACTIVE_INSULIN_ENTRY_TYPE = 'reported_active_bolus';
var SENSOR_GLUCOSE_ENTRY_TYPE = 'sgv';

var isNewData = (function() {
  var lastUpdateTime;

  return function(data) {
    var oldLastUpdateTime = lastUpdateTime;
    lastUpdateTime = data['lastMedicalDeviceDataUpdateServerTime'];
    return lastUpdateTime !== oldLastUpdateTime;
  };
})();

function addTimeToEntry(pumpTimeString, entry) {
  var timeUTC = Date.parse(pumpTimeString + ' ' + config.PUMP_TIMEZONE);
  entry['date'] = timeUTC;
  entry['dateString'] = new Date(timeUTC).toISOString();
  return entry;
}

function transformForNightscout(data) {
  var entries = [];

  if(data['activeInsulin'] && data['activeInsulin']['datetime'] && data['activeInsulin']['amount'] >= 0) {
    entries.push(
      addTimeToEntry(
        data['activeInsulin']['datetime'],
        {
          'type': ACTIVE_INSULIN_ENTRY_TYPE,
          'insulin': data['activeInsulin']['amount']
        }
      )
    );
  }

  if(data['sgs'] && data['sgs'].length) {
    var sgvs = data['sgs'].filter(function(entry) {
      return entry['kind'] === 'SG' && entry['sg'] !== 0;
    });
    // TODO: don't assume minimed will continue giving sensor glucose values ordered by date ascending
    for(var i = Math.max(0, sgvs.length - config.NUM_RECORDS_TO_SUBMIT); i < sgvs.length; i++) {
      var sgv = sgvs[i];
      entries.push(
        addTimeToEntry(
          sgv['datetime'],
          {
            'type': SENSOR_GLUCOSE_ENTRY_TYPE,
            'sgv': sgv['sg'],
          }
        )
      );
    }
  }

  entries.forEach(function(entry) {
    entry['device'] = 'MiniMed Connect ' + data['medicalDeviceFamily'] + ' ' + data['medicalDeviceSerialNumber'];
  });

  var activeEntry = entries.filter(function(e) { return e['type'] === ACTIVE_INSULIN_ENTRY_TYPE; })[0];
  var activeIns = activeEntry ? activeEntry['insulin'] + ' at ' + activeEntry['dateString'] : 'unknown';
  var sgvEntries = entries.filter(function(e) { return e['type'] === SENSOR_GLUCOSE_ENTRY_TYPE; });
  var recentSgv = sgvEntries.length ? sgvEntries[sgvEntries.length - 1]['sgv'] + ' at ' + sgvEntries[sgvEntries.length - 1]['dateString'] : 'unknown';
  casper.log('active insulin ' + activeIns, 'info');
  casper.log('sensor glucose ' + recentSgv, 'info');

  return entries;
}

function sendToNightscout(data, callback) {
  var endpoint = config.NIGHTSCOUT_HOST + '/api/v1/entries.json';
  var entries = transformForNightscout(data);
  if(entries.length === 0) {
    casper.log('No data found in CareLink JSON: ' + JSON.stringify(data), 'error');
    return callback.apply(casper);
  }

  casper.log('POST ' + endpoint + ' ' + JSON.stringify(entries), 'info');
  casper.open(endpoint, {
    method: 'post',
    data: JSON.stringify(entries),
    headers: {
{'fraction_non_alphanumeric': 0.17865278229742923, 'fraction_numerical': 0.006182883176049463, 'mean_word_length': 3.3657670454545454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '555843', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1958, 'n_words': 372}