Exclude excluded patterns from merge and adoption
Filter excluded and non-exact patterns in SQL when loading patterns for the pattern analysis worker. Both merge group building and uncategorised adoption only see non-excluded exact patterns, so excluded patterns are preserved as punch-out overrides. Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -85,12 +85,18 @@ func (h *patternAnalysisHandler) Process(ctx context.Context, banner coredata.Co
|
||||
func(ctx context.Context, tx pg.Tx) error {
|
||||
scope := coredata.NewScopeFromObjectID(banner.ID)
|
||||
|
||||
var patterns coredata.CookiePatterns
|
||||
if err := patterns.LoadAllByCookieBannerID(ctx, tx, scope, banner.ID, nil); err != nil {
|
||||
return fmt.Errorf("cannot load patterns: %w", err)
|
||||
var exactPatterns coredata.CookiePatterns
|
||||
if err := exactPatterns.LoadAllByCookieBannerID(
|
||||
ctx,
|
||||
tx,
|
||||
scope,
|
||||
banner.ID,
|
||||
coredata.NewCookiePatternFilter(new(coredata.CookiePatternMatchTypeExact), nil, new(false)),
|
||||
); err != nil {
|
||||
return fmt.Errorf("cannot load exact patterns: %w", err)
|
||||
}
|
||||
|
||||
mergeGroups := findMergeGroups(patterns, patternMergeThreshold)
|
||||
mergeGroups := findMergeGroups(exactPatterns, patternMergeThreshold)
|
||||
|
||||
merged := false
|
||||
for key, group := range mergeGroups {
|
||||
@@ -176,15 +182,8 @@ func findMergeGroups(
|
||||
patterns coredata.CookiePatterns,
|
||||
threshold int,
|
||||
) map[mergeGroupKey][]*coredata.CookiePattern {
|
||||
var exact []*coredata.CookiePattern
|
||||
for _, p := range patterns {
|
||||
if p.MatchType == coredata.CookiePatternMatchTypeExact {
|
||||
exact = append(exact, p)
|
||||
}
|
||||
}
|
||||
|
||||
prefixCounts := make(map[mergeGroupKey][]*coredata.CookiePattern)
|
||||
for _, p := range exact {
|
||||
for _, p := range patterns {
|
||||
for _, pfx := range separatorPrefixes(p.Pattern) {
|
||||
key := mergeGroupKey{categoryID: p.CookieCategoryID, prefix: pfx}
|
||||
prefixCounts[key] = append(prefixCounts[key], p)
|
||||
@@ -297,14 +296,13 @@ func (h *patternAnalysisHandler) adoptUncategorisedPatterns(
|
||||
return false, fmt.Errorf("cannot load uncategorised category: %w", err)
|
||||
}
|
||||
|
||||
prefixMatchType := coredata.CookiePatternMatchTypePrefix
|
||||
var prefixPatterns coredata.CookiePatterns
|
||||
if err := prefixPatterns.LoadAllByCookieBannerID(
|
||||
ctx,
|
||||
tx,
|
||||
scope,
|
||||
banner.ID,
|
||||
coredata.NewCookiePatternFilter(&prefixMatchType, nil, nil),
|
||||
coredata.NewCookiePatternFilter(new(coredata.CookiePatternMatchTypePrefix), nil, new(false)),
|
||||
); err != nil {
|
||||
return false, fmt.Errorf("cannot load prefix patterns: %w", err)
|
||||
}
|
||||
@@ -324,7 +322,7 @@ func (h *patternAnalysisHandler) adoptUncategorisedPatterns(
|
||||
tx,
|
||||
scope,
|
||||
banner.ID,
|
||||
coredata.NewCookiePatternFilter(&exactMatchType, &uncategorised.ID, nil),
|
||||
coredata.NewCookiePatternFilter(&exactMatchType, &uncategorised.ID, new(false)),
|
||||
); err != nil {
|
||||
return false, fmt.Errorf("cannot load uncategorised exact patterns: %w", err)
|
||||
}
|
||||
|
||||
@@ -218,29 +218,6 @@ func TestFindMergeGroups(t *testing.T) {
|
||||
},
|
||||
)
|
||||
|
||||
t.Run(
|
||||
"skips non-exact patterns",
|
||||
func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
patterns := coredata.CookiePatterns{
|
||||
makePattern("ph_phc_abc123"),
|
||||
makePattern("ph_phc_def456"),
|
||||
makePattern("ph_phc_ghi789"),
|
||||
{
|
||||
Pattern: "ph_phc_",
|
||||
MatchType: coredata.CookiePatternMatchTypePrefix,
|
||||
},
|
||||
}
|
||||
|
||||
groups := findMergeGroups(patterns, 3)
|
||||
require.Len(t, groups, 1)
|
||||
|
||||
group := groups[mergeGroupKey{categoryID: gid.Nil, prefix: "ph_phc_"}]
|
||||
assert.Len(t, group, 3)
|
||||
},
|
||||
)
|
||||
|
||||
t.Run(
|
||||
"leftover patterns form group under shorter prefix",
|
||||
func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user