improve precision, tests

This commit is contained in:
Paul Gauthier 2024-11-20 18:00:17 -08:00
parent ec39f018e2
commit e9e51db9c7
2 changed files with 6 additions and 4 deletions

View file

@ -97,12 +97,12 @@ class Analytics:
if not uuid_str:
return False
# Convert percentage to hex threshold (1% = "04", 10% = "1a", etc)
# Using first 2 hex digits (0-ff) means each digit is 1/256 of the space
# Convert percentage to hex threshold (1% = "04...", 10% = "1a...", etc)
# Using first 6 hex digits
if percent == 0:
return False
threshold = format(int(255 * percent / 100), "02x")
return uuid_str[:2] <= threshold
threshold = format(int(0xFFFFFF * percent / 100), "06x")
return uuid_str[:6] <= threshold
def get_data_file_path(self):
data_file = Path.home() / ".aider" / "analytics.json"

View file

@ -113,6 +113,8 @@ def test_is_uuid_in_percentage():
# Test basic percentage thresholds
assert analytics.is_uuid_in_percentage("00000000000000000000000000000000", 1) is True
assert analytics.is_uuid_in_percentage("01999000000000000000000000000000", 1) is True
assert analytics.is_uuid_in_percentage("02000000000000000000000000000000", 1) is True
assert analytics.is_uuid_in_percentage("02910000000000000000000000000001", 1) is False
assert analytics.is_uuid_in_percentage("03000000000000000000000000000000", 1) is False
assert analytics.is_uuid_in_percentage("ff000000000000000000000000000000", 1) is False