nsmcp::AccessAnalytics method correlateDuplicatePeers (public)

 <instance of nsmcp::AccessAnalytics[i]> correlateDuplicatePeers \
    candidates total omitted

Defined in /usr/local/ns/tcl/nsmcp/log-peer-context.tcl

Match bounded duplicate-parameter warnings to stored access requests. Called only for a peer-authorized summary. The identifier/time index bounds each lookup; ambiguous matches and missing retained addresses stay unknown.

Parameters:
candidates (required)
At most 512 warning identifiers with time and direct metadata.
total (required)
All physical duplicate warnings in the selected window.
omitted (required)
Warnings without a usable identifier or beyond the bound.
Returns:
Typed JSON fields with peer and attention rankings.

Testcases:
No testcase defined.
Source code:
if {[llength $candidates]>512 || $total != [llength $candidates]+$omitted} {
    error "invalid duplicate warning correlation bounds"
}
if {![info exists :settings] || ![nsf::object::exists ${:database}]
    || ![${:database} info has type ::nsmcp::UnifiedAccessDatabase]} {
    return [list {} object [list available boolean false reason string continuous-access-store-required  warningRecords number $total candidateRecords number [llength $candidates] omittedRecords number $omitted]]
}
set began [clock milliseconds]
set store [:accessRecordReaderOpen ${:settings}]
try {
    set db [$store session]
    $db execute BEGIN
    set peerFloor [expr {[clock seconds]-[dict get ${:settings} peerRetention]}]
    set peers {}
    set resolved {}
    set missing 0
    set ambiguous 0
    set unknownAddress 0
    set botClaim 0
    set noBotClaim 0
    set unknownAgent 0
    set authenticated 0
    foreach candidate $candidates {
        set requestId [dict get $candidate requestIdentifier]
        set at [dict get $candidate timestamp]
        if {![::nsmcp::logRecordParser validConnectionIdentifier $requestId]
            || ![string match "-conn:[ns_info server]:*" $requestId]
            || ![string is double -strict $at]} {
            incr missing
            continue
        }
        set lo [expr {$at-60}]
        set hi [expr {$at+1}]
        set rows [$db execute {SELECT lower(hex(r.peer)) AS peer FROM main.rows r
            WHERE r.identifier=:requestId AND r.t>=:lo AND r.t<=:hi AND r.kind=0
            AND EXISTS(SELECT 1 FROM main.files f WHERE f.fid=r.fid AND f.active=1)
            LIMIT 2}]
        if {![llength $rows]} {incr missing; continue}
        if {[llength $rows]>1} {incr ambiguous; continue}
        set digest [dict get [lindex $rows 0] peer]
        if {![regexp {^[0-9a-f]{64}$} $digest]} {incr unknownAddress; continue}
        if {![dict exists $resolved $digest]} {
            set lookup "SELECT address FROM main.peer_addresses WHERE k=X'$digest' AND seen>=:peerFloor"
            set address [$db scalar $lookup]
            if {![ns_ip valid $address]} {set address {}}
            dict set resolved $digest $address
        }
        set address [dict get $resolved $digest]
        if {$address eq {}} {incr unknownAddress; continue}
        set auth [dict get $candidate authenticationState]
        if {$auth ni {anonymous authenticated unknown}} {set auth unknown}
        set agent [dict get $candidate userAgent]
        if {$agent eq {}} {
            set claim unknown
            incr unknownAgent
        } elseif {[regexp -nocase {bot|spider|crawl|scrap} $agent]} {
            set claim bot
            incr botClaim
        } else {
            set claim no-bot
            incr noBotClaim
        }
        if {$auth eq "authenticated"} {incr authenticated}
        if {![dict exists $peers $address]} {
            dict set peers $address {count 0 authenticationStates {anonymous 0 authenticated 0 unknown 0} botClaimRecords 0 noBotClaimRecords 0 unknownAgentRecords 0}
        }
        set entry [dict get $peers $address]
        dict incr entry count
        set states [dict get $entry authenticationStates]
        dict incr states $auth
        dict set entry authenticationStates $states
        if {$claim eq "bot"} {
            dict incr entry botClaimRecords
        } elseif {$claim eq "no-bot"} {
            dict incr entry noBotClaimRecords
        } else {
            dict incr entry unknownAgentRecords
        }
        dict set peers $address $entry
    }
    $db execute COMMIT
    set ranked {}
    dict for {address entry} $peers {
        set priority [expr {[dict get $entry authenticationStates authenticated]>0 ? 2 :
            ([dict get $entry noBotClaimRecords]>0 ? 1 : 0)}]
        lappend ranked [list $priority [dict get $entry count] $address $entry]
    }
    set ranked [lsort -integer -decreasing -index 0 [lsort -integer -decreasing -index 1 [lsort -index 2 $ranked]]]
    set rows {}
    set index 0
    set rankedOmitted 0
    foreach item $ranked {
        lassign $item priority count address entry
        if {$index>=20} {incr rankedOmitted $count; continue}
        set states {}
        dict for {name n} [dict get $entry authenticationStates] {lappend states $name number $n}
        lappend rows $index object [list peer string $address count number $count  authenticationStates object $states botClaimRecords number [dict get $entry botClaimRecords]  noBotClaimRecords number [dict get $entry noBotClaimRecords]  unknownAgentRecords number [dict get $entry unknownAgentRecords]]
        incr index
    }
    return [list {} object [list available boolean true scope string selected-duplicate-warning-records  warningRecords number $total candidateRecords number [llength $candidates]  omittedRecords number $omitted matchedWithAddress number [expr {$total-$omitted-$missing-$ambiguous-$unknownAddress}]  missingAccessRecords number $missing ambiguousAccessRecords number $ambiguous  unknownAddressRecords number $unknownAddress authenticatedRecords number $authenticated  botClaimRecords number $botClaim noBotClaimRecords number $noBotClaim  unknownAgentRecords number $unknownAgent distinctPeers number [dict size $peers]  peerOutputLimit number 20 peerRanking string authenticated-then-no-bot-claim-then-count  peers array $rows rankedOmittedPeers number [expr {[llength $ranked]-$index}]  rankedOmittedRecords number $rankedOmitted queryMilliseconds number [expr {[clock milliseconds]-$began}]  limitations string {Matches physical duplicate warnings to committed active access records by exact connection identifier and request start within 60 seconds before to 1 second after the warning. At most 512 warnings are joined; omittedRecords includes missing identifiers and capacity overflow. Missing or ambiguous access matches and expired or absent peer addresses remain unknown. Peer retention can be shorter than request retention. Bot claim means a case-insensitive bot/spider/crawl/scrap token in the warning User-Agent; no-bot claim does not establish a human. Authentication comes from the warning diagnostic, not a User-Agent or pool. IP addresses do not identify people. Suppressed occurrences have no individual request identifiers and are excluded.}]]
} finally {
    $store close
}
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: