nsmcp::CodexLogSummaryTool method execute (public)

 <instance of nsmcp::CodexLogSummaryTool[i]> execute context \
    arguments

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

Read ERROR/WARN counts and bounded examples from the Codex log DB.

Parameters:
context (required)
Authorized request context.
arguments (required)
Validated rooted JSON argument triples.
Returns:
Rooted JSON result with counts and bounded examples.

Testcases:
No testcase defined.
Source code:
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: