<commit_before>// console.log polyfill
window.log = function() {
	log.history = log.history || [];
	log.history.push(arguments);
	if(this.console){
		console.log( Array.prototype.slice.call(arguments) );
	}
};

function BaseChannelPath() {
	var base_channel = "/helios/phoenix"
	return base_channel;
}

function SubChannelPath(path) {
	return BaseChannelPath() + "/" + path;
}

function AuthorizeMachine(info){
	$.post(cake.base_url + "machines/add", info, function(data) {
		window.location.reload();
	});
}

function AddMachineEntry(payload, columns) {
	try { var msg = $.parseJSON(payload); }
	catch(e) { log("Failed to parse: " + payload); return; }
	
	var row = $('<tr>');
	columns.forEach(function(col) {
		if (!MachineAuthorized(msg)) {
			if (col == "actions") {
				row.append($('<td>').append(
					$('<a>')
					.click(function() { AuthorizeMachine(msg); return false; })
					.text('Authorize'))
				);
			} else if (col == "status") {
				row.append($('<td>').text("Authorize this machine to see its stat."));
			} else {
				row.append($('<td>').text((col in msg) ? msg[col] : ''));
			}
		}
	});
	$('.content table')
		.find('tbody')
		.append(row);
}

function MachineAuthorized(obj) {
	if (registered_systems == undefined)
		return false;
	
	var found = false;
	registered_systems.forEach(function(val) {
		found = (val.name == obj.name && val.public_key == obj.public_key);
	});
	
	return found;
}

function HandlePing(payload) {
	try { var msg = $.parseJSON(payload); }
	catch(e) { log("Failed to parse: " + payload); return; }
	
	$('.content table tbody tr')
		.each(function( index, value ) {
			var elem = $(value);
			if (elem.text().length == 0)
				return;
			
			if ($(".name", elem).text() == msg.name) {
				var monitoring = msg.monitoring ? "monitoring" : "dead";
				var status = "online <b>(" + monitoring + ")</b>";
				status += "<br />cpu: " + (msg.cpu * 100).toFixed(2) + "%";
				status += "<br />mem: " + (msg.mem * 100).toFixed(2) + "%";
				$(".status", elem).html(status);
				$(elem).attr("ping", Date.now());
			}
		});
	
	console.log(msg);
}

function HandleOfflines(dead_interval) {
	$('.content table tbody tr')
		.each(function( index, value ) {
			var elem = $(value);
			if (elem.text().length == 0)
				return;
			
			var last_ping = $(elem).attr("ping");
			if (typeof last_ping !== typeof undefined && last_ping !== false) {
				last_ping = +last_ping;
			} else {
				last_ping = 0;
			}
			
			if (Date.now() - last_ping >= dead_interval)
				$(".status", elem).html("<b>offline</b>");
		});
}

window.Command = function(action, name) {
	/* no-op */
}

$(document).ready(function() {
	var columns = [];
	var thead = $('.content table thead tr th')
		.each(function( index, value ) {
			var header = $('a', value).length == 1 ? $('a', value).text() : $(value).text();
			header = header.toLowerCase().replace(' ', '_');
			columns.push(header);
		});

	var url = "ws://192.168.56.1:8080/";
	if( /phoenix\.heliosinteractive\.com/.test(window.location.href) )
		url = "wss://phoenix.heliosinteractive.com/ws/";
	var client                      = mqtt.connect(url);
	var echo_channel 	= SubChannelPath("echo");
	var ping_channel 	= SubChannelPath("ping");
	var ping_interval 	= 3 * 1000;
	var dead_interval 	= 5 * ping_interval;
	
	client.subscribe(echo_channel);
	client.subscribe(ping_channel);
	
	client.on("connect", function() {
		$(".machines h3").text("Machines (connected)");
	});
	client.on("close", function() {
		$(".machines h3").text("Machines (disconnected)");
	});
	
	client.on("message", function(topic, payload) {
		if (topic == echo_channel)
			AddMachineEntry(payload, columns);
		else if (topic == ping_channel)
			HandlePing(payload);
	});
	
	window.Command = function(action, name) {
		client.publish(SubChannelPath(name), action);
	}<commit_msg>read mqtt url from what server side has echoed<commit_after>// console.log polyfill
window.log = function() {
	log.history = log.history || [];
	log.history.push(arguments);
	if(this.console){
		console.log( Array.prototype.slice.call(arguments) );
	}
};

function BaseChannelPath() {
	var base_channel = "/helios/phoenix"
	return base_channel;
}

function SubChannelPath(path) {
	return BaseChannelPath() + "/" + path;
}

function AuthorizeMachine(info){
	$.post(config.base_url + "machines/add", info, function(data) {
		window.location.reload();
	});
}

function AddMachineEntry(payload, columns) {
	try { var msg = $.parseJSON(payload); }
	catch(e) { log("Failed to parse: " + payload); return; }
	
	var row = $('<tr>');
	columns.forEach(function(col) {
		if (!MachineAuthorized(msg)) {
			if (col == "actions") {
				row.append($('<td>').append(
					$('<a>')
					.click(function() { AuthorizeMachine(msg); return false; })
					.text('Authorize'))
				);
			} else if (col == "status") {
				row.append($('<td>').text("Authorize this machine to see its stat."));
			} else {
				row.append($('<td>').text((col in msg) ? msg[col] : ''));
			}
		}
	});
	$('.content table')
		.find('tbody')
		.append(row);
}

function MachineAuthorized(obj) {
	if (registered_systems == undefined)
		return false;
	
	var found = false;
	registered_systems.forEach(function(val) {
		found = (val.name == obj.name && val.public_key == obj.public_key);
	});
	
	return found;
}

function HandlePing(payload) {
	try { var msg = $.parseJSON(payload); }
	catch(e) { log("Failed to parse: " + payload); return; }
	
	$('.content table tbody tr')
		.each(function( index, value ) {
			var elem = $(value);
			if (elem.text().length == 0)
				return;
			
			if ($(".name", elem).text() == msg.name) {
				var monitoring = msg.monitoring ? "monitoring" : "dead";
				var status = "online <b>(" + monitoring + ")</b>";
				status += "<br />cpu: " + (msg.cpu * 100).toFixed(2) + "%";
				status += "<br />mem: " + (msg.mem * 100).toFixed(2) + "%";
				$(".status", elem).html(status);
				$(elem).attr("ping", Date.now());
			}
		});
	
	console.log(msg);
}

function HandleOfflines(dead_interval) {
	$('.content table tbody tr')
		.each(function( index, value ) {
			var elem = $(value);
			if (elem.text().length == 0)
				return;
			
			var last_ping = $(elem).attr("ping");
			if (typeof last_ping !== typeof undefined && last_ping !== false) {
				last_ping = +last_ping;
			} else {
				last_ping = 0;
			}
			
			if (Date.now() - last_ping >= dead_interval)
				$(".status", elem).html("<b>offline</b>");
		});
}

window.Command = function(action, name) {
	/* no-op */
}

$(document).ready(function() {
	
	if (config.mqtt_url == "")
		return;
	
	var columns = [];
	var thead = $('.content table thead tr th')
		.each(function( index, value ) {
			var header = $('a', value).length == 1 ? $('a', value).text() : $(value).text();
			header = header.toLowerCase().replace(' ', '_');
			columns.push(header);
		});

	var client			= mqtt.connect(config.mqtt_url);
	var echo_channel 	= SubChannelPath("echo");
	var ping_channel 	= SubChannelPath("ping");
	var ping_interval 	= 3 * 1000;
	var dead_interval 	= 5 * ping_interval;
	
	client.subscribe(echo_channel);
	client.subscribe(ping_channel);
	
	client.on("connect", function() {
		$(".machines h3").text("Machines (connected)");
	});
	client.on("close", function() {
		$(".machines h3").text("Machines (disconnected)");
	});
	
	client.on("message", function(topic, payload) {
		if (topic == echo_channel)
			AddMachineEntry(payload, columns);
		else if (topic == ping_channel)
			HandlePing(payload);
	});
	
	window.Command = function(action, name) {
		client.publish(SubChannelPath(name), action);
	}
{'fraction_non_alphanumeric': 0.20153703458327812, 'fraction_numerical': 0.00702265800980522, 'mean_word_length': 3.6621371216800496, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '5486717', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2587, 'n_words': 548}