<commit_before>'use strict';

var _ = require('lodash');
var $ = (global && global.$) || require('jquery');
var d3 = (global && global.d3) || require('d3');
var shiroTrie = require('shiro-trie');

var Storages = require('js-storage');

var language = require('../language')();
var sandbox = require('../sandbox')();
var profile = require('../profilefunctions')();
var units = require('../units')();
var levels = require('../levels');
var times = require('../times');
var receiveDData = require('./receiveddata');

var brushing = false;

var client = {};

$('#loadingMessageText').html('Connecting to server');

client.hashauth = require('../hashauth').init(client, $);

client.headers = function headers () {
  if (client.authorized) {
    return {
      Authorization: 'Bearer ' + client.authorized.token
    };
  } else if (client.hashauth) {
    return {
      'api-secret': client.hashauth.hash()
    };
  } else {
    return {};
  }
};

client.crashed = function crashed () {
  $('#centerMessagePanel').show();
  $('#loadingMessageText').html('It appears the server has crashed. Please go to Heroku or Azure and reboot the server.');
}

client.init = function init (callback) {

  client.browserUtils = require('./browser-utils')($);

  var token = client.browserUtils.queryParms().token;
  var secret = client.hashauth.apisecrethash || Storages.localStorage.get('apisecrethash');

  var src = '/api/v1/status.json?t=' + new Date().getTime();

  if (secret) {
    src += '&secret=' + secret;
  } else if (token) {
    src += '&token=' + token;
  }

  $.ajax({
    method: 'GET'
    , url: src
    , headers: client.headers()
  }).done(function success (serverSettings) {
    if (serverSettings.runtimeState !== 'loaded') {
      console.log('Server is still loading data');
      $('#loadingMessageText').html('Server is loading data, retrying load in 2 seconds');
      window.setTimeout(window.Nightscout.client.init(), 2000);
      return;
    }
    client.settingsFailed = false;
    console.log('Application appears to be online');
    $('#centerMessagePanel').hide();
    client.load(serverSettings, callback);
  }).fail(function fail (jqXHR) {

    // check if we couldn't reach the server at all, show offline message
    if (!jqXHR.readyState) {
      console.log('Application appears to be OFFLINE');
      $('#loadingMessageText').html('Connecting to Nightscout server failed, retrying every 2 seconds');
      window.setTimeout(window.Nightscout.client.init(), 2000);
      return;
    }

    //no server setting available, use defaults, auth, etc
    if (client.settingsFailed) {
      console.log('Already tried to get settings after auth, but failed');
    } else {
      client.settingsFailed = true;

      // detect browser language
      var lang = Storages.localStorage.get('language') || (navigator.language || navigator.userLanguage).toLowerCase();
      if (lang !== 'zh_cn' && lang !== 'zh-cn' && lang !== 'zh_tw' && lang !== 'zh-tw') {
        lang = lang.substring(0, 2);
      } else {
        lang = lang.replace('-', '_');
      }
      if (language.languages.find(l => l.code === lang)) {
        language.set(lang);
      } else {
        language.set('en');
      }

      client.translate = language.translate;
      // auth failed, hide loader and request for key
      $('#centerMessagePanel').hide();
      client.hashauth.requestAuthentication(function afterRequest () {
        client.init(callback);<commit_msg>Increase load interval to 5 seconds<commit_after>'use strict';

var _ = require('lodash');
var $ = (global && global.$) || require('jquery');
var d3 = (global && global.d3) || require('d3');
var shiroTrie = require('shiro-trie');

var Storages = require('js-storage');

var language = require('../language')();
var sandbox = require('../sandbox')();
var profile = require('../profilefunctions')();
var units = require('../units')();
var levels = require('../levels');
var times = require('../times');
var receiveDData = require('./receiveddata');

var brushing = false;

var client = {};

$('#loadingMessageText').html('Connecting to server');

client.hashauth = require('../hashauth').init(client, $);

client.headers = function headers () {
  if (client.authorized) {
    return {
      Authorization: 'Bearer ' + client.authorized.token
    };
  } else if (client.hashauth) {
    return {
      'api-secret': client.hashauth.hash()
    };
  } else {
    return {};
  }
};

client.crashed = function crashed () {
  $('#centerMessagePanel').show();
  $('#loadingMessageText').html('It appears the server has crashed. Please go to Heroku or Azure and reboot the server.');
}

client.init = function init (callback) {

  client.browserUtils = require('./browser-utils')($);

  var token = client.browserUtils.queryParms().token;
  var secret = client.hashauth.apisecrethash || Storages.localStorage.get('apisecrethash');

  var src = '/api/v1/status.json?t=' + new Date().getTime();

  if (secret) {
    src += '&secret=' + secret;
  } else if (token) {
    src += '&token=' + token;
  }

  $.ajax({
    method: 'GET'
    , url: src
    , headers: client.headers()
  }).done(function success (serverSettings) {
    if (serverSettings.runtimeState !== 'loaded') {
      console.log('Server is still loading data');
      $('#loadingMessageText').html('Server is starting and still loading data, retrying load in 5 seconds');
      window.setTimeout(window.Nightscout.client.init(), 5000);
      return;
    }
    client.settingsFailed = false;
    console.log('Application appears to be online');
    $('#centerMessagePanel').hide();
    client.load(serverSettings, callback);
  }).fail(function fail (jqXHR) {

    // check if we couldn't reach the server at all, show offline message
    if (!jqXHR.readyState) {
      console.log('Application appears to be OFFLINE');
      $('#loadingMessageText').html('Connecting to Nightscout server failed, retrying every 5 seconds');
      window.setTimeout(window.Nightscout.client.init(), 5000);
      return;
    }

    //no server setting available, use defaults, auth, etc
    if (client.settingsFailed) {
      console.log('Already tried to get settings after auth, but failed');
    } else {
      client.settingsFailed = true;

      // detect browser language
      var lang = Storages.localStorage.get('language') || (navigator.language || navigator.userLanguage).toLowerCase();
      if (lang !== 'zh_cn' && lang !== 'zh-cn' && lang !== 'zh_tw' && lang !== 'zh-tw') {
        lang = lang.substring(0, 2);
      } else {
        lang = lang.replace('-', '_');
      }
      if (language.languages.find(l => l.code === lang)) {
        language.set(lang);
      } else {
        language.set('en');
      }

      client.translate = language.translate;
      // auth failed, hide loader and request for key
      $('#centerMessagePanel').hide();
      client.hashauth.requestAuthentication(function afterRequest () {
        client.init(callback);
{'fraction_non_alphanumeric': 0.168033376492591, 'fraction_numerical': 0.004747518342684506, 'mean_word_length': 3.3477173233270796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1159318', 'n_tokens_mistral': 2070, 'n_tokens_neox': 2023, 'n_words': 519}