<commit_before>$(function () {
	$(document).ready(function () {
		var server = $('input[name="hwaddr"]').val();
		var csrf = $('input[name="csrfmiddlewaretoken"]').val();
		var secret = $('input[name="secret"]').val();

		cpu_usage_set(csrf, server, secret);
		setInterval(processes(csrf, server, secret), 500);
//		loadavg_set (csrf, server, secret);
	});
});

function processes(csrf, server, secret) {
	var address = '/ajax/server/' + server + '/metrics/processes/';

	function displayTable(data) {
		var html = '';
		for (var i = 0; i < data.length; i++) {
			html += '<tr>';
			html += '<td>' + data[i].pid + '</td>';
			html += '<td>';
			if (data[i].cpu > 50) {
				html += '<span class="label label-danger">';
			} else {
				html += '<span class="label label-success">';
			}
			html += data[i].user + '</span></td>';
			html += '<td>' + data[i].cpu + '</td>';                                       
			html += '<td>' + data[i].mem + '</td>';                                       
			html += '<td>' + data[i].name + '</td>';                                       
			html += '<td>' + data[i].command + '</td>';
			html += '</tr>';
		}
		return html;
	}	

	console.log(address, csrf, server, secret);
	$.ajax({
		url: address,
		type: 'POST',
		dataType: 'json',
		headers: {
			'X-CSRFToken': csrf
		},
		cache: false,
		data: {
			'server': server,
			'secret': secret
		},
		success: function(data) {
			var tableData = JSON.parse(data);
			console.log(tableData);
			html = displayTable(tableData);
			$('#running_processes_table').find('<tbody>').html(html);
		}
	});
}


