From 0073239a40dc1b8ec57dfa822541e6d721626162 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 18 Apr 2023 18:26:02 +0200 Subject: [PATCH] Support for CPU `load_average` entity. --- platypush/plugins/system/__init__.py | 18 ++++++++++++++++-- platypush/schemas/system.py | 3 ++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/platypush/plugins/system/__init__.py b/platypush/plugins/system/__init__.py index 80bce33f90..20e5392e4b 100644 --- a/platypush/plugins/system/__init__.py +++ b/platypush/plugins/system/__init__.py @@ -1,10 +1,11 @@ import socket from datetime import datetime -from typing import Union, List, Optional, Dict +from typing import Tuple, Union, List, Optional, Dict from typing_extensions import override from platypush.entities import Entity +from platypush.entities.devices import Device from platypush.entities.managers import EntityManager from platypush.entities.sensors import NumericSensor, PercentSensor from platypush.entities.system import ( @@ -194,7 +195,7 @@ class SystemPlugin(SensorPlugin, EntityManager): return self._cpu_frequency_per_cpu() if per_cpu else self._cpu_frequency_avg() @action - def load_avg(self) -> List[float]: + def load_avg(self) -> Tuple[float, float, float]: """ Get the average load as a vector that represents the load within the last 1, 5 and 15 minutes. """ @@ -792,6 +793,7 @@ class SystemPlugin(SensorPlugin, EntityManager): 'cpu': { 'frequency': self._cpu_frequency_avg(), 'info': self._cpu_info, + 'load_avg': self.load_avg().output, 'stats': self._cpu_stats(), 'times': self._cpu_times_avg(), 'percent': self.cpu_percent().output / 100.0, # type: ignore @@ -839,6 +841,18 @@ class SystemPlugin(SensorPlugin, EntityManager): for key, time_percent in cpu['times'].items() ], ), + Device( + id='system:cpu:load_avg', + name='Load Average', + children=[ + NumericSensor( + id=f'system:cpu:load_avg:{mins}', + name=f'Last {mins} minute(s)', + value=round(val, 2), + ) + for val, mins in zip(cpu['load_avg'], [1, 5, 15]) + ], + ), NumericSensor( id='system:cpu:frequency', name='Frequency', diff --git a/platypush/schemas/system.py b/platypush/schemas/system.py index 3f0855a8fd..fb49638208 100644 --- a/platypush/schemas/system.py +++ b/platypush/schemas/system.py @@ -1,5 +1,5 @@ from dataclasses import dataclass, field -from typing import List, Optional +from typing import List, Optional, Tuple from marshmallow import pre_load from marshmallow.validate import Range @@ -222,6 +222,7 @@ class CpuData: times: CpuTimes frequency: CpuFrequency stats: CpuStats + load_avg: Tuple[float, float, float] percent: float = percent_field()