Implement get all limits in the RESTFUL API #477

This commit is contained in:
Nicolargo 2015-01-09 22:28:00 +01:00 committed by Alessio Sergi
parent 19f118e814
commit 4ca94bc1fe
3 changed files with 52 additions and 8 deletions

View File

@ -189,6 +189,13 @@ class GlancesStats(object):
ret[p] = self._plugins[p].get_raw() ret[p] = self._plugins[p].get_raw()
return ret return ret
def getAllLimitsAsDict(self):
"""Return all the stats limits (dict)"""
ret = {}
for p in self._plugins:
ret[p] = self._plugins[p].get_limits()
return ret
def get_plugin_list(self): def get_plugin_list(self):
"""Return the plugin list.""" """Return the plugin list."""
self._plugins self._plugins
@ -240,6 +247,13 @@ class GlancesStatsServer(GlancesStats):
ret[p] = self.all_stats[p] ret[p] = self.all_stats[p]
return ret return ret
def getAllLimitsAsDict(self):
"""Return the stats limits as a dict"""
ret = {}
for p in self._plugins:
ret[p] = self._plugins[p].get_limits()
return ret
def getAllPlugins(self): def getAllPlugins(self):
"""Return the plugins list.""" """Return the plugins list."""
return [p for p in self._plugins] return [p for p in self._plugins]

View File

@ -66,6 +66,7 @@ class GlancesBottle(object):
# REST API # REST API
self._app.route('/api/2/pluginslist', method="GET", callback=self._api_plugins) self._app.route('/api/2/pluginslist', method="GET", callback=self._api_plugins)
self._app.route('/api/2/all', method="GET", callback=self._api_all) self._app.route('/api/2/all', method="GET", callback=self._api_all)
self._app.route('/api/2/all/limits', method="GET", callback=self._api_all_limits)
self._app.route('/api/2/:plugin', method="GET", callback=self._api) self._app.route('/api/2/:plugin', method="GET", callback=self._api)
self._app.route('/api/2/:plugin/limits', method="GET", callback=self._api_limits) self._app.route('/api/2/:plugin/limits', method="GET", callback=self._api_limits)
self._app.route('/api/2/:plugin/:item', method="GET", callback=self._api_item) self._app.route('/api/2/:plugin/:item', method="GET", callback=self._api_item)
@ -153,6 +154,23 @@ class GlancesBottle(object):
abort(404, "Cannot get stats (%s)" % str(e)) abort(404, "Cannot get stats (%s)" % str(e))
return statval return statval
def _api_all_limits(self):
"""
Glances API RESTFul implementation
Return the JSON representation of all the plugins limits
HTTP/200 if OK
HTTP/400 if plugin is not found
HTTP/404 if others error
"""
response.content_type = 'application/json'
try:
# Get the JSON value of the stat ID
limits = json.dumps(self.stats.getAllLimitsAsDict())
except Exception as e:
abort(404, "Cannot get limits (%s)" % (str(e)))
return limits
def _api(self, plugin): def _api(self, plugin):
""" """
Glances API RESTFul implementation Glances API RESTFul implementation

View File

@ -46,7 +46,8 @@ pid = None
# Unitary test is only available from a GNU/Linus machine # Unitary test is only available from a GNU/Linus machine
if not is_linux: if not is_linux:
print('ERROR: RESTFul API unitaries tests should be ran on GNU/Linux operating system') print(
'ERROR: RESTFul API unitaries tests should be ran on GNU/Linux operating system')
sys.exit(2) sys.exit(2)
else: else:
print('Unitary tests for {0} {1}'.format(appname, version)) print('Unitary tests for {0} {1}'.format(appname, version))
@ -82,25 +83,25 @@ class TestGlances(unittest.TestCase):
def test_000_start_server(self): def test_000_start_server(self):
"""Start the Glances Web Server""" """Start the Glances Web Server"""
print('INFO: [TEST_000] Start the Glances Web Server') print('INFO: [TEST_000] Start the Glances Web Server')
global pid global pid
cmdline = "/usr/bin/python -m glances -w -p %s" % SERVER_PORT cmdline = "/usr/bin/python -m glances -w -p %s" % SERVER_PORT
print("Run the Glances Web Server on port %s" % SERVER_PORT) print("Run the Glances Web Server on port %s" % SERVER_PORT)
args = shlex.split(cmdline) args = shlex.split(cmdline)
pid = subprocess.Popen(args) pid = subprocess.Popen(args)
print("Please wait...") print("Please wait...")
time.sleep(1) time.sleep(1)
self.assertTrue(pid is not None) self.assertTrue(pid is not None)
def test_001_all(self): def test_001_all(self):
"""All""" """All"""
method = "all" method = "all"
print('INFO: [TEST_001] Connection test') print('INFO: [TEST_001] Get all stats')
print("HTTP RESTFul request: %s/%s" % (URL, method)) print("HTTP RESTFul request: %s/%s" % (URL, method))
req = requests.get("%s/%s" % (URL, method)) req = requests.get("%s/%s" % (URL, method))
self.assertTrue(req.ok) self.assertTrue(req.ok)
@ -118,7 +119,7 @@ class TestGlances(unittest.TestCase):
def test_003_plugins(self): def test_003_plugins(self):
"""Plugins""" """Plugins"""
method = "pluginslist" method = "pluginslist"
print('INFO: [TEST_003] Plugins') print('INFO: [TEST_003] Plugins')
plist = requests.get("%s/%s" % (URL, method)) plist = requests.get("%s/%s" % (URL, method))
@ -146,7 +147,7 @@ class TestGlances(unittest.TestCase):
ilist = requests.get("%s/%s" % (URL, method)) ilist = requests.get("%s/%s" % (URL, method))
for i in ilist.json(): for i in ilist.json():
print("HTTP RESTFul request: %s/%s/%s" % (URL, method,i)) print("HTTP RESTFul request: %s/%s/%s" % (URL, method, i))
req = requests.get("%s/%s/%s" % (URL, method, i)) req = requests.get("%s/%s/%s" % (URL, method, i))
self.assertTrue(req.ok) self.assertTrue(req.ok)
self.assertIsInstance(req.json(), types.DictType) self.assertIsInstance(req.json(), types.DictType)
@ -163,6 +164,17 @@ class TestGlances(unittest.TestCase):
self.assertTrue(req.ok) self.assertTrue(req.ok)
self.assertIsInstance(req.json(), types.DictType) self.assertIsInstance(req.json(), types.DictType)
def test_006_all_limits(self):
"""All"""
method = "all/limits"
print('INFO: [TEST_006] Get all limits')
print("HTTP RESTFul request: %s/%s" % (URL, method))
req = requests.get("%s/%s" % (URL, method))
self.assertTrue(req.ok)
self.assertIsInstance(req.json(), types.DictType)
def test_999_stop_server(self): def test_999_stop_server(self):
"""Stop the Glances Web Server""" """Stop the Glances Web Server"""
print('INFO: [TEST_999] Stop the Glances Web Server') print('INFO: [TEST_999] Stop the Glances Web Server')