diff options
author | nicolargo <nicolas@nicolargo.com> | 2023-07-08 19:00:39 +0200 |
---|---|---|
committer | nicolargo <nicolas@nicolargo.com> | 2023-07-08 19:00:39 +0200 |
commit | 1cca7547592b7c14d512d0ab0a0ed577e691acdb (patch) | |
tree | b574523bade94ad4ed2bbf6b0b20d4a744b731f2 | |
parent | e7d79a1dd631f1be792275ecc8344e460c273c69 (diff) |
Done fore Core plugin2404-refactor-the-code-to-make-it-compliant-with-python-3-typing
-rw-r--r-- | glances/data/item.py | 2 | ||||
-rw-r--r-- | glances/plugins/core/model.py | 39 | ||||
-rw-r--r-- | glances/plugins/load/model.py | 4 | ||||
-rw-r--r-- | glances/plugins/plugin/model.py | 2 | ||||
-rwxr-xr-x | unitest-restful.py | 4 |
5 files changed, 40 insertions, 11 deletions
diff --git a/glances/data/item.py b/glances/data/item.py index 5b5fe36f..2b4021fc 100644 --- a/glances/data/item.py +++ b/glances/data/item.py @@ -21,6 +21,8 @@ class GlancesDataUnit: BYTE = 'B' CORE = 'C' TEMPERATURE = '°' + INTEGER = '' + FLOAT = '' @dataclass diff --git a/glances/plugins/core/model.py b/glances/plugins/core/model.py index c4a8816b..b28dd26f 100644 --- a/glances/plugins/core/model.py +++ b/glances/plugins/core/model.py @@ -10,16 +10,31 @@ """CPU core plugin.""" from glances.plugins.plugin.model import GlancesPluginModel +from glances.data.item import GlancesDataUnit +from glances.data.plugin import GlancesDataPlugin import psutil +# ============================================================================= # Fields description +# ============================================================================= +# key: stat identifier +# description: human readable description +# short_name: shortname to use in user interfaces +# unit: unit type +# min_symbol: Auto unit should be used if value > than 1 'X' (K, M, G)... +# rate: if True, the value is a rate (per second, compute automaticaly) +# ============================================================================= + fields_description = { - 'phys': {'description': 'Number of physical cores (hyper thread CPUs are excluded).', 'unit': 'number'}, + 'phys': { + 'description': 'Number of physical cores (hyper thread CPUs are excluded).', + 'unit': GlancesDataUnit.INTEGER + }, 'log': { 'description': 'Number of logical CPUs. A logical CPU is the number of \ physical cores multiplied by the number of threads that can run on each core.', - 'unit': 'number', + 'unit': GlancesDataUnit.INTEGER }, } @@ -34,12 +49,24 @@ class PluginModel(GlancesPluginModel): def __init__(self, args=None, config=None): """Init the plugin.""" - super(PluginModel, self).__init__(args=args, config=config, fields_description=fields_description) + super(PluginModel, self).__init__(args=args, + config=config, + fields_description=fields_description) # We dot not want to display the stat in the curse interface # The core number is displayed by the load plugin self.display_curse = False + # Init the data + # TODO: to be done in the top level plugin class + self.stats = GlancesDataPlugin(name=self.plugin_name, + description='Glances {} plugin'.format(self.plugin_name.upper()), + fields_description=fields_description) + + # TODO: To be done in the top level plugin class + def get_raw(self): + return self.stats.export() + # Do *NOT* uncomment the following line # @GlancesPluginModel._check_decorator @GlancesPluginModel._log_result_decorator @@ -63,7 +90,7 @@ class PluginModel(GlancesPluginModel): stats["phys"] = psutil.cpu_count(logical=False) stats["log"] = psutil.cpu_count() except NameError: - self.reset() + stats = self.get_init_value() elif self.input_method == 'snmp': # Update stats using SNMP @@ -71,6 +98,4 @@ class PluginModel(GlancesPluginModel): pass # Update the stats - self.stats = stats - - return self.stats + self.stats.update_data(stats) diff --git a/glances/plugins/load/model.py b/glances/plugins/load/model.py index c803b202..c619a302 100644 --- a/glances/plugins/load/model.py +++ b/glances/plugins/load/model.py @@ -76,7 +76,9 @@ class PluginModel(GlancesPluginModel): # Call CorePluginModel in order to display the core number try: - self.nb_log_core = CorePluginModel(args=self.args).update()["log"] + core_plugin = CorePluginModel(args=self.args) + core_plugin.update() + self.nb_log_core = core_plugin.get_raw()['log'] except Exception as e: logger.warning('Error: Can not retrieve the CPU core number (set it to 1) ({})'.format(e)) self.nb_log_core = 1 diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py index 7b3a2824..e793d5da 100644 --- a/glances/plugins/plugin/model.py +++ b/glances/plugins/plugin/model.py @@ -390,7 +390,7 @@ class GlancesPluginModel(object): Stats should be a list of dict (processlist, network...) """ - return json_dumps_dictlist(self.stats, item) + return json_dumps_dictlist(self.get_raw(), item) def get_stats_value(self, item, value): """Return the stats object for a specific item=value in JSON format. diff --git a/unitest-restful.py b/unitest-restful.py index a697ff78..4f09835b 100755 --- a/unitest-restful.py +++ b/unitest-restful.py @@ -108,11 +108,11 @@ class TestGlances(unittest.TestCase): print("HTTP RESTful request: %s/%s" % (URL, p)) req = self.http_get("%s/%s" % (URL, p)) self.assertTrue(req.ok) - if p in ('uptime', 'now'): + if p in ('now'): self.assertIsInstance(req.json(), text_type) elif p in ('fs', 'percpu', 'sensors', 'alert', 'processlist', 'diskio', 'hddtemp', 'batpercent', 'network', 'folders', 'amps', 'ports', - 'irq', 'wifi', 'gpu'): + 'irq', 'wifi', 'gpu', 'containers'): self.assertIsInstance(req.json(), list) elif p in ('psutilversion', 'help'): pass |