Skip to content

Commit

Permalink
Merge branch 'issue1536' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolargo committed Nov 2, 2019
2 parents 9943c30 + 83bc9de commit a1c63f4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 43 deletions.
4 changes: 4 additions & 0 deletions conf/glances.conf
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ battery_critical=95

[processlist]
disable=False
# Sort key: if not defined, the sort is automatically done by Glances (recommended)
# Should be one of the following:
# cpu_percent, memory_percent, io_counters, name, cpu_times, username
#sort_key=memory_percent
# Define CPU/MEM (per process) thresholds in %
# Default values if not defined: 50/70/90
cpu_careful=50
Expand Down
4 changes: 2 additions & 2 deletions glances/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ def get_event_sort_key(self, event_type):
def set_process_sort(self, event_type):
"""Define the process auto sort key from the alert type."""
if glances_processes.auto_sort:
glances_processes.sort_key = self.get_event_sort_key(event_type)
glances_processes.set_sort_key(self.get_event_sort_key(event_type))

def reset_process_sort(self):
"""Reset the process auto sort key."""
if glances_processes.auto_sort:
glances_processes.sort_key = 'cpu_percent'
glances_processes.set_sort_key('auto')

def add(self, event_state, event_type, event_value,
proc_list=None, proc_desc="", peak_time=6):
Expand Down
48 changes: 13 additions & 35 deletions glances/outputs/glances_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ class _GlancesCurses(object):
'U': {'switch': 'network_cumul'},
'W': {'switch': 'disable_wifi'},
# Processes sort hotkeys
'a': {'auto_sort': True, 'sort_key': 'cpu_percent'},
'c': {'auto_sort': False, 'sort_key': 'cpu_percent'},
'i': {'auto_sort': False, 'sort_key': 'io_counters'},
'm': {'auto_sort': False, 'sort_key': 'memory_percent'},
'p': {'auto_sort': False, 'sort_key': 'name'},
't': {'auto_sort': False, 'sort_key': 'cpu_times'},
'u': {'auto_sort': False, 'sort_key': 'username'},
'a': {'sort_key': 'auto'},
'c': {'sort_key': 'cpu_percent'},
'i': {'sort_key': 'io_counters'},
'm': {'sort_key': 'memory_percent'},
'p': {'sort_key': 'name'},
't': {'sort_key': 'cpu_times'},
'u': {'sort_key': 'username'},
}

_sort_loop = ['cpu_percent', 'memory_percent', 'username',
Expand Down Expand Up @@ -276,9 +276,9 @@ def _init_colors(self):
'DEFAULT': self.no_color,
'UNDERLINE': curses.A_UNDERLINE,
'BOLD': A_BOLD,
'SORT': A_BOLD,
'SORT': curses.A_UNDERLINE | A_BOLD,
'OK': self.default_color2,
'MAX': self.default_color2 | curses.A_BOLD,
'MAX': self.default_color2 | A_BOLD,
'FILTER': self.filter_color,
'TITLE': self.title_color,
'PROCESS': self.default_color2,
Expand Down Expand Up @@ -308,21 +308,6 @@ def set_cursor(self, value):
except Exception:
pass

# def get_key(self, window):
# # Catch ESC key AND numlock key (issue #163)
# keycode = [0, 0]
# keycode[0] = window.getch()
# keycode[1] = window.getch()
#
# if keycode != [-1, -1]:
# logger.debug("Keypressed (code: %s)" % keycode)
#
# if keycode[0] == 27 and keycode[1] != -1:
# # Do not escape on specials keys
# return -1
# else:
# return keycode[0]

def get_key(self, window):
# @TODO: Check issue #163
ret = window.getch()
Expand All @@ -340,14 +325,9 @@ def __catch_key(self, return_to_browser=False):
self._hotkeys[hotkey]['switch'],
not getattr(self.args,
self._hotkeys[hotkey]['switch']))
if self.pressedkey == ord(hotkey) and 'auto_sort' in self._hotkeys[hotkey]:
setattr(glances_processes,
'auto_sort',
self._hotkeys[hotkey]['auto_sort'])
if self.pressedkey == ord(hotkey) and 'sort_key' in self._hotkeys[hotkey]:
setattr(glances_processes,
'sort_key',
self._hotkeys[hotkey]['sort_key'])
glances_processes.set_sort_key(self._hotkeys[hotkey]['sort_key'],
self._hotkeys[hotkey]['sort_key'] == 'auto')

# Other actions...
if self.pressedkey == ord('\x1b') or self.pressedkey == ord('q'):
Expand Down Expand Up @@ -400,14 +380,12 @@ def __catch_key(self, return_to_browser=False):
glances_processes.enable()
elif self.pressedkey == curses.KEY_LEFT:
# "<" (left arrow) navigation through process sort
setattr(glances_processes, 'auto_sort', False)
next_sort = (self.loop_position() - 1) % len(self._sort_loop)
glances_processes.sort_key = self._sort_loop[next_sort]
glances_processes.set_sort_key(self._sort_loop[next_sort], False)
elif self.pressedkey == curses.KEY_RIGHT:
# ">" (right arrow) navigation through process sort
setattr(glances_processes, 'auto_sort', False)
next_sort = (self.loop_position() + 1) % len(self._sort_loop)
glances_processes.sort_key = self._sort_loop[next_sort]
glances_processes.set_sort_key(self._sort_loop[next_sort], False)

# Return the key code
return self.pressedkey
Expand Down
5 changes: 5 additions & 0 deletions glances/plugins/glances_processlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ def __init__(self, args=None, config=None):
# Use to optimize space (see https://github.com/nicolargo/glances/issues/959)
self.pid_max = glances_processes.pid_max

# Set the default sort key if it is defined in the configuration file
if 'processlist' in config.as_dict() and 'sort_key' in config.as_dict()['processlist']:
logger.debug('Configuration overwrites processes sort key by {}'.format(config.as_dict()['processlist']['sort_key']))
glances_processes.set_sort_key(config.as_dict()['processlist']['sort_key'], False)

# Note: 'glances_processes' is already init in the processes.py script

def get_key(self):
Expand Down
18 changes: 12 additions & 6 deletions glances/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@


class GlancesProcesses(object):

"""Get processed stats using the psutil library."""

def __init__(self, cache_timeout=60):
Expand All @@ -50,8 +49,11 @@ def __init__(self, cache_timeout=60):
self.io_old = {}

# Init stats
self.auto_sort = True
self._sort_key = 'cpu_percent'
self.auto_sort = None
self._sort_key = None
# Default processes sort key is 'auto'
# Can be overwrite from the configuration file (issue#1536) => See glances_processlist.py init
self.set_sort_key('auto', auto=True)
self.processlist = []
self.reset_processcount()

Expand Down Expand Up @@ -368,10 +370,14 @@ def sort_key(self):
"""Get the current sort key."""
return self._sort_key

@sort_key.setter
def sort_key(self, key):
def set_sort_key(self, key, auto=True):
"""Set the current sort key."""
self._sort_key = key
if key == 'auto':
self.auto_sort = True
self._sort_key = 'cpu_percent'
else:
self.auto_sort = auto
self._sort_key = key


def weighted(value):
Expand Down

0 comments on commit a1c63f4

Please sign in to comment.