diff --git a/lib/API/Dashboard.js b/lib/API/Dashboard.js index 64b127722..9214fd380 100644 --- a/lib/API/Dashboard.js +++ b/lib/API/Dashboard.js @@ -236,7 +236,7 @@ Dashboard.init = function() { * @param {} processes * @return this */ -Dashboard.refresh = function(processes) { +Dashboard.refresh = function(processes, opts) { debug('Monit refresh'); if(!processes) { @@ -254,13 +254,43 @@ Dashboard.refresh = function(processes) { mem += proc.monit.memory; }) + var sortField = 'name', sortOrder = 'asc', sort, + fields = { + name: 'pm2_env.name', + namespace: 'pm2_env.namespace', + pid: 'pid', + id: 'pm_id', + cpu: 'monit.cpu', + memory: 'monit.memory', + uptime: 'pm2_env.pm_uptime', + status: 'pm2_env.status' + } + + if (opts && opts.sort) { + sort = opts.sort.split(':'); + if(fields[sort[0].toLowerCase()]) { + sortField = sort[0].toLowerCase(); + sortOrder = sort.length === 2 ? sort[1] : 'asc'; + } + } + // Sort process list processes.sort(function(a, b) { - if (a.pm2_env.name < b.pm2_env.name) - return -1; - if (a.pm2_env.name > b.pm2_env.name) - return 1; - return 0; + var fieldA = getNestedProperty(fields[sortField], a) + var fieldB = getNestedProperty(fields[sortField], b) + + if (sortOrder === 'desc') { + if (fieldA > fieldB) + return -1 + if (fieldA < fieldB) + return 1 + } else { + if (fieldA < fieldB) + return -1 + if (fieldA > fieldB) + return 1 + } + return 0 }); // Loop to get process infos @@ -457,3 +487,22 @@ function gradient(p, rgb_beginning, rgb_end) { return "#" + ((1 << 24) + (rgb[0] << 16) + (rgb[1] << 8) + rgb[2]).toString(16).slice(1); } + +/** + * Get nested property + * + * @param {String} propertyName + * @param {Object} obj + * @returns {String} property value + */ +function getNestedProperty(propertyName, obj) { + var parts = propertyName.split("."), + length = parts.length, + property = obj || {}; + + for (var i = 0; i < length; i++) { + property = property[parts[i]]; + } + + return property; +} \ No newline at end of file diff --git a/lib/API/Extra.js b/lib/API/Extra.js index e30aba121..bf3605fcb 100644 --- a/lib/API/Extra.js +++ b/lib/API/Extra.js @@ -684,11 +684,16 @@ module.exports = function(CLI) { * @method dashboard * @return */ - CLI.prototype.dashboard = function(cb) { + CLI.prototype.dashboard = function(opts, cb) { var that = this; var Dashboard = require('./Dashboard'); + if (typeof opts == "function") { + cb = opts; + opts = null; + } + if (cb) return cb(new Error('Dashboard cant be called programmatically')); @@ -717,7 +722,7 @@ module.exports = function(CLI) { that.exitCli(cst.ERROR_EXIT); } - Dashboard.refresh(list); + Dashboard.refresh(list, opts); setTimeout(function() { refreshDashboard(); diff --git a/lib/binaries/CLI.js b/lib/binaries/CLI.js index 2ef3523ef..a9c19bc47 100644 --- a/lib/binaries/CLI.js +++ b/lib/binaries/CLI.js @@ -836,7 +836,7 @@ commander.command('prettylist') commander.command('monit') .description('launch termcaps monitoring') .action(function() { - pm2.dashboard(); + pm2.dashboard(commander); }); commander.command('imonit') @@ -849,7 +849,7 @@ commander.command('dashboard') .alias('dash') .description('launch dashboard with monitoring and logs') .action(function() { - pm2.dashboard(); + pm2.dashboard(commander); });