From f5e668d545228dbeb535ffcde3808dbcc6fe6c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A1=E5=8F=A3=E6=B5=AA=E4=BA=BA?= Date: Thu, 15 Oct 2026 01:00:13 +0800 Subject: [PATCH] perf: use two-second minimum Raspberry Pi collection cadence --- plugin/manifest.json | 4 ++-- plugin/worker.py | 10 +++++++++- tests/test_metrics.py | 7 ++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/plugin/manifest.json b/plugin/manifest.json index 4f882b3..80a59c1 100644 --- a/plugin/manifest.json +++ b/plugin/manifest.json @@ -1,7 +1,7 @@ { "id": "pigway-agent", "name": "PIGWay Raspberry Pi Agent", - "version": "1.3.0", + "version": "1.3.1", "protocol_version": 1, "entrypoint": "worker.py", "runtime": "python3", @@ -40,6 +40,6 @@ }, "legacy_connection_fields": true, "collection": { - "interval_seconds": 1 + "interval_seconds": 2 } } diff --git a/plugin/worker.py b/plugin/worker.py index 5738bf5..a3dc85f 100644 --- a/plugin/worker.py +++ b/plugin/worker.py @@ -56,6 +56,14 @@ def request(device, endpoint, method='GET', payload=None, query=None): raise AdapterError('RESULT_UNKNOWN' if method == 'PUT' else 'DEVICE_UNAVAILABLE') from None +def collection_interval(value): + declaration = value.get('collection', {}) + interval = declaration.get('interval_seconds') if isinstance(declaration, dict) else None + if not isinstance(interval, (int, float)) or isinstance(interval, bool) or not math.isfinite(interval) or not 1 <= interval <= 3600: + interval = MANIFEST['collection']['interval_seconds'] + return max(MANIFEST['collection']['interval_seconds'], interval) + + def collect(device): value = request(device, '/status') if not isinstance(value.get('system'), dict) or not isinstance(value.get('machine'), dict): @@ -88,7 +96,7 @@ def collect(device): return {'schema_version': 1, 'observed_at': value.get('timestamp'), 'identity': {'reported_name': value['machine'].get('name'), 'category': 'host', 'details': value.get('device', {})}, - 'collection': value.get('collection', MANIFEST['collection']), + 'collection': {'interval_seconds': collection_interval(value)}, 'capabilities': MANIFEST['capabilities'], 'metrics': metrics, 'resources': [{'id': 'host', 'type': 'host'}], 'alerts': value.get('display', {}).get('alerts', []), diff --git a/tests/test_metrics.py b/tests/test_metrics.py index bba7ff8..aad9897 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -21,9 +21,14 @@ class MetricsTests(unittest.TestCase): self.assertEqual(values['network_rx_bytes_per_second'],0) self.assertEqual(values['network_tx_bytes_per_second'],1234) self.assertEqual(next(m['observed_at'] for m in result['metrics'] if m['key']=='network_tx_bytes_per_second'),120) - self.assertEqual(result['collection']['interval_seconds'],1) + self.assertEqual(result['collection']['interval_seconds'],2) self.assertTrue(result['alerts_complete']) + def test_collection_minimum_and_slower_device(self): + self.assertEqual(worker.collection_interval({'collection':{'interval_seconds':1}}),2) + self.assertEqual(worker.collection_interval({'collection':{'interval_seconds':5}}),5) + self.assertEqual(worker.collection_interval({'collection':{'interval_seconds':True}}),2) + def test_absent_or_boolean_fields_not_invented(self): value={'system': {'cpu_percent': True}, 'machine':{}, 'network': {'link_speed_mbps': None, 'rx_bytes_per_second':float('nan'), 'tx_bytes_per_second':True}} with patch.object(worker,'request',return_value=value): result=worker.collect({})