MediaWiki:Refresh.js

From Biowikifarm Metawiki
Jump to: navigation, search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences
//var ajaxLink = "/testwiki/js2/Widgets/TaskMonitor/time.xml";
//var ajaxLink = "http://www.keytonature.net/fedora/";
//var ajaxLink = "http://www.keytonature.eu:8183/K2N-files/time.xml";
var ajaxLink = "/w/media/K2N-files/time.xml";

var refreshTimesArray = [30, 60, 300, 900, 3600, 500000];
var refreshTime = 5;
var crtRefreshId = 0;

function getJobsArray(xml) {
	var jobs = [];
	
    $(xml).each(function() {
    	var base = $(this).find('Base').text();
    	var name = $(this).find('Name').text();
    	var b_date_p = $(this).find('B_Date_P').text();
    	var e_date_p = $(this).find('E_Date_P').text();
    	var modified = $(this).find('Modified').text();
        var running = $(this).find('Running').text();
    	var number = $(this).find('Number').text();
    	
    	jobs.push({Base: base, Name: name, B_Date_P: b_date_p, E_Date_P: e_date_p, Modified: modified, Running: running, Number: number});	
	});
	
	return jobs;
}

function getTasksArray(xml) {
	var tasks = [];
	var i = 0;
//   	console.log("Get tasks array(%s)", xml.text());

    $(xml).find('row').each(function() {
    	var taskfields = {};
    	
    	$(this).find('field').each(function() {
			taskfields[$(this).attr('name')] = $(this).text();
//			console.log("task[%s] = %s", taskfields[$(this).attr('name')], $(this).text());	
    	});
    	
    	tasks.push(taskfields);
    });
    
//	console.log("Task count: %d", tasks.length);	
    return tasks;
}

function displayTasks(xml) {
	// general info
	var info = {};
	$(xml).find('Times').find('field').each(function() {
		if ($(this).attr('name') == "Last_Time")
			info['lastHarvestingTime'] = $(this).text();
		else
			info[$(this).attr('name')] = $(this).text();
//		console.log("info[%s] = %s", $(this).attr('name'), $(this).text());	
	});
	
	var item = $("#tasksInfo");
	var template = $("#tasks_info").html();
	
    item.setTemplate(template);
    item.processTemplate(info);
	
	// success tasks
	var tasks = getTasksArray($(xml).find('Success'));
	
	item = $("#successTasks");
	template = $("#success_tasks_template").html();
    
    item.setTemplate(template);
    item.processTemplate(tasks);

	// failed jobs
	tasks = getTasksArray($(xml).find('Failure'));
	
	item = $("#failureJobs");
	template = $("#failure_jobs_template").html();
    
    item.setTemplate(template);
    item.processTemplate(tasks);

	// running tasks
	tasks = getTasksArray($(xml).find('Running'));
	
	item = $("#runningTasks");
	template = $("#running_tasks_template").html();
    
    item.setTemplate(template);
    item.processTemplate(tasks);

	// waiting tasks
	tasks = getTasksArray($(xml).find('Waiting'));
	
	item = $("#waitingJobs");
	template = $("#waiting_jobs_template").html();
    
    item.setTemplate(template);
    item.processTemplate(tasks);
}

function updateStatus() {
	$.ajax({
	    url: ajaxLink,
	    type: 'GET',
	    cache: false,
	    dataType: 'xml',
	    timeout: (refreshTime-1)*1000,
	    error: function(){
			var item = $("#taskList");
			var template = $("#tasks_error").html();
		    
		    item.setTemplate(template);
		    item.processTemplate(tasks);
	    },
	    success: function(xml){
	    	displayTasks(xml);
	    }
	});
}

function setupTimer(timerRefreshId) {
	if (timerRefreshId != crtRefreshId)
		return ;
	 
	updateStatus();
	setTimeout("setupTimer(" + timerRefreshId + ")", refreshTime*1000);
}

function setRefreshTime(secs) {
	refreshTime = secs;
	crtRefreshId++;
	setTimeout("setupTimer(" + crtRefreshId + ")", refreshTime*1000);
	
	for (var i = 0; i < refreshTimesArray.length; i++) 
		if (refreshTimesArray[i] == secs) {
			$("#r" + (i + 1)).addClass("currentRefresh");
		} else {
			$("#r" + (i + 1)).removeClass("currentRefresh");
		}
}

$(function() {
	setRefreshTime(refreshTimesArray[0]);
	updateStatus();
	
	$("#r1").click(function() { setRefreshTime(refreshTimesArray[0]); });
	$("#r2").click(function() { setRefreshTime(refreshTimesArray[1]); });
	$("#r3").click(function() { setRefreshTime(refreshTimesArray[2]); });
	$("#r4").click(function() { setRefreshTime(refreshTimesArray[3]); });
	$("#r5").click(function() { setRefreshTime(refreshTimesArray[4]); });
	$("#r6").click(function() { setRefreshTime(refreshTimesArray[5]); });
});