Class ::nsmcp::CodexLogSummaryTool (public)

 ::nx::Class ::nsmcp::CodexLogSummaryTool[i]

Defined in /usr/local/ns/tcl/nsmcp/codex-logs.tcl

Testcases:
No testcase defined.
Source code:
    :property {name codex_log_summary}
    :property {permission diagnostics.logs.read}
    :property pool:required
    :property redactor:required
    :property {description {Summarize Codex app-server ERROR and WARN diagnostic records from its configured read-only SQLite pool. Defaults to the last ten minutes. Supply both from and to as Unix seconds for an exact half-open window of at most one day. Returned examples are newest first and bounded.}}
    :property {inputSchema {
        {"type":"object","properties":{
            "from":{"type":"integer","minimum":0},
            "to":{"type":"integer","minimum":1},
            "lastMinutes":{"type":"integer","minimum":1,"maximum":1440},
            "limit":{"type":"integer","minimum":1,"maximum":50}
        },"additionalProperties":false}
    }}

    :method options {arguments} {
        if {[lindex $arguments 1] ne "object"} {
            return -code error -errorcode {NSMCP RPC -32602} "Expected arguments object"
        }
        set values {}
        foreach {key type value} [lindex $arguments 2] {
            if {$key ni {from to lastMinutes limit} || $type ne "number"
                || ![regexp {^(0|[1-9][0-9]*)$} $value] || [dict exists $values $key]} {
                return -code error -errorcode {NSMCP RPC -32602} "Invalid Codex log argument"
            }
            dict set values $key $value
        }
        if {[dict exists $values from] != [dict exists $values to]
            || ([dict exists $values from] && [dict exists $values lastMinutes])} {
            return -code error -errorcode {NSMCP RPC -32602} "Supply both from and to, or lastMinutes"
        }
        set limit [expr {[dict exists $values limit] ? [dict get $values limit] : 20}]
        if {$limit < 1 || $limit > 50} {
            return -code error -errorcode {NSMCP RPC -32602} "limit must be 1..50"
        }
        if {[dict exists $values from]} {
            set from [dict get $values from]
            set to [dict get $values to]
            if {$to <= $from || $to - $from > 86400} {
                return -code error -errorcode {NSMCP RPC -32602} "Window must be positive and at most one day"
            }
        } else {
            set minutes [expr {[dict exists $values lastMinutes] ? [dict get $values lastMinutes] : 10}]
            if {$minutes < 1 || $minutes > 1440} {
                return -code error -errorcode {NSMCP RPC -32602} "lastMinutes must be 1..1440"
            }
            set to [expr {[clock seconds] + 1}]
            set from [expr {$to - $minutes * 60}]
        }
        return [list $from $to $limit]
    }

    :public method validate {arguments} {
        #
        # Validate a bounded Codex diagnostic time window and result limit.
        #
        # @param arguments Rooted JSON argument triples.
        # @return No meaningful value; invalid input raises RPC -32602.
        #
        :options $arguments
    }

    :public method dependenciesPresent {} {
        return [expr {[:cget -pool] in [ns_db pools]}]
    }

    :public method execute {context arguments} {
        #
        # Read ERROR/WARN counts and bounded examples from the Codex log DB.
        #
        # @param context Authorized request context.
        # @param arguments Validated rooted JSON argument triples.
        # @return Rooted JSON result with counts and bounded examples.
        #
        lassign [:options $arguments] from to limit
        set samples {}
        set sampleCount 0
        ns_db withhandle -pool [:cget -pool] -timeout 1s db {
            if {[ns_db dbtype $db] ne "sqlite"} {
                error "Codex log pool must use nsdbsqlite"
            }
            set row [ns_db 1row $db "
                SELECT COUNT(*) AS total,
                       COALESCE(SUM(CASE WHEN level='ERROR' THEN 1 ELSE 0 END),0) AS errors,
                       COALESCE(SUM(CASE WHEN level='WARN' THEN 1 ELSE 0 END),0) AS warnings
                  FROM logs
                 WHERE ts >= $from AND ts < $to
                   AND level IN ('ERROR','WARN')"]
            set total [ns_set get $row total]
            set errors [ns_set get $row errors]
            set warnings [ns_set get $row warnings]
            ns_set free $row
            set rows [ns_db select $db "
                SELECT ts, level, target, feedback_log_body
                  FROM logs
                 WHERE ts >= $from AND ts < $to
                   AND level IN ('ERROR','WARN')
                 ORDER BY ts DESC, ts_nanos DESC
                 LIMIT $limit"]
            try {
                while {[ns_db getrow $db $rows]} {
                    set body [[:cget -redactor] message [ns_set get $rows feedback_log_body]]
                    set clipped [expr {[string length $body] > 1024}]
                    lappend samples $sampleCount object [list  timestamp number [ns_set get $rows ts]  level string [ns_set get $rows level]  target string [ns_set get $rows target]  message string [string range $body 0 1023] truncated boolean $clipped]
                    incr sampleCount
                }
            } finally {
                ns_set free $rows
            }
        }
        return [list {} object [list available boolean true source string codex-sqlite  from number $from to number $to total number $total  errors number $errors warnings number $warnings  returned number $sampleCount omitted number [expr {$total - $sampleCount}]  records array $samples]]
    }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: