nsmcp::AccessAnalytics method correlatePeerHistory (public)

 <instance of nsmcp::AccessAnalytics[i]> correlatePeerHistory \
    candidates redactor

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

Read at most five peers and 128 recent requests per peer from partitions with a peer/time index. No unindexed fallback or log import is attempted.

Parameters:
candidates (required)
redactor (required)

Testcases:
No testcase defined.
Source code:
if {[llength $candidates]>5} {error "too many peer history candidates"}
if {![info exists :settings] || ![nsf::object::exists ${:database}]} {
    return [dict create available false reason continuous-access-store-required histories {}]
}
if {![dict exists ${:settings} partitionInterval] || [dict get ${:settings} partitionInterval]<=0} {
    return [dict create available false reason partitioned-peer-index-required histories {}]
}
set store [:accessRecordReaderOpen ${:settings}]
try {
    set db [$store session]
    set histories {}
    set index 0
    foreach candidate $candidates {
        set samples [dict get $candidate samples]
        set at [dict get [lindex $samples 0] timestamp]
        set peerKey [dict get [lindex $samples 0] peerKey]
        if {![regexp {^[0-9a-f]{64}$} $peerKey]} {error "invalid peer history key"}
        set lo [expr {max($at-43200,[clock seconds]-[dict get ${:settings} retention])}]
        set hi [expr {min($at+43200,[clock seconds]+1)}]
        set rows {}
        set indexed true
        foreach part [$db execute {SELECT name FROM main.partitions
            WHERE stop>:lo AND start<:hi ORDER BY start}] {
            set name [dict get $part name]
            if {![regexp {^p_[0-9]+$} $name]} {error "record-store-invalid-partition"}
            if {![$db scalar "SELECT EXISTS(SELECT 1 FROM main.sqlite_schema
                WHERE type='index' AND name='${name}_peer_time')"]} {
                set indexed false
                break
            }
            foreach row [$db execute "SELECT r.t,r.status,substr(r.path,1,512) AS path,
                substr(r.method,1,32) AS method,substr(r.label,1,1024) AS userAgent
                FROM main.$name r WHERE r.kind=0 AND r.peer=X'$peerKey'
                AND r.t>=:lo AND r.t<:hi
                AND EXISTS(SELECT 1 FROM main.files f WHERE f.fid=r.fid AND f.active=1)
                ORDER BY r.t DESC LIMIT 129"] {
                lappend rows [list [dict get $row t] $row]
            }
        }
        set records {}
        if {$indexed} {
            set rows [lrange [lsort -real -decreasing -index 0 $rows] 0 128]
            set clipped [expr {[llength $rows]>128}]
            foreach pair [lrange $rows 0 127] {
                set row [lindex $pair 1]
                lappend records [dict create status [dict get $row status]  path [$redactor message [lindex [split [dict get $row path] ?] 0]]  method [dict get $row method]  userAgent [$redactor message [dict get $row userAgent]]]
            }
        } else {
            set clipped false
        }
        lappend histories [dict create available $indexed  reason [expr {$indexed ? {} : "peer-index-unavailable"}]  from $lo to $hi  returnedRecords [llength $records] recordsTruncated $clipped  records $records]
        incr index
    }
    return [dict create available true histories $histories scope indexed-same-peer-history]
} finally {$store close}
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: