Loading tools/edit_monitor/daemon_manager.py +1 −1 Original line number Diff line number Diff line Loading @@ -84,7 +84,7 @@ class DaemonManager: "edit_monitor", self.user_name, "ENABLE_EDIT_MONITOR", "EDIT_MONITOR_ROLLOUT_PERCENTAGE", 10, ): logging.warning("Edit monitor is disabled, exiting...") return Loading tools/edit_monitor/utils.py +2 −20 Original line number Diff line number Diff line Loading @@ -21,7 +21,7 @@ def is_feature_enabled( feature_name: str, user_name: str, enable_flag: str = None, rollout_flag: str = None, rollout_percent: int = 100, ) -> bool: """Determine whether the given feature is enabled. Loading @@ -46,26 +46,8 @@ def is_feature_enabled( logging.info("feature: %s is enabled", feature_name) return True if not rollout_flag: return True hash_object = hashlib.sha256() hash_object.update((user_name + feature_name).encode("utf-8")) hash_number = int(hash_object.hexdigest(), 16) % 100 roll_out_percentage = os.environ.get(rollout_flag, "0") try: percentage = int(roll_out_percentage) if percentage < 0 or percentage > 100: logging.warning( "Rollout percentage: %s out of range, disable the feature.", roll_out_percentage, ) return False return hash_number < percentage except ValueError: logging.warning( "Invalid rollout percentage: %s, disable the feature.", roll_out_percentage, ) return False return hash_number < rollout_percent tools/edit_monitor/utils_test.py +2 −39 Original line number Diff line number Diff line Loading @@ -46,60 +46,23 @@ class EnableFeatureTest(unittest.TestCase): ) ) @mock.patch.dict( os.environ, {ROLLOUT_TEST_FEATURE_FLAG: 'invalid'}, clear=True ) def test_feature_disabled_with_invalid_rollout_percentage(self): self.assertFalse( utils.is_feature_enabled( TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG, ROLLOUT_TEST_FEATURE_FLAG, ) ) @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '101'}, clear=True) def test_feature_disabled_with_rollout_percentage_too_high(self): self.assertFalse( utils.is_feature_enabled( TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG, ROLLOUT_TEST_FEATURE_FLAG, ) ) @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '-1'}, clear=True) def test_feature_disabled_with_rollout_percentage_too_low(self): self.assertFalse( utils.is_feature_enabled( TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG, ROLLOUT_TEST_FEATURE_FLAG, ) ) @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '90'}, clear=True) def test_feature_enabled_with_rollout_percentage(self): self.assertTrue( utils.is_feature_enabled( TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG, ROLLOUT_TEST_FEATURE_FLAG, 90, ) ) @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '10'}, clear=True) def test_feature_disabled_with_rollout_percentage(self): self.assertFalse( utils.is_feature_enabled( TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG, ROLLOUT_TEST_FEATURE_FLAG, 10, ) ) Loading Loading
tools/edit_monitor/daemon_manager.py +1 −1 Original line number Diff line number Diff line Loading @@ -84,7 +84,7 @@ class DaemonManager: "edit_monitor", self.user_name, "ENABLE_EDIT_MONITOR", "EDIT_MONITOR_ROLLOUT_PERCENTAGE", 10, ): logging.warning("Edit monitor is disabled, exiting...") return Loading
tools/edit_monitor/utils.py +2 −20 Original line number Diff line number Diff line Loading @@ -21,7 +21,7 @@ def is_feature_enabled( feature_name: str, user_name: str, enable_flag: str = None, rollout_flag: str = None, rollout_percent: int = 100, ) -> bool: """Determine whether the given feature is enabled. Loading @@ -46,26 +46,8 @@ def is_feature_enabled( logging.info("feature: %s is enabled", feature_name) return True if not rollout_flag: return True hash_object = hashlib.sha256() hash_object.update((user_name + feature_name).encode("utf-8")) hash_number = int(hash_object.hexdigest(), 16) % 100 roll_out_percentage = os.environ.get(rollout_flag, "0") try: percentage = int(roll_out_percentage) if percentage < 0 or percentage > 100: logging.warning( "Rollout percentage: %s out of range, disable the feature.", roll_out_percentage, ) return False return hash_number < percentage except ValueError: logging.warning( "Invalid rollout percentage: %s, disable the feature.", roll_out_percentage, ) return False return hash_number < rollout_percent
tools/edit_monitor/utils_test.py +2 −39 Original line number Diff line number Diff line Loading @@ -46,60 +46,23 @@ class EnableFeatureTest(unittest.TestCase): ) ) @mock.patch.dict( os.environ, {ROLLOUT_TEST_FEATURE_FLAG: 'invalid'}, clear=True ) def test_feature_disabled_with_invalid_rollout_percentage(self): self.assertFalse( utils.is_feature_enabled( TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG, ROLLOUT_TEST_FEATURE_FLAG, ) ) @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '101'}, clear=True) def test_feature_disabled_with_rollout_percentage_too_high(self): self.assertFalse( utils.is_feature_enabled( TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG, ROLLOUT_TEST_FEATURE_FLAG, ) ) @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '-1'}, clear=True) def test_feature_disabled_with_rollout_percentage_too_low(self): self.assertFalse( utils.is_feature_enabled( TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG, ROLLOUT_TEST_FEATURE_FLAG, ) ) @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '90'}, clear=True) def test_feature_enabled_with_rollout_percentage(self): self.assertTrue( utils.is_feature_enabled( TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG, ROLLOUT_TEST_FEATURE_FLAG, 90, ) ) @mock.patch.dict(os.environ, {ROLLOUT_TEST_FEATURE_FLAG: '10'}, clear=True) def test_feature_disabled_with_rollout_percentage(self): self.assertFalse( utils.is_feature_enabled( TEST_FEATURE, TEST_USER, ENABLE_TEST_FEATURE_FLAG, ROLLOUT_TEST_FEATURE_FLAG, 10, ) ) Loading