Skip to content

Add country rank filter#247

Merged
ksy36 merged 8 commits into
MozillaSecurity:mainfrom
martinbalfanz:country-rank-filter
Jul 9, 2026
Merged

Add country rank filter#247
ksy36 merged 8 commits into
MozillaSecurity:mainfrom
martinbalfanz:country-rank-filter

Conversation

@martinbalfanz

Copy link
Copy Markdown
Contributor

No description provided.

@jgraham jgraham requested a review from ksy36 July 2, 2026 13:55
# Normalize incoming domains to match the domain_normalized field
domains = list({nd for d in domains if (nd := normalize_domain(d))})
# Only import for buckets that don't already have rank data.
already_ranked = set(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think if triage creates a new cluster bucket for a domain that already has rank rows on another bucket, already_ranked contains that domain, so this new bucket gets excluded and doesn't receive rank? (until there is a full run)

So something like this would make sure these buckets get a rank:

  buckets = list(                                                                                                                                                                                                                 
      Bucket.objects.filter(                                                                                                                                                                                                      
          domain_normalized__in=domains,                                                                                                                                                                                          
          country_ranks__isnull=True,                                                                                                                                                                                             
      ).only("id", "domain_normalized")                                                                                                                                                                                           
  )   

Though if a bucket already has some rank rows (e.g. us_rank) but is missing a new country (e.g. poland_rank), it will still be excluded on partial runs with this. But it might be fine? Not sure how often that will happen. We can run the full command manually/ or run it once a month on a schedule

if not domains:
LOG.info("No buckets with a normalized domain — nothing to import")
return

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The two identical domains = [b.domain_normalized for b in buckets] lines can be simplified with a check for buckets:

if not buckets:                                                                                                                                                                                                                 
      LOG.info("No buckets with a normalized domain — nothing to import")                                                                                                                                                         
      return

domains = [b.domain_normalized for b in buckets]

name='updated_at',
field=models.DateTimeField(default=django.utils.timezone.now),
),
]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would it be possible to squash the 2 migrations into one? Since we haven't deployed this yet.

.then((columns) => registerCountryRankFilters(columns))
.catch((e) => console.debug("Failed to load country rank columns:", e))
.finally(() => app.mount("#app"));
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe instead of fetching it main.js, this can be in the bucket_filter_config.js

let countryRankReady = null;

export function ensureCountryRankFilters() {
  if (!countryRankReady) {
    countryRankReady = listCountryRankColumns()
      .then((columns) => registerCountryRankFilters(columns))
      .catch((e) => console.debug("Failed to load country rank columns:", e));
  }
  return countryRankReady;
}

And in List.vue it can trigger the call before doing the fetch (so filter and going directly to links like #q=poland_rank:<=1000 work):

fetch: _throttle(
      async function () {
        await ensureCountryRankFilters();
        if (!this.advancedMode) {
....

…d lint

- Fix a bug where a bucket sharing a domain with an already-ranked
  bucket (e.g. a newly triaged cluster) was excluded from partial
  imports even without rank rows of its own.
- Squash the two BucketCountryRank migrations into one.
- Fix ruff-format and eslint/prettier violations flagged by CI.
@martinbalfanz

Copy link
Copy Markdown
Contributor Author

@ksy36 thanks for the review! I think I addressed all the feedback. Let me know if there's anything else I should check :)

@ksy36 ksy36 merged commit 4c85060 into MozillaSecurity:main Jul 9, 2026
4 checks passed
@ksy36 ksy36 mentioned this pull request Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants