package app import ( "context" "encoding/json" "os" "strings" "time" ) // decisionCost totals the decision-model calls of each request: tokens the // model reported, or about len/4 of the request body where it reported none // (the local model), so jevUsd is what the same calls would cost on Jev. // The filter keeps the scan to the requests asked about (decisions is indexed by // request_id and created_at). func decisionCost(filter string) string { return `SELECT request_id, count(*) AS calls, sum(coalesce(nullif(json_extract(response_json,'$.usage.input_tokens'),0), length(request_json)/4)) AS inputTokens, coalesce(sum(json_extract(response_json,'$.usage.output_tokens')),0) AS outputTokens, sum(coalesce(json_extract(response_json,'$.usage.input_tokens'),0) = 0) AS estimatedCalls, group_concat(DISTINCT model) AS models FROM decisions WHERE error IS NULL AND ` + filter + ` GROUP BY request_id` } // jevUsd prices decision input tokens at JEV_PRICE_PER_MILLION; Jev does not bill output. func (s *Service) jevUsd(row map[string]any) { tokens, _ := row["inputTokens"].(int64) row["jevUsd"] = float64(tokens) * s.cfg.JevPricePerMillion / 1e6 } // attachCosts adds each row's decision calls, tokens and Jev price. func (s *Service) attachCosts(ctx context.Context, rows []map[string]any) error { if len(rows) == 0 { return nil } ids := make([]any, len(rows)) for i, row := range rows { ids[i] = row["requestId"] } costs, err := s.log.adminRows(ctx, decisionCost("request_id IN (?"+strings.Repeat(",?", len(ids)-1)+")"), ids...) if err != nil { return err } byID := map[any]map[string]any{} for _, c := range costs { byID[c["request_id"]] = c } for _, row := range rows { c := byID[row["requestId"]] if c == nil { c = map[string]any{"calls": int64(0), "inputTokens": int64(0)} } row["decisionCalls"], row["inputTokens"], row["decisionModels"] = c["calls"], c["inputTokens"], c["models"] s.jevUsd(row) } return nil } type adminRequestPage struct { Rows []map[string]any `json:"rows"` NextBefore int64 `json:"nextBefore,omitempty"` } type adminRequestTrace struct { Requests []map[string]any `json:"requests"` Completions []map[string]any `json:"completions"` Decisions []map[string]any `json:"decisions"` Evidence []map[string]any `json:"evidence"` } func (s *Service) adminOverview(ctx context.Context) (any, error) { since := time.Now().Add(-24 * time.Hour).Unix() requests, err := s.log.adminRows(ctx, `SELECT count(*) AS total, coalesce(sum(outcome != 'ok'),0) AS errors FROM requests WHERE created_at >= ? AND endpoint IN ('search','contents','answer')`, since) if err != nil { return nil, err } models, err := s.log.adminRows(ctx, `SELECT count(*) AS calls, coalesce(sum(error IS NOT NULL),0) AS errors, coalesce(avg(latency_ms),0) AS meanLatencyMs FROM ( SELECT error,latency_ms FROM completions WHERE created_at >= ? UNION ALL SELECT error,latency_ms FROM decisions WHERE created_at >= ?)`, since, since) if err != nil { return nil, err } tokens, err := s.log.adminRows(ctx, `SELECT coalesce(sum(prompt_tokens),0) AS input, coalesce(sum(completion_tokens),0) AS output, count(*) AS calls, coalesce(sum(prompt_tokens IS NOT NULL AND completion_tokens IS NOT NULL),0) AS reportedCalls FROM completions WHERE created_at >= ?`, since) if err != nil { return nil, err } decisions, err := s.log.adminRows(ctx, `SELECT count(DISTINCT r.request_id) AS requests, coalesce(sum(d.calls),0) AS calls, coalesce(sum(d.inputTokens),0) AS inputTokens, coalesce(sum(d.outputTokens),0) AS outputTokens, coalesce(sum(d.estimatedCalls),0) AS estimatedCalls FROM (SELECT DISTINCT request_id FROM requests WHERE created_at >= ? AND endpoint IN ('search','contents','answer')) r LEFT JOIN (`+decisionCost("created_at >= ?")+`) d USING (request_id)`, since, since) if err != nil { return nil, err } s.jevUsd(decisions[0]) var dbBytes int64 for _, suffix := range []string{"", "-wal", "-shm"} { if info, err := os.Stat(s.cfg.DBPath + suffix); err == nil { dbBytes += info.Size() } } return map[string]any{"since": since, "requests": requests[0], "models": models[0], "tokens": tokens[0], "decisions": decisions[0], "databaseBytes": dbBytes, "config": map[string]any{ "jevModel": s.cfg.JevModel, "summarizerModel": s.cfg.LLMModel, "jevPricePerMillion": s.cfg.JevPricePerMillion, "jevConfigured": s.cfg.JevKey != "", "summarizerConfigured": s.cfg.LLMKey != "", "searchConfigured": s.cfg.KagiSession != "" || s.cfg.KagiToken != "", "browserConfigured": s.cfg.BrowserPath != "", "passageCandidates": s.cfg.Passages, "evidenceCharacters": s.cfg.EvidenceChars, "requestTimeoutSeconds": s.cfg.RequestTimeout.Seconds(), }}, nil } func (s *Service) adminRequests(ctx context.Context, before int64, endpoint string, errorsOnly bool) (adminRequestPage, error) { query := `SELECT id,created_at AS createdAt,request_id AS requestId,endpoint,caller,detail,outcome FROM requests WHERE endpoint IN ('search','contents','answer')` args := []any{} if before > 0 { query += " AND id < ?" args = append(args, before) } if endpoint != "" { query += " AND endpoint = ?" args = append(args, endpoint) } if errorsOnly { query += " AND outcome != 'ok'" } rows, err := s.log.adminRows(ctx, query+" ORDER BY id DESC LIMIT 31", args...) if err != nil { return adminRequestPage{}, err } if err := s.attachCosts(ctx, rows); err != nil { return adminRequestPage{}, err } page := adminRequestPage{Rows: rows} if len(rows) > 30 { page.Rows = rows[:30] page.NextBefore = rows[29]["id"].(int64) } return page, err } func (l *Log) adminTrace(ctx context.Context, id string) (adminRequestTrace, error) { var trace adminRequestTrace for _, part := range []struct { target *[]map[string]any query string }{ {&trace.Requests, `SELECT id,created_at AS createdAt,endpoint,caller,detail,outcome FROM requests WHERE request_id = ? ORDER BY id LIMIT 100`}, {&trace.Completions, `SELECT id,created_at AS createdAt,task,model,request_json AS request,response_text AS response,error,prompt_tokens AS inputTokens,completion_tokens AS outputTokens,latency_ms AS latencyMs FROM completions WHERE request_id = ? ORDER BY id LIMIT 200`}, {&trace.Decisions, `SELECT id,created_at AS createdAt,task,model,request_json AS request,response_json AS response,error,latency_ms AS latencyMs FROM decisions WHERE request_id = ? ORDER BY id LIMIT 200`}, {&trace.Evidence, `SELECT id,source_id AS sourceId,policy_version AS policy,packet_json AS packet FROM evidence WHERE request_id = ? ORDER BY id LIMIT 100`}, } { rows, err := l.adminRows(ctx, part.query, id) if err != nil { return trace, err } *part.target = rows } return trace, nil } // SQL projections are the admin read model; this does not modify training rows. // Parse valid JSON cells for inspection, retaining malformed legacy cells as text. func (l *Log) adminRows(ctx context.Context, query string, args ...any) ([]map[string]any, error) { rows, err := l.db.QueryContext(ctx, query, args...) if err != nil { return nil, err } defer rows.Close() columns, err := rows.Columns() if err != nil { return nil, err } result := []map[string]any{} for rows.Next() { values, pointers := make([]any, len(columns)), make([]any, len(columns)) for i := range values { pointers[i] = &values[i] } if err := rows.Scan(pointers...); err != nil { return nil, err } row := map[string]any{} for i, name := range columns { value := values[i] if b, ok := value.([]byte); ok { value = string(b) } if text, ok := value.(string); ok && (name == "detail" || name == "request" || name == "packet") { var decoded any if json.Unmarshal([]byte(text), &decoded) == nil { value = decoded } } row[name] = value } result = append(result, row) } return result, rows.Err() }