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
14 changes: 7 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2146,19 +2146,19 @@ def validate_profile_input(value: str) -> bool:
total_api_calls = _api_stats["control_d_api_calls"] + _api_stats["blocklist_fetches"]
if total_api_calls > 0:
print(f"{Colors.BOLD}API Statistics:{Colors.ENDC}")
print(f" • Control D API calls: {_api_stats['control_d_api_calls']:>6,}")
print(f" • Blocklist fetches: {_api_stats['blocklist_fetches']:>6,}")
print(f" • Total API requests: {total_api_calls:>6,}")
print(f" • Control D API calls: {_api_stats['control_d_api_calls']:>7,}")
print(f" • Blocklist fetches: {_api_stats['blocklist_fetches']:>7,}")
print(f" • Total API requests: {total_api_calls:>7,}")
print()

# Display cache statistics if any cache activity occurred
if _cache_stats["hits"] + _cache_stats["misses"] + _cache_stats["validations"] > 0:
print(f"{Colors.BOLD}Cache Statistics:{Colors.ENDC}")
print(f" • Hits (in-memory): {_cache_stats['hits']:>6,}")
print(f" • Misses (downloaded): {_cache_stats['misses']:>6,}")
print(f" • Validations (304): {_cache_stats['validations']:>6,}")
print(f" • Hits (in-memory): {_cache_stats['hits']:>7,}")
print(f" • Misses (downloaded): {_cache_stats['misses']:>7,}")
print(f" • Validations (304): {_cache_stats['validations']:>7,}")
if _cache_stats["errors"] > 0:
print(f" • Errors (non-fatal): {_cache_stats['errors']:>6,}")
print(f" • Errors (non-fatal): {_cache_stats['errors']:>7,}")

# Calculate cache effectiveness
total_requests = _cache_stats["hits"] + _cache_stats["misses"] + _cache_stats["validations"]
Comment on lines +2161 to 2164
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description says statistics output alignment/number formatting was standardized across cache statistics, but the Cache effectiveness line later in this same block still uses its previous spacing/width ({cache_effectiveness:>6.1f}%), so the numeric column won’t fully align with the :>7, counts above. Consider updating that line as well so the section is consistently aligned end-to-end.

Copilot uses AI. Check for mistakes.
Expand Down
46 changes: 30 additions & 16 deletions tests/test_api_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
class TestAPITracking(unittest.TestCase):
"""Tests for API call tracking and statistics"""

def setUp(self):
"""Save original counter values before each test."""
import main
self.original_control_d_calls = main._api_stats["control_d_api_calls"]
self.original_blocklist_fetches = main._api_stats["blocklist_fetches"]

def tearDown(self):
"""Restore original counter values after each test."""
import main
main._api_stats["control_d_api_calls"] = self.original_control_d_calls
main._api_stats["blocklist_fetches"] = self.original_blocklist_fetches

def test_api_stats_initialized(self):
"""Test that _api_stats is properly initialized"""
import main
Expand All @@ -23,8 +35,8 @@ def test_api_get_increments_counter(self, mock_client_class):
"""Test that _api_get increments the API call counter"""
import main

# Reset counter
main._api_stats["control_d_api_calls"] = 0
# Record initial value
initial_count = main._api_stats["control_d_api_calls"]

# Mock the client and response
mock_client = MagicMock()
Expand All @@ -35,16 +47,16 @@ def test_api_get_increments_counter(self, mock_client_class):
# Call _api_get
main._api_get(mock_client, "http://test.url")

# Verify counter was incremented
self.assertEqual(main._api_stats["control_d_api_calls"], 1)
# Verify counter was incremented by 1
self.assertEqual(main._api_stats["control_d_api_calls"], initial_count + 1)

@patch('main.httpx.Client')
def test_api_post_increments_counter(self, mock_client_class):
"""Test that _api_post increments the API call counter"""
import main

# Reset counter
main._api_stats["control_d_api_calls"] = 0
# Record initial value
initial_count = main._api_stats["control_d_api_calls"]

# Mock the client and response
mock_client = MagicMock()
Expand All @@ -55,16 +67,16 @@ def test_api_post_increments_counter(self, mock_client_class):
# Call _api_post
main._api_post(mock_client, "http://test.url", {"key": "value"})

# Verify counter was incremented
self.assertEqual(main._api_stats["control_d_api_calls"], 1)
# Verify counter was incremented by 1
self.assertEqual(main._api_stats["control_d_api_calls"], initial_count + 1)

@patch('main.httpx.Client')
def test_api_delete_increments_counter(self, mock_client_class):
"""Test that _api_delete increments the API call counter"""
import main

# Reset counter
main._api_stats["control_d_api_calls"] = 0
# Record initial value
initial_count = main._api_stats["control_d_api_calls"]

# Mock the client and response
mock_client = MagicMock()
Expand All @@ -75,16 +87,18 @@ def test_api_delete_increments_counter(self, mock_client_class):
# Call _api_delete
main._api_delete(mock_client, "http://test.url")

# Verify counter was incremented
self.assertEqual(main._api_stats["control_d_api_calls"], 1)
# Verify counter was incremented by 1
self.assertEqual(main._api_stats["control_d_api_calls"], initial_count + 1)

@patch('main._gh')
def test_gh_get_increments_blocklist_counter(self, mock_gh_client):
"""Test that _gh_get increments the blocklist fetch counter"""
import main

# Reset counters
main._api_stats["blocklist_fetches"] = 0
# Record initial value
initial_count = main._api_stats["blocklist_fetches"]

# Clear cache to ensure we make a fresh request
main._cache.clear()

# Mock the streaming response
Expand All @@ -103,8 +117,8 @@ def test_gh_get_increments_blocklist_counter(self, mock_gh_client):
except Exception:
pass # May fail on validation, we just care about the counter

# Verify blocklist counter was incremented
self.assertGreaterEqual(main._api_stats["blocklist_fetches"], 1)
# Verify blocklist counter was incremented by at least 1
self.assertGreaterEqual(main._api_stats["blocklist_fetches"], initial_count + 1)


if __name__ == '__main__':
Expand Down
Loading