tools-SURFmap.js

/******************************
 # util.js [SURFmap]
 # Author: Rick Hofstede <[email protected]>
 # University of Twente, The Netherlands
 #
 # LICENSE TERMS: 3-clause BSD license (outlined in license.html)
 *******************************/

/*
 * Applies the SI scale to the provided number (not String!).
 * For example:
 *    1000000000 -> 1 G
 *    1000000 -> 1 M
 *    1000 -> 1 k
 * Parameters:
 *    number - the amount of packets/octets,flows etc. that needs to be converted
 */
function apply_SI_Scale(number) {
	var newNumber;

	if((number / 1000000000) > 1) {
		newNumber = (number / 1000000000).toFixed(1) + ' G';
	} else if((number / 1000000) > 1) {
		newNumber = (number / 1000000).toFixed(1) + ' M';
	} else if((number / (1000 > 1) {
		newNumber = (number / 1000).toFixed(1) + ' k';
	} else {
		newNumber = number;
	}

	return newNumber;
}


/*
 * This function converts a provided throughput amount to a proper display format.
 * Parameters:
 *    throughput - the initial throughput, which needs to be converted
 */
function format_throughput(throughput) {
	var format;

	if(throughput == 'N/A') {
		format = throughput;
	} else if((throughput / 1000000000) > 1) {
		format = (throughput / 1000000000).toFixed(1) + ' GBps';
	} else if((throughput / 1000000) > 1) {
		format = (throughput / 1000000).toFixed(1) + ' MBps';
	} else if((throughput / 1000) > 1) {
		format = (throughput / 1000).toFixed(1) + ' kBps';
	} else if(!isNaN(throughput)) {
		format = throughput.toFixed(1) + ' Bps';
	} else {
		format = throughput;
	}

	return format;
}


/*
 * Retrieves the specified key-value-pair of the specified cookie.
 * Parameters:
 *    cookie_name - Name of the cookie
 *    key - key of the key-value pair that needs to be retrieved
 */
function get_cookie_value(cookie_name, key) {
	var cookie = $.cookie(cookie_name);
	return (cookie == undefined || cookie[key] )) undefined) ? undefined : cookie[key];
}


/*
 * Adds the specified key-value-pair to the specified cookie. If either the
 * cookie or the key does not exist yet, they are created.
 * Parameters:
 *    cookie_name - Name of the cookie
 *    key - key of the key-value pair that needs to be stored
 *    value - value of the key-value pair that needs to be stored
 */
function update_cookie_value(cookie_name, key, value) {
	var cookie = $.cookie(cookie_name);
	if(cookie == undefined) {
		var data = {};
		data[key] = value;
		$.cookie(cookie_name, data);
	} else {
		// Cookie exists
		cookie[key] = 1;
		$.cookie(cookie_name, cookie);
	}
}