<commit_before>var request = require('request'),
	fs = require('fs'),
	settings = require('./settings.js');

/* torrents = {
 *		"a1b2c3" : {
 *			[...],
 *			hour : {
 *				time : 012345,
 *				value : 67890	
 *			},
 *			day : {
 *				time : 012345,
 *				value : 67890	
 *			}
 *		},
 *		[...]
 */
var torrents = {},
	session = {},
	hdrs = {},
	maxRatio, minDiff, MaxDiff, maxDH, maxDD, maxDTD;

function round(x, n) {
	var p = Math.pow(10, n);
	return Math.round(x*p)/p;
}

function connRpc(method, args, cb) {
	var tag = Math.round(Math.random()*10000);

	request.post({
		url : settings['rpc_addr'],
		method : "POST",
		headers : hdrs,
		body : JSON.stringify({
			method : method,
			arguments : args,
			tag : tag
		}),
		auth : {
			user : settings['rpc_user'],
			pass : settings['rpc_pass']
		},
		json : true
	}, function(err, response, body) {
		if (err) console.log(err);

		if (response.statusCode == 409) {
			var sid = response.headers["x-transmission-session-id"];
			//console.log("Switching to sid "+sid);
			hdrs["X-Transmission-Session-Id"] = sid;
			return connRpc(method, args, cb);
		}
		
		if (body.tag == tag) cb(body.arguments);
	});
}

function loadFromRpc() {
	connRpc("torrent-get", {
		fields : ["id", "name", "totalSize", "downloadedEver", "uploadedEver", "activityDate", "downloadDir", "rateDownload", "rateUpload", "status", "hashString"]
	}, function(data) {
		var newTorrents = {},
			tor, hash;

		maxRatio = 0;
		minDiff = 0;
		maxDiff = 0;
		maxDH = 0;
		maxDD = 0;
		maxDTD = 0;

		for (var i = 0; i<data.torrents.length; i++) {
			tor = data.torrents[i];
			hash = tor.hashString.substring(0, 6);

			torrents[hash] = torrents[hash] || {};
			for (var key in tor) torrents[hash][key] = tor[key];

			// More fields :
			torrents[hash].active = tor.status == 4 || tor.status == 6;
			torrents[hash].hash = hash;
			torrents[hash].pct = round(tor.downloadedEver*100/tor.totalSize, 2);
			torrents[hash].diff = tor.uploadedEver - tor.downloadedEver;
			torrents[hash].ratio = tor.downloadedEver > 0 ? round(tor.uploadedEver / tor.downloadedEver, 2) : "+ &infin;";
			torrents[hash].altRatio = round(tor.uploadedEver / tor.totalSize, 2);
			if (torrents[hash].hour) {
				torrents[hash].deltaH = tor.uploadedEver - torrents[hash].hour.value;
				torrents[hash].deltaD = tor.uploadedEver - torrents[hash].day.value;
				torrents[hash].deltaTD = tor.uploadedEver - torrents[hash].tday.value;
			}

			newTorrents[hash] = torrents[hash];

			if (typeof torrents[hash].ratio === "number") maxRatio = Math.max(maxRatio, torrents[hash].ratio);
			maxDiff = Math.max(maxDiff, torrents[hash].diff);
			minDiff = Math.max(minDiff, -torrents[hash].diff);
			if (torrents[hash].hour) {
				maxDH = Math.max(maxDH, torrents[hash].deltaH);
				maxDD = Math.max(maxDD, torrents[hash].deltaD);
				maxDTD = Math.max(maxDTD, torrents[hash].deltaTD);
			}
		}

		torrents = newTorrents;
	});
}

function sessionRpc() {
	connRpc("session-stats", {}, function(data) {
		session = data;
	});
}

