##// END OF EJS Templates
system-info: optimized code based on linting
super-admin -
r4938:fd6cd0ae default
parent child Browse files
Show More
@@ -24,6 +24,7 b' import sys'
24 24 import time
25 25 import platform
26 26 import collections
27 import psutil
27 28 from functools import wraps
28 29
29 30 import pkg_resources
@@ -35,16 +36,8 b' import configparser'
35 36 log = logging.getLogger(__name__)
36 37
37 38
38 psutil = None
39
40 try:
41 # cygwin cannot have yet psutil support.
42 import psutil as psutil
43 except ImportError:
44 pass
45
46
47 39 _NA = 'NOT AVAILABLE'
40 _NA_FLOAT = 0.0
48 41
49 42 STATE_OK = 'ok'
50 43 STATE_ERR = 'error'
@@ -74,7 +67,7 b' def register_sysinfo(func):'
74 67
75 68
76 69 # HELPERS
77 def percentage(part, whole):
70 def percentage(part: (int, float), whole: (int, float)):
78 71 whole = float(whole)
79 72 if whole > 0:
80 73 return round(100 * float(part) / whole, 1)
@@ -160,7 +153,7 b' class SysInfo(object):'
160 153 # SysInfo functions
161 154 @register_sysinfo
162 155 def python_info():
163 value = dict(version=' '.join(platform._sys_version()),
156 value = dict(version=f'{platform.python_version()}:{platform.python_implementation()}',
164 157 executable=sys.executable)
165 158 return SysInfoRes(value=value)
166 159
@@ -232,8 +225,6 b' def uptime():'
232 225
233 226 value = dict(boot_time=0, uptime=0, text='')
234 227 state = STATE_OK_DEFAULT
235 if not psutil:
236 return SysInfoRes(value=value, state=state)
237 228
238 229 boot_time = psutil.boot_time()
239 230 value['boot_time'] = boot_time
@@ -259,8 +250,6 b' def memory():'
259 250 total=0, buffers=0, text='')
260 251
261 252 state = STATE_OK_DEFAULT
262 if not psutil:
263 return SysInfoRes(value=value, state=state)
264 253
265 254 value.update(dict(psutil.virtual_memory()._asdict()))
266 255 value['used_real'] = value['total'] - value['available']
@@ -273,7 +262,7 b' def memory():'
273 262 format_byte_size_binary(value['total']),
274 263 value['percent_used'],)
275 264
276 keys = value.keys()[::]
265 keys = list(value.keys())[::]
277 266 keys.pop(keys.index('percent'))
278 267 keys.pop(keys.index('percent_used'))
279 268 keys.pop(keys.index('text'))
@@ -293,21 +282,20 b' def memory():'
293 282
294 283 @register_sysinfo
295 284 def machine_load():
296 value = {'1_min': _NA, '5_min': _NA, '15_min': _NA, 'text': ''}
285 value = {'1_min': _NA_FLOAT, '5_min': _NA_FLOAT, '15_min': _NA_FLOAT, 'text': ''}
297 286 state = STATE_OK_DEFAULT
298 if not psutil:
299 return SysInfoRes(value=value, state=state)
300 287
301 288 # load averages
302 289 if hasattr(psutil.os, 'getloadavg'):
303 290 value.update(dict(
304 zip(['1_min', '5_min', '15_min'], psutil.os.getloadavg())))
291 zip(['1_min', '5_min', '15_min'], psutil.os.getloadavg())
292 ))
305 293
306 294 human_value = value.copy()
307 295 human_value['text'] = '1min: {}, 5min: {}, 15min: {}'.format(
308 296 value['1_min'], value['5_min'], value['15_min'])
309 297
310 if state['type'] == STATE_OK and value['15_min'] > 5:
298 if state['type'] == STATE_OK and value['15_min'] > 5.0:
311 299 msg = 'Warning: your machine load is very high.'
312 300 state = {'message': msg, 'type': STATE_WARN}
313 301
@@ -319,9 +307,6 b' def cpu():'
319 307 value = {'cpu': 0, 'cpu_count': 0, 'cpu_usage': []}
320 308 state = STATE_OK_DEFAULT
321 309
322 if not psutil:
323 return SysInfoRes(value=value, state=state)
324
325 310 value['cpu'] = psutil.cpu_percent(0.5)
326 311 value['cpu_usage'] = psutil.cpu_percent(0.5, percpu=True)
327 312 value['cpu_count'] = psutil.cpu_count()
@@ -341,8 +326,6 b' def storage():'
341 326
342 327 value = dict(percent=0, used=0, total=0, path=path, text='')
343 328 state = STATE_OK_DEFAULT
344 if not psutil:
345 return SysInfoRes(value=value, state=state)
346 329
347 330 try:
348 331 value.update(dict(psutil.disk_usage(path)._asdict()))
@@ -374,10 +357,8 b' def storage_inodes():'
374 357 from rhodecode.model.settings import VcsSettingsModel
375 358 path = VcsSettingsModel().get_repos_location()
376 359
377 value = dict(percent=0, free=0, used=0, total=0, path=path, text='')
360 value = dict(percent=0.0, free=0, used=0, total=0, path=path, text='')
378 361 state = STATE_OK_DEFAULT
379 if not psutil:
380 return SysInfoRes(value=value, state=state)
381 362
382 363 try:
383 364 i_stat = os.statvfs(path)
General Comments 0
You need to be logged in to leave comments. Login now