Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 257 additions & 0 deletions src/__tests__/telemetry-deduplication.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
24 changes: 23 additions & 1 deletion src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ export function createRouter(service: ASyncService, tcpService: ASyncTCPService)

// ========== IoT device routes ==========
async function handleApiCall(req: Request, res: Response, asyncPath: boolean): Promise<void> {
const startTime = Date.now();
const address = normalizeAddress(req.params.Address);
const method = req.params.method;
const clientIp = getClientIp(req);
Expand All @@ -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);
}

Expand Down
7 changes: 0 additions & 7 deletions src/service/ASyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ export class ASyncService {
}

async getResponse(address: string, method: string, argMap: MultiValueMap, origin: string): Promise<string> {
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';
}

Expand All @@ -46,7 +44,6 @@ export class ASyncService {
}

if (methodId === -1) {
this.telemetry.recordApiCall(address, method, false, Date.now() - startTime);
return 'API send error';
}

Expand All @@ -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');
Expand Down
6 changes: 0 additions & 6 deletions src/service/ASyncTCPService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,26 +490,22 @@ export class ASyncTCPService implements TCPCallback {
}

async getResponse(address: string, method: string, argMap: MultiValueMap, _origin: string): Promise<string> {
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';
}

Expand All @@ -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';
}

Expand Down
Loading
Loading