function loadFromLog() {
	fs.readFile("../scrap/"+settings['file_storage'], "utf8", function(err, data) {
		if (err) {
			console.log(err);
			return res.status(500).send(err);
		}

		var lines = data.split('\n'),
			now = new Date()/1000,
			hourAgo = now - 36000,<commit_msg>Fixed hourAgo constant, giving ten hours history instead of one hour.<commit_after>var request = require('request'),
	fs = require('fs'),
	settings = require('./settings.js');

/* torrents = {
 *		"a1b2c3" : {
 *			[...],
 *			hour : {
 *				time : 012345,
 *				value : 67890	
 *			},
 *			day : {
 *				time : 012345,
 *				value : 67890	
 *			}
 *		},
 *		[...]
 */
var torrents = {},
	session = {},
	hdrs = {},
	maxRatio, minDiff, MaxDiff, maxDH, maxDD, maxDTD;

function round(x, n) {
	var p = Math.pow(10, n);
	return Math.round(x*p)/p;
}

function connRpc(method, args, cb) {
	var tag = Math.round(Math.random()*10000);

	request.post({
		url : settings['rpc_addr'],
		method : "POST",
		headers : hdrs,
		body : JSON.stringify({
			method : method,
			arguments : args,
			tag : tag
		}),
		auth : {
			user : settings['rpc_user'],
			pass : settings['rpc_pass']
		},
		json : true
	}, function(err, response, body) {
		if (err) console.log(err);

		if (response.statusCode == 409) {
			var sid = response.headers["x-transmission-session-id"];
			//console.log("Switching to sid "+sid);
			hdrs["X-Transmission-Session-Id"] = sid;
			return connRpc(method, args, cb);
		}
		
		if (body.tag == tag) cb(body.arguments);
	});
}

function loadFromRpc() {
	connRpc("torrent-get", {
		fields : ["id", "name", "totalSize", "downloadedEver", "uploadedEver", "activityDate", "downloadDir", "rateDownload", "rateUpload", "status", "hashString"]
	}, function(data) {
		var newTorrents = {},
			tor, hash;

		maxRatio = 0;
		minDiff = 0;
		maxDiff = 0;
		maxDH = 0;
		maxDD = 0;
		maxDTD = 0;

		for (var i = 0; i<data.torrents.length; i++) {
			tor = data.torrents[i];
			hash = tor.hashString.substring(0, 6);

			torrents[hash] = torrents[hash] || {};
			for (var key in tor) torrents[hash][key] = tor[key];

			// More fields :
			torrents[hash].active = tor.status == 4 || tor.status == 6;
			torrents[hash].hash = hash;
			torrents[hash].pct = round(tor.downloadedEver*100/tor.totalSize, 2);
			torrents[hash].diff = tor.uploadedEver - tor.downloadedEver;
			torrents[hash].ratio = tor.downloadedEver > 0 ? round(tor.uploadedEver / tor.downloadedEver, 2) : "+ &infin;";
			torrents[hash].altRatio = round(tor.uploadedEver / tor.totalSize, 2);
			if (torrents[hash].hour) {
				torrents[hash].deltaH = tor.uploadedEver - torrents[hash].hour.value;
				torrents[hash].deltaD = tor.uploadedEver - torrents[hash].day.value;
				torrents[hash].deltaTD = tor.uploadedEver - torrents[hash].tday.value;
			}

			newTorrents[hash] = torrents[hash];

			if (typeof torrents[hash].ratio === "number") maxRatio = Math.max(maxRatio, torrents[hash].ratio);
			maxDiff = Math.max(maxDiff, torrents[hash].diff);
			minDiff = Math.max(minDiff, -torrents[hash].diff);
			if (torrents[hash].hour) {
				maxDH = Math.max(maxDH, torrents[hash].deltaH);
				maxDD = Math.max(maxDD, torrents[hash].deltaD);
				maxDTD = Math.max(maxDTD, torrents[hash].deltaTD);
			}
		}

		torrents = newTorrents;
	});
}

function sessionRpc() {
	connRpc("session-stats", {}, function(data) {
		session = data;
	});
}

function loadFromLog() {
	fs.readFile("../scrap/"+settings['file_storage'], "utf8", function(err, data) {
		if (err) {
			console.log(err);
			return res.status(500).send(err);
		}

		var lines = data.split('\n'),
			now = new Date()/1000,
			hourAgo = now - 3600,
{'fraction_non_alphanumeric': 0.1847220129576616, 'fraction_numerical': 0.01973783335844508, 'mean_word_length': 3.4670255720053835, 'pattern_counts': {'":': 0, '<': 5, '<?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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7012284', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2501, 'n_words': 481}