function cpu_usage_set (csrf, server, secret) {
	var address = '/ajax/server/' + server + '/metrics/cpu_usage/';
	var lastValue = null;

	function requestCpuUsageData(series, lastValue, csrf, server, secret) {
		var address = '/ajax/server/' + server + '/metrics/cpu_usage/';
		$.ajax({
			url: address,
			type: 'POST',
			dataType: 'json',
			headers: {
				'X-CSRFToken': csrf
			},
			cache: false,
			data: {
				'server': server,
				'secret': secret,
				'lastvalue': lastValue
			},
			success: function(data) {
				if (typeof element === 'undefined') {
					var element = [null, null];
				}

				if (data[0] != element[0]) {
					series.addPoint(data[0], true, true);
					element[0] = data[0];
				}
			},
			error: function(data, textStatus, errorThrown) {
				console.log('error: ' + textStatus);
				console.log('error: ' + errorThrown);
			}
		});
	}

	function updateCpuUsageChart(series, lastValue, csrf, server, secret) {
		setInterval(function() {
			requestCpuUsageData(series, lastValue, csrf, server, secret);
		}, 1000);
	}

	Highcharts.setOptions({
		global: {
			useUTC: false 
		}
	});

	$.ajax({
		url: address,
		type: 'POST',
		dataType: 'json',
		headers: {
			'X-CSRFToken': csrf
		},
		cache: false,
		data: {
			'server': server,
			'secret': secret,
			'lastvalue': lastValue
		},
		success: function(data) {
			console.log('naplneni daty');
			if (data != null) {
				var optionalData = [];
				var optionalLength = 30;
				console.log(data);
				var dataLength = data.length;
				data.reverse();
				for(var i = 0; i < (optionalLength - dataLength); i++) {
					optionalData.push([
							(data[0][0] - (i + 1) * 5),
							null
						]);
				}
				optionalData = optionalData.concat(data);
				var cpuUsageChart = new Highcharts.Chart({
					chart: {
						renderTo: 'cpu_usage',
						events: {
							load: function() {
								updateCpuUsageChart(
									this.series[0],
									data[0][0],
									csrf,
									server,
									secret
								);
							}
						}
					},
					title: {
						text: 'CPU Usage'
					},
					xAxis: {
						type: 'datetime',
						labels: {
							formatter: function() {
								return Highcharts.dateFormat('%H:%M:%S', this.value * 1000);
							}
						}
					},
					yAxis: {
						title: {
							text: 'Value'
						},
					plotLines: [{
						value: 0,
						width: 1,
						color: '#808080'
					}]
					},
					tooltip: {
						formatter: function () {
							return '<b>' + Highcharts.numberFormat(this.y, 0) + this.series.name + '</b><br/>' +
								Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x*1000);
								
						}
					},
					legend: {
						enabled: false
					},
					exporting: {
						enabled: false
					},
					series: [{
						name: '% CPU used',
						data: optionalData,
						zones: [{
								value: 10,
								color: '#7cb5ec'
							}, {
								value: 80,
								color: '#90ed7d'
							},{
								value: 95,
								color: 'orange'
							},{
								color: 'red'
							}
						]
					}]
				});
			}<commit_msg>Added loading running process data to Ajax call<commit_after>$(function () {
	$(document).ready(function () {
		var server = $('input[name="hwaddr"]').val();
		var csrf = $('input[name="csrfmiddlewaretoken"]').val();
		var secret = $('input[name="secret"]').val();

		cpu_usage_set(csrf, server, secret);
		processes(csrf, server, secret);
//		loadavg_set (csrf, server, secret);
	});
});

function processes(csrf, server, secret) {
	var address = '/ajax/server/' + server + '/metrics/processes/';

	$('#running_processes_table').dataTable({
		lengthMenu: [[15, 50, 100], [15, 50, 100]],
		order: [[ 2, "desc" ]],
		draw: 1,
		processing: true,
		serverSide: true,
		ajax: {
			url: address,
			type: 'POST',
			dataType: 'json',
			dataSrc: function(json) {
				var data = [];
				for (var i = 0; i < json.length; i++) {

					var user = '';
					if (json[i]['cp']  > 50) {
						user += '<span class="label label-danger">';
					} else {
						user += '<span class="label label-success">';
					}
					user += json[i]['user'];
					user += '</span>';

					data.push([
						json[i]['pid'],
						user,
						json[i]['cp'],
						json[i]['mem'],
						json[i]['command'][0],
						json[i]['command'].join(' ')
					]);
				}
				console.log(data);
				return data;
			},
			headers: {
				'X-CSRFToken': csrf
			},
			cache: false,
			data: {
				'server': server,
				'secret': secret
			}
		}
	}); //dataTable function end
}

function cpu_usage_set (csrf, server, secret) {
	var address = '/ajax/server/' + server + '/metrics/cpu_usage/';
	var lastValue = null;

	function requestCpuUsageData(series, lastValue, csrf, server, secret) {
		var address = '/ajax/server/' + server + '/metrics/cpu_usage/';
		$.ajax({
			url: address,
			type: 'POST',
			dataType: 'json',
			headers: {
				'X-CSRFToken': csrf
			},
			cache: false,
			data: {
				'server': server,
				'secret': secret,
				'lastvalue': lastValue
			},
			success: function(data) {
				if (typeof element === 'undefined') {
					var element = [null, null];
				}

				if (data[0] != element[0]) {
					series.addPoint(data[0], true, true);
					element[0] = data[0];
				}
			},
			error: function(data, textStatus, errorThrown) {
				console.log('error: ' + textStatus);
				console.log('error: ' + errorThrown);
			}
		});
	}

	function updateCpuUsageChart(series, lastValue, csrf, server, secret) {
		setInterval(function() {
			requestCpuUsageData(series, lastValue, csrf, server, secret);
		}, 1000);
	}

	Highcharts.setOptions({
		global: {
			useUTC: false
		}
	});

	$.ajax({
		url: address,
		type: 'POST',
		dataType: 'json',
		headers: {
			'X-CSRFToken': csrf
		},
		cache: false,
		data: {
			'server': server,
			'secret': secret,
			'lastvalue': lastValue
		},
		success: function(data) {
			console.log('naplneni daty');
			if (data != null) {
				var optionalData = [];
				var optionalLength = 30;
				console.log(data);
				var dataLength = data.length;
				data.reverse();
				for(var i = 0; i < (optionalLength - dataLength); i++) {
					optionalData.push([
							(data[0][0] - (i + 1) * 5),
							null
						]);
				}
				optionalData = optionalData.concat(data);
				var cpuUsageChart = new Highcharts.Chart({
					chart: {
						renderTo: 'cpu_usage',
						events: {
							load: function() {
								updateCpuUsageChart(
									this.series[0],
									data[0][0],
									csrf,
									server,
									secret
								);
							}
						}
					},
					title: {
						text: 'CPU Usage'
					},
					xAxis: {
						type: 'datetime',
						labels: {
							formatter: function() {
								return Highcharts.dateFormat('%H:%M:%S', this.value * 1000);
							}
						}
					},
					yAxis: {
						title: {
							text: 'Value'
						},
					plotLines: [{
						value: 0,
						width: 1,
						color: '#808080'
					}]
					},
					tooltip: {
						formatter: function () {
							return '<b>' + Highcharts.numberFormat(this.y, 0) + this.series.name + '</b><br/>' +
								Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x*1000);

						}
					},
					legend: {
						enabled: false
					},
					exporting: {
						enabled: false
					},
					series: [{
						name: '% CPU used',
						data: optionalData,
						zones: [{
								value: 10,
								color: '#7cb5ec'
							}, {
								value: 80,
								color: '#90ed7d'
							},{
								value: 95,
								color: 'orange'
							},{
								color: 'red'
							}
						]
					}]
				});
			}
{'fraction_non_alphanumeric': 0.18394611902395938, 'fraction_numerical': 0.013249420337860219, 'mean_word_length': 2.1883139739528334, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6506181', 'n_tokens_mistral': 4412, 'n_tokens_neox': 3105, 'n_words': 662}