Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add containers data #46

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changes
=======

0.9.0 - 2024-11-15
------------------

- Add support for Glances containers

0.8.0 - 2024-06-09
------------------

Expand Down
8 changes: 8 additions & 0 deletions glances_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ async def get_ha_sensor_data(self) -> dict[str, Any]:
# Glances v4 provides a list of containers
containers_data = self.data.get("containers")
if containers_data:
sensor_data["containers"] = {}
active_containers = [
container
for container in containers_data
Expand All @@ -199,6 +200,13 @@ async def get_ha_sensor_data(self) -> dict[str, Any]:
for container in active_containers:
mem_use += container["memory"].get("usage", 0)
sensor_data["docker"]["docker_memory_use"] = round(mem_use / 1024**2, 1)
for container in active_containers:
sensor_data["containers"][container["name"]] = {
"container_cpu_use": round(container["cpu"].get("total", 0), 1),
"container_memory_use": round(
container["memory"].get("usage", 0) / 1024**2, 1
),
}
if data := self.data.get("raid"):
sensor_data["raid"] = data
if data := self.data.get("uptime"):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "glances_api"
version = "0.8.0"
version = "0.9.0"
description = "Python API for interacting with Glances"
authors = ["Fabian Affolter <fabian@affolter-engineering.ch>"]
homepage = "https://github.com/home-assistant-ecosystem/python-glances-api"
Expand Down
25 changes: 25 additions & 0 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,16 @@
"fan_speed": 0,
},
},
"containers": {
"container1": {
"container_cpu_use": 50.9,
"container_memory_use": 1068.4,
},
"container2": {
"container_cpu_use": 26.2,
"container_memory_use": 81.2,
},
},
}

RESPONSE_V4: dict[str, Any] = {
Expand Down Expand Up @@ -377,6 +387,16 @@
"network": {
"eth0": {"is_up": None, "rx": 6377770.0, "speed": 1.0, "tx": 41670.0},
},
"containers": {
"container1": {
"container_cpu_use": 0.4,
"container_memory_use": 0.0,
},
"container2": {
"container_cpu_use": 0.0,
"container_memory_use": 0.0,
},
},
}


Expand Down Expand Up @@ -404,6 +424,7 @@ async def test_plugins_list(httpx_mock: HTTPXMock) -> None:


@pytest.mark.asyncio
@pytest.mark.httpx_mock(can_send_already_matched_responses=True)
async def test_exisiting_endpoint(httpx_mock: HTTPXMock) -> None:
"""Test the a valid endpoint."""
httpx_mock.add_response(json=RESPONSE)
Expand Down Expand Up @@ -446,6 +467,10 @@ async def test_ha_sensor_data_with_incomplete_container_information(
ha_sensor_data = HA_SENSOR_DATA.copy()
ha_sensor_data["docker"]["docker_memory_use"] = 0
ha_sensor_data["docker"]["docker_cpu_use"] = 0
ha_sensor_data["containers"]["container1"]["container_memory_use"] = 0
ha_sensor_data["containers"]["container1"]["container_cpu_use"] = 0
ha_sensor_data["containers"]["container2"]["container_memory_use"] = 0
ha_sensor_data["containers"]["container2"]["container_cpu_use"] = 0

httpx_mock.add_response(json=response)

Expand Down
Loading