Skip to content

Commit

Permalink
Update 'containers` data key (#21)
Browse files Browse the repository at this point in the history
* Update 'containers` data key

* fix tests

* more fixes

* fix formatting
  • Loading branch information
engrbm87 authored Oct 20, 2023
1 parent 06756df commit 9fab51c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
7 changes: 5 additions & 2 deletions glances_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,12 @@ async def get_ha_sensor_data(self) -> dict[str, Any]:
"tx": round(network["tx"] / 1024, 1),
"speed": round(network["speed"] / 1024**3, 1),
}
if "docker" in self.data and (data := self.data["docker"].get("containers")):
data = self.data.get("dockers") or self.data.get("containers")
if data and (containers_data := data.get("containers")):
active_containers = [
container for container in data if container["Status"] == "running"
container
for container in containers_data
if container["Status"] == "running"
]
sensor_data["docker"] = {"docker_active": len(active_containers)}
cpu_use = 0.0
Expand Down
14 changes: 8 additions & 6 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
"key": "disk_name",
},
],
"docker": {
"containers": {
"version": {},
"version_podman": {},
"containers": [
{
"key": "name",
Expand Down Expand Up @@ -101,7 +103,7 @@
},
"memory_usage": 50126848,
},
]
],
},
"fs": [
{
Expand Down Expand Up @@ -316,10 +318,10 @@ async def test_ha_sensor_data_with_incomplete_container_information(
):
"""Test the return value for ha sensors when container memory and cpu data is not exposed by glances."""
TEST_RESPONSE = RESPONSE
del TEST_RESPONSE["docker"]["containers"][0]["memory"]["usage"]
del TEST_RESPONSE["docker"]["containers"][0]["cpu"]["total"]
del TEST_RESPONSE["docker"]["containers"][1]["memory"]["usage"]
del TEST_RESPONSE["docker"]["containers"][1]["cpu"]["total"]
del TEST_RESPONSE["containers"]["containers"][0]["memory"]["usage"]
del TEST_RESPONSE["containers"]["containers"][0]["cpu"]["total"]
del TEST_RESPONSE["containers"]["containers"][1]["memory"]["usage"]
del TEST_RESPONSE["containers"]["containers"][1]["cpu"]["total"]

TEST_HA_SENSOR_DATA = HA_SENSOR_DATA
TEST_HA_SENSOR_DATA["docker"]["docker_memory_use"] = 0
Expand Down

0 comments on commit 9fab51c

Please sign in to comment.