Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow --sort option on pm2 monit #5951

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions lib/API/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
9 changes: 7 additions & 2 deletions lib/API/Extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down Expand Up @@ -717,7 +722,7 @@ module.exports = function(CLI) {
that.exitCli(cst.ERROR_EXIT);
}

Dashboard.refresh(list);
Dashboard.refresh(list, opts);

setTimeout(function() {
refreshDashboard();
Expand Down
4 changes: 2 additions & 2 deletions lib/binaries/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ commander.command('prettylist')
commander.command('monit')
.description('launch termcaps monitoring')
.action(function() {
pm2.dashboard();
pm2.dashboard(commander);
});

commander.command('imonit')
Expand All @@ -849,7 +849,7 @@ commander.command('dashboard')
.alias('dash')
.description('launch dashboard with monitoring and logs')
.action(function() {
pm2.dashboard();
pm2.dashboard(commander);
});


Expand Down