From 8d60e9c74550d82fabe1fea4b6e210e47b84d2af Mon Sep 17 00:00:00 2001 From: SW Date: Thu, 10 Sep 2026 09:10:14 +0200 Subject: [PATCH] feat: incremental history cache, errors tab, Forgejo migration - Cache current log file incrementally by line count/inode to avoid re-processing the whole file every refresh. - Collect 4xx/5xx errors during aggregation and add [4] ERRORS tab. - Update README URLs and brew tap to point to git.janvete.com. - Replace GitHub Actions workflow with Forgejo Actions release workflow. --- {.github => .forgejo}/workflows/release.yml | 66 +++++++++---- README.md | 18 +++- go.mod | 11 ++- go.sum | 8 +- internal/parser/caddy.go | 69 ++++++++++++- internal/sshclient/client.go | 104 +++++++++++++++----- internal/tui/app.go | 100 +++++++++++++++++-- 7 files changed, 313 insertions(+), 63 deletions(-) rename {.github => .forgejo}/workflows/release.yml (51%) diff --git a/.github/workflows/release.yml b/.forgejo/workflows/release.yml similarity index 51% rename from .github/workflows/release.yml rename to .forgejo/workflows/release.yml index 8278657..6226053 100644 --- a/.github/workflows/release.yml +++ b/.forgejo/workflows/release.yml @@ -5,21 +5,23 @@ on: tags: - 'v[0-9]*' -permissions: - contents: write - jobs: release: runs-on: ubuntu-latest + container: + image: golang:1.27 steps: - - uses: actions/checkout@v4 - - - uses: actions/setup-go@v5 - with: - go-version-file: go.mod + - name: Checkout source + env: + FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }} + run: | + git clone "https://token:${FORGEJO_TOKEN}@git.janvete.com/seth/Caddyview.git" src + cd src + git checkout "${{ github.ref_name }}" - name: Build binaries and archives + working-directory: src run: | VERSION=${{ github.ref_name }} mkdir -p dist @@ -37,16 +39,42 @@ jobs: echo "=== SHA256 checksums ===" sha256sum dist/*.tar.gz - - name: Create GitHub Release + - name: Create Forgejo Release + working-directory: src env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }} + FORGEJO_URL: https://git.janvete.com run: | VERSION=${{ github.ref_name }} - gh release create "$VERSION" dist/*.tar.gz \ - --title "$VERSION" \ - --generate-notes + API="${FORGEJO_URL}/api/v1/repos/seth/Caddyview" + + RELEASE_JSON=$(curl -sS -H "Authorization: token ${FORGEJO_TOKEN}" \ + -H "Content-Type: application/json" \ + -X POST \ + -d "{\"tag_name\":\"${VERSION}\",\"name\":\"${VERSION}\",\"draft\":false,\"prerelease\":false}" \ + "${API}/releases") + + echo "Release response: ${RELEASE_JSON}" + RELEASE_ID=$(echo "${RELEASE_JSON}" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2) + + if [ -z "${RELEASE_ID}" ]; then + echo "Failed to create release" + exit 1 + fi + + echo "release_id=${RELEASE_ID}" >> "$GITHUB_ENV" + + for ARCHIVE in dist/*.tar.gz; do + NAME=$(basename "${ARCHIVE}") + curl -sS -H "Authorization: token ${FORGEJO_TOKEN}" \ + -H "Content-Type: application/gzip" \ + --data-binary "@${ARCHIVE}" \ + "${API}/releases/${RELEASE_ID}/assets?name=${NAME}" + echo "Uploaded ${NAME}" + done - name: Update Homebrew formula + working-directory: src env: TAP_TOKEN: ${{ secrets.TAP_TOKEN }} run: | @@ -61,23 +89,23 @@ jobs: cat > tap/Formula/caddyview.rb << FORMULA class Caddyview < Formula desc "TUI SSH monitor for Caddy web server logs" - homepage "https://github.com/janvete/caddyview" + homepage "https://git.janvete.com/seth/Caddyview" version "${VER}" license "MIT" on_macos do if Hardware::CPU.arm? - url "https://github.com/janvete/caddyview/releases/download/${VERSION}/caddyview-${VERSION}-darwin-arm64.tar.gz" + url "https://git.janvete.com/seth/Caddyview/releases/download/${VERSION}/caddyview-${VERSION}-darwin-arm64.tar.gz" sha256 "${SHA_ARM64}" else - url "https://github.com/janvete/caddyview/releases/download/${VERSION}/caddyview-${VERSION}-darwin-amd64.tar.gz" + url "https://git.janvete.com/seth/Caddyview/releases/download/${VERSION}/caddyview-${VERSION}-darwin-amd64.tar.gz" sha256 "${SHA_AMD64}" end end on_linux do if Hardware::CPU.intel? - url "https://github.com/janvete/caddyview/releases/download/${VERSION}/caddyview-${VERSION}-linux-amd64.tar.gz" + url "https://git.janvete.com/seth/Caddyview/releases/download/${VERSION}/caddyview-${VERSION}-linux-amd64.tar.gz" sha256 "${SHA_LINUX}" end end @@ -93,8 +121,8 @@ jobs: FORMULA cd tap - git config user.email "github-actions[bot]@users.noreply.github.com" - git config user.name "github-actions[bot]" + git config user.email "forgejo-actions[bot]@users.noreply.github.com" + git config user.name "forgejo-actions[bot]" git add Formula/caddyview.rb git commit -m "chore: bump caddyview to ${VERSION}" git push diff --git a/README.md b/README.md index 90fd827..d4b4ba2 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,17 @@ Remote Caddy log viewer over SSH. ### macOS (Homebrew) ```bash -brew tap janvete/caddyview +brew tap janvete/tools brew install caddyview ``` ### Linux +Download the latest release from the [releases page](https://git.janvete.com/seth/Caddyview/releases) and extract it: + ```bash -curl -L https://github.com/janvete/caddyview/releases/latest/download/caddyview-v0.6.0-linux-amd64.tar.gz | tar xz +VERSION=v0.6.0 +curl -L "https://git.janvete.com/seth/Caddyview/releases/download/${VERSION}/caddyview-${VERSION}-linux-amd64.tar.gz" | tar xz sudo mv caddyview /usr/local/bin/ ``` @@ -25,6 +28,8 @@ caddyview ssh root@192.168.x.x caddyview ssh -p 2222 admin@server.com ``` +Press `[1]` for live traffic, `[2]` for last 24 hours, `[3]` for last 7 days, and `[4]` to view recent 4xx/5xx errors with URL and origin. + ## Required Caddyfile Changes ### 1. Add a log block to your Caddyfile @@ -68,3 +73,12 @@ chown caddy:caddy /var/log/caddy/access.log ```bash systemctl reload caddy ``` + +## Development + +The source repository is hosted at `https://git.janvete.com/seth/Caddyview`. +To clone via SSH use the dedicated SSH endpoint: + +```bash +git clone ssh://git@git-ssh.janvete.com:554/seth/Caddyview.git +``` diff --git a/go.mod b/go.mod index bfb2e16..3dc6677 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,16 @@ module github.com/janvete/caddyview go 1.26.4 +require ( + github.com/charmbracelet/bubbletea v1.3.10 + github.com/charmbracelet/lipgloss v1.1.0 + github.com/spf13/cobra v1.10.2 + golang.org/x/crypto v0.53.0 +) + require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect - github.com/charmbracelet/bubbletea v1.3.10 // indirect github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect - github.com/charmbracelet/lipgloss v1.1.0 // indirect github.com/charmbracelet/x/ansi v0.10.1 // indirect github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect github.com/charmbracelet/x/term v0.2.1 // indirect @@ -20,10 +25,8 @@ require ( github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/termenv v0.16.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect - github.com/spf13/cobra v1.10.2 // indirect github.com/spf13/pflag v1.0.9 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect - golang.org/x/crypto v0.53.0 // indirect golang.org/x/sys v0.46.0 // indirect golang.org/x/text v0.38.0 // indirect ) diff --git a/go.sum b/go.sum index 9e5244d..266f4ff 100644 --- a/go.sum +++ b/go.sum @@ -44,14 +44,14 @@ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJu go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= golang.org/x/crypto v0.53.0 h1:QZ4Muo8THX6CizN2vPPd5fBGHyogrdK9fG4wLPFUsto= golang.org/x/crypto v0.53.0/go.mod h1:DNLU434OwVakk9PzuwV8w62mAJpRJL3vsgcfp4Qnsio= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k= -golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= -golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc= +golang.org/x/term v0.44.0/go.mod h1:7ze4MdzUzLXpSAoFP1H0bOI9aXDqveSvatT5vKcFh2Y= golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE= golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/parser/caddy.go b/internal/parser/caddy.go index 0f2fb73..c7879d5 100644 --- a/internal/parser/caddy.go +++ b/internal/parser/caddy.go @@ -98,6 +98,18 @@ type Bucket struct { Bytes int64 } +// -- ErrorEntry (4xx/5xx detail) ---------------------------------------------- + +// ErrorEntry holds one failed request for the errors panel. +type ErrorEntry struct { + Time time.Time + Status int + Method string + URI string + Host string + RemoteIP string +} + // -- AggregatedHistory (server-side aggregation result) ----------------------- // AggregatedBucket is one time bucket returned by server-side awk aggregation. @@ -115,12 +127,13 @@ type AggregatedHistory struct { TotalReqs int64 TotalBytes int64 DurSumSecs float64 + Errors []ErrorEntry } -// FilterSince drops buckets older than the window and recomputes totals from -// the remaining buckets, so the chart and the totals always agree. TopHosts and -// TopIPs come from whole rotated files and may slightly overcount at the oldest -// edge of the window — acceptable for a monitoring view. +// FilterSince drops buckets and errors older than the window and recomputes +// totals from the remaining buckets, so the chart and totals always agree. +// TopHosts and TopIPs come from whole rotated files and may slightly overcount +// at the oldest edge of the window — acceptable for a monitoring view. func (h *AggregatedHistory) FilterSince(since int64) { avgDur := 0.0 if h.TotalReqs > 0 { @@ -139,6 +152,14 @@ func (h *AggregatedHistory) FilterSince(since int64) { h.TotalReqs = reqs h.TotalBytes = bytes h.DurSumSecs = avgDur * float64(reqs) + + keptErrs := h.Errors[:0] + for _, e := range h.Errors { + if e.Time.Unix() >= since { + keptErrs = append(keptErrs, e) + } + } + h.Errors = keptErrs } // ToStats converts aggregated history to a Stats object for use in the TUI panels. @@ -186,6 +207,22 @@ func (h *AggregatedHistory) ToBuckets(size time.Duration) []Bucket { return out } +// RecentErrors returns the last n errors sorted by time (newest first). +func (h *AggregatedHistory) RecentErrors(n int) []ErrorEntry { + if n <= 0 { + return nil + } + sorted := make([]ErrorEntry, len(h.Errors)) + copy(sorted, h.Errors) + sort.Slice(sorted, func(i, j int) bool { + return sorted[i].Time.After(sorted[j].Time) + }) + if len(sorted) > n { + return sorted[:n] + } + return sorted +} + // ParseAggregatedOutput parses the concatenated output of per-file server-side // awk aggregations. Sections may repeat (one set per log file) and are merged: // bucket counts sum by timestamp, host/IP counts sum by key, totals sum. @@ -198,6 +235,8 @@ func (h *AggregatedHistory) ToBuckets(size time.Duration) []Bucket { // // IPS // +// ERRORS +// \t\t\t\t\t // TOTALS // func ParseAggregatedOutput(raw string) (*AggregatedHistory, error) { @@ -215,7 +254,7 @@ func ParseAggregatedOutput(raw string) (*AggregatedHistory, error) { continue } switch line { - case "BUCKETS", "HOSTS", "IPS", "TOTALS": + case "BUCKETS", "HOSTS", "IPS", "ERRORS", "TOTALS": section = line sawSection = true continue @@ -259,6 +298,26 @@ func ParseAggregatedOutput(raw string) (*AggregatedHistory, error) { count, _ := strconv.ParseInt(fields[0], 10, 64) h.TopIPs[fields[1]] += count + case "ERRORS": + // Tab-separated: ts status method uri host remote_ip + parts := strings.Split(line, "\t") + if len(parts) < 6 { + continue + } + ts, err := strconv.ParseInt(parts[0], 10, 64) + if err != nil { + continue + } + status, _ := strconv.Atoi(parts[1]) + h.Errors = append(h.Errors, ErrorEntry{ + Time: time.Unix(ts, 0), + Status: status, + Method: parts[2], + URI: parts[3], + Host: parts[4], + RemoteIP: parts[5], + }) + case "TOTALS": if len(fields) < 2 { continue diff --git a/internal/sshclient/client.go b/internal/sshclient/client.go index 1d17a03..db276f1 100644 --- a/internal/sshclient/client.go +++ b/internal/sshclient/client.go @@ -73,7 +73,6 @@ func (c *Client) StreamLines(path string, lines chan<- string, done <-chan struc stdout, err := session.StdoutPipe() if err != nil { - session.Close() return err } @@ -231,10 +230,11 @@ func parseCPUStat(raw string) float64 { return (totalDiff - idleDiff) / totalDiff * 100 } -// historyAwkProg aggregates Caddy JSON log lines into hourly summary buckets. -// It runs entirely on the server and uses only POSIX awk constructs (works -// with gawk, mawk and busybox awk). IPs are capped to the top 50 per file so -// the output stays small even on servers with millions of unique clients. +// historyAwkProg aggregates Caddy JSON log lines into hourly summary buckets +// and collects the last 100 4xx/5xx errors per file. It runs entirely on the +// server and uses only POSIX awk constructs (works with gawk, mawk and busybox +// awk). IPs are capped to the top 50 per file so the output stays small even on +// servers with millions of unique clients. const historyAwkProg = ` BEGIN { OFS=" " } { @@ -244,6 +244,7 @@ BEGIN { OFS=" " } ts = substr($0,p+5,20)+0 if (ts < cutoff) next b = int(ts/bucket)*bucket + p = index($0,"\"status\":") st = (p>0) ? substr($0,p+9,5)+0 : 0 p = index($0,"\"size\":") @@ -254,6 +255,11 @@ BEGIN { OFS=" " } if (p>0) { rest=substr($0,p+8); q=index(rest,"\""); ho=(q>0)?substr(rest,1,q-1):"?" } else ho="?" p = index($0,"\"remote_ip\":\"") if (p>0) { rest=substr($0,p+13); q=index(rest,"\""); ip=(q>0)?substr(rest,1,q-1):"?" } else ip="?" + p = index($0,"\"uri\":\"") + if (p>0) { rest=substr($0,p+7); q=index(rest,"\""); uri=(q>0)?substr(rest,1,q-1):"?" } else uri="?" + p = index($0,"\"method\":\"") + if (p>0) { rest=substr($0,p+10); q=index(rest,"\""); meth=(q>0)?substr(rest,1,q-1):"?" } else meth="?" + bc[b]++; bb[b]+=sz if (st>=500) bs5[b]++ else if (st>=400) bs4[b]++ @@ -261,6 +267,14 @@ BEGIN { OFS=" " } else if (st>=200) bs2[b]++ hc[ho]++; ic[ip]++ tot++; totb+=sz; totd+=du + + if (st >= 400) { + err[++errN] = ts "\t" st "\t" meth "\t" uri "\t" ho "\t" ip + if (errN > 100) { + for (i=2; i<=errN; i++) err[i-1] = err[i] + errN-- + } + } } END { print "BUCKETS" @@ -277,6 +291,8 @@ END { delete ic[mi] k--; n-- } + print "ERRORS" + for (i=1; i<=errN; i++) print err[i] print "TOTALS" print tot+0, totb+0, totd+0 }` @@ -287,11 +303,13 @@ END { // // Rotated log files are immutable, so each one is aggregated exactly once and // the result is cached on the server (/tmp/caddyview_*.cache); after a -// rotation only the single new file is scanned. The current log file is always -// scanned fresh, so the view never goes stale. Buckets are always hourly; the -// caller re-buckets client-side, which lets all tabs share the same caches. +// rotation only the single new file is scanned. The current log file is cached +// incrementally: only newly appended lines are processed on refresh, so even a +// 100 MB active log refreshes in seconds. Buckets are always hourly; the caller +// re-buckets client-side, which lets all tabs share the same caches. // -// since: unix cutoff for the current (still growing) log file +// since: unix cutoff used client-side after aggregation (ignored server-side so +// the cache can be reused across different windows). // maxAgeDays: only look at files modified within this many days func (c *Client) LoadAggregatedHistory(logPath string, since int64, maxAgeDays int) (*parser.AggregatedHistory, error) { lastSlash := strings.LastIndex(logPath, "/") @@ -315,25 +333,65 @@ func (c *Client) LoadAggregatedHistory(logPath string, since int64, maxAgeDays i base, base, stem, ext, stem, ext, ) - cmd := fmt.Sprintf( - `find /tmp -maxdepth 1 -name 'caddyview_*' -mtime +8 -delete 2>/dev/null; `+ - `find %s %s -type f -mtime -%d 2>/dev/null | sort | while read -r f; do `+ - `if [ "$f" = "%s" ]; then `+ - `awk -v cutoff=%d -v bucket=3600 '%s' < "$f"; `+ - `else `+ - `key=$(printf '%%s' "$f" | tr -c 'A-Za-z0-9' '_'); c="/tmp/caddyview_${key}.cache"; `+ - `if [ -s "$c" ] && [ "$c" -nt "$f" ]; then cat "$c"; else `+ - `t=$(mktemp /tmp/caddyview.XXXXXX 2>/dev/null) || t="/tmp/caddyview_tmp.$$"; `+ - `case "$f" in *.gz) zcat "$f" ;; *) cat "$f" ;; esac 2>/dev/null `+ - `| awk -v cutoff=0 -v bucket=3600 '%s' > "$t" && mv "$t" "$c" && cat "$c" || cat "$t" 2>/dev/null; `+ - `fi; fi; done`, + cacheKey := func(path string) string { + return strings.Map(func(r rune) rune { + if (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') { + return r + } + return '_' + }, path) + } + key := cacheKey(logPath) + + // Shell script that aggregates each file. The current log is handled + // incrementally via a line-count/inode marker; rotated files use immutable caches. + script := fmt.Sprintf( + `set -e +find /tmp -maxdepth 1 -name 'caddyview_*' -mtime +8 -delete 2>/dev/null || true +find %s %s -type f -mtime -%d 2>/dev/null | sort | while read -r f; do + if [ "$f" = "%s" ]; then + c="/tmp/caddyview_%s.cache" + m="/tmp/caddyview_%s.marker" + read inode < <(stat -c '%%i' "$f" 2>/dev/null || echo "0") + read lines < <(wc -l < "$f" 2>/dev/null || echo "0") + use_old=false + if [ -s "$c" ] && [ -s "$m" ]; then + read old_inode old_lines < "$m" + if [ "$inode" = "$old_inode" ] && [ "$lines" -ge "$old_lines" ]; then + use_old=true + fi + fi + if [ "$use_old" = true ]; then + cat "$c" + if [ "$lines" -gt "$old_lines" ]; then + tail -n +$((old_lines + 1)) "$f" 2>/dev/null | awk -v cutoff=0 -v bucket=3600 '%s' + fi + else + awk -v cutoff=0 -v bucket=3600 '%s' < "$f" > "$c" + fi + echo "$inode $lines" > "$m" + if [ "$use_old" != "true" ]; then cat "$c"; fi + else + key=$(printf '%%s' "$f" | tr -c 'A-Za-z0-9' '_') + c="/tmp/caddyview_${key}.cache" + if [ -s "$c" ] && [ "$c" -nt "$f" ]; then + cat "$c" + else + t=$(mktemp /tmp/caddyview.XXXXXX 2>/dev/null) || t="/tmp/caddyview_tmp.$$" + case "$f" in *.gz) zcat "$f" ;; *) cat "$f" ;; esac 2>/dev/null \ + | awk -v cutoff=0 -v bucket=3600 '%s' > "$t" && mv "$t" "$c" && cat "$c" || cat "$t" 2>/dev/null + fi + fi +done`, dir, namePattern, maxAgeDays+1, logPath, - since, historyAwkProg, + key, key, + historyAwkProg, + historyAwkProg, historyAwkProg, ) - out, err := c.RunCommand(cmd) + out, err := c.RunCommand(script) if err != nil && strings.TrimSpace(out) == "" { return nil, fmt.Errorf("aggregation failed: %w", err) } diff --git a/internal/tui/app.go b/internal/tui/app.go index 4b477df..2b1032f 100644 --- a/internal/tui/app.go +++ b/internal/tui/app.go @@ -18,12 +18,13 @@ import ( type viewTab int const ( - tabLive viewTab = 0 - tabDay viewTab = 1 - tabWeek viewTab = 2 + tabLive viewTab = 0 + tabDay viewTab = 1 + tabWeek viewTab = 2 + tabErrors viewTab = 3 ) -var tabLabels = []string{"[1] LIVE", "[2] 1 DAY", "[3] 1 WEEK"} +var tabLabels = []string{"[1] LIVE", "[2] 1 DAY", "[3] 1 WEEK", "[4] ERRORS"} // tabWindow returns how far back each history tab reaches. func tabWindow(t viewTab) time.Duration { @@ -86,6 +87,7 @@ const historyRefreshInterval = 5 * time.Minute type historyState struct { stats *parser.Stats buckets []parser.Bucket + errors []parser.ErrorEntry loaded bool loading bool loadedAt time.Time @@ -244,6 +246,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "3": m.activeTab = tabWeek return m, m.ensureHistory(tabWeek) + case "4": + m.activeTab = tabErrors + return m, m.ensureHistory(tabDay) } case tickMsg: @@ -252,7 +257,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.ticks%sysStatsEvery == 0 { cmds = append(cmds, fetchSysStats(m.client)) } - if m.activeTab != tabLive { + if m.activeTab == tabErrors { + if cmd := m.maybeRefreshHistory(tabDay); cmd != nil { + cmds = append(cmds, cmd) + } + } else if m.activeTab != tabLive { if cmd := m.maybeRefreshHistory(m.activeTab); cmd != nil { cmds = append(cmds, cmd) } @@ -280,6 +289,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if msg.err == nil && msg.agg != nil { h.stats = msg.agg.ToStats() h.buckets = msg.agg.ToBuckets(tabBucketSize(msg.tab)) + h.errors = msg.agg.RecentErrors(100) } case errMsg: @@ -335,8 +345,10 @@ func (m model) View() string { parts = append(parts, m.renderHistory(0, "LAST 24 HOURS")) case tabWeek: parts = append(parts, m.renderHistory(1, "LAST 7 DAYS")) + case tabErrors: + parts = append(parts, m.renderErrors()) } - parts = append(parts, styleDim.Render(" [1] LIVE [2] 1 DAY [3] 1 WEEK [Q] QUIT")) + parts = append(parts, styleDim.Render(" [1] LIVE [2] 1 DAY [3] 1 WEEK [4] ERRORS [Q] QUIT")) return lipgloss.JoinVertical(lipgloss.Left, parts...) } @@ -451,6 +463,72 @@ func (m model) renderHistory(idx int, label string) string { return lipgloss.JoinVertical(lipgloss.Left, chart, bottom) } +// -- errors view -------------------------------------------------------------- + +func (m model) renderErrors() string { + h := m.history[0] // errors are loaded together with the 1 DAY history + if h.loading { + return lipgloss.NewStyle().Padding(2, 4). + Render(styleWarn.Render("▒▒ LOADING ERRORS ▒▒")) + } + if !h.loaded { + return lipgloss.NewStyle().Padding(2, 4).Render(styleDim.Render("No data loaded.")) + } + if h.err != nil { + return lipgloss.NewStyle().Padding(2, 4). + Render(styleBad.Render(fmt.Sprintf("ERROR: %v", h.err))) + } + errs := h.errors + if len(errs) == 0 { + return lipgloss.NewStyle().Padding(2, 4). + Render(styleDim.Render("No 4xx/5xx requests found in the last 24 hours.")) + } + + age := time.Since(h.loadedAt).Round(time.Second) + title := fmt.Sprintf("RECENT 4xx/5xx ERRORS — %d entries — updated %s ago", len(errs), age) + + // Column widths (responsive, but fixed for simplicity) + timeW := 19 + statusW := 7 + methodW := 7 + hostW := 28 + ipW := 16 + uriW := m.width - timeW - statusW - methodW - hostW - ipW - 14 + if uriW < 20 { + uriW = 20 + } + + header := fmt.Sprintf("%-*s %-*s %-*s %-*s %-*s %s", + timeW, "TIME", + statusW, "STATUS", + methodW, "METHOD", + hostW, "HOST", + ipW, "REMOTE IP", + "URI", + ) + rows := []string{styleHeader.Render(title), "", styleHeader.Render(header)} + for _, e := range errs { + statusStr := fmt.Sprintf("%d", e.Status) + var statusStyled string + if e.Status >= 500 { + statusStyled = styleBad.Render(statusStr) + } else { + statusStyled = styleWarn.Render(statusStr) + } + host := truncate(e.Host, hostW-1) + uri := truncate(e.URI, uriW-1) + rows = append(rows, fmt.Sprintf("%-*s %-*s %-*s %-*s %-*s %s", + timeW, e.Time.Format("2006-01-02 15:04:05"), + statusW, statusStyled, + methodW, e.Method, + hostW, host, + ipW, e.RemoteIP, + uri, + )) + } + return styleBorder.Width(m.width - 4).Render(strings.Join(rows, "\n")) +} + // -- bar chart ---------------------------------------------------------------- func (m model) renderBarChart(buckets []parser.Bucket, bucketSize time.Duration, label string, stats *parser.Stats) string { @@ -630,6 +708,16 @@ func topN(m map[string]int64, n int) []kv { return pairs } +func truncate(s string, max int) string { + if max <= 0 { + return "" + } + if len(s) <= max { + return s + } + return s[:max-1] + "…" +} + func bucketSizeLabel(d time.Duration) string { switch { case d >= 24*time.Hour: