function generateJSON(res, accounts, type) {
	console.log("Function generateJSON called.");
	// Generate a uuid to name the json file where the result will be stored. This prevents from different users overwritting each other's JSON.
	var uuid = uuidv1().toString() + ".json";
	uuid = "a"+uuid;
	console.log("Uuid is " + uuid);
	// Generating the links part
	var links = new Array();
	for (var i = 1; i < accounts.length; i++) {
		var txInformationToDisplay = "";
		if (accounts[i][2] == 1) {
			//txInformationToDisplay = "hash:"+accounts[i][4];
			txInformationToDisplay = "hash:" + accounts[i][4] + "; ether:" + accounts[i][3];
		} else {
			//txInformationToDisplay = "txNumber:"+accounts[i][2];
			txInformationToDisplay = "number:" + accounts[i][2] + "; ether:" + accounts[i][3];
		}
		links.push({
			source: accounts[i][0],
			target: accounts[i][1],
			tx: txInformationToDisplay
		});
	}
	// Generating the nodes part
	var nodes = new Array();
	var content = null;
	try {
		content = fs.readFileSync('/home/ether/EthereumTracking/TFM/R/result.csv', 'utf8');
	} catch (error) {
		console.log("Error reading result.csv file.");
		// TODO render an error message
		return null;
	}
	// thanks to https://csv.js.org/parse/api/#sync-api
	var records = parse(content, {
		columns: true,
		skip_empty_lines: true
	});
	if (type == "normal") {
		for (var i = 0; i < records.length; i++) {
			nodes.push({
				name: records[i][Object.keys(records[i])[0]].substring(0, 7),
				id: records[i][Object.keys(records[i])[0]]
			});
		}
	} else {
		// Get fill maximum value to configure the color range
		var maxColor = 0;
		for (var i = 0; i < records.length; i++) {
			var resultInt = parseFloat(records[i].result);
			if (resultInt > maxColor) {
				maxColor = resultInt;
			}
		}
		// We will set a different color gradient depending on the graph's type
		var colorHexRange = 240;
		for (var i = 0; i < records.length; i++) {
			var mappedValue = 255 - colorHexRange;
			if (maxColor == 0) {
				mappedValue = 255 - colorHexRange;
			} else {
				mappedValue = Math.floor(((records[i].result)/maxColor)*colorHexRange) + mappedValue;
			}
			var fillValue = mappedValue.toString(16);
			var fillValueAux = Math.floor(mappedValue*mappedValue/383).toString(16);
			if (fillValueAux.length < 2) {
				fillValueAux = "0" + fillValueAux;
			}
			var fillString = "";
			if (type == "betweenness") {
				// blue - betweenness
				fillString = "#" + fillValue + "0080";
			} else if (type == "closeness") {
				// green - closeness
				fillString = "#00" + fillValue + fillValueAux;
			} else if (type == "pageRank") {
				// red - pageRank
				fillString = "#" + fillValue + fillValueAux + "00";
			}
			nodes.push({
				name: records[i][Object.keys(records[i])[0]].substring(0, 7),
				id: records[i][Object.keys(records[i])[0]],
				fill: fillString
			});
		}
	}
	// Writing to JSON file
	var jsonOutput = {
		"nodes": nodes,
		"links": links
	};
	try {
		fs.writeFileSync('/home/ether/EthereumTracking/TFM/EthereumStats/public/wallets/' + uuid, JSON.stringify(jsonOutput), 'utf8');
		return [uuid, jsonOutput];
	} catch (error) {
		console.log("Error while writing result.json");
		return null;
	}
	console.log("JSON created.");
}
// This function writes the nodes and links information into a JSON file, so that the view is able to represent the graph
This function writes the nodes and links information into a JSON file, so that the view is able to represent the graph
This function writes the nodes and links information into a JSON file so that the view is able to represent the graph
function generateJSON(res, accounts, type) {
	console.log("Function generateJSON called.");
	var uuid = uuidv1().toString() + ".json";
	uuid = "a"+uuid;
	console.log("Uuid is " + uuid);
	var links = new Array();
	for (var i = 1; i < accounts.length; i++) {
		var txInformationToDisplay = "";
		if (accounts[i][2] == 1) {
			txInformationToDisplay = "hash:" + accounts[i][4] + "; ether:" + accounts[i][3];
		} else {
			txInformationToDisplay = "number:" + accounts[i][2] + "; ether:" + accounts[i][3];
		}
		links.push({
			source: accounts[i][0],
			target: accounts[i][1],
			tx: txInformationToDisplay
		});
	}
	var nodes = new Array();
	var content = null;
	try {
		content = fs.readFileSync('/home/ether/EthereumTracking/TFM/R/result.csv', 'utf8');
	} catch (error) {
		console.log("Error reading result.csv file.");
		return null;
	}
	var records = parse(content, {
		columns: true,
		skip_empty_lines: true
	});
	if (type == "normal") {
		for (var i = 0; i < records.length; i++) {
			nodes.push({
				name: records[i][Object.keys(records[i])[0]].substring(0, 7),
				id: records[i][Object.keys(records[i])[0]]
			});
		}
	} else {
		var maxColor = 0;
		for (var i = 0; i < records.length; i++) {
			var resultInt = parseFloat(records[i].result);
			if (resultInt > maxColor) {
				maxColor = resultInt;
			}
		}
		var colorHexRange = 240;
		for (var i = 0; i < records.length; i++) {
			var mappedValue = 255 - colorHexRange;
			if (maxColor == 0) {
				mappedValue = 255 - colorHexRange;
			} else {
				mappedValue = Math.floor(((records[i].result)/maxColor)*colorHexRange) + mappedValue;
			}
			var fillValue = mappedValue.toString(16);
			var fillValueAux = Math.floor(mappedValue*mappedValue/383).toString(16);
			if (fillValueAux.length < 2) {
				fillValueAux = "0" + fillValueAux;
			}
			var fillString = "";
			if (type == "betweenness") {
				fillString = "#" + fillValue + "0080";
			} else if (type == "closeness") {
				fillString = "#00" + fillValue + fillValueAux;
			} else if (type == "pageRank") {
				fillString = "#" + fillValue + fillValueAux + "00";
			}
			nodes.push({
				name: records[i][Object.keys(records[i])[0]].substring(0, 7),
				id: records[i][Object.keys(records[i])[0]],
				fill: fillString
			});
		}
	}
	var jsonOutput = {
		"nodes": nodes,
		"links": links
	};
	try {
		fs.writeFileSync('/home/ether/EthereumTracking/TFM/EthereumStats/public/wallets/' + uuid, JSON.stringify(jsonOutput), 'utf8');
		return [uuid, jsonOutput];
	} catch (error) {
		console.log("Error while writing result.json");
		return null;
	}
	console.log("JSON created.");
}
function generateJSON ( res , accounts , type ) { console . log ( "Function generateJSON called." ) ; var uuid = uuidv1 ( ) . toString ( ) + ".json" ; uuid = "a" + uuid ; console . log ( "Uuid is " + uuid ) ; var links = new Array ( ) ; for ( var i = 1 ; i < accounts . length ; i ++ ) { var txInformationToDisplay = "" ; if ( accounts [ i ] [ 2 ] == 1 ) { txInformationToDisplay = "hash:" + accounts [ i ] [ 4 ] + "; ether:" + accounts [ i ] [ 3 ] ; } else { txInformationToDisplay = "number:" + accounts [ i ] [ 2 ] + "; ether:" + accounts [ i ] [ 3 ] ; } links . push ( { source : accounts [ i ] [ 0 ] , target : accounts [ i ] [ 1 ] , tx : txInformationToDisplay } ) ; } var nodes = new Array ( ) ; var content = null ; try { content = fs . readFileSync ( '/home/ether/EthereumTracking/TFM/R/result.csv' , 'utf8' ) ; } catch ( error ) { console . log ( "Error reading result.csv file." ) ; return null ; } var records = parse ( content , { columns : true , skip_empty_lines : true } ) ; if ( type == "normal" ) { for ( var i = 0 ; i < records . length ; i ++ ) { nodes . push ( { name : records [ i ] [ Object . keys ( records [ i ] ) [ 0 ] ] . substring ( 0 , 7 ) , id : records [ i ] [ Object . keys ( records [ i ] ) [ 0 ] ] } ) ; } } else { var maxColor = 0 ; for ( var i = 0 ; i < records . length ; i ++ ) { var resultInt = parseFloat ( records [ i ] . result ) ; if ( resultInt > maxColor ) { maxColor = resultInt ; } } var colorHexRange = 240 ; for ( var i = 0 ; i < records . length ; i ++ ) { var mappedValue = 255 - colorHexRange ; if ( maxColor == 0 ) { mappedValue = 255 - colorHexRange ; } else { mappedValue = Math . floor ( ( ( records [ i ] . result ) / maxColor ) * colorHexRange ) + mappedValue ; } var fillValue = mappedValue . toString ( 16 ) ; var fillValueAux = Math . floor ( mappedValue * mappedValue / 383 ) . toString ( 16 ) ; if ( fillValueAux . length < 2 ) { fillValueAux = "0" + fillValueAux ; } var fillString = "" ; if ( type == "betweenness" ) { fillString = "#" + fillValue + "0080" ; } else if ( type == "closeness" ) { fillString = "#00" + fillValue + fillValueAux ; } else if ( type == "pageRank" ) { fillString = "#" + fillValue + fillValueAux + "00" ; } nodes . push ( { name : records [ i ] [ Object . keys ( records [ i ] ) [ 0 ] ] . substring ( 0 , 7 ) , id : records [ i ] [ Object . keys ( records [ i ] ) [ 0 ] ] , fill : fillString } ) ; } } var jsonOutput = { "nodes" : nodes , "links" : links } ; try { fs . writeFileSync ( '/home/ether/EthereumTracking/TFM/EthereumStats/public/wallets/' + uuid , JSON . stringify ( jsonOutput ) , 'utf8' ) ; return [ uuid , jsonOutput ] ; } catch ( error ) { console . log ( "Error while writing result.json" ) ; return null ; } console . log ( "JSON created." ) ; }
This function writes the nodes and links information into a JSON file, so that the view is able to represent the graph
This function writes the nodes and links information into a JSON file so that the view is able to represent the graph
// Generate a uuid to name the json file where the result will be stored. This prevents from different users overwritting each other's JSON. // Generating the links part //txInformationToDisplay = "hash:"+accounts[i][4]; //txInformationToDisplay = "txNumber:"+accounts[i][2]; // Generating the nodes part // TODO render an error message // thanks to https://csv.js.org/parse/api/#sync-api // Get fill maximum value to configure the color range // We will set a different color gradient depending on the graph's type // blue - betweenness // green - closeness // red - pageRank // Writing to JSON file