From 99a0e053db27a39fca0d61c316ff9b5980c51142 Mon Sep 17 00:00:00 2001 From: Kevin Meinhardt Date: Tue, 26 Nov 2024 15:02:55 +0100 Subject: [PATCH] Simple is better than complex --- src/olympia/blocklist/mlbf.py | 70 ++++++++++------------------------- 1 file changed, 19 insertions(+), 51 deletions(-) diff --git a/src/olympia/blocklist/mlbf.py b/src/olympia/blocklist/mlbf.py index 8117ad94b6e..0fd09e6eeed 100644 --- a/src/olympia/blocklist/mlbf.py +++ b/src/olympia/blocklist/mlbf.py @@ -298,58 +298,26 @@ def generate_and_write_stash(self, previous_mlbf: 'MLBF' = None): If a block type needs a new filter, we do not include any items for that block type in the stash to prevent double counting items. """ - # Map block types to hard coded stash keys for compatibility - # with the expected keys in remote settings. - STASH_KEYS = { - BlockType.BLOCKED: 'blocked', - BlockType.SOFT_BLOCKED: 'softblocked', - } - UNBLOCKED_STASH_KEY = 'unblocked' - - # Base stash includes all of the expected keys from STASH_KEYS + unblocked - stash_json = {key: [] for key in [UNBLOCKED_STASH_KEY, *STASH_KEYS.values()]} - + stash_json = {'blocked': [], 'softblocked': [], 'unblocked': []} diffs = self.generate_diffs(previous_mlbf) - soft_block_enabled = waffle.switch_is_active('enable-soft-blocking') - set_blocked_any_type = set() - # Build the stash json from the diff of each block type - for block_type in BlockType: - skip_no_changes = not self.should_upload_stash(block_type, previous_mlbf) - skip_soft_block = ( - block_type == BlockType.SOFT_BLOCKED and not soft_block_enabled - ) - - # If there are no additions or removals, or if soft blocking is disabled. - if any([skip_no_changes, skip_soft_block]): - continue - - added, removed, _ = diffs[block_type] - # Keep track of items added to all block types - # regardless if we will end up writing to the stash or not. - # This is used to filter out items from the unblocked list. - set_blocked_any_type.update(added) - - # If we are not going to upload a filter for this block type, - # include the added items in the stash for this block type. - if not self.should_upload_filter(block_type, previous_mlbf): - stash_json[STASH_KEYS[block_type]] = added - # For now, add all items removed from the block type to the - # unblocked list. Items that are also added to another block list - # will be filtered out after we have collected all added items. - stash_json[UNBLOCKED_STASH_KEY].extend(removed) - - # Filter out any items that are removed from one block type - # and added to another. We do not support transitions from one - # block type to another so we should not include those items in the - # unblocked list. - stash_json[UNBLOCKED_STASH_KEY] = [ - item - for item in stash_json[UNBLOCKED_STASH_KEY] - if item not in set_blocked_any_type - ] - - # Write the stash json to the stash file, even if there are no changes. - # We should be able to inspect that the changes even if there are none. + blocked_added, blocked_removed, _ = diffs[BlockType.BLOCKED] + + if not self.should_upload_filter(BlockType.BLOCKED): + stash_json['blocked'] = blocked_added + stash_json['unblocked'] = blocked_removed + + if waffle.switch_is_active('enable-soft-blocking'): + soft_blocked_added, soft_blocked_removed, _ = diffs[BlockType.SOFT_BLOCKED] + if not self.should_upload_filter(BlockType.SOFT_BLOCKED): + stash_json['softblocked'] = soft_blocked_added + added_items = set(blocked_added + soft_blocked_added) + stash_json['unblocked'] = [ + unblocked + for unblocked in (blocked_removed + soft_blocked_removed) + if unblocked not in added_items + ] + + # write stash stash_path = self.stash_path with self.storage.open(stash_path, 'w') as json_file: log.info(f'Writing to file {stash_path}')