Skip to content

refactor: optimize SMS log aggregator store stats#825

Merged
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
DevALVIN-24:fix-sms-stats-iteration
Jun 29, 2026
Merged

refactor: optimize SMS log aggregator store stats#825
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
DevALVIN-24:fix-sms-stats-iteration

Conversation

@DevALVIN-24

Copy link
Copy Markdown
Contributor

Closes #745

🚀 Overview

Fixes: Performance degradation in getStoreStats() when SMS log store reaches its maximum capacity.

The /api/sms/logs?action=store-stats endpoint calls SMSLogAggregator.getStoreStats() to return aggregated summary statistics. Previously, this operation could consume significant CPU and memory because it potentially serialized or iterated over the entire raw log array (up to 5,000 entries) per request.

🛠️ Changes Made

  • Pre-computed Stats: Introduced totalMessages, successfulMessages, and failedMessages counters to the SMSLogAggregator state.
  • Incremental Updates: Refactored addToStore to incrementally update these static counters dynamically when a new log record is processed.
  • O(1) Stats Calculation: Updated getStoreStats() to simply compute and return the cached counters, ensuring O(1) constant time complexity for the stats endpoint.
  • Tests Added: Created src/lib/logging/sms-aggregator.test.ts with a unit test that verifies the accuracy of the tracked statistics over 1,000 insertions without touching the array.

🧪 Verification

  • Verified getStoreStats() operates in constant time regardless of current smsLogStore size.
  • Unit test confirms stats accurately reflect updates across random successful/failed distributions and accurately calculates the successRate.

- Replaced full-store iteration in getStoreStats() with pre-computed counters.
- Added totalMessages, failedCount, and successRate to the stats response.
- Implemented unit tests to verify stats are computed correctly and incrementally over 1000 insertions.
@drips-wave

drips-wave Bot commented Jun 29, 2026

Copy link
Copy Markdown

@DevALVIN-24 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Thank you for contributing to the project.

@RUKAYAT-CODER RUKAYAT-CODER merged commit 5878fa8 into rinafcode:main Jun 29, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance] SMS log getStoreStats() returns raw store data with no pagination or size limit

2 participants