Add CookiePatternFilter to push adoption filtering to SQL

The adoptUncategorisedPatterns method loaded all patterns for a
banner then filtered in Go. This adds a CookiePatternFilter
(match_type + cookie_category_id) and wires it into
LoadAllByCookieBannerID so the two targeted loads only fetch
the rows they need.

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-30 12:25:30 +04:00
parent 33abbfbb8f
commit edcb5ba9c7
4 changed files with 180 additions and 3 deletions

View File

@@ -331,6 +331,7 @@ func (cps *CookiePatterns) LoadAllByCookieBannerID(
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
filter *CookiePatternFilter,
) error {
q := `
SELECT
@@ -351,14 +352,16 @@ FROM
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
AND %s
ORDER BY
created_at ASC, id ASC;
`
q = fmt.Sprintf(q, scope.SQLFragment())
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {

View File

@@ -0,0 +1,83 @@
// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"github.com/jackc/pgx/v5"
"go.probo.inc/probo/pkg/gid"
)
type CookiePatternFilter struct {
matchType *CookiePatternMatchType
cookieCategoryID *gid.GID
}
func NewCookiePatternFilter(
matchType *CookiePatternMatchType,
cookieCategoryID *gid.GID,
) *CookiePatternFilter {
return &CookiePatternFilter{
matchType: matchType,
cookieCategoryID: cookieCategoryID,
}
}
func (f *CookiePatternFilter) SQLFragment() string {
if f == nil {
return "TRUE"
}
return `
(
CASE
WHEN @has_match_type_filter::boolean = false THEN TRUE
WHEN @has_match_type_filter::boolean = true THEN
match_type = @filter_match_type::cookie_pattern_match_type
ELSE TRUE
END
AND
CASE
WHEN @has_cookie_category_id_filter::boolean = false THEN TRUE
WHEN @has_cookie_category_id_filter::boolean = true THEN
cookie_category_id = @filter_cookie_category_id::text
ELSE TRUE
END
)`
}
func (f *CookiePatternFilter) SQLArguments() pgx.StrictNamedArgs {
if f == nil {
return pgx.StrictNamedArgs{}
}
args := pgx.StrictNamedArgs{
"has_match_type_filter": false,
"filter_match_type": nil,
"has_cookie_category_id_filter": false,
"filter_cookie_category_id": nil,
}
if f.matchType != nil {
args["has_match_type_filter"] = true
args["filter_match_type"] = string(*f.matchType)
}
if f.cookieCategoryID != nil {
args["has_cookie_category_id_filter"] = true
args["filter_cookie_category_id"] = *f.cookieCategoryID
}
return args
}