<commit_before>/**
 * Created by pupboss on 3/11/16.
 */
'use strict';

var eventproxy = require('eventproxy');
var moment = require('moment');
var charset = require('superagent-charset');
var request = require('superagent');
var constant = require('./constant');
var config = require('../config');
var mail = require('../utility/mail');

charset(request);

var base_url_index = 4;

var normal_agent = function (u_id, passwd, target, callback) {

  get_cookie(u_id, passwd, function (err, cookie) {
    if (err) {
      return callback(err, null);
    } else {
      get_dom(target, cookie, function (err, final) {
        return callback(err, final);
      })
    }
  });
};

var get_cookie = function (u_id, passwd, callback) {
  request
    .post(constant.urls[base_url_index] + 'j_acegi_security_check')
    .send('j_username=' + u_id)
    .send('j_password=' + <PASSWORD>)
    .timeout(3000)
    .set('Accept', 'application/json')
    .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36')
    .charset('gbk')
    .end(function(err, res) {
      if (err) {
        return callback(constant.cookie.net_error, null);
      }
      var result = res.redirects[0];
      if (result.indexOf('frameset.jsp') > 0) {
        result = result.replace(constant.urls[base_url_index] + 'frameset.jsp;jsessionid=', '');
        return callback(null, result);
      } else {
        return callback(constant.cookie.user_error, null);
      }
    });
};

var get_dom = function (target, cookie, callback) {
  request
    .get(constant.urls[base_url_index] + target)
    .set('Cookie', 'JSESSIONID=' + cookie + '; AJSTAT_ok_times=1')
    .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36')
    .charset('gbk')
    .end(function(err, res) {
      if (err) {
        return callback(constant.cookie.net_error, null);
      }
      var result = res.text;
      if (result.indexOf('综合教务') < 0) {
        return callback(constant.cookie.net_error, null);
      }
      return callback(null, result);
    });
};

var net_speed = function (u_id, passwd, url, callback) {
  request
    .post(url + 'j_acegi_security_check')
    .send('j_username=' + u_id)
    .send('j_password=' + <PASSWORD>)
    .timeout(3000)
    .set('Accept', 'application/json')
    .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36')
    .charset('gbk')
    .end(function(err, res) {
      if (err) {
        return callback(constant.cookie.net_error, null);
      }
      var result = res.redirects[0];
      if (result.indexOf('frameset.jsp') > 0) {
        result = result.replace(constant.urls[base_url_index] + 'frameset.jsp;jsessionid=', '');
        return callback(null, result);
      } else {
        return callback(constant.cookie.user_error, null);
      }
    });
};

var test_speed = function (callback) {

  if (!config.admin.enable) {
    return;
  }

  var content = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZ') + '\n\n';
  var results = [];

  var ep = new eventproxy();

  ep.after('test_speed', constant.urls.length, function () {

    results = results.sort(function (a, b) {
      return a.res - b.res;
    });
    base_url_index = constant.urls.indexOf(results[0].url);

    content += '\n\nChange url to index ' + base_url_index + ' : ' + results[0].url + '\n\n';
    var subj = '';
    subj = subj.concat('【教务在线2.0 ', config.server_name, '】维护日志');
    mail(subj, content, function(err, final) {
    });
    return callback(content);
  });
  
  constant.urls.forEach(function (url) {

    var start = new Date();
    net_speed(config.admin.user_id, config.admin.password, url, function (err) {
      var end = new Date();
      var time_diff = end - start;
      if (err) {
        time_diff = '3600';
      }
      content += 'test: ' + url + '\n' + 'speed: ' + time_diff + '(ms)\n\n';
      results.push({ res: time_diff, url: url });
      ep.emit('test_speed');
    })
  });
};

module.exports = {
  normal_agent: normal_agent,
  get_cookie: get_cookie,
  test_speed: test_speed,
  base_url_index: base_url_index
};
<commit_msg>Removed auto fix mail notification<commit_after>/**
 * Created by pupboss on 3/11/16.
 */
'use strict';

var eventproxy = require('eventproxy');
var moment = require('moment');
var charset = require('superagent-charset');
var request = require('superagent');
var constant = require('./constant');
var config = require('../config');

charset(request);

var base_url_index = 4;

var normal_agent = function (u_id, passwd, target, callback) {

  get_cookie(u_id, passwd, function (err, cookie) {
    if (err) {
      return callback(err, null);
    } else {
      get_dom(target, cookie, function (err, final) {
        return callback(err, final);
      })
    }
  });
};

var get_cookie = function (u_id, passwd, callback) {
  request
    .post(constant.urls[base_url_index] + 'j_acegi_security_check')
    .send('j_username=' + u_id)
    .send('j_password=' + <PASSWORD>)
    .timeout(3000)
    .set('Accept', 'application/json')
    .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36')
    .charset('gbk')
    .end(function(err, res) {
      if (err) {
        return callback(constant.cookie.net_error, null);
      }
      var result = res.redirects[0];
      if (result.indexOf('frameset.jsp') > 0) {
        result = result.replace(constant.urls[base_url_index] + 'frameset.jsp;jsessionid=', '');
        return callback(null, result);
      } else {
        return callback(constant.cookie.user_error, null);
      }
    });
};

var get_dom = function (target, cookie, callback) {
  request
    .get(constant.urls[base_url_index] + target)
    .set('Cookie', 'JSESSIONID=' + cookie + '; AJSTAT_ok_times=1')
    .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36')
    .charset('gbk')
    .end(function(err, res) {
      if (err) {
        return callback(constant.cookie.net_error, null);
      }
      var result = res.text;
      if (result.indexOf('综合教务') < 0) {
        return callback(constant.cookie.net_error, null);
      }
      return callback(null, result);
    });
};

var net_speed = function (u_id, passwd, url, callback) {
  request
    .post(url + 'j_acegi_security_check')
    .send('j_username=' + u_id)
    .send('j_password=' + <PASSWORD>)
    .timeout(3000)
    .set('Accept', 'application/json')
    .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36')
    .charset('gbk')
    .end(function(err, res) {
      if (err) {
        return callback(constant.cookie.net_error, null);
      }
      var result = res.redirects[0];
      if (result.indexOf('frameset.jsp') > 0) {
        result = result.replace(constant.urls[base_url_index] + 'frameset.jsp;jsessionid=', '');
        return callback(null, result);
      } else {
        return callback(constant.cookie.user_error, null);
      }
    });
};

var test_speed = function (callback) {

  if (!config.admin.enable) {
    return;
  }

  var content = moment().format('YYYY-MM-DDTHH:mm:ss.SSSZ') + '\n\n';
  var results = [];

  var ep = new eventproxy();

  ep.after('test_speed', constant.urls.length, function () {

    results = results.sort(function (a, b) {
      return a.res - b.res;
    });
    base_url_index = constant.urls.indexOf(results[0].url);

    content += '\n\nChange url to index ' + base_url_index + ' : ' + results[0].url + '\n\n';
    return callback(content);
  });
  
  constant.urls.forEach(function (url) {

    var start = new Date();
    net_speed(config.admin.user_id, config.admin.password, url, function (err) {
      var end = new Date();
      var time_diff = end - start;
      if (err) {
        time_diff = '3600';
      }
      content += 'test: ' + url + '\n' + 'speed: ' + time_diff + '(ms)\n\n';
      results.push({ res: time_diff, url: url });
      ep.emit('test_speed');
    })
  });
};

module.exports = {
  normal_agent: normal_agent,
  get_cookie: get_cookie,
  test_speed: test_speed,
  base_url_index: base_url_index
};

{'fraction_non_alphanumeric': 0.16535714285714287, 'fraction_numerical': 0.025, 'mean_word_length': 3.015774378585086, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '3781300', 'n_tokens_mistral': 3078, 'n_tokens_neox': 2943, 'n_words': 609}