WARNING: Replace the get_raw command for processlist

issue2181
nicolargo 2023-12-02 16:41:52 +01:00
parent 322d8c715a
commit 5fa7ae4264
4 changed files with 21 additions and 7 deletions

View File

@ -81,7 +81,7 @@ test-min-with-upgrade: venv-min-upgrade ## Upgrade deps and run unit tests in mi
./venv-min/bin/python ./unitest.py
test-restful-api: ## Run unit tests of the RESTful API
./venv-min/bin/python ./unitest-restful.py
./venv/bin/python ./unitest-restful.py
# ===================================================================
# Linters and profilers

View File

@ -84,15 +84,19 @@ class GlancesRestfulApi(object):
self.url_prefix)
# FastAPI Init
if self.args.password:
self._app = FastAPI(dependencies=[Depends(self.authentication)])
else:
self._app = FastAPI()
# Change the default root path
if self.url_prefix != '/':
self._app.include_router(APIRouter(prefix=self.url_prefix))
# Set path for WebUI
# self.STATIC_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static/public')
self.STATIC_PATH = 'static/public'
self.STATIC_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static/public')
# TEMPLATE_PATH.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static/templates'))
self.TEMPLATE_PATH = 'static/templates'
self.TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static/templates')
self._templates = Jinja2Templates(directory=self.TEMPLATE_PATH)
# FastAPI Enable CORS
@ -133,6 +137,7 @@ class GlancesRestfulApi(object):
return self._app()
# TODO: the password comparaison is not working for the moment.
# if the password is wrong, authentication is working...
# Perahps because the password is hashed in the GlancesPassword class
# and the one given by creds.password is not hashed ?
def authentication(self, creds: Annotated[HTTPBasicCredentials, Depends(security)]):

View File

@ -135,6 +135,15 @@ class PluginModel(GlancesPluginModel):
"""Return the key of the list."""
return 'pid'
def get_raw(self):
"""Overwrite the default get_raw methode in order to return dict in the values.
for example:
pmem(rss=6377472, vms=13946880, shared=4100096, text=913408, lib=0, data=2289664, dirty=0)
will be replaced by:
{'rss': 6377472, 'vms': 13946880, 'shared': 4100096, 'text': 913408, 'lib': 0, 'data': 2289664, 'dirty': 0}
"""
return [{k: (v._asdict() if hasattr(v, '_asdict') else v) for k, v in p.items()} for p in self.stats]
def update(self):
"""Update processes stats using the input method."""
# Init new stats

View File

@ -54,12 +54,12 @@ class TestGlances(unittest.TestCase):
"""Start the Glances Web Server."""
global pid
print('INFO: [TEST_000] Start the Glances Web Server')
print('INFO: [TEST_000] Start the Glances Web Server API')
if os.path.isfile('./venv/bin/python'):
cmdline = "./venv/bin/python"
else:
cmdline = "python"
cmdline += " -m glances -B localhost -w -p %s" % SERVER_PORT
cmdline += " -m glances -B 0.0.0.0 -w -p %s --disable-webui" % SERVER_PORT
print("Run the Glances Web Server on port %s" % SERVER_PORT)
args = shlex.split(cmdline)
pid = subprocess.Popen(args)