Always create a common tracker pattern during mapping

When neither pattern nor domain matching finds an existing common
tracker pattern, upsert one with no third party association and a
lower confidence (0.5) so every seen pattern is cataloged.

Also fix table name in LoadByOrganizationIDAndCommonThirdPartyID
(vendors -> third_parties).

Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-05-15 12:40:14 +04:00
parent 81707fe429
commit 3543d76c4f
2 changed files with 36 additions and 1 deletions

View File

@@ -85,6 +85,10 @@ func (h *trackerMappingHandler) Process(ctx context.Context, tp coredata.Tracker
commonPatternID, thirdPartyID = h.matchByDomain(ctx, tx, tp)
}
if commonPatternID == nil {
commonPatternID = h.createUnmatchedPattern(ctx, tx, tp)
}
if commonPatternID != nil || thirdPartyID != nil {
if err := tp.UpdateMapping(ctx, tx, commonPatternID, thirdPartyID); err != nil {
return fmt.Errorf("cannot update tracker pattern mapping: %w", err)
@@ -179,6 +183,37 @@ func (h *trackerMappingHandler) matchByDomain(
return &commonPattern.ID, thirdPartyID
}
func (h *trackerMappingHandler) createUnmatchedPattern(
ctx context.Context,
tx pg.Tx,
tp coredata.TrackerPattern,
) *gid.GID {
now := time.Now()
commonPattern := coredata.CommonTrackerPattern{
ID: gid.New(gid.NilTenant, coredata.CommonTrackerPatternEntityType),
TrackerType: tp.TrackerType,
Pattern: tp.Pattern,
MatchType: tp.MatchType,
Description: tp.Description,
MaxAgeSeconds: tp.MaxAgeSeconds,
Confidence: 0.5,
CreatedAt: now,
UpdatedAt: now,
}
actualID, _, err := commonPattern.Upsert(ctx, tx)
if err != nil {
h.logger.ErrorCtx(
ctx,
"cannot upsert common tracker pattern for unmatched pattern",
log.Error(err),
)
return nil
}
return &actualID
}
func (h *trackerMappingHandler) resolveThirdParty(
ctx context.Context,
conn pg.Querier,

View File

@@ -1352,7 +1352,7 @@ SELECT
created_at,
updated_at
FROM
vendors
third_parties
WHERE
%s
AND organization_id = @organization_id