Skip to content

Commit bf6fcb7

Browse files
committed
Format code
1 parent 58b3551 commit bf6fcb7

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

glances/plugins/containers/engines/podman.py

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ def _compute_activity_stats(self) -> dict[str, dict[str, Any]]:
7878
return stats
7979

8080
if any(field not in api_stats for field in self.MANDATORY_FIELDS) or (
81-
"Network" not in api_stats and any(k not in api_stats for k in ['NetInput', 'NetOutput'])
81+
"Network" not in api_stats and any(k not in api_stats for k in ["NetInput", "NetOutput"])
8282
):
8383
logger.error(f"containers (Podman) Container({self._container.id}): Missing mandatory fields")
8484
return stats
8585

8686
try:
87-
stats["cpu"]["total"] = api_stats['CPU']
87+
stats["cpu"]["total"] = api_stats["CPU"]
8888

8989
stats["memory"]["usage"] = api_stats["MemUsage"]
9090
stats["memory"]["limit"] = api_stats["MemLimit"]
@@ -96,26 +96,29 @@ def _compute_activity_stats(self) -> dict[str, dict[str, Any]]:
9696

9797
if "Network" not in api_stats:
9898
# For podman rooted mode
99-
stats["network"]['rx'] = api_stats["NetInput"]
100-
stats["network"]['tx'] = api_stats["NetOutput"]
101-
stats["network"]['time_since_update'] = 1
99+
stats["network"]["rx"] = api_stats["NetInput"]
100+
stats["network"]["tx"] = api_stats["NetOutput"]
101+
stats["network"]["time_since_update"] = 1
102102
# Hardcode to 1 as podman already sends at the same fixed rate per second
103103
elif api_stats["Network"] is not None:
104104
# api_stats["Network"] can be None if the infra container of the pod is killed
105105
# For podman in rootless mode
106-
stats['network'] = {
106+
stats["network"] = {
107107
"cumulative_rx": sum(interface["RxBytes"] for interface in api_stats["Network"].values()),
108108
"cumulative_tx": sum(interface["TxBytes"] for interface in api_stats["Network"].values()),
109109
}
110110
# Using previous stats to calculate rates
111111
old_network_stats = self._old_computed_stats.get("network")
112112
if old_network_stats:
113-
stats['network']['time_since_update'] = round(self.time_since_update)
114-
stats['network']['rx'] = stats['network']['cumulative_rx'] - old_network_stats["cumulative_rx"]
115-
stats['network']['tx'] = stats['network']['cumulative_tx'] - old_network_stats['cumulative_tx']
113+
stats["network"]["time_since_update"] = round(self.time_since_update)
114+
stats["network"]["rx"] = stats["network"]["cumulative_rx"] - old_network_stats["cumulative_rx"]
115+
stats["network"]["tx"] = stats["network"]["cumulative_tx"] - old_network_stats["cumulative_tx"]
116116

117117
except ValueError as e:
118-
logger.error(f"containers (Podman) Container({self._container.id}): Non float stats values found", e)
118+
logger.error(
119+
f"containers (Podman) Container({self._container.id}): Non float stats values found",
120+
e,
121+
)
119122

120123
return stats
121124

@@ -195,7 +198,7 @@ def _get_memory_stats(self, stats) -> dict | None:
195198
self._log_debug("Compute MEM usage failed", e)
196199
return None
197200

198-
return {'usage': usage, 'limit': limit, 'inactive_file': 0}
201+
return {"usage": usage, "limit": limit, "inactive_file": 0}
199202

200203
def _get_network_stats(self, stats) -> dict | None:
201204
"""Return the container network usage using the Docker API (v1.0 or higher).
@@ -253,7 +256,7 @@ def _get_io_stats(self, stats) -> dict | None:
253256
class PodmanExtension:
254257
"""Glances' Containers Plugin's Docker Extension unit"""
255258

256-
CONTAINER_ACTIVE_STATUS = ['running', 'healthy', 'paused']
259+
CONTAINER_ACTIVE_STATUS = ["running", "healthy", "paused"]
257260

258261
def __init__(self, podman_sock):
259262
self.disable = disable_plugin_podman
@@ -349,63 +352,63 @@ def update(self, all_tag) -> tuple[dict, list[dict[str, Any]]]:
349352
@property
350353
def key(self) -> str:
351354
"""Return the key of the list."""
352-
return 'name'
355+
return "name"
353356

354357
def generate_stats(self, container) -> dict[str, Any]:
355358
# Init the stats for the current container
356359
stats = {
357-
'key': self.key,
358-
'name': nativestr(container.name),
359-
'id': container.id,
360-
'image': ','.join(container.image.tags if container.image.tags else []),
361-
'status': container.attrs['State'],
362-
'created': container.attrs['Created'],
363-
'command': container.attrs.get('Command') or [],
364-
'io': {},
365-
'cpu': {},
366-
'memory': {},
367-
'network': {},
368-
'io_rx': None,
369-
'io_wx': None,
370-
'cpu_percent': None,
371-
'memory_percent': None,
372-
'network_rx': None,
373-
'network_tx': None,
374-
'ports': '',
375-
'uptime': None,
360+
"key": self.key,
361+
"name": nativestr(container.name),
362+
"id": container.id,
363+
"image": ",".join(container.image.tags if container.image.tags else []),
364+
"status": container.attrs["State"],
365+
"created": container.attrs["Created"],
366+
"command": container.attrs.get("Command") or [],
367+
"io": {},
368+
"cpu": {},
369+
"memory": {},
370+
"network": {},
371+
"io_rx": None,
372+
"io_wx": None,
373+
"cpu_percent": None,
374+
"memory_percent": None,
375+
"network_rx": None,
376+
"network_tx": None,
377+
"ports": "",
378+
"uptime": None,
376379
}
377380

378381
stats_fetcher = self.container_stats_fetchers[container.id]
379382
activity_stats = stats_fetcher.activity_stats
380383
stats.update(activity_stats)
381384

382385
# Additional fields
383-
stats['cpu_percent'] = stats['cpu'].get('total')
384-
stats['memory_usage'] = stats['memory'].get('usage')
385-
if stats['memory'].get('cache') is not None:
386-
stats['memory_usage'] -= stats['memory']['cache']
387-
stats['memory_inactive_file'] = stats['memory'].get('inactive_file')
388-
stats['memory_limit'] = stats['memory'].get('limit')
386+
stats["cpu_percent"] = stats["cpu"].get("total")
387+
stats["memory_usage"] = stats["memory"].get("usage")
388+
if stats["memory"].get("cache") is not None:
389+
stats["memory_usage"] -= stats["memory"]["cache"]
390+
stats["memory_inactive_file"] = stats["memory"].get("inactive_file")
391+
stats["memory_limit"] = stats["memory"].get("limit")
389392

390-
if all(k in stats['io'] for k in ('ior', 'iow', 'time_since_update')):
391-
stats['io_rx'] = stats['io']['ior'] // stats['io']['time_since_update']
392-
stats['io_wx'] = stats['io']['iow'] // stats['io']['time_since_update']
393+
if all(k in stats["io"] for k in ("ior", "iow", "time_since_update")):
394+
stats["io_rx"] = stats["io"]["ior"] // stats["io"]["time_since_update"]
395+
stats["io_wx"] = stats["io"]["iow"] // stats["io"]["time_since_update"]
393396

394-
if all(k in stats['network'] for k in ('rx', 'tx', 'time_since_update')):
395-
stats['network_rx'] = stats['network']['rx'] // stats['network']['time_since_update']
396-
stats['network_tx'] = stats['network']['tx'] // stats['network']['time_since_update']
397+
if all(k in stats["network"] for k in ("rx", "tx", "time_since_update")):
398+
stats["network_rx"] = stats["network"]["rx"] // stats["network"]["time_since_update"]
399+
stats["network_tx"] = stats["network"]["tx"] // stats["network"]["time_since_update"]
397400

398-
started_at = datetime.fromtimestamp(container.attrs['StartedAt'])
399-
stats['uptime'] = pretty_date(started_at)
401+
started_at = datetime.fromtimestamp(container.attrs["StartedAt"])
402+
stats["uptime"] = pretty_date(started_at)
400403

401404
# Manage special chars in command (see issue#2733)
402-
stats['command'] = replace_special_chars(' '.join(stats['command']))
405+
stats["command"] = replace_special_chars(" ".join(stats["command"]))
403406

404407
# Manage ports (see issue#2054)
405-
if hasattr(container, 'ports'):
406-
stats['ports'] = ','.join(
408+
if hasattr(container, "ports"):
409+
stats["ports"] = ",".join(
407410
[
408-
f'{container.ports[cp][0]["HostPort"]}->{cp}' if container.ports[cp] else f'{cp}'
411+
(f'{container.ports[cp][0]["HostPort"]}->{cp}' if container.ports[cp] else f"{cp}")
409412
for cp in container.ports
410413
]
411414
)

0 commit comments

Comments
 (0)