From 34a86fc32cb03ac38c6651ad296466c5334b6cb3 Mon Sep 17 00:00:00 2001 From: Kyle B Date: Fri, 16 Jun 2017 10:57:03 -0400 Subject: [PATCH 1/3] Implement top Implementation of the "top" command. Uses the column formatting from scan, which breaks with long names. --- src/Terminal.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Terminal.js b/src/Terminal.js index af5fb4aad..a5cd28229 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -954,8 +954,38 @@ var Terminal = { } break; case "top": - //TODO List each's script RAM usage - post("Not yet implemented"); + if(commandArray.length != 1) { + post("Incorrect usage of top command. Usage: top"); return; + } + + post("Script RAM Usage Threads"); + var currRunningScripts = Player.getCurrentServer().runningScripts; + for(i = 0; i < currRunningScripts.length; i++) { + //Check if script is on current server + for(j = 0; j < Player.getCurrentServer().scripts.length; j++) { + if(currRunningScripts[i] === Player.getCurrentServer().scripts[j].filename) { + var script = Player.getCurrentServer().scripts[j]; + + //Add script name + var entry = script.filename; + + //Calculate padding and add RAM usage + var numSpaces = 26 - entry.length; + var spaces = Array(numSpaces+1).join(" "); + entry += spaces; + var ramUsage = (script.ramUsage * script.threads * Math.pow(1.02, script.threads - 1)).toFixed(3); + entry += ramUsage + "GB"; + + //Calculate padding and add thread count + numSpaces = 26 - (ramUsage.length + 2); + spaces = Array(numSpaces+1).join(" "); + entry += spaces; + entry += script.threads; + + post(entry); + } + } + } break; default: post("Command not found"); From 96be33344fb0f22add0d4e595de389f8efdbf720 Mon Sep 17 00:00:00 2001 From: Kyle B Date: Fri, 16 Jun 2017 13:23:42 -0400 Subject: [PATCH 2/3] Update "top" command Updated the entry construction to use `.join()` and added/updated comments. --- src/Terminal.js | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Terminal.js b/src/Terminal.js index a5cd28229..0020b829c 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -957,32 +957,31 @@ var Terminal = { if(commandArray.length != 1) { post("Incorrect usage of top command. Usage: top"); return; } - + post("Script RAM Usage Threads"); + var currRunningScripts = Player.getCurrentServer().runningScripts; - for(i = 0; i < currRunningScripts.length; i++) { - //Check if script is on current server - for(j = 0; j < Player.getCurrentServer().scripts.length; j++) { - if(currRunningScripts[i] === Player.getCurrentServer().scripts[j].filename) { + //Iterate through active scripts on current server + for(var i = 0; i < currRunningScripts.length; i++) { + //Iterate through scripts on current server + for(var j = 0; j < Player.getCurrentServer().scripts.length; j++) { + if(currRunningScripts[i] === Player.getCurrentServer().scripts[j].filename) { //If the script is active var script = Player.getCurrentServer().scripts[j]; - //Add script name - var entry = script.filename; + //Calculate name padding + var numSpacesScript = 26 - script.filename.length; // 26 is the width of each column + var spacesScript = Array(numSpacesScript+1).join(" "); - //Calculate padding and add RAM usage - var numSpaces = 26 - entry.length; - var spaces = Array(numSpaces+1).join(" "); - entry += spaces; - var ramUsage = (script.ramUsage * script.threads * Math.pow(1.02, script.threads - 1)).toFixed(3); - entry += ramUsage + "GB"; + //Calculate and transform RAM usage + var ramUsage = script.ramUsage * script.threads * Math.pow(1.02, script.threads - 1); + ramUsage = ramUsage.toFixed(3) + "GB"; //Rounds RAM to three decimal places - //Calculate padding and add thread count - numSpaces = 26 - (ramUsage.length + 2); - spaces = Array(numSpaces+1).join(" "); - entry += spaces; - entry += script.threads; + //Calculate RAM padding + var numSpacesRAM = 26 - ramUsage.length; + var spacesRAM = Array(numSpacesRAM+1).join(" "); - post(entry); + var entry = [script.filename, spacesScript, ramUsage, spacesRAM, script.threads]; + post(entry.join("")); } } } From 92b100f3ce4aaf88f5bb26fbb405db57ed7b2414 Mon Sep 17 00:00:00 2001 From: Kyle B Date: Fri, 16 Jun 2017 14:17:40 -0400 Subject: [PATCH 3/3] Update "top" command- columns and loops Switched the RAM and thread columns so that the RAM usage does not need to be rounded. Updated structure to remove nested loops. --- src/Terminal.js | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/src/Terminal.js b/src/Terminal.js index 0020b829c..229c548f5 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -954,37 +954,35 @@ var Terminal = { } break; case "top": - if(commandArray.length != 1) { - post("Incorrect usage of top command. Usage: top"); return; - } + if(commandArray.length != 1) { + post("Incorrect usage of top command. Usage: top"); return; + } - post("Script RAM Usage Threads"); + post("Script Threads RAM Usage"); - var currRunningScripts = Player.getCurrentServer().runningScripts; - //Iterate through active scripts on current server - for(var i = 0; i < currRunningScripts.length; i++) { - //Iterate through scripts on current server - for(var j = 0; j < Player.getCurrentServer().scripts.length; j++) { - if(currRunningScripts[i] === Player.getCurrentServer().scripts[j].filename) { //If the script is active - var script = Player.getCurrentServer().scripts[j]; + var currRunningScripts = Player.getCurrentServer().runningScripts; + var currScripts = Player.getCurrentServer().scripts; + //Iterate through scripts on current server + for(var i = 0; i < currScripts.length; i++) { + if(currRunningScripts.includes(currScripts[i].filename)) { //If the script is running + var script = currScripts[i]; - //Calculate name padding - var numSpacesScript = 26 - script.filename.length; // 26 is the width of each column - var spacesScript = Array(numSpacesScript+1).join(" "); + //Calculate name padding + var numSpacesScript = 26 - script.filename.length; //26 -> width of name column + var spacesScript = Array(numSpacesScript+1).join(" "); - //Calculate and transform RAM usage - var ramUsage = script.ramUsage * script.threads * Math.pow(1.02, script.threads - 1); - ramUsage = ramUsage.toFixed(3) + "GB"; //Rounds RAM to three decimal places + //Calculate thread padding + var numSpacesThread = 16 - (script.threads + "").length; //16 -> width of thread column + var spacesThread = Array(numSpacesThread+1).join(" "); - //Calculate RAM padding - var numSpacesRAM = 26 - ramUsage.length; - var spacesRAM = Array(numSpacesRAM+1).join(" "); + //Calculate and transform RAM usage + var ramUsage = script.ramUsage * script.threads * Math.pow(1.02, script.threads - 1); + ramUsage = ramUsage + "GB"; - var entry = [script.filename, spacesScript, ramUsage, spacesRAM, script.threads]; - post(entry.join("")); - } - } - } + var entry = [script.filename, spacesScript, script.threads, spacesThread, ramUsage]; + post(entry.join("")); + } + } break; default: post("Command not found");