From eead518f959d87d2e4395b76691002d7773d2539 Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Wed, 9 Nov 2022 14:58:03 +0100 Subject: [PATCH] Gracefully handle unknown service UUIDs (#10) --- src/metrics.ts | 2 +- tests/aggregator.test.ts | 17 ++++ tests/fixtures/issues/gh-9-unknown-uuid.json | 85 ++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/issues/gh-9-unknown-uuid.json diff --git a/src/metrics.ts b/src/metrics.ts index 1764220..e2c7736 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -137,5 +137,5 @@ function uuidToServerName(uuid: string): string { } } } - throw new Error(`Could not resolve UUID ${uuid} to service`) + return 'custom' } diff --git a/tests/aggregator.test.ts b/tests/aggregator.test.ts index 2f44eb0..17168cc 100644 --- a/tests/aggregator.test.ts +++ b/tests/aggregator.test.ts @@ -5,6 +5,7 @@ import dysonData from './fixtures/dyson.json' import emptyData from './fixtures/empty.json' import tpLinkData from './fixtures/tp-link.json' import harmonyData from './fixtures/harmony.json' +import unknownUuidData from './fixtures/issues/gh-9-unknown-uuid.json' describe('Metrics aggregator', () => { const timestamp = new Date('2000-01-01 00:00:00 UTC') @@ -154,4 +155,20 @@ describe('Metrics aggregator', () => { new Metric('speaker_volume_percentage', 50, timestamp, expectedLabels4), ]) }) + + test('Aggregates metrics with unknown service UUID as "custom"', () => { + const expectedLabels = { + bridge: 'Test bridge', + device_id: 'AA:AA:AA:AA:AA:AA', + name: 'Phoscon-GW', + } + + const unknowmnUuid = DeviceBoundary.parse(unknownUuidData) + + expect(aggregate([unknowmnUuid], timestamp)).toEqual([ + new Metric('custom_heartrate_seconds', 5, timestamp, expectedLabels), + new Metric('custom_transition_time_seconds', 0.4, timestamp, expectedLabels), + new Metric('custom_restart', 0, timestamp, expectedLabels), + ]) + }) }) diff --git a/tests/fixtures/issues/gh-9-unknown-uuid.json b/tests/fixtures/issues/gh-9-unknown-uuid.json new file mode 100644 index 0000000..ce4ee91 --- /dev/null +++ b/tests/fixtures/issues/gh-9-unknown-uuid.json @@ -0,0 +1,85 @@ +{ + "ipAddress": "192.168.0.1", + "instance": { + "host": "192.168.0.1", + "port": 51826, + "url": "http://192.168.0.1:51826", + "deviceID": "AA:AA:AA:AA:AA:AA", + "txt": { + "c#": "5", + "ff": "0", + "id": "AA:AA:AA:AA:AA:AA", + "md": "homebridge", + "pv": "1.1", + "s#": "1", + "sf": "0", + "ci": "2", + "sh": "Hv0v9A==" + }, + "name": "Test bridge" + }, + "accessories": { + "accessories": [ + { + "aid": 3, + "services": [ + { + "type": "00000012-0000-1000-8000-656261617577", + "iid": 8, + "characteristics": [ + { + "type": "23", + "iid": 9, + "value": "Phoscon-GW", + "perms": ["pr"], + "description": "Name", + "format": "string", + "maxLen": 64 + }, + { + "type": "00000024-0000-1000-8000-656261617577", + "iid": 10, + "value": 5, + "perms": ["pr", "ev", "pw"], + "description": "Heartrate", + "format": "uint16", + "unit": "seconds", + "minValue": 1, + "maxValue": 30, + "minStep": 1 + }, + { + "type": "00000023-0000-1000-8000-656261617577", + "iid": 11, + "value": "Thu Mar 04 2021 22:57:10", + "perms": ["pr", "ev"], + "description": "Last Updated", + "format": "string" + }, + { + "type": "0000002F-0000-1000-8000-656261617577", + "iid": 12, + "value": 0.4, + "perms": ["pr", "ev", "pw"], + "description": "Transition Time", + "format": "float", + "unit": "seconds", + "minValue": 0, + "maxValue": 3600, + "minStep": 0.1 + }, + { + "type": "00000058-0000-1000-8000-656261617577", + "iid": 13, + "value": 0, + "perms": ["pr", "ev", "pw"], + "description": "Restart", + "format": "bool" + } + ] + } + ] + } + ] + } +}