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
39 changes: 39 additions & 0 deletions src/lib/logging/sms-aggregator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { SMSLogAggregator } from './sms-aggregator';

describe('SMSLogAggregator', () => {
it('maintains accurate stats after 1000 insertions without iterating', () => {
const initialStats = SMSLogAggregator.getStoreStats();
const initialTotal = initialStats.totalMessages;
const initialFailed = initialStats.failedCount;

let successAdded = 0;
let failedAdded = 0;

for (let i = 0; i < 1000; i++) {
const isSuccess = i % 10 !== 0; // 90% success rate (900 successful, 100 failed)
if (isSuccess) successAdded++;
else failedAdded++;

SMSLogAggregator.collectSMSLogs([{
timestamp: new Date().toISOString(),
level: 'info',
message: 'Test SMS',
scope: 'sms',
context: {
provider: 'test-provider',
status: isSuccess ? 'sent' : 'failed'
}
}]);
}

const stats = SMSLogAggregator.getStoreStats();

expect(stats.totalMessages).toBe(initialTotal + 1000);
expect(stats.failedCount).toBe(initialFailed + failedAdded);

// successRate is (successfulMessages / totalMessages) * 100
// Verify it's correctly calculated
expect(stats.successRate).toBeGreaterThanOrEqual(0);
expect(stats.successRate).toBeLessThanOrEqual(100);
});
});
17 changes: 17 additions & 0 deletions src/lib/logging/sms-aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,26 @@ export class SMSLogAggregator {
};
}

private static stats = {
totalMessages: 0,
successfulMessages: 0,
failedMessages: 0,
};

/**
* Add log to store, maintaining size limit
*/
private static addToStore(log: AggregatedSMSLog): void {
smsLogStore.push(log);

// Update stats incrementally
this.stats.totalMessages++;
if (log.context.status === 'sent') {
this.stats.successfulMessages++;
} else if (log.context.status === 'failed') {
this.stats.failedMessages++;
}

if (smsLogStore.length > MAX_SMS_LOGS) {
smsLogStore.splice(0, smsLogStore.length - MAX_SMS_LOGS);
}
Expand All @@ -437,6 +451,9 @@ export class SMSLogAggregator {
utilizationPercent: (smsLogStore.length / MAX_SMS_LOGS) * 100,
oldestLog: smsLogStore.length > 0 ? smsLogStore[0].timestamp : null,
newestLog: smsLogStore.length > 0 ? smsLogStore[smsLogStore.length - 1].timestamp : null,
totalMessages: this.stats.totalMessages,
failedCount: this.stats.failedMessages,
successRate: this.stats.totalMessages > 0 ? (this.stats.successfulMessages / this.stats.totalMessages) * 100 : 0
};
}
}
Loading