<commit_before>/*******************************************************************************
 * DIANNE  - Framework for distributed artificial neural networks
 * Copyright (C) 2015  Ghent University, iMinds
 *
 * This file is part of DIANNE.
 *
 * DIANNE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     <NAME>, <NAME>
 *******************************************************************************/

var currentMode;


function submitJob(){
	var array =  $("#submit-form").serializeArray();
	
	var job = {};
    $.each(array, function() {
    	if(this.name === 'config'){
    		// parse out config to json object
    		if(this.value !== undefined && this.value !==""){
    			job.config = configStringToObject(this.value);
    		} else {
    			job.config = {};
    		}
    	} else {
    		job[this.name] = this.value || '';
    	}
    });
    
    // check if it is an upload nn
    if(uploadNN !== undefined && job.nn == uploadNN.name){
    	job.nn = uploadNN;
    }
    
    // insert name attribute to the config
    if(job.name!==undefined && job.name!==""){
    	job.config.name = job.name;
	}

    // insert custom strategy implementation to the config
    if(job.strategy!==undefined && job.strategy!==""){
    	job.config.strategy = job.strategy;
	}
    
    if(job.type==="LEARN"){
    	DIANNE.learn(job.nn, job.dataset, job.config);
    } else if(job.type==="EVALUATE"){
    	DIANNE.eval(job.nn, job.dataset, job.config);
    } else if(job.type==="ACT"){
    	DIANNE.act(job.nn, job.dataset, job.config);
    }
}

// also allow to upload a nn modules.txt or strategy .java file from your filesystem
var uploadNN = undefined;
function upload(evt){
    var f = evt.target.files[0]; 
    if (f) {
    	var r = new FileReader();
    	r.onload = function(e) { 
    		var contents = e.target.result;
    		
    		if(f.name==="modules.txt"){
    			// nn uploaded
	    		uploadNN = JSON.parse(contents);
				$('#submit-nn').val(uploadNN.name);
    		} else if(f.name.endsWith(".java")){
    			// strategy uploaded
    			$('#submit-strategy').val(contents);
    		}
    	}
    	r.readAsText(f);
    } else {
    	alert("You should upload a modules.txt file defining the neural network.");
    }
}
document.getElementById('nn-file-input').addEventListener('change', upload, false);
document.getElementById('strategy-file-input').addEventListener('change', upload, false);


function resubmitJob(jobId){
	DIANNE.job(jobId).then(function(job){
		if(clean!==undefined && job.type==="LEARN"){
			job.config['clean'] = $('#clean').is(':checked');
		}
		
		$("#submit-name").val(job.name);
		$("#submit-type").val(job.type);
		$("#submit-nn").val(job.nn);
		$("#submit-dataset").val(job.dataset);
		$("#submit-config").val(configObjectToString(job.config));
		$("#submit-strategy").val(job.config.strategy);
		
		$('#submit-modal').modal('show');
	});
}

function stopJob(jobId){
	DIANNE.stop(jobId);
}

function refreshJobs(){
 	// queued jobs
 	DIANNE.queuedJobs().then(function(data){
 		$("#jobs-queue").empty();
 	    $.each(data, function(i) {
 	        var job = data[i];

 	        var template = $('#job-item').html();
     	  	Mustache.parse(template);
     	  	var rendered = Mustache.render(template, job);
     	  	$(rendered).appendTo($("#jobs-queue"));
 	    });
 	});
 	
 	// running jobs
 	DIANNE.runningJobs().then(function(data){
 	 	$("#jobs-running").empty();
 	    $.each(data, function(i) {
 	        var job = data[i];

     	  	job.devices = job.targets.length;
     	  	job.time = moment(job.started).from(moment());
 	        
     	  	// for dashboard list
 	        var template = $('#job-item').html();
     	  	Mustache.parse(template);
     	  	var rendered = Mustache.render(template, job);
     	  	$(rendered).appendTo($("#jobs-running"));
     	  	
     	  	// if new running job, add jobs details (hidden)
     	  	if(!$("#"+job.id).length){
	     	  	var template2 = $('#job').html();
	     	  	Mustache.parse(template2);
	     	  	var rendered2 = Mustache.render(template2, job);
	     	  	var panel = $(rendered2).prependTo($("#dashboard"))
	     	  	if(currentMode!=="jobs"){
	     	  		panel.hide();
	     	  	}
	     	  	
	     	  	// initialize error/q chart
	     	  	createResultChart($('#'+job.id+"-progress"), job);
     	  	}
     	  	
     	  	if(job.type==="EVALUATE" || job.type==="ACT"){
	     	  	// update progress bars
	     	  	createResultChart($('#'+job.id+"-progress"), job);
	     	 }
 	    });
 	});
 	
 	// finished jobs
 	DIANNE.finishedJobs().then(function(data){
 	 	$("#jobs-finished").empty();
 	    $.each(data, function(i) {
 	        var job = data[i];
 	        var template = $('#job-item').html();
     	  	Mustache.parse(template);
     	  	var rendered = Mustache.render(template, job);
     	  	$(rendered).prependTo($("#jobs-finished"));
 	    });
 	});
}

function refreshStatus(){
	DIANNE.status().then(function(data){
		var status = data;
		// format
		status.uptime = moment.duration(moment().diff(moment(status.bootTime))).humanize();
		status.spaceLeft = status.spaceLeft/1000000000;
		status.spaceLeft = status.spaceLeft.toFixed(1);

 	 	$("#status").empty();
 	    var template = $('#stat').html();
 	    Mustache.parse(template);
     	var rendered = Mustache.render(template, status);
     	$(rendered).appendTo($("#status"));
 	   
		// status chart
        createStatusChart($('#status-chart'), status);
 	});
}

function addNotification(notification){
	notification.time = moment(notification.timestamp).fromNow();
	notification.level = notification.level.toLowerCase();
	var template = $('#notification').html();
	Mustache.parse(template);
	var rendered = Mustache.render(template, notification);
	$(rendered).prependTo($("#alerts"));
}



function showDetails(jobId){
	DIANNE.job(jobId).then(function(job){<commit_msg>fix notification timestamp<commit_after>/*******************************************************************************
 * DIANNE  - Framework for distributed artificial neural networks
 * Copyright (C) 2015  Ghent University, iMinds
 *
 * This file is part of DIANNE.
 *
 * DIANNE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *     <NAME>, <NAME>
 *******************************************************************************/

var currentMode;


function submitJob(){
	var array =  $("#submit-form").serializeArray();
	
	var job = {};
    $.each(array, function() {
    	if(this.name === 'config'){
    		// parse out config to json object
    		if(this.value !== undefined && this.value !==""){
    			job.config = configStringToObject(this.value);
    		} else {
    			job.config = {};
    		}
    	} else {
    		job[this.name] = this.value || '';
    	}
    });
    
    // check if it is an upload nn
    if(uploadNN !== undefined && job.nn == uploadNN.name){
    	job.nn = uploadNN;
    }
    
    // insert name attribute to the config
    if(job.name!==undefined && job.name!==""){
    	job.config.name = job.name;
	}

    // insert custom strategy implementation to the config
    if(job.strategy!==undefined && job.strategy!==""){
    	job.config.strategy = job.strategy;
	}
    
    if(job.type==="LEARN"){
    	DIANNE.learn(job.nn, job.dataset, job.config);
    } else if(job.type==="EVALUATE"){
    	DIANNE.eval(job.nn, job.dataset, job.config);
    } else if(job.type==="ACT"){
    	DIANNE.act(job.nn, job.dataset, job.config);
    }
}

// also allow to upload a nn modules.txt or strategy .java file from your filesystem
var uploadNN = undefined;
function upload(evt){
    var f = evt.target.files[0]; 
    if (f) {
    	var r = new FileReader();
    	r.onload = function(e) { 
    		var contents = e.target.result;
    		
    		if(f.name==="modules.txt"){
    			// nn uploaded
	    		uploadNN = JSON.parse(contents);
				$('#submit-nn').val(uploadNN.name);
    		} else if(f.name.endsWith(".java")){
    			// strategy uploaded
    			$('#submit-strategy').val(contents);
    		}
    	}
    	r.readAsText(f);
    } else {
    	alert("You should upload a modules.txt file defining the neural network.");
    }
}
document.getElementById('nn-file-input').addEventListener('change', upload, false);
document.getElementById('strategy-file-input').addEventListener('change', upload, false);


function resubmitJob(jobId){
	DIANNE.job(jobId).then(function(job){
		if(clean!==undefined && job.type==="LEARN"){
			job.config['clean'] = $('#clean').is(':checked');
		}
		
		$("#submit-name").val(job.name);
		$("#submit-type").val(job.type);
		$("#submit-nn").val(job.nn);
		$("#submit-dataset").val(job.dataset);
		$("#submit-config").val(configObjectToString(job.config));
		$("#submit-strategy").val(job.config.strategy);
		
		$('#submit-modal').modal('show');
	});
}

function stopJob(jobId){
	DIANNE.stop(jobId);
}

function refreshJobs(){
 	// queued jobs
 	DIANNE.queuedJobs().then(function(data){
 		$("#jobs-queue").empty();
 	    $.each(data, function(i) {
 	        var job = data[i];

 	        var template = $('#job-item').html();
     	  	Mustache.parse(template);
     	  	var rendered = Mustache.render(template, job);
     	  	$(rendered).appendTo($("#jobs-queue"));
 	    });
 	});
 	
 	// running jobs
 	DIANNE.runningJobs().then(function(data){
 	 	$("#jobs-running").empty();
 	    $.each(data, function(i) {
 	        var job = data[i];

     	  	job.devices = job.targets.length;
     	  	job.time = moment(job.started).from(moment());
 	        
     	  	// for dashboard list
 	        var template = $('#job-item').html();
     	  	Mustache.parse(template);
     	  	var rendered = Mustache.render(template, job);
     	  	$(rendered).appendTo($("#jobs-running"));
     	  	
     	  	// if new running job, add jobs details (hidden)
     	  	if(!$("#"+job.id).length){
	     	  	var template2 = $('#job').html();
	     	  	Mustache.parse(template2);
	     	  	var rendered2 = Mustache.render(template2, job);
	     	  	var panel = $(rendered2).prependTo($("#dashboard"))
	     	  	if(currentMode!=="jobs"){
	     	  		panel.hide();
	     	  	}
	     	  	
	     	  	// initialize error/q chart
	     	  	createResultChart($('#'+job.id+"-progress"), job);
     	  	}
     	  	
     	  	if(job.type==="EVALUATE" || job.type==="ACT"){
	     	  	// update progress bars
	     	  	createResultChart($('#'+job.id+"-progress"), job);
	     	 }
 	    });
 	});
 	
 	// finished jobs
 	DIANNE.finishedJobs().then(function(data){
 	 	$("#jobs-finished").empty();
 	    $.each(data, function(i) {
 	        var job = data[i];
 	        var template = $('#job-item').html();
     	  	Mustache.parse(template);
     	  	var rendered = Mustache.render(template, job);
     	  	$(rendered).prependTo($("#jobs-finished"));
 	    });
 	});
}

function refreshStatus(){
	DIANNE.status().then(function(data){
		var status = data;
		// format
		status.uptime = moment.duration(moment().diff(moment(status.bootTime))).humanize();
		status.spaceLeft = status.spaceLeft/1000000000;
		status.spaceLeft = status.spaceLeft.toFixed(1);

 	 	$("#status").empty();
 	    var template = $('#stat').html();
 	    Mustache.parse(template);
     	var rendered = Mustache.render(template, status);
     	$(rendered).appendTo($("#status"));
 	   
		// status chart
        createStatusChart($('#status-chart'), status);
 	});
}

function addNotification(notification){
	notification.time = moment(Number(notification.timestamp)).fromNow();
	notification.level = notification.level.toLowerCase();
	var template = $('#notification').html();
	Mustache.parse(template);
	var rendered = Mustache.render(template, notification);
	$(rendered).prependTo($("#alerts"));
}



function showDetails(jobId){
	DIANNE.job(jobId).then(function(job){
{'fraction_non_alphanumeric': 0.19121406988455877, 'fraction_numerical': 0.0034090028666615016, 'mean_word_length': 2.958295001533272, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '2013897', 'n_tokens_mistral': 4339, 'n_tokens_neox': 4092, 'n_words': 845}