diff --git a/aider/analytics.py b/aider/analytics.py index 537da259e..02f6b1f92 100644 --- a/aider/analytics.py +++ b/aider/analytics.py @@ -78,11 +78,28 @@ class Analytics: if not self.user_id: return False - # Define percentage of users to ask PERCENT = 1 + return self.is_uuid_in_percentage(self.user_id, PERCENT) + + def is_uuid_in_percentage(self, uuid_str, percent): + """Check if a UUID string falls within the first X percent of the UUID space. + + Args: + uuid_str: UUID string to test + percent: Percentage threshold (0-100) + + Returns: + bool: True if UUID falls within the first X percent + """ + if not (0 <= percent <= 100): + raise ValueError("Percentage must be between 0 and 100") + + if not uuid_str: + return False + # Convert percentage to hex threshold (1% = "03", 10% = "1a", etc) - threshold = format(int(256 * PERCENT / 100), "02x") - return self.user_id < threshold + threshold = format(int(256 * percent / 100), "02x") + return uuid_str < threshold def get_data_file_path(self): data_file = Path.home() / ".aider" / "analytics.json" diff --git a/tests/basic/test_analytics.py b/tests/basic/test_analytics.py index baeb21ddc..9d7df7167 100644 --- a/tests/basic/test_analytics.py +++ b/tests/basic/test_analytics.py @@ -105,3 +105,33 @@ def test_need_to_ask(temp_data_dir): analytics.permanently_disable = True assert analytics.need_to_ask(True) is False + + +def test_is_uuid_in_percentage(): + analytics = Analytics() + + # Test basic percentage thresholds + assert analytics.is_uuid_in_percentage("00", 1) is True + assert analytics.is_uuid_in_percentage("02", 1) is True + assert analytics.is_uuid_in_percentage("03", 1) is False + assert analytics.is_uuid_in_percentage("ff", 1) is False + + assert analytics.is_uuid_in_percentage("00", 10) is True + assert analytics.is_uuid_in_percentage("19", 10) is True + assert analytics.is_uuid_in_percentage("1a", 10) is False + assert analytics.is_uuid_in_percentage("ff", 10) is False + + # Test edge cases + assert analytics.is_uuid_in_percentage("00", 0) is False + assert analytics.is_uuid_in_percentage("00", 100) is True + assert analytics.is_uuid_in_percentage("ff", 100) is True + + # Test invalid inputs + with pytest.raises(ValueError): + analytics.is_uuid_in_percentage("00", -1) + with pytest.raises(ValueError): + analytics.is_uuid_in_percentage("00", 101) + + # Test empty/None UUID + assert analytics.is_uuid_in_percentage("", 50) is False + assert analytics.is_uuid_in_percentage(None, 50) is False