From ca7bfc31417930486939d35aedc1dfd02aa5cbef Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 17 Nov 2026 02:49:45 +0000 Subject: [PATCH] Fix duplicate telemetry recording for API calls Each HTTP API call was recording TWO telemetry events because both UDP and TCP service layers called recordApiCall() independently when Promise.all ran them in parallel. Root cause: - handleApiCall in routes.ts called both service.getResponse() and tcpService.getResponse() via Promise.all - Each service method internally called telemetry.recordApiCall() - Result: false/0 event from the failing path, then true/Nms event from the successful path Fix: 1. Removed all telemetry recording from service layer (ASyncService and ASyncTCPService getResponse methods) 2. Added single telemetry recording at HTTP handler level in routes.ts 3. Record exactly ONE event per call: success with response time when device replies, failure on real timeout/error/not-connected 4. Improved response selection logic to prefer successful responses over error messages Added regression tests (telemetry-deduplication.test.ts): - Verify exactly one event per API call - Test success/failure scenarios - Test timeout and device-not-found cases - Test multiple concurrent calls Added historical data deduplication in analyse.ts: - Heuristic to de-duplicate false/0 followed by true within ~5s - Same device+method pairs from double-recording bug - Automatic cleanup when analyzing old telemetry data All tests pass, ASCII-only log output maintained. Co-authored-by: James Brown --- src/__tests__/telemetry-deduplication.test.ts | 257 ++++++++++++++++++ src/api/routes.ts | 24 +- src/service/ASyncService.ts | 7 - src/service/ASyncTCPService.ts | 6 - tools/telemetry/analyse.ts | 56 +++- 5 files changed, 335 insertions(+), 15 deletions(-) create mode 100644 src/__tests__/telemetry-deduplication.test.ts diff --git a/src/__tests__/telemetry-deduplication.test.ts b/src/__tests__/telemetry-deduplication.test.ts new file mode 100644 index 0000000..1ee5d2c --- /dev/null +++ b/src/__tests__/telemetry-deduplication.test.ts @@ -0,0 +1,257 @@ +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; +import express from 'express'; +import request from 'supertest'; +import { TelemetryService } from '../service/TelemetryService'; +import { createRouter } from '../api/routes'; +import { unlinkSync, existsSync } from 'fs'; + +const TEST_DB_PATH = './test-dedup-telemetry.db'; + +describe('Telemetry Deduplication', () => { + let app: express.Application; + let mockService: any; + let mockTcpService: any; + + beforeEach(() => { + if (existsSync(TEST_DB_PATH)) { + unlinkSync(TEST_DB_PATH); + } + TelemetryService.resetInstance(); + TelemetryService.getInstance(TEST_DB_PATH, 30); + + app = express(); + app.use(express.json()); + + // Mock UDP service + mockService = { + checkServices: vi.fn(), + getResponse: vi.fn(), + getDeviceAddress: vi.fn(), + }; + + // Mock TCP service + mockTcpService = { + getResponse: vi.fn(), + getDeviceAddress: vi.fn(), + handleDeviceConnection: vi.fn(), + getLoginChallenge: vi.fn(), + }; + + app.use('/api', createRouter(mockService, mockTcpService)); + }); + + afterEach(() => { + const telemetry = TelemetryService.getInstance(); + telemetry.close(); + if (existsSync(TEST_DB_PATH)) { + unlinkSync(TEST_DB_PATH); + } + }); + + describe('Single Event Recording', () => { + it('should record exactly one event when TCP succeeds', async () => { + const device = '0xabc123'; + const method = 'getValue'; + + // UDP returns "No device found", TCP succeeds + mockService.getResponse.mockResolvedValue('No device found'); + mockTcpService.getResponse.mockResolvedValue('42'); + + await request(app) + .get(`/api/${device}/${method}`) + .expect(201); + + const telemetry = TelemetryService.getInstance(); + const events = telemetry.getEvents({ device, limit: 100, offset: 0 }); + + // Should have exactly one api_call event + const apiCallEvents = events.events.filter(e => e.eventType === 'api_call'); + expect(apiCallEvents).toHaveLength(1); + + // Should be a success + expect(apiCallEvents[0].apiMethod).toBe(method); + expect(apiCallEvents[0].apiSuccess).toBe(true); + expect(apiCallEvents[0].apiResponseTime).toBeGreaterThanOrEqual(0); + }); + + it('should record exactly one event when UDP succeeds', async () => { + const device = '0xdef456'; + const method = 'setValue'; + + // UDP succeeds, TCP returns "No device found" + mockService.getResponse.mockResolvedValue('OK'); + mockTcpService.getResponse.mockResolvedValue('No device found'); + + await request(app) + .get(`/api/${device}/${method}`) + .expect(201); + + const telemetry = TelemetryService.getInstance(); + const events = telemetry.getEvents({ device, limit: 100, offset: 0 }); + + // Should have exactly one api_call event + const apiCallEvents = events.events.filter(e => e.eventType === 'api_call'); + expect(apiCallEvents).toHaveLength(1); + + // Should be a success + expect(apiCallEvents[0].apiMethod).toBe(method); + expect(apiCallEvents[0].apiSuccess).toBe(true); + expect(apiCallEvents[0].apiResponseTime).toBeGreaterThanOrEqual(0); + }); + + it('should record exactly one failure event when device not found', async () => { + const device = '0xffffff'; + const method = 'test'; + + // Both services return "No device found" + mockService.getResponse.mockResolvedValue('No device found'); + mockTcpService.getResponse.mockResolvedValue('No device found'); + + await request(app) + .get(`/api/${device}/${method}`) + .expect(201); + + const telemetry = TelemetryService.getInstance(); + const events = telemetry.getEvents({ device, limit: 100, offset: 0 }); + + // Should have exactly one api_call event + const apiCallEvents = events.events.filter(e => e.eventType === 'api_call'); + expect(apiCallEvents).toHaveLength(1); + + // Should be a failure + expect(apiCallEvents[0].apiMethod).toBe(method); + expect(apiCallEvents[0].apiSuccess).toBe(false); + expect(apiCallEvents[0].apiResponseTime).toBeGreaterThanOrEqual(0); + }); + + it('should record exactly one failure event on timeout', async () => { + const device = '0x123456'; + const method = 'slowMethod'; + + // Both services timeout + mockService.getResponse.mockResolvedValue('Timed out'); + mockTcpService.getResponse.mockResolvedValue('Timed out'); + + await request(app) + .get(`/api/${device}/${method}`) + .expect(201); + + const telemetry = TelemetryService.getInstance(); + const events = telemetry.getEvents({ device, limit: 100, offset: 0 }); + + // Should have exactly one api_call event + const apiCallEvents = events.events.filter(e => e.eventType === 'api_call'); + expect(apiCallEvents).toHaveLength(1); + + // Should be a failure + expect(apiCallEvents[0].apiMethod).toBe(method); + expect(apiCallEvents[0].apiSuccess).toBe(false); + expect(apiCallEvents[0].apiResponseTime).toBeGreaterThanOrEqual(0); + }); + + it('should record exactly one failure event on API send error', async () => { + const device = '0x789abc'; + const method = 'failMethod'; + + // UDP has send error, TCP not found + mockService.getResponse.mockResolvedValue('API send error'); + mockTcpService.getResponse.mockResolvedValue('No device found'); + + await request(app) + .get(`/api/${device}/${method}`) + .expect(201); + + const telemetry = TelemetryService.getInstance(); + const events = telemetry.getEvents({ device, limit: 100, offset: 0 }); + + // Should have exactly one api_call event + const apiCallEvents = events.events.filter(e => e.eventType === 'api_call'); + expect(apiCallEvents).toHaveLength(1); + + // Should be a failure + expect(apiCallEvents[0].apiMethod).toBe(method); + expect(apiCallEvents[0].apiSuccess).toBe(false); + }); + }); + + describe('Multiple Calls', () => { + it('should record exactly one event per call for multiple calls', async () => { + const device = '0xmulti'; + const methods = ['method1', 'method2', 'method3']; + + mockService.getResponse.mockResolvedValue('No device found'); + mockTcpService.getResponse.mockResolvedValue('response'); + + for (const method of methods) { + await request(app) + .get(`/api/${device}/${method}`) + .expect(201); + } + + const telemetry = TelemetryService.getInstance(); + const events = telemetry.getEvents({ device, limit: 100, offset: 0 }); + + // Should have exactly 3 api_call events (one per call) + const apiCallEvents = events.events.filter(e => e.eventType === 'api_call'); + expect(apiCallEvents).toHaveLength(3); + + // All should be successes + apiCallEvents.forEach(event => { + expect(event.apiSuccess).toBe(true); + }); + }); + }); + + describe('Response Time Measurement', () => { + it('should record non-zero response time for successful calls', async () => { + const device = '0xtiming'; + const method = 'timedMethod'; + + // Simulate some delay + mockService.getResponse.mockImplementation(() => + new Promise(resolve => setTimeout(() => resolve('No device found'), 10)) + ); + mockTcpService.getResponse.mockImplementation(() => + new Promise(resolve => setTimeout(() => resolve('success'), 20)) + ); + + await request(app) + .get(`/api/${device}/${method}`) + .expect(201); + + const telemetry = TelemetryService.getInstance(); + const events = telemetry.getEvents({ device, limit: 100, offset: 0 }); + + const apiCallEvents = events.events.filter(e => e.eventType === 'api_call'); + expect(apiCallEvents).toHaveLength(1); + + // Response time should reflect the actual call duration + expect(apiCallEvents[0].apiResponseTime).toBeGreaterThanOrEqual(20); + }); + + it('should record response time even for failures', async () => { + const device = '0xfailtime'; + const method = 'failMethod'; + + mockService.getResponse.mockImplementation(() => + new Promise(resolve => setTimeout(() => resolve('No device found'), 15)) + ); + mockTcpService.getResponse.mockImplementation(() => + new Promise(resolve => setTimeout(() => resolve('No device found'), 15)) + ); + + await request(app) + .get(`/api/${device}/${method}`) + .expect(201); + + const telemetry = TelemetryService.getInstance(); + const events = telemetry.getEvents({ device, limit: 100, offset: 0 }); + + const apiCallEvents = events.events.filter(e => e.eventType === 'api_call'); + expect(apiCallEvents).toHaveLength(1); + + expect(apiCallEvents[0].apiSuccess).toBe(false); + expect(apiCallEvents[0].apiResponseTime).toBeGreaterThanOrEqual(15); + }); + }); +}); diff --git a/src/api/routes.ts b/src/api/routes.ts index 5a63c42..62ece60 100644 --- a/src/api/routes.ts +++ b/src/api/routes.ts @@ -972,6 +972,7 @@ export function createRouter(service: ASyncService, tcpService: ASyncTCPService) // ========== IoT device routes ========== async function handleApiCall(req: Request, res: Response, asyncPath: boolean): Promise { + const startTime = Date.now(); const address = normalizeAddress(req.params.Address); const method = req.params.method; const clientIp = getClientIp(req); @@ -993,7 +994,28 @@ export function createRouter(service: ASyncService, tcpService: ASyncTCPService) tcpService.getResponse(address, method, argMap, clientDesignator), ]); - const response = responseTCP ?? responseUDP; + // Prefer successful responses over error messages + const isSuccess = (r: string) => + r !== 'No device found' && + r !== 'Timed out' && + r !== 'API send error' && + r !== ''; + + let response: string; + if (isSuccess(responseTCP)) { + response = responseTCP; + } else if (isSuccess(responseUDP)) { + response = responseUDP; + } else { + // Both failed, prefer TCP error message + response = responseTCP ?? responseUDP; + } + + const responseTime = Date.now() - startTime; + const success = isSuccess(response); + + telemetry.recordApiCall(address, method, success, responseTime); + res.status(201).send(response); } diff --git a/src/service/ASyncService.ts b/src/service/ASyncService.ts index 1bba271..7038146 100644 --- a/src/service/ASyncService.ts +++ b/src/service/ASyncService.ts @@ -29,10 +29,8 @@ export class ASyncService { } async getResponse(address: string, method: string, argMap: MultiValueMap, origin: string): Promise { - const startTime = Date.now(); let instance = this.getLatestClient(address.toLowerCase()); if (!instance) { - this.telemetry.recordApiCall(address, method, false, Date.now() - startTime); return 'No device found'; } @@ -46,7 +44,6 @@ export class ASyncService { } if (methodId === -1) { - this.telemetry.recordApiCall(address, method, false, Date.now() - startTime); return 'API send error'; } @@ -68,10 +65,6 @@ export class ASyncService { } const response = instance?.getResponse(methodId) ?? (resendCount === 0 ? 'Timed out' : ''); - const responseTime = Date.now() - startTime; - const success = resendCount > 0 && response !== 'Timed out'; - - this.telemetry.recordApiCall(address, method, success, responseTime); if (resendCount === 0) { console.log('Timed out'); diff --git a/src/service/ASyncTCPService.ts b/src/service/ASyncTCPService.ts index e15053b..1ffc88c 100644 --- a/src/service/ASyncTCPService.ts +++ b/src/service/ASyncTCPService.ts @@ -490,26 +490,22 @@ export class ASyncTCPService implements TCPCallback { } async getResponse(address: string, method: string, argMap: MultiValueMap, _origin: string): Promise { - const startTime = Date.now(); const addrLower = address.toLowerCase(); if (!this.clientMap.has(addrLower) && !this.addressToClient.has(addrLower)) { const knownAddrs = [...this.clientMap.keys()]; console.log('No device found for', addrLower, '| Known TCP clients:', knownAddrs.length, knownAddrs); - this.telemetry.recordApiCall(address, method, false, Date.now() - startTime); return 'No device found'; } const idx = this.clientMap.get(addrLower); if (idx === undefined) { console.log('Address in map but idx undefined (race?) for', addrLower); - this.telemetry.recordApiCall(address, method, false, Date.now() - startTime); return 'No device found'; } const client = this.clientIndexList.get(idx); if (!client) { console.log('Client #' + idx + ' not in clientIndexList (disconnected?)'); - this.telemetry.recordApiCall(address, method, false, Date.now() - startTime); return 'No device found'; } @@ -525,11 +521,9 @@ export class ASyncTCPService implements TCPCallback { await new Promise((r) => setTimeout(r, 100)); const list = this.clientResponse.get(addrLower); if (list?.length) { - this.telemetry.recordApiCall(address, method, true, Date.now() - startTime); return list[0]; } } - this.telemetry.recordApiCall(address, method, false, Date.now() - startTime); return 'Timed out'; } diff --git a/tools/telemetry/analyse.ts b/tools/telemetry/analyse.ts index ca33ada..7af979f 100644 --- a/tools/telemetry/analyse.ts +++ b/tools/telemetry/analyse.ts @@ -114,6 +114,57 @@ function parseJsonlFile(path: string): Event[] { .map(line => JSON.parse(line)); } +/** + * Deduplicate API call events from historical data that were double-recorded + * before the telemetry fix. Removes false/0 events followed by true events + * for the same device+method within ~5 seconds. + * + * NOTE: This is a heuristic for historical data cleanup. New telemetry + * recording (post-fix) ensures exactly one event per call. + */ +function deduplicateApiCallEvents(events: Event[]): Event[] { + const deduplicated: Event[] = []; + let skipNext = new Set(); + + for (let i = 0; i < events.length; i++) { + if (skipNext.has(i)) continue; + + const event = events[i]; + + // Only process api_call events + if (event.eventType !== 'api_call') { + deduplicated.push(event); + continue; + } + + // Look for potential duplicate: false/0 followed by true within 5s + if (event.apiSuccess === false && event.apiResponseTime === 0 && i + 1 < events.length) { + const nextEvent = events[i + 1]; + + // Check if next event is a matching successful call + if (nextEvent.eventType === 'api_call' && + nextEvent.device === event.device && + nextEvent.apiMethod === event.apiMethod && + nextEvent.apiSuccess === true && + (nextEvent.timestamp - event.timestamp) < 5000) { + + // Skip the false/0 event, keep only the successful one + skipNext.add(i); + continue; + } + } + + deduplicated.push(event); + } + + const removed = events.length - deduplicated.length; + if (removed > 0) { + console.error(`Deduplicated ${removed} historical duplicate api_call events\n`); + } + + return deduplicated; +} + function analyzeDevice(events: Event[]): DeviceAnalysis { const sessions: DeviceSession[] = []; let currentSession: DeviceSession | null = null; @@ -518,8 +569,11 @@ async function main() { } console.error(`Analyzing: ${inputFile}\n`); - const events = parseJsonlFile(inputFile); + let events = parseJsonlFile(inputFile); console.error(`Loaded ${events.length} events\n`); + + // Deduplicate historical double-recorded API calls + events = deduplicateApiCallEvents(events); const deviceEvents = new Map(); for (const event of events) {