Skip to content

Commit

Permalink
Fix for update bugs and some code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
darxtrix committed Nov 16, 2018
2 parents e9a9825 + 55a1baf commit 7675c6f
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 17 deletions.
1 change: 0 additions & 1 deletion ptop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

__dir__ = os.path.dirname(os.path.abspath(__file__))


with open(os.path.join(os.path.dirname(__dir__),'VERSION'),'r') as version_file:
__version__ = version_file.read()

Expand Down
2 changes: 1 addition & 1 deletion ptop/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
]

INVALID_PROCESSES = [
'zombie'
'zombie'
]

# This will be overriden by process sensor
Expand Down
10 changes: 8 additions & 2 deletions ptop/interfaces/GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class PtopGUI(npyscreen.NPSApp):
This controls the rendering of the main window and acts as the registering point
for all other widgets
'''
def __init__(self,statistics,stop_event,arg):
def __init__(self,statistics,stop_event,arg,sensor_refresh_rates):
self.statistics = statistics
# Command line arguments passed, currently used for selecting themes
self.arg = arg
Expand All @@ -301,6 +301,8 @@ def __init__(self,statistics,stop_event,arg):
self.update_thread = None
# Flag to check if user is interacting (not used)
self.is_user_interacting = False
# GUI refresh rate should be minimum of all sensor refresh rates
self.refresh_rate = min(sensor_refresh_rates.values())

# Main form
self.window = None
Expand Down Expand Up @@ -662,4 +664,8 @@ def main(self):

# time(ms) to wait for user interactions
self.keypress_timeout_default = 10
self.draw()

if self.refresh_rate < 1000:
self.keypress_timeout_default = int(self.refresh_rate/100)

self.draw()
104 changes: 94 additions & 10 deletions ptop/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import string
import random
import os
import platform

from ptop import __version__, _log_file
from ptop.statistics import Statistics
from ptop.interfaces import PtopGUI
from ptop.plugins import SENSORS_LIST
from ptop.constants import SUPPORTED_THEMES
from ptop import __version__
from huepy import *

logger = logging.getLogger('ptop.main')
Expand All @@ -27,7 +27,7 @@ def _update():
os_name = "{0} {1}".format(platform.system(),
platform.release()
)
resp = requests.get("http://ptop-telemetry.darxtrix.in",params={os_name: os_name, version: __version__})
resp = requests.get("https://ptop-telemetry.darxtrix.in", params={'os_name': os_name, 'version': __version__}, timeout=2)
NEW_VERSION = str(resp.text)
if NEW_VERSION != CURRENT_VERSION:
sys.stdout.write(blue("A new version is available, would you like to update (Y/N) ? "))
Expand Down Expand Up @@ -81,27 +81,110 @@ def main():
simple
blackonwhite
''')

parser.add_argument('-csrt',
dest='csrt',
action='store',
type=float,
required=False,
help=
'''
CPU sensor response time;
update interval in
milli seconds less than 1000.
For example 500
''')

parser.add_argument('-dsrt',
dest='dsrt',
action='store',
type=float,
required=False,
help=
'''
Disk sensor response time;
Input sensor
update interval in
milli seconds less than 1000.
For example 500
''')

parser.add_argument('-msrt',
dest='msrt',
action='store',
type=float,
required=False,
help=
'''
Memory sensor response time;
Input sensor
update interval in
milli seconds less than 1000.
For example 500
''')

parser.add_argument('-psrt',
dest='psrt',
action='store',
type=float,
required=False,
help=
'''
Process sensor response time;
Input sensor
update interval in
milli seconds less than 1000.
For example 500
''')

parser.add_argument('-ssrt',
dest='ssrt',
action='store',
type=float,
required=False,
help=
'''
System sensor response time;
Input sensor
update interval in
milli seconds less than 1000.
For example 500
''')

parser.add_argument('-v',
action='version',
version='ptop {}'.format(__version__))

results = parser.parse_args()
if results.theme:
theme = results.theme
else:
theme = 'elegant'

# commandline arguments massaging
theme = (results.theme if results.theme else 'elegant')
csrt = (results.csrt if results.csrt else 1000)
csrt = (1000 if csrt>1000 else csrt)
dsrt = (results.dsrt if results.dsrt else 1000)
dsrt = (1000 if dsrt>1000 else dsrt)
msrt = (results.msrt if results.msrt else 1000)
msrt = (1000 if msrt>1000 else msrt)
psrt = (results.psrt if results.psrt else 1000)
psrt = (1000 if psrt>1000 else psrt)
ssrt = (results.ssrt if results.ssrt else 1000)
ssrt = (1000 if ssrt>1000 else ssrt)
nsrt = 1000 # network sensor rate is always 1 second

srts = [csrt, dsrt, msrt, nsrt, psrt, ssrt]
sensor_refresh_rates = {SENSORS_LIST[i]: srts[i] for i in range(len(SENSORS_LIST))}

# try to update ptop
_update()


# TODO :: Catch the exception of the child thread and kill the application gracefully
# https://stackoverflow.com/questions/2829329/catch-a-threads-exception-in-the-caller-thread-in-python
s = Statistics(SENSORS_LIST,global_stop_event)
s = Statistics(SENSORS_LIST,global_stop_event,sensor_refresh_rates)
# internally uses a thread Job
s.generate()
logger.info('Statistics generating started')
app = PtopGUI(s.statistics,global_stop_event,theme)

app = PtopGUI(s.statistics,global_stop_event,theme,sensor_refresh_rates)
# blocking call
logger.info('Starting the GUI application')
app.run()
Expand All @@ -121,7 +204,8 @@ def main():
global_stop_event.set()
# don't clear the log file
logger.info("Exception :: main.py "+str(e))
print(sys.exc_info())
raise SystemExit

if __name__ == '__main__':
main()
main()
2 changes: 1 addition & 1 deletion ptop/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
cpu_sensor,
disk_sensor,
memory_sensor,
network_sensor,
process_sensor,
system_sensor,
network_sensor
]
5 changes: 3 additions & 2 deletions ptop/statistics/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@


class Statistics:
def __init__(self,sensors_list,stop_event):
def __init__(self,sensors_list,stop_event,sensor_refresh_rates):
'''
Record keeping for primitive system parameters
'''
self.sensor_refresh_rates = sensor_refresh_rates
self.plugin_dir = os.path.join(os.path.dirname(__file__),'plugins') #plugins directory
self.plugins = sensors_list # plugins list
self.statistics = {} # statistics object to be passed to the GUI
Expand All @@ -31,7 +32,7 @@ def generate(self):
'''
for sensor in self.plugins:
# update the sensors value periodically
job = ThreadJob(sensor.update,self.stop_event,sensor.interval)
job = ThreadJob(sensor.update,self.stop_event,self.sensor_refresh_rates[sensor]/1000)
job.start()


Expand Down

0 comments on commit 7675c6f

Please sign in to comment.