From a5b0a524f660a8121d1c9fd571dd2b2c062c00db Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 18 Apr 2023 01:19:06 +0200 Subject: [PATCH] Added `CpuStats` entity to `system`. --- .../components/panels/Entities/CpuStats.vue | 1 + .../src/components/panels/Entities/meta.json | 8 ++++ platypush/entities/system.py | 18 ++++++++ platypush/message/response/system/__init__.py | 22 ---------- platypush/plugins/system/__init__.py | 41 ++++++++++++------- platypush/schemas/system.py | 15 ++++++- 6 files changed, 68 insertions(+), 37 deletions(-) create mode 120000 platypush/backend/http/webapp/src/components/panels/Entities/CpuStats.vue diff --git a/platypush/backend/http/webapp/src/components/panels/Entities/CpuStats.vue b/platypush/backend/http/webapp/src/components/panels/Entities/CpuStats.vue new file mode 120000 index 00000000..792c7695 --- /dev/null +++ b/platypush/backend/http/webapp/src/components/panels/Entities/CpuStats.vue @@ -0,0 +1 @@ +Device.vue \ No newline at end of file diff --git a/platypush/backend/http/webapp/src/components/panels/Entities/meta.json b/platypush/backend/http/webapp/src/components/panels/Entities/meta.json index aaf4e2fd..cbaa8bf8 100644 --- a/platypush/backend/http/webapp/src/components/panels/Entities/meta.json +++ b/platypush/backend/http/webapp/src/components/panels/Entities/meta.json @@ -23,6 +23,14 @@ } }, + "cpu_stats": { + "name": "System", + "name_plural": "System", + "icon": { + "class": "fas fa-gauge" + } + }, + "cpu_times": { "name": "System", "name_plural": "System", diff --git a/platypush/entities/system.py b/platypush/entities/system.py index 2e3831b8..60525a20 100644 --- a/platypush/entities/system.py +++ b/platypush/entities/system.py @@ -70,3 +70,21 @@ if 'cpu_times' not in Base.metadata: __mapper_args__ = { 'polymorphic_identity': __tablename__, } + + +if 'cpu_stats' not in Base.metadata: + + class CpuStats(Entity): + """ + ``CpuStats`` ORM (container) model. + """ + + __tablename__ = 'cpu_stats' + + id = Column( + Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True + ) + + __mapper_args__ = { + 'polymorphic_identity': __tablename__, + } diff --git a/platypush/message/response/system/__init__.py b/platypush/message/response/system/__init__.py index 3f28dab7..ad499e4c 100644 --- a/platypush/message/response/system/__init__.py +++ b/platypush/message/response/system/__init__.py @@ -28,28 +28,6 @@ class SensorResponse(SystemResponse): pass -class CpuStatsResponse(CpuResponse): - def __init__( - self, - ctx_switches: int, - interrupts: int, - soft_interrupts: int, - syscalls: int, - *args, - **kwargs - ): - super().__init__( - *args, - output={ - 'ctx_switches': ctx_switches, - 'interrupts': interrupts, - 'soft_interrupts': soft_interrupts, - 'syscalls': syscalls, - }, - **kwargs - ) - - class CpuFrequencyResponse(CpuResponse): # noinspection PyShadowingBuiltins def __init__(self, min: int, max: int, current: int, *args, **kwargs): diff --git a/platypush/plugins/system/__init__.py b/platypush/plugins/system/__init__.py index 09b01570..8e3644c0 100644 --- a/platypush/plugins/system/__init__.py +++ b/platypush/plugins/system/__init__.py @@ -6,15 +6,15 @@ from typing_extensions import override from platypush.entities import Entity from platypush.entities.managers import EntityManager -from platypush.entities.sensors import PercentSensor +from platypush.entities.sensors import NumericSensor, PercentSensor from platypush.entities.system import ( Cpu, CpuInfo as CpuInfoModel, + CpuStats as CpuStatsModel, CpuTimes as CpuTimesModel, ) from platypush.message.response.system import ( CpuResponseList, - CpuStatsResponse, CpuFrequencyResponse, VirtualMemoryUsageResponse, SwapMemoryUsageResponse, @@ -41,6 +41,8 @@ from platypush.plugins.sensor import SensorPlugin from platypush.schemas.system import ( CpuInfo, CpuInfoSchema, + CpuStats, + CpuStatsSchema, CpuTimes, CpuTimesSchema, SystemInfoSchema, @@ -144,22 +146,20 @@ class SystemPlugin(SensorPlugin, EntityManager): return list(percent) # type: ignore return percent - @action - def cpu_stats(self) -> CpuStatsResponse: - """ - Get CPU stats. - :return: :class:`platypush.message.response.system.CpuStatsResponse` - """ + def _cpu_stats(self) -> CpuStats: import psutil stats = psutil.cpu_stats() + return CpuStatsSchema().load(stats._asdict()) # type: ignore - return CpuStatsResponse( - ctx_switches=stats.ctx_switches, - interrupts=stats.interrupts, - soft_interrupts=stats.soft_interrupts, - syscalls=stats.syscalls, - ) + @action + def cpu_stats(self) -> CpuStats: + """ + Get CPU stats. + + :return: .. schema:: system.CpuStatsSchema + """ + return CpuStatsSchema().dump(self._cpu_stats()) # type: ignore @action def cpu_frequency( @@ -791,6 +791,7 @@ class SystemPlugin(SensorPlugin, EntityManager): { 'cpu': { 'info': self._cpu_info, + 'stats': self._cpu_stats(), 'times': self._cpu_times_avg(), 'percent': self.cpu_percent().output / 100.0, # type: ignore }, @@ -813,6 +814,18 @@ class SystemPlugin(SensorPlugin, EntityManager): name='Info', **cpu['info'], ), + CpuStatsModel( + id='system:cpu:stats', + name='Statistics', + children=[ + NumericSensor( + id=f'system:cpu:stats:{key}', + name=key, + value=value, + ) + for key, value in cpu['stats'].items() + ], + ), CpuTimesModel( id='system:cpu:times', name='Times', diff --git a/platypush/schemas/system.py b/platypush/schemas/system.py index c123ca64..32014318 100644 --- a/platypush/schemas/system.py +++ b/platypush/schemas/system.py @@ -189,6 +189,18 @@ class CpuTimes: guest_nice: Optional[float] = percent_field() +@dataclass +class CpuStats: + """ + CPU stats data class. + """ + + ctx_switches: int + interrupts: int + soft_interrupts: int + syscalls: int + + @dataclass class CpuData: """ @@ -197,6 +209,7 @@ class CpuData: info: CpuInfo times: CpuTimes + stats: CpuStats percent: float = percent_field() @@ -211,5 +224,5 @@ class SystemInfo: CpuInfoSchema = class_schema(CpuInfo, base_schema=CpuInfoBaseSchema) CpuTimesSchema = class_schema(CpuTimes, base_schema=CpuTimesBaseSchema) -CpuDataSchema = class_schema(CpuTimes, base_schema=DataClassSchema) +CpuStatsSchema = class_schema(CpuStats, base_schema=DataClassSchema) SystemInfoSchema = class_schema(SystemInfo, base_schema=DataClassSchema)