Add cookie pattern analysis worker for prefix auto-detection
Background worker polls cookie_banners with pattern_analysis_requested_at set, groups EXACT patterns sharing a common prefix, and merges groups of 3+ into a PREFIX pattern. Detection sets the flag when new EXACT patterns are created. The worker relinks cookies, removes orphaned patterns, and updates the draft version via ensureDraftVersionForBanner. Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -30,19 +30,25 @@ import (
|
||||
|
||||
type (
|
||||
CookieBanner struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
Name string `db:"name"`
|
||||
Origin string `db:"origin"`
|
||||
State CookieBannerState `db:"state"`
|
||||
PrivacyPolicyURL *string `db:"privacy_policy_url"`
|
||||
CookiePolicyURL string `db:"cookie_policy_url"`
|
||||
ConsentExpiryDays int `db:"consent_expiry_days"`
|
||||
ConsentMode CookieConsentMode `db:"consent_mode"`
|
||||
ShowBranding bool `db:"show_branding"`
|
||||
DefaultLanguage string `db:"default_language"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
Name string `db:"name"`
|
||||
Origin string `db:"origin"`
|
||||
State CookieBannerState `db:"state"`
|
||||
PrivacyPolicyURL *string `db:"privacy_policy_url"`
|
||||
CookiePolicyURL string `db:"cookie_policy_url"`
|
||||
ConsentExpiryDays int `db:"consent_expiry_days"`
|
||||
ConsentMode CookieConsentMode `db:"consent_mode"`
|
||||
ShowBranding bool `db:"show_branding"`
|
||||
DefaultLanguage string `db:"default_language"`
|
||||
PatternAnalysisRequestedAt *time.Time `db:"pattern_analysis_requested_at"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
CookieBannerPatternAnalysisTask struct {
|
||||
BannerID gid.GID
|
||||
TenantID gid.TenantID
|
||||
}
|
||||
|
||||
CookieBanners []*CookieBanner
|
||||
@@ -91,6 +97,7 @@ SELECT
|
||||
consent_mode,
|
||||
show_branding,
|
||||
default_language,
|
||||
pattern_analysis_requested_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -143,6 +150,7 @@ SELECT
|
||||
consent_mode,
|
||||
show_branding,
|
||||
default_language,
|
||||
pattern_analysis_requested_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -196,6 +204,7 @@ SELECT
|
||||
consent_mode,
|
||||
show_branding,
|
||||
default_language,
|
||||
pattern_analysis_requested_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -253,6 +262,7 @@ SELECT
|
||||
consent_mode,
|
||||
show_branding,
|
||||
default_language,
|
||||
pattern_analysis_requested_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -303,6 +313,7 @@ SELECT
|
||||
consent_mode,
|
||||
show_branding,
|
||||
default_language,
|
||||
pattern_analysis_requested_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -389,6 +400,7 @@ INSERT INTO cookie_banners (
|
||||
consent_mode,
|
||||
show_branding,
|
||||
default_language,
|
||||
pattern_analysis_requested_at,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
@@ -404,26 +416,28 @@ INSERT INTO cookie_banners (
|
||||
@consent_mode,
|
||||
@show_branding,
|
||||
@default_language,
|
||||
@pattern_analysis_requested_at,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": b.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": b.OrganizationID,
|
||||
"name": b.Name,
|
||||
"origin": b.Origin,
|
||||
"state": b.State,
|
||||
"privacy_policy_url": b.PrivacyPolicyURL,
|
||||
"cookie_policy_url": b.CookiePolicyURL,
|
||||
"consent_expiry_days": b.ConsentExpiryDays,
|
||||
"consent_mode": b.ConsentMode,
|
||||
"show_branding": b.ShowBranding,
|
||||
"default_language": b.DefaultLanguage,
|
||||
"created_at": b.CreatedAt,
|
||||
"updated_at": b.UpdatedAt,
|
||||
"id": b.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": b.OrganizationID,
|
||||
"name": b.Name,
|
||||
"origin": b.Origin,
|
||||
"state": b.State,
|
||||
"privacy_policy_url": b.PrivacyPolicyURL,
|
||||
"cookie_policy_url": b.CookiePolicyURL,
|
||||
"consent_expiry_days": b.ConsentExpiryDays,
|
||||
"consent_mode": b.ConsentMode,
|
||||
"show_branding": b.ShowBranding,
|
||||
"default_language": b.DefaultLanguage,
|
||||
"pattern_analysis_requested_at": b.PatternAnalysisRequestedAt,
|
||||
"created_at": b.CreatedAt,
|
||||
"updated_at": b.UpdatedAt,
|
||||
}
|
||||
|
||||
_, err := tx.Exec(ctx, q, args)
|
||||
@@ -557,3 +571,77 @@ WHERE
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *CookieBannerPatternAnalysisTask) ClaimNextForUpdateSkipLocked(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
tenant_id
|
||||
FROM
|
||||
cookie_banners
|
||||
WHERE
|
||||
pattern_analysis_requested_at IS NOT NULL
|
||||
ORDER BY
|
||||
pattern_analysis_requested_at ASC
|
||||
FOR UPDATE SKIP LOCKED
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
var tenantIDStr string
|
||||
if err := tx.QueryRow(ctx, q).Scan(&t.BannerID, &tenantIDStr); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
return fmt.Errorf("cannot claim banner for pattern analysis: %w", err)
|
||||
}
|
||||
|
||||
if err := t.TenantID.UnmarshalText([]byte(tenantIDStr)); err != nil {
|
||||
return fmt.Errorf("cannot parse tenant ID: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *CookieBannerPatternAnalysisTask) ClearPatternAnalysisFlag(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE cookie_banners
|
||||
SET pattern_analysis_requested_at = NULL
|
||||
WHERE id = @id
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{"id": t.BannerID}
|
||||
|
||||
_, err := tx.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot clear pattern analysis flag: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *CookieBanner) SetPatternAnalysisRequested(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE cookie_banners
|
||||
SET pattern_analysis_requested_at = NOW()
|
||||
WHERE id = @id
|
||||
AND pattern_analysis_requested_at IS NULL
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{"id": b.ID}
|
||||
|
||||
_, err := tx.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot set pattern analysis requested: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -381,6 +381,68 @@ INSERT INTO cookie_patterns (
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cp *CookiePattern) InsertIfNotExists(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
scope Scoper,
|
||||
) (bool, error) {
|
||||
q := `
|
||||
INSERT INTO cookie_patterns (
|
||||
id,
|
||||
tenant_id,
|
||||
organization_id,
|
||||
cookie_banner_id,
|
||||
cookie_category_id,
|
||||
pattern,
|
||||
match_type,
|
||||
display_name,
|
||||
duration,
|
||||
description,
|
||||
source,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
@id,
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@cookie_banner_id,
|
||||
@cookie_category_id,
|
||||
@pattern,
|
||||
@match_type,
|
||||
@display_name,
|
||||
@duration,
|
||||
@description,
|
||||
@source,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
ON CONFLICT (cookie_banner_id, pattern) DO NOTHING
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": cp.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": cp.OrganizationID,
|
||||
"cookie_banner_id": cp.CookieBannerID,
|
||||
"cookie_category_id": cp.CookieCategoryID,
|
||||
"pattern": cp.Pattern,
|
||||
"match_type": cp.MatchType,
|
||||
"display_name": cp.DisplayName,
|
||||
"duration": cp.Duration,
|
||||
"description": cp.Description,
|
||||
"source": cp.Source,
|
||||
"created_at": cp.CreatedAt,
|
||||
"updated_at": cp.UpdatedAt,
|
||||
}
|
||||
|
||||
result, err := tx.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("cannot insert cookie pattern: %w", err)
|
||||
}
|
||||
|
||||
return result.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
func (cp *CookiePattern) Update(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
|
||||
16
pkg/coredata/migrations/20260429T102803Z.sql
Normal file
16
pkg/coredata/migrations/20260429T102803Z.sql
Normal file
@@ -0,0 +1,16 @@
|
||||
-- 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.
|
||||
|
||||
ALTER TABLE cookie_banners
|
||||
ADD COLUMN pattern_analysis_requested_at TIMESTAMP WITH TIME ZONE;
|
||||
Reference in New Issue
Block a user