Compare commits
47 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12a757ba1e | |||
| 623a6e5f85 | |||
| 20966d0c94 | |||
| f95a64a18b | |||
| 1c0c44ec3c | |||
| ee4468ca23 | |||
| 697a77bdab | |||
| 8f452a35e6 | |||
| 1d285c3c4e | |||
| 0fb45f6c50 | |||
| be24fa3859 | |||
| 39619b2845 | |||
| 40c925e7f9 | |||
| 8f69e469e6 | |||
| 3024816525 | |||
| 1d7bc42d59 | |||
| 518ae8c2bf | |||
| 6eb1809309 | |||
| 1c01902a71 | |||
| 9d302ad475 | |||
| 7875f132f7 | |||
| 0d789fb97c | |||
| 5ddaed298b | |||
| 1528568c30 | |||
| 6f238cdf25 | |||
| ffe451edaa | |||
| c9bde52cb1 | |||
| 0603746d7c | |||
| 25632f3427 | |||
| e51cdb0c50 | |||
| 1cb05d404b | |||
| 4196066e57 | |||
| 47e96c7d92 | |||
| bae2ecb79a | |||
| bd0d15a1ae | |||
| 689498c5f4 | |||
| 34e260a612 | |||
| 47eff3a75c | |||
| 0210b49219 | |||
| 70a150152c | |||
| f4b54db399 | |||
| e857cfc665 | |||
| e66008f341 | |||
| a238ce320b | |||
| 18b41c1b45 | |||
| b74242e6d9 | |||
| 4e378b882a |
@@ -0,0 +1,422 @@
|
|||||||
|
name: Build Debian Packages
|
||||||
|
|
||||||
|
on:
|
||||||
|
# APT publishing is release-driven: we build + publish only on `v*` tag
|
||||||
|
# pushes. PRs into master still build the .debs as a sanity check (no
|
||||||
|
# publish). Manual dispatch is kept as an escape hatch.
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
env:
|
||||||
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-deb:
|
||||||
|
name: Build .deb for ${{ matrix.target }}
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- target: x86_64-unknown-linux-gnu
|
||||||
|
arch: amd64
|
||||||
|
- target: aarch64-unknown-linux-gnu
|
||||||
|
arch: arm64
|
||||||
|
- target: armv7-unknown-linux-gnueabihf
|
||||||
|
arch: armhf
|
||||||
|
- target: riscv64gc-unknown-linux-gnu
|
||||||
|
arch: riscv64
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install Rust toolchain
|
||||||
|
uses: dtolnay/rust-toolchain@stable
|
||||||
|
with:
|
||||||
|
targets: ${{ matrix.target }}
|
||||||
|
|
||||||
|
- name: Install cargo-deb
|
||||||
|
run: cargo install cargo-deb
|
||||||
|
|
||||||
|
- name: Install build dependencies
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y dpkg-dev
|
||||||
|
|
||||||
|
- name: Install cross-compilation tools (ARM64)
|
||||||
|
if: matrix.target == 'aarch64-unknown-linux-gnu'
|
||||||
|
run: |
|
||||||
|
sudo dpkg --add-architecture arm64
|
||||||
|
# Disable all existing sources and create new ones with proper arch specifications
|
||||||
|
sudo mv /etc/apt/sources.list /etc/apt/sources.list.backup
|
||||||
|
sudo mv /etc/apt/sources.list.d /etc/apt/sources.list.d.backup || true
|
||||||
|
sudo mkdir -p /etc/apt/sources.list.d
|
||||||
|
# Clear APT cache and lists
|
||||||
|
sudo rm -rf /var/lib/apt/lists/*
|
||||||
|
sudo mkdir -p /var/lib/apt/lists/partial
|
||||||
|
# Create new sources.list with both amd64 and arm64
|
||||||
|
cat << EOF | sudo tee /etc/apt/sources.list
|
||||||
|
deb [arch=amd64] http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse
|
||||||
|
deb [arch=amd64] http://archive.ubuntu.com/ubuntu $(lsb_release -sc)-updates main universe restricted multiverse
|
||||||
|
deb [arch=amd64] http://archive.ubuntu.com/ubuntu $(lsb_release -sc)-backports main universe restricted multiverse
|
||||||
|
deb [arch=amd64] http://security.ubuntu.com/ubuntu $(lsb_release -sc)-security main universe restricted multiverse
|
||||||
|
deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports $(lsb_release -sc) main universe restricted multiverse
|
||||||
|
deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports $(lsb_release -sc)-updates main universe restricted multiverse
|
||||||
|
deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports $(lsb_release -sc)-backports main universe restricted multiverse
|
||||||
|
deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports $(lsb_release -sc)-security main universe restricted multiverse
|
||||||
|
EOF
|
||||||
|
echo "=== Contents of /etc/apt/sources.list ==="
|
||||||
|
cat /etc/apt/sources.list
|
||||||
|
echo "=== Contents of /etc/apt/sources.list.d/ ==="
|
||||||
|
ls -la /etc/apt/sources.list.d/ || true
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y gcc-aarch64-linux-gnu libdrm-dev:arm64 libdrm-amdgpu1:arm64
|
||||||
|
|
||||||
|
- name: Install cross-compilation tools (ARMhf)
|
||||||
|
if: matrix.target == 'armv7-unknown-linux-gnueabihf'
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y gcc-arm-linux-gnueabihf
|
||||||
|
|
||||||
|
- name: Install cross-compilation tools (RISC-V)
|
||||||
|
if: matrix.target == 'riscv64gc-unknown-linux-gnu'
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y gcc-riscv64-linux-gnu
|
||||||
|
|
||||||
|
- name: Install GPU libraries (x86_64)
|
||||||
|
if: matrix.target == 'x86_64-unknown-linux-gnu'
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y libdrm-dev libdrm-amdgpu1
|
||||||
|
|
||||||
|
- name: Configure cross-compilation (ARM64)
|
||||||
|
if: matrix.target == 'aarch64-unknown-linux-gnu'
|
||||||
|
run: |
|
||||||
|
mkdir -p .cargo
|
||||||
|
cat >> .cargo/config.toml << EOF
|
||||||
|
[target.aarch64-unknown-linux-gnu]
|
||||||
|
linker = "aarch64-linux-gnu-gcc"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
- name: Configure cross-compilation (ARMhf)
|
||||||
|
if: matrix.target == 'armv7-unknown-linux-gnueabihf'
|
||||||
|
run: |
|
||||||
|
mkdir -p .cargo
|
||||||
|
cat >> .cargo/config.toml << EOF
|
||||||
|
[target.armv7-unknown-linux-gnueabihf]
|
||||||
|
linker = "arm-linux-gnueabihf-gcc"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
- name: Configure cross-compilation (RISC-V)
|
||||||
|
if: matrix.target == 'riscv64gc-unknown-linux-gnu'
|
||||||
|
run: |
|
||||||
|
mkdir -p .cargo
|
||||||
|
cat >> .cargo/config.toml << EOF
|
||||||
|
[target.riscv64gc-unknown-linux-gnu]
|
||||||
|
linker = "riscv64-linux-gnu-gcc"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
- name: Cache cargo registry
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: ~/.cargo/registry
|
||||||
|
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
|
||||||
|
- name: Cache cargo index
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: ~/.cargo/git
|
||||||
|
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
|
||||||
|
- name: Cache target directory
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: target
|
||||||
|
key: ${{ runner.os }}-target-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
|
||||||
|
- name: Build socktop .deb package
|
||||||
|
run: |
|
||||||
|
cargo deb --package socktop --target ${{ matrix.target }} --no-strip
|
||||||
|
|
||||||
|
- name: Build socktop_agent .deb package (with GPU support)
|
||||||
|
if: matrix.target == 'x86_64-unknown-linux-gnu' || matrix.target == 'aarch64-unknown-linux-gnu'
|
||||||
|
run: |
|
||||||
|
cargo deb --package socktop_agent --target ${{ matrix.target }} --no-strip
|
||||||
|
|
||||||
|
- name: Build socktop_agent .deb package (without GPU support)
|
||||||
|
if: matrix.target == 'armv7-unknown-linux-gnueabihf' || matrix.target == 'riscv64gc-unknown-linux-gnu'
|
||||||
|
run: |
|
||||||
|
cargo deb --package socktop_agent --target ${{ matrix.target }} --no-strip --no-default-features
|
||||||
|
|
||||||
|
- name: Copy packages to debs directory
|
||||||
|
run: |
|
||||||
|
mkdir -p debs
|
||||||
|
cp target/${{ matrix.target }}/debian/*.deb debs/
|
||||||
|
|
||||||
|
- name: List generated packages
|
||||||
|
run: ls -lh debs/
|
||||||
|
|
||||||
|
- name: Upload .deb packages as artifacts
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: debian-packages-${{ matrix.arch }}
|
||||||
|
path: debs/*.deb
|
||||||
|
if-no-files-found: error
|
||||||
|
retention-days: 90
|
||||||
|
|
||||||
|
# Combine all artifacts into a single downloadable archive
|
||||||
|
combine-artifacts:
|
||||||
|
name: Combine all .deb packages
|
||||||
|
needs: build-deb
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Download AMD64 packages
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: debian-packages-amd64
|
||||||
|
path: all-debs
|
||||||
|
|
||||||
|
- name: Download ARM64 packages
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: debian-packages-arm64
|
||||||
|
path: all-debs
|
||||||
|
|
||||||
|
- name: Download ARMhf packages
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: debian-packages-armhf
|
||||||
|
path: all-debs
|
||||||
|
|
||||||
|
- name: Download RISC-V packages
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: debian-packages-riscv64
|
||||||
|
path: all-debs
|
||||||
|
|
||||||
|
- name: List all packages
|
||||||
|
run: |
|
||||||
|
echo "All generated .deb packages:"
|
||||||
|
ls -lh all-debs/
|
||||||
|
|
||||||
|
- name: Upload combined artifacts
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: all-debian-packages
|
||||||
|
path: all-debs/*.deb
|
||||||
|
if-no-files-found: error
|
||||||
|
retention-days: 90
|
||||||
|
|
||||||
|
- name: Generate checksums
|
||||||
|
run: |
|
||||||
|
cd all-debs
|
||||||
|
sha256sum *.deb > SHA256SUMS
|
||||||
|
cat SHA256SUMS
|
||||||
|
|
||||||
|
- name: Upload checksums
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: checksums
|
||||||
|
path: all-debs/SHA256SUMS
|
||||||
|
retention-days: 90
|
||||||
|
|
||||||
|
# Publish packages to gh-pages APT repository
|
||||||
|
publish-apt-repo:
|
||||||
|
name: Publish to APT Repository
|
||||||
|
needs: combine-artifacts
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
# Publish only on `v*` release tags — keep gh-pages stable between
|
||||||
|
# releases instead of overwriting same-version .debs on every commit.
|
||||||
|
if: startsWith(github.ref, 'refs/tags/v')
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Download all packages
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: all-debian-packages
|
||||||
|
path: debs
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y dpkg-dev gpg
|
||||||
|
|
||||||
|
- name: Checkout gh-pages branch
|
||||||
|
run: |
|
||||||
|
git fetch origin gh-pages:gh-pages || echo "gh-pages branch doesn't exist yet"
|
||||||
|
if git show-ref --verify --quiet refs/heads/gh-pages; then
|
||||||
|
git checkout gh-pages
|
||||||
|
else
|
||||||
|
git checkout --orphan gh-pages
|
||||||
|
git rm -rf . 2>/dev/null || true
|
||||||
|
# Create basic structure
|
||||||
|
mkdir -p dists/stable/main/{binary-amd64,binary-arm64,binary-armhf,binary-riscv64}
|
||||||
|
mkdir -p pool/main
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Copy packages to pool
|
||||||
|
run: |
|
||||||
|
mkdir -p pool/main
|
||||||
|
cp debs/*.deb pool/main/
|
||||||
|
ls -lh pool/main/
|
||||||
|
|
||||||
|
- name: Generate Packages files
|
||||||
|
run: |
|
||||||
|
for arch in amd64 arm64 armhf riscv64; do
|
||||||
|
mkdir -p dists/stable/main/binary-$arch
|
||||||
|
dpkg-scanpackages --arch $arch pool/main /dev/null > dists/stable/main/binary-$arch/Packages 2>/dev/null || true
|
||||||
|
if [ -s dists/stable/main/binary-$arch/Packages ]; then
|
||||||
|
gzip -9 -k -f dists/stable/main/binary-$arch/Packages
|
||||||
|
echo "Generated Packages file for $arch"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
- name: Generate Release file
|
||||||
|
run: |
|
||||||
|
cat > dists/stable/Release << EOF
|
||||||
|
Origin: socktop
|
||||||
|
Label: socktop
|
||||||
|
Suite: stable
|
||||||
|
Codename: stable
|
||||||
|
Architectures: amd64 arm64 armhf riscv64
|
||||||
|
Components: main
|
||||||
|
Description: socktop APT repository
|
||||||
|
Date: $(date -Ru)
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# Add MD5Sum
|
||||||
|
echo "MD5Sum:" >> dists/stable/Release
|
||||||
|
for arch in amd64 arm64 armhf riscv64; do
|
||||||
|
for file in dists/stable/main/binary-$arch/Packages*; do
|
||||||
|
if [ -f "$file" ]; then
|
||||||
|
md5sum "$file" | awk '{print " " $1, "'$(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null)'", "'"${file#dists/stable/}"'"}' >> dists/stable/Release
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
|
# Add SHA256
|
||||||
|
echo "SHA256:" >> dists/stable/Release
|
||||||
|
for arch in amd64 arm64 armhf riscv64; do
|
||||||
|
for file in dists/stable/main/binary-$arch/Packages*; do
|
||||||
|
if [ -f "$file" ]; then
|
||||||
|
sha256sum "$file" | awk '{print " " $1, "'$(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null)'", "'"${file#dists/stable/}"'"}' >> dists/stable/Release
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
done
|
||||||
|
|
||||||
|
- name: Set GPG available flag
|
||||||
|
id: check_gpg
|
||||||
|
env:
|
||||||
|
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
|
||||||
|
run: |
|
||||||
|
if [ -n "$GPG_PRIVATE_KEY" ]; then
|
||||||
|
echo "available=true" >> $GITHUB_OUTPUT
|
||||||
|
else
|
||||||
|
echo "available=false" >> $GITHUB_OUTPUT
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Import GPG key
|
||||||
|
if: steps.check_gpg.outputs.available == 'true'
|
||||||
|
env:
|
||||||
|
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
|
||||||
|
run: |
|
||||||
|
echo "$GPG_PRIVATE_KEY" | gpg --batch --import
|
||||||
|
gpg --list-secret-keys
|
||||||
|
|
||||||
|
- name: Sign repository
|
||||||
|
if: steps.check_gpg.outputs.available == 'true'
|
||||||
|
env:
|
||||||
|
GPG_KEY_ID: ${{ secrets.GPG_KEY_ID }}
|
||||||
|
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
|
||||||
|
run: |
|
||||||
|
if [ -n "$GPG_PASSPHRASE" ]; then
|
||||||
|
echo "$GPG_PASSPHRASE" | gpg --batch --yes --no-tty --pinentry-mode loopback --passphrase-fd 0 \
|
||||||
|
--default-key "$GPG_KEY_ID" \
|
||||||
|
-abs -o dists/stable/Release.gpg dists/stable/Release
|
||||||
|
echo "$GPG_PASSPHRASE" | gpg --batch --yes --no-tty --pinentry-mode loopback --passphrase-fd 0 \
|
||||||
|
--default-key "$GPG_KEY_ID" \
|
||||||
|
--clearsign -o dists/stable/InRelease dists/stable/Release
|
||||||
|
else
|
||||||
|
gpg --batch --yes --no-tty --pinentry-mode loopback \
|
||||||
|
--default-key "$GPG_KEY_ID" \
|
||||||
|
-abs -o dists/stable/Release.gpg dists/stable/Release
|
||||||
|
gpg --batch --yes --no-tty --pinentry-mode loopback \
|
||||||
|
--default-key "$GPG_KEY_ID" \
|
||||||
|
--clearsign -o dists/stable/InRelease dists/stable/Release
|
||||||
|
fi
|
||||||
|
gpg --armor --export "$GPG_KEY_ID" > KEY.gpg
|
||||||
|
echo "✓ Repository signed"
|
||||||
|
|
||||||
|
- name: Create unsigned repository notice
|
||||||
|
if: steps.check_gpg.outputs.available == 'false'
|
||||||
|
run: |
|
||||||
|
echo "⚠️ Warning: GPG_PRIVATE_KEY not set. Repository will be UNSIGNED."
|
||||||
|
echo "⚠️ Add GPG secrets to sign the repository automatically."
|
||||||
|
echo "To add secrets: Settings → Secrets and variables → Actions → Repository secrets"
|
||||||
|
|
||||||
|
- name: Copy index.html if exists
|
||||||
|
run: |
|
||||||
|
git checkout ${{ github.ref_name }} -- index.html 2>/dev/null || echo "No index.html in source branch"
|
||||||
|
|
||||||
|
- name: Commit and push to gh-pages
|
||||||
|
run: |
|
||||||
|
git config user.name "GitHub Actions"
|
||||||
|
git config user.email "actions@github.com"
|
||||||
|
git add .
|
||||||
|
|
||||||
|
if git diff --staged --quiet; then
|
||||||
|
echo "No changes to commit"
|
||||||
|
else
|
||||||
|
COMMIT_MSG="Update APT repository"
|
||||||
|
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
|
||||||
|
COMMIT_MSG="$COMMIT_MSG - Release ${{ github.ref_name }}"
|
||||||
|
else
|
||||||
|
COMMIT_MSG="$COMMIT_MSG - $(date -u +'%Y-%m-%d %H:%M:%S UTC')"
|
||||||
|
fi
|
||||||
|
git commit -m "$COMMIT_MSG"
|
||||||
|
git push origin gh-pages
|
||||||
|
echo "✓ Published to gh-pages"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Optional: Create a release with the .deb files if this is a tag
|
||||||
|
create-release:
|
||||||
|
name: Create GitHub Release
|
||||||
|
needs: combine-artifacts
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: startsWith(github.ref, 'refs/tags/v')
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
steps:
|
||||||
|
- name: Download all packages
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: all-debian-packages
|
||||||
|
path: release-debs
|
||||||
|
|
||||||
|
- name: Download checksums
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: checksums
|
||||||
|
path: release-debs
|
||||||
|
|
||||||
|
- name: Create Release
|
||||||
|
uses: softprops/action-gh-release@v1
|
||||||
|
with:
|
||||||
|
files: release-debs/*
|
||||||
|
draft: false
|
||||||
|
prerelease: false
|
||||||
|
generate_release_notes: true
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
+13
@@ -1,3 +1,16 @@
|
|||||||
/target
|
/target
|
||||||
.vscode/
|
.vscode/
|
||||||
/socktop-wasm-test/target
|
/socktop-wasm-test/target
|
||||||
|
/.cargo/
|
||||||
|
|
||||||
|
# Documentation files from development sessions (context-specific, not for public repo)
|
||||||
|
/OPTIMIZATION_PROCESS_DETAILS.md
|
||||||
|
/THREAD_SUPPORT.md
|
||||||
|
|
||||||
|
# APT Repository - Safety: Never commit private keys!
|
||||||
|
*.asc
|
||||||
|
*-private.key
|
||||||
|
*-secret.key
|
||||||
|
gpg-private-backup.key
|
||||||
|
secring.gpg
|
||||||
|
# Note: Release.gpg, InRelease, and KEY.gpg (public) ARE safe to commit
|
||||||
|
|||||||
Generated
+1264
-761
File diff suppressed because it is too large
Load Diff
+3
-2
@@ -24,8 +24,9 @@ serde_json = "1.0"
|
|||||||
sysinfo = "0.37"
|
sysinfo = "0.37"
|
||||||
|
|
||||||
# CLI UI
|
# CLI UI
|
||||||
ratatui = "0.28"
|
ratatui = "0.30"
|
||||||
crossterm = "0.27"
|
crossterm = "0.29"
|
||||||
|
unicode-width = "0.2"
|
||||||
|
|
||||||
# web server (remote-agent)
|
# web server (remote-agent)
|
||||||
axum = { version = "0.7", features = ["ws"] }
|
axum = { version = "0.7", features = ["ws"] }
|
||||||
|
|||||||
@@ -0,0 +1,156 @@
|
|||||||
|
# Debian Packaging Implementation Summary
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
Successfully implemented Debian packaging for socktop using `cargo-deb`, with GitHub Actions automation for building packages for both AMD64 and ARM64 architectures.
|
||||||
|
|
||||||
|
## Branches Created
|
||||||
|
|
||||||
|
1. **`feature/debian-packaging`** - Main branch with debian packaging implementation
|
||||||
|
2. **`feature/man-pages`** - Separate branch for man pages work (to be researched further)
|
||||||
|
|
||||||
|
## What Was Added
|
||||||
|
|
||||||
|
### 1. Cargo.toml Updates
|
||||||
|
|
||||||
|
Both `socktop/Cargo.toml` and `socktop_agent/Cargo.toml` were updated with:
|
||||||
|
- `[package.metadata.deb]` sections
|
||||||
|
- Package metadata (maintainer, description, dependencies)
|
||||||
|
- Asset definitions (binaries, documentation)
|
||||||
|
- Systemd service configuration (agent only)
|
||||||
|
|
||||||
|
### 2. Systemd Service
|
||||||
|
|
||||||
|
**File**: `socktop_agent/socktop-agent.service`
|
||||||
|
- Runs as `socktop` user/group
|
||||||
|
- Listens on port 3000 by default
|
||||||
|
- Security hardening enabled
|
||||||
|
- Disabled by default (user must explicitly enable)
|
||||||
|
|
||||||
|
### 3. Maintainer Scripts
|
||||||
|
|
||||||
|
**Directory**: `socktop_agent/debian/`
|
||||||
|
|
||||||
|
- **`postinst`**: Creates `socktop` user/group, sets up `/var/lib/socktop` directory
|
||||||
|
- **`postrm`**: Cleanup on package removal/purge
|
||||||
|
|
||||||
|
### 4. GitHub Actions Workflow
|
||||||
|
|
||||||
|
**File**: `.github/workflows/build-deb.yml`
|
||||||
|
|
||||||
|
Features:
|
||||||
|
- Builds for both x86_64 and ARM64
|
||||||
|
- Triggered on:
|
||||||
|
- Push to `master` or `feature/debian-packaging`
|
||||||
|
- Pull requests to `master`
|
||||||
|
- Version tags (v*)
|
||||||
|
- Manual workflow dispatch
|
||||||
|
- Creates artifacts:
|
||||||
|
- `debian-packages-amd64`
|
||||||
|
- `debian-packages-arm64`
|
||||||
|
- `all-debian-packages` (combined)
|
||||||
|
- `checksums` (SHA256SUMS)
|
||||||
|
- Automatic GitHub releases for version tags
|
||||||
|
|
||||||
|
### 5. Documentation
|
||||||
|
|
||||||
|
**File**: `docs/DEBIAN_PACKAGING.md`
|
||||||
|
|
||||||
|
Comprehensive guide covering:
|
||||||
|
- Building packages locally
|
||||||
|
- Cross-compilation for ARM64
|
||||||
|
- Installation and configuration
|
||||||
|
- Using GitHub Actions artifacts
|
||||||
|
- Creating local APT repositories
|
||||||
|
- Troubleshooting
|
||||||
|
|
||||||
|
## Package Details
|
||||||
|
|
||||||
|
### socktop (TUI Client)
|
||||||
|
- **Binary**: `/usr/bin/socktop`
|
||||||
|
- **Size**: ~3.5 MB (x86_64)
|
||||||
|
- **Dependencies**: Auto-detected
|
||||||
|
|
||||||
|
### socktop_agent (Daemon)
|
||||||
|
- **Binary**: `/usr/bin/socktop_agent`
|
||||||
|
- **Service**: `socktop-agent.service`
|
||||||
|
- **User/Group**: `socktop` (created automatically)
|
||||||
|
- **State directory**: `/var/lib/socktop`
|
||||||
|
- **Size**: ~6.7 MB (x86_64)
|
||||||
|
- **Dependencies**: Auto-detected
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
Both packages successfully built locally:
|
||||||
|
```
|
||||||
|
✓ socktop_1.50.0-1_amd64.deb
|
||||||
|
✓ socktop-agent_1.50.1-1_amd64.deb
|
||||||
|
```
|
||||||
|
|
||||||
|
Verified:
|
||||||
|
- Package contents (dpkg -c)
|
||||||
|
- Package metadata (dpkg -I)
|
||||||
|
- Systemd service file inclusion
|
||||||
|
- Maintainer scripts inclusion
|
||||||
|
- Documentation inclusion
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### For Users
|
||||||
|
|
||||||
|
Download pre-built packages from GitHub Actions artifacts:
|
||||||
|
1. Go to Actions tab
|
||||||
|
2. Select latest "Build Debian Packages" run
|
||||||
|
3. Download architecture-specific artifact
|
||||||
|
4. Install: `sudo dpkg -i socktop*.deb`
|
||||||
|
|
||||||
|
### For Developers
|
||||||
|
|
||||||
|
Build locally:
|
||||||
|
```bash
|
||||||
|
cargo install cargo-deb
|
||||||
|
cargo deb --package socktop
|
||||||
|
cargo deb --package socktop_agent
|
||||||
|
```
|
||||||
|
|
||||||
|
Cross-compile for ARM64:
|
||||||
|
```bash
|
||||||
|
rustup target add aarch64-unknown-linux-gnu
|
||||||
|
sudo apt install gcc-aarch64-linux-gnu libc6-dev-arm64-cross
|
||||||
|
cargo deb --package socktop --target aarch64-unknown-linux-gnu
|
||||||
|
```
|
||||||
|
|
||||||
|
## Next Steps
|
||||||
|
|
||||||
|
To get packages in official APT repositories:
|
||||||
|
|
||||||
|
1. **Short term**: Host packages on GitHub Releases (automated)
|
||||||
|
2. **Medium term**: Create PPA for Ubuntu users
|
||||||
|
3. **Long term**: Submit to Debian/Ubuntu official repositories
|
||||||
|
|
||||||
|
## Files Modified/Created
|
||||||
|
|
||||||
|
```
|
||||||
|
Modified:
|
||||||
|
socktop/Cargo.toml
|
||||||
|
socktop_agent/Cargo.toml
|
||||||
|
|
||||||
|
Created:
|
||||||
|
.github/workflows/build-deb.yml
|
||||||
|
docs/DEBIAN_PACKAGING.md
|
||||||
|
socktop_agent/socktop-agent.service
|
||||||
|
socktop_agent/debian/postinst
|
||||||
|
socktop_agent/debian/postrm
|
||||||
|
```
|
||||||
|
|
||||||
|
## Commit
|
||||||
|
|
||||||
|
```
|
||||||
|
532ed16 Add Debian packaging support with cargo-deb
|
||||||
|
```
|
||||||
|
|
||||||
|
## Resources
|
||||||
|
|
||||||
|
- [cargo-deb documentation](https://github.com/kornelski/cargo-deb)
|
||||||
|
- [Debian Policy Manual](https://www.debian.org/doc/debian-policy/)
|
||||||
|
- Full documentation in `docs/DEBIAN_PACKAGING.md`
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2025 Witty One Off
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -5,6 +5,8 @@ socktop is a remote system monitor with a rich TUI, inspired by top/btop, talkin
|
|||||||
- Linux agent: near-zero CPU when idle (request-driven, no always-on sampler)
|
- Linux agent: near-zero CPU when idle (request-driven, no always-on sampler)
|
||||||
- TUI: smooth graphs, sortable process table, scrollbars, readable colors
|
- TUI: smooth graphs, sortable process table, scrollbars, readable colors
|
||||||
|
|
||||||
|
[socktop.io](https://www.socktop.io)
|
||||||
|
|
||||||
<img src="./docs/socktop_demo.apng" width="100%">
|
<img src="./docs/socktop_demo.apng" width="100%">
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -29,6 +31,8 @@ socktop is a remote system monitor with a rich TUI, inspired by top/btop, talkin
|
|||||||
- Only top-level processes listed (threads hidden) — matches btop/top
|
- Only top-level processes listed (threads hidden) — matches btop/top
|
||||||
- Optional GPU metrics (can be disabled)
|
- Optional GPU metrics (can be disabled)
|
||||||
- Optional auth token for the agent
|
- Optional auth token for the agent
|
||||||
|
- Compact layout for small windows: automatically drops the panes that no longer fit so
|
||||||
|
the CPU graph and per-core bars stay visible (see [Compact mode](#compact-mode))
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -51,15 +55,23 @@ exec bash # or: exec zsh / exec fish
|
|||||||
|
|
||||||
Windows (for the brave): install from https://rustup.rs with the MSVC toolchain. Yes, you’ll need Visual Studio Build Tools. You chose Windows — enjoy the ride.
|
Windows (for the brave): install from https://rustup.rs with the MSVC toolchain. Yes, you’ll need Visual Studio Build Tools. You chose Windows — enjoy the ride.
|
||||||
|
|
||||||
### Raspberry Pi / Ubuntu / PopOS (required)
|
### Raspberry Pi / Ubuntu / PopOS (required for GPU support)
|
||||||
|
|
||||||
Install GPU support with apt command below
|
**Note:** GPU monitoring is only supported on x86_64 and aarch64 (64-bit ARM) platforms. ARMv7 (32-bit) and RISC-V builds do not include GPU support.
|
||||||
|
|
||||||
|
For 64-bit systems with GPU support:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install libdrm-dev libdrm-amdgpu1
|
sudo apt-get install libdrm-dev libdrm-amdgpu1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
For ARMv7 (32-bit Raspberry Pi), build with `--no-default-features` to disable GPU support:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo build --release -p socktop_agent --no-default-features
|
||||||
|
```
|
||||||
|
|
||||||
_Additional note for Raspberry Pi users. Please update your system to use the newest kernel available through app, kernel version 6.6+ will use considerably less overall CPU to run the agent. For example on a rpi4 the kernel < 6.6 the agent will consume .8 cpu but on the same hardware on > 6.6 the agent will consume only .2 cpu. (these numbers indicate continuous polling at web socket endpoints, when not in use the usage is 0)_
|
_Additional note for Raspberry Pi users. Please update your system to use the newest kernel available through app, kernel version 6.6+ will use considerably less overall CPU to run the agent. For example on a rpi4 the kernel < 6.6 the agent will consume .8 cpu but on the same hardware on > 6.6 the agent will consume only .2 cpu. (these numbers indicate continuous polling at web socket endpoints, when not in use the usage is 0)_
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -203,6 +215,8 @@ socktop --verify-hostname --tls-ca /path/to/cert.pem wss://HOST:8443/ws
|
|||||||
# shorthand:
|
# shorthand:
|
||||||
socktop -t /path/to/cert.pem wss://HOST:8443/ws
|
socktop -t /path/to/cert.pem wss://HOST:8443/ws
|
||||||
# Note: providing --tls-ca/-t automatically upgrades ws:// to wss:// if you forget
|
# Note: providing --tls-ca/-t automatically upgrades ws:// to wss:// if you forget
|
||||||
|
# force the small-window layout at any terminal size (normally automatic):
|
||||||
|
socktop --compact ws://HOST:3000/ws
|
||||||
```
|
```
|
||||||
|
|
||||||
Intervals (client-driven):
|
Intervals (client-driven):
|
||||||
@@ -214,6 +228,29 @@ The agent stays idle unless queried. When queried, it collects just what’s nee
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Compact mode
|
||||||
|
|
||||||
|
In a short terminal the fixed layout runs out of rows and the CPU graph and per-core bars
|
||||||
|
are the first things to collapse — exactly the panes you are most likely watching. Once
|
||||||
|
the window is too short for the Disks pane to show even one disk, socktop switches to a
|
||||||
|
compact layout:
|
||||||
|
|
||||||
|
- **Disks is dropped.** It is the pane that degrades worst when partially drawn.
|
||||||
|
- **Memory and Swap move side by side** into the row Disks vacated.
|
||||||
|
- **GPU shrinks to a single line** — utilisation and VRAM only, no device name. On a host
|
||||||
|
with no GPU the pane disappears entirely.
|
||||||
|
- **Everything reclaimed goes to the CPU graph and per-core bars**, which stay usable well
|
||||||
|
below the size where they used to vanish.
|
||||||
|
|
||||||
|
The switch is automatic and needs no configuration. Pass `--compact` to pin the compact
|
||||||
|
layout at any window size:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
socktop --compact ws://HOST:3000/ws
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Connection Profiles (Named)
|
## Connection Profiles (Named)
|
||||||
|
|
||||||
You can save frequently used connection settings (URL + optional TLS CA path) under a short name and reuse them later.
|
You can save frequently used connection settings (URL + optional TLS CA path) under a short name and reuse them later.
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
-----BEGIN PGP PUBLIC KEY BLOCK-----
|
||||||
|
|
||||||
|
mQGNBGkih7QBDADgX6sYMx2Lp6qcZxeCCizcy4TFsxcRJfp5mfbMplVES0hQToIP
|
||||||
|
EMC11JqPwQdLliXKjUr8Z2kgM2oqvH+dkdgzUGrw6kTK8YHc+qs37iJAOVS9D72X
|
||||||
|
tTld282NrtFwzb74nS2GKPkpWI7aSKBpHtWFPX/1ONsc56qGqFd3wwikEvCz8MeJ
|
||||||
|
HwCD1JZ9F+2DyyXWsTJNgDwPloJSUbtyVuk2gd6PeTg7AQdx92Pk/mggmYbHtP8N
|
||||||
|
wy072ku1g8K/hplmwIOGpSx1JWvAQkDU/Bb/jSqrYg2wSHO7IQnYE8I3x/zglYBl
|
||||||
|
FYNh47TVQr0zPVSYR1MQkHU5YLBTDc5UgDvtcsYUiTtq4D/m8HWmKja0/UKGxvDJ
|
||||||
|
P5sUPcp4dk77RdoCtUe5HImYGS8lo5N3+t0lz8sd9rYmRiIO4f7FJaJqJeHbUJyn
|
||||||
|
iw/GCQh5D5/D571dICrEq/QhL+k5KhJljPGoVMGPFXJIc7q+CxvGp2oOo5fOlbOn
|
||||||
|
3kSrM93AJPwT8FMAEQEAAbRFSmFzb24gV2l0dHkgKHNvY2t0b3AgYXB0IHNpZ25p
|
||||||
|
bmcga2V5KSA8amFzb25wd2l0dHkrc29ja3RvcEBwcm90b24ubWU+iQHOBBMBCgA4
|
||||||
|
FiEEHnVWqAU5uDlLwoINESwaeYRl+/IFAmkih7QCGwMFCwkIBwIGFQoJCAsCBBYC
|
||||||
|
AwECHgECF4AACgkQESwaeYRl+/KV+gwAzfZVZEhO7MQV2EmNeKVK1GycFSm2oUAl
|
||||||
|
ZbwNIEHu6+tOzqXJb8o65BtGlbLSGavsMpgRCK2SL83DdLOkutG1ahQiJr+5GaXC
|
||||||
|
zbQgX+VWqGPZtQ+I6/rVoYZPMTCrqpAmFgvVpqv0xod7w8/wny8/XmhQ37KY2/0l
|
||||||
|
B38oNTvdA7C8jzSrI6kr3XqurvQRW7z+MnC+nCp9Ob9bYtY0kpd4U3NrVdb8m32U
|
||||||
|
d5LVFwD1OGvzLOSqyJ33IKjSJc4KLvW+aEsHXe+fHO9UEzH8Nbo5MmVvX3QIHiyq
|
||||||
|
jD4zN16AGsGYqCK4irtQCiD3wBOdsG/RVkgIcdlmAH3EGEp7Ux8+7v1PXYI+UrSs
|
||||||
|
XE7f1xFTJ2r5TMex6W3he073Em4qhQsrnMF5syTZsM6N+5UqXVOM1RuDVVXr7929
|
||||||
|
hC3G8pK/A2W5Lwpxl2yzock2CxhvUn7M/xm4VbcPlWTCUd/QzU8VtsgaGHcuhi5e
|
||||||
|
xHY1AU07STLB9RinjBVf2bmk4oDQcmB6uQGNBGkih7QBDACrjE+xSWP92n931/5t
|
||||||
|
+tXcujwFlIpSZdbSQFr0B0YyjPRUP4FSzEGu8vuM5ChUfWKhmN1dDr5C4qFo9NgQ
|
||||||
|
6oCN2HubajSGyXNwnOMlMb5ck79Ubmy9yDV9/ZLqpJJiozGap2/EnNoDhaANlmUg
|
||||||
|
rfqUHpIB8XC2IZ0Itt05tp/u78dJiB+R6ReZn/bVUafNV4jIqYZfLRzI3FTJ4xvK
|
||||||
|
FGs/ER+JajAdJQ8LPfazmDQSGw0huguxhopZwKQ/qWZMn1OHq/ZaPvCqbQt3irLw
|
||||||
|
dLPDC4pEaYGRyADYeyuarG0DVyUQ9XRc/NufKDvOAn33LpBPBpcvNQAsVhWTCYl7
|
||||||
|
ogQ+suVYVN8Tu7v4bUSHKwzXKvLN/ojJX/Fh7eTW4TPsgLHNHAEDUkSQozIe9vO6
|
||||||
|
o+vydDqRxuXJgdkR7lqP6PQDYrhRYZGJf57eKf6VtTKYFaMbiMWPU+vcHeB0/iDe
|
||||||
|
Pv81qro2LD2PG5WCzDpNETBceCTjykb9r0VHx4/JsiojKmsAEQEAAYkBtgQYAQoA
|
||||||
|
IBYhBB51VqgFObg5S8KCDREsGnmEZfvyBQJpIoe0AhsMAAoJEBEsGnmEZfvyNp8M
|
||||||
|
AIH+6+hGB3qADdnhNgb+3fN0511eK9Uk82lxgGARLcD8GN1UP0HlvEqkxCHy3PUe
|
||||||
|
tHcsuYVz7i8pmpEGdFx9zv7MelenUsJniUQ++OZKx6iUG/MYqz//NxY+5lyRmcu2
|
||||||
|
aYvUxhkgf9zgxXTkTyV2VV32mX//cHcwc+c/089QAPzCMaSrHdNK+ED9+k8uquJ1
|
||||||
|
lSL9Bm15z/EV42v9Q/4KTM5OBLHpNw0Rvn9C0iuZVwHXBrrA/HSGXpA54AqNUMpZ
|
||||||
|
kRPgLQcy5yVE2y1aXLXt2XdTn6YPzrAjNoazYYuCWHYIZU7dGkIswpsDirDLKHdD
|
||||||
|
onb3VShmSpemYjsuFiqhfi6qwCkeHsz/CpQAp70SZ+z9oB8H80PJVKPbPIP3zEf3
|
||||||
|
i7bcsqHA7stF+8sJclXgxBUBeDJ3O2jN/scBOcvNA6xoRp7+oJbnjDRuxBmh+fVg
|
||||||
|
TIuw2++vTF2Ml0EMv7ePTpr7b1DofuJRNYGkuAIMVXHjLTqMiTJUce3OUy003zMg
|
||||||
|
Dg==
|
||||||
|
=AaPQ
|
||||||
|
-----END PGP PUBLIC KEY BLOCK-----
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
# socktop APT Repository
|
||||||
|
|
||||||
|
This repository contains Debian packages for socktop and socktop-agent.
|
||||||
|
|
||||||
|
## Adding this repository
|
||||||
|
|
||||||
|
Add the repository to your system:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Add the GPG key
|
||||||
|
curl -fsSL https://jasonwitty.github.io/socktop/KEY.gpg | sudo gpg --dearmor -o /usr/share/keyrings/socktop-archive-keyring.gpg
|
||||||
|
|
||||||
|
# Add the repository
|
||||||
|
echo "deb [signed-by=/usr/share/keyrings/socktop-archive-keyring.gpg] https://jasonwitty.github.io/socktop stable main" | sudo tee /etc/apt/sources.list.d/socktop.list
|
||||||
|
|
||||||
|
# Update and install
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install socktop socktop-agent
|
||||||
|
```
|
||||||
|
|
||||||
|
## Manual Installation
|
||||||
|
|
||||||
|
You can also download and install packages manually from the `pool/main/` directory.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
wget https://jasonwitty.github.io/socktop/pool/main/socktop_VERSION_ARCH.deb
|
||||||
|
sudo dpkg -i socktop_VERSION_ARCH.deb
|
||||||
|
```
|
||||||
|
|
||||||
|
## Supported Architectures
|
||||||
|
|
||||||
|
- amd64 (x86_64)
|
||||||
|
- arm64 (aarch64)
|
||||||
|
- armhf (32-bit ARM)
|
||||||
|
|
||||||
|
## Building from Source
|
||||||
|
|
||||||
|
See the main repository at https://github.com/jasonwitty/socktop
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
-----BEGIN PGP SIGNED MESSAGE-----
|
||||||
|
Hash: SHA512
|
||||||
|
|
||||||
|
Origin: socktop
|
||||||
|
Label: socktop
|
||||||
|
Suite: stable
|
||||||
|
Codename: stable
|
||||||
|
Architectures: amd64 arm64 armhf
|
||||||
|
Components: main
|
||||||
|
Description: socktop APT repository
|
||||||
|
Date: Sun, 23 Nov 2025 04:05:21 +0000
|
||||||
|
MD5Sum:
|
||||||
|
0bddefb2f13cb7c86cd05fe1ce20310f 1549 main/binary-amd64/Packages
|
||||||
|
674f0e552cbb7dc65380651a2a8d279e 799 main/binary-amd64/Packages.gz
|
||||||
|
SHA256:
|
||||||
|
babfbb4839e7fdfbc83742c16996791b0402a1315889b530330b338380398263 1549 main/binary-amd64/Packages
|
||||||
|
f8c48d0f7bf53eb02c6dbf5f1cdd046fe71b87273cf763c5bb2e95d9757a7a82 799 main/binary-amd64/Packages.gz
|
||||||
|
|
||||||
|
-----BEGIN PGP SIGNATURE-----
|
||||||
|
|
||||||
|
iQGzBAEBCgAdFiEEHnVWqAU5uDlLwoINESwaeYRl+/IFAmkiiAYACgkQESwaeYRl
|
||||||
|
+/KBsAv/eYhnK/XrNtPhLyw/zX2cGfUtBsBZrypFhV/n+TvudAIwQaqxDEvLlBUn
|
||||||
|
HBAhMKDQXGs7V45+nOgDX4rKWUqJh4SPbJgNbVte2PX7U+hsMpZBsYp3vkjApgTO
|
||||||
|
pq2CCkViyBXgTY+6vUigtvfJ9afTTWI6Qm4dLXZ7hxErBxgHQyowOoO/sF92cNOu
|
||||||
|
AosBMpE+qSy7sVqJU5g/JXJh0kddKFotXHSGA1kFMzJafJC/n5nLrusDzFJRQqyH
|
||||||
|
Io+6inYWjlb5o79z0tJzAvG1mgplLRppMBjoVJ/RJ+gT+QE70kokR6wvsgDqsKNd
|
||||||
|
mvB0TNj0zY0g6Is6V3XMyf0u+6BtLTbua913HPiqBfErgeV58vzsst+y0It42TXi
|
||||||
|
aw+UF2Kw/YhPq1rZFxgnAVcMja3qlXWpH57gmgIPovBCsPsiywWiHLsSHRzAI22b
|
||||||
|
zeTsUST/4toR/ruZVbUZvWoWAR4tzsSuwXJFx/hhinTQQTNHErXASOX986UaL9L7
|
||||||
|
o2/pTKLe
|
||||||
|
=IeBY
|
||||||
|
-----END PGP SIGNATURE-----
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
Origin: socktop
|
||||||
|
Label: socktop
|
||||||
|
Suite: stable
|
||||||
|
Codename: stable
|
||||||
|
Architectures: amd64 arm64 armhf
|
||||||
|
Components: main
|
||||||
|
Description: socktop APT repository
|
||||||
|
Date: Sun, 23 Nov 2025 04:05:21 +0000
|
||||||
|
MD5Sum:
|
||||||
|
0bddefb2f13cb7c86cd05fe1ce20310f 1549 main/binary-amd64/Packages
|
||||||
|
674f0e552cbb7dc65380651a2a8d279e 799 main/binary-amd64/Packages.gz
|
||||||
|
SHA256:
|
||||||
|
babfbb4839e7fdfbc83742c16996791b0402a1315889b530330b338380398263 1549 main/binary-amd64/Packages
|
||||||
|
f8c48d0f7bf53eb02c6dbf5f1cdd046fe71b87273cf763c5bb2e95d9757a7a82 799 main/binary-amd64/Packages.gz
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
-----BEGIN PGP SIGNATURE-----
|
||||||
|
|
||||||
|
iQGzBAABCgAdFiEEHnVWqAU5uDlLwoINESwaeYRl+/IFAmkiiAEACgkQESwaeYRl
|
||||||
|
+/KzeAv+OUIbxud5FboerwpAJULV+rS3+VX4kvwg/daVZ3yX3tJNrsyNCHgmWLVu
|
||||||
|
fLeEFFc2Ax9GvFW4jrbxRAGD+3TXQEEFkb5lGzYyDjlgVzR6wLiVTTrmzWoK+cbB
|
||||||
|
4DMozqeLiZFfQjq4UFn3+mwiYFX9Dj7PVF0M60XAUJSObbJFmaEPZIfx6wcZfkiL
|
||||||
|
lLLk1eeU5MPiyudPOhVGgaD76KrUCw+8DBNKoCKIEcCY0LvuKtUK8mWYXRSPSved
|
||||||
|
4Znd3QZz063Z6R+Lj1XlGLoTPResna28T/Nca+2JgLhbrihsLMcHoFxmrvFP9FpT
|
||||||
|
MChKngj7NnGt0yqHH5J16hdwMra/vvhmF0yoQ0loIcy+q06tYEqOcau8tvAjfbId
|
||||||
|
k3rgQgnxxVE8WUmV9Bugp7jhNMO+ImKWMwzEr6wGd9ZHqpknUlAaWeO73VP+qtAN
|
||||||
|
6mEqWhkqvXGg+srH6qp3Sg0W28dYG29X3Kx8jOp7HeyvA/gLZRN7L+bq/XaA7WFA
|
||||||
|
1hba6LIY
|
||||||
|
=QoLf
|
||||||
|
-----END PGP SIGNATURE-----
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
Package: socktop
|
||||||
|
Version: 1.50.0-1
|
||||||
|
Architecture: amd64
|
||||||
|
Maintainer: Jason Witty <jasonpwitty+socktop@proton.me>
|
||||||
|
Installed-Size: 3459
|
||||||
|
Filename: pool/main/socktop_1.50.0-1_amd64.deb
|
||||||
|
Size: 1278940
|
||||||
|
MD5sum: 0215e178e306d9379669065e8c78582b
|
||||||
|
SHA1: 04e0416389f5cecd584fd1f6b3568711f2645eee
|
||||||
|
SHA256: 69eb04b1de48541c95950a97b16357fcd9c51ffaceb143f63de4a9d758fad297
|
||||||
|
Section: admin
|
||||||
|
Priority: optional
|
||||||
|
Homepage: https://github.com/jasonwitty/socktop
|
||||||
|
Description: Remote system monitor over WebSocket, TUI like top
|
||||||
|
socktop is a remote system monitor with a rich terminal user interface (TUI)
|
||||||
|
that connects to remote hosts running the socktop_agent over WebSocket. It
|
||||||
|
provides real-time monitoring of CPU, memory, processes, and more with an
|
||||||
|
interface similar to the traditional 'top' command.
|
||||||
|
|
||||||
|
Package: socktop-agent
|
||||||
|
Version: 1.50.2-1
|
||||||
|
Architecture: amd64
|
||||||
|
Maintainer: Jason Witty <jasonpwitty+socktop@proton.me>
|
||||||
|
Installed-Size: 6793
|
||||||
|
Filename: pool/main/socktop-agent_1.50.2-1_amd64.deb
|
||||||
|
Size: 1896272
|
||||||
|
MD5sum: 22e78d03e83dcf84d6ec4a009b285902
|
||||||
|
SHA1: 26a9f4fedfdba06a047044027223f2944cf72ba6
|
||||||
|
SHA256: 11922af475146f60347a9c52cff4bbce1ce524bdb4293b2c436f3c71876e17d5
|
||||||
|
Section: admin
|
||||||
|
Priority: optional
|
||||||
|
Homepage: https://github.com/jasonwitty/socktop
|
||||||
|
Description: Socktop agent daemon. Serves host metrics over WebSocket.
|
||||||
|
socktop_agent is the daemon component that runs on remote hosts to collect and
|
||||||
|
serve system metrics over WebSocket. It gathers CPU, memory, disk, network,
|
||||||
|
GPU, and process information that can be monitored remotely by the socktop TUI
|
||||||
|
client.
|
||||||
|
|
||||||
Binary file not shown.
@@ -0,0 +1,5 @@
|
|||||||
|
Archive: stable
|
||||||
|
Component: main
|
||||||
|
Origin: socktop
|
||||||
|
Label: socktop
|
||||||
|
Architecture: amd64
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>socktop APT Repository</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 50px auto;
|
||||||
|
padding: 20px;
|
||||||
|
line-height: 1.6;
|
||||||
|
}
|
||||||
|
code {
|
||||||
|
background: #f4f4f4;
|
||||||
|
padding: 2px 6px;
|
||||||
|
border-radius: 3px;
|
||||||
|
}
|
||||||
|
pre {
|
||||||
|
background: #f4f4f4;
|
||||||
|
padding: 15px;
|
||||||
|
border-radius: 5px;
|
||||||
|
overflow-x: auto;
|
||||||
|
}
|
||||||
|
h1 { color: #333; }
|
||||||
|
h2 { color: #555; margin-top: 30px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>socktop APT Repository</h1>
|
||||||
|
<p>System monitor with remote agent support for Linux systems.</p>
|
||||||
|
|
||||||
|
<h2>Adding this repository</h2>
|
||||||
|
<pre><code># Add the GPG key
|
||||||
|
curl -fsSL https://jasonwitty.github.io/socktop/KEY.gpg | sudo gpg --dearmor -o /usr/share/keyrings/socktop-archive-keyring.gpg
|
||||||
|
|
||||||
|
# Add the repository
|
||||||
|
echo "deb [signed-by=/usr/share/keyrings/socktop-archive-keyring.gpg] https://jasonwitty.github.io/socktop stable main" | sudo tee /etc/apt/sources.list.d/socktop.list
|
||||||
|
|
||||||
|
# Update and install
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install socktop socktop-agent</code></pre>
|
||||||
|
|
||||||
|
<h2>Manual Installation</h2>
|
||||||
|
<p>Download packages from <a href="pool/main/">pool/main/</a></p>
|
||||||
|
|
||||||
|
<h2>Supported Architectures</h2>
|
||||||
|
<ul>
|
||||||
|
<li>amd64 (x86_64)</li>
|
||||||
|
<li>arm64 (aarch64)</li>
|
||||||
|
<li>armhf (32-bit ARM)</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h2>Source Code</h2>
|
||||||
|
<p>Visit the <a href="https://github.com/jasonwitty/socktop">GitHub repository</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,274 @@
|
|||||||
|
# Debian Packaging for socktop
|
||||||
|
|
||||||
|
This document describes how to build and use Debian packages for socktop and socktop_agent.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Install `cargo-deb`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install cargo-deb
|
||||||
|
```
|
||||||
|
|
||||||
|
## Building Packages Locally
|
||||||
|
|
||||||
|
### Build for your current architecture (x86_64)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build socktop TUI client
|
||||||
|
cargo deb --package socktop
|
||||||
|
|
||||||
|
# Build socktop_agent daemon
|
||||||
|
cargo deb --package socktop_agent
|
||||||
|
```
|
||||||
|
|
||||||
|
The `.deb` files will be created in `target/debian/`.
|
||||||
|
|
||||||
|
### Cross-compile for ARM64 (Raspberry Pi, etc.)
|
||||||
|
|
||||||
|
First, install cross-compilation tools:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install gcc-aarch64-linux-gnu libc6-dev-arm64-cross
|
||||||
|
```
|
||||||
|
|
||||||
|
Add the ARM64 target:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
rustup target add aarch64-unknown-linux-gnu
|
||||||
|
```
|
||||||
|
|
||||||
|
Configure the linker by creating `.cargo/config.toml`:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[target.aarch64-unknown-linux-gnu]
|
||||||
|
linker = "aarch64-linux-gnu-gcc"
|
||||||
|
```
|
||||||
|
|
||||||
|
Build the packages:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build for ARM64
|
||||||
|
cargo deb --package socktop --target aarch64-unknown-linux-gnu
|
||||||
|
cargo deb --package socktop_agent --target aarch64-unknown-linux-gnu
|
||||||
|
```
|
||||||
|
|
||||||
|
## Installing Packages
|
||||||
|
|
||||||
|
### Install socktop TUI client
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo dpkg -i socktop_*.deb
|
||||||
|
```
|
||||||
|
|
||||||
|
### Install socktop_agent daemon
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo dpkg -i socktop_agent_*.deb
|
||||||
|
```
|
||||||
|
|
||||||
|
The agent package will:
|
||||||
|
- Create a `socktop` system user and group
|
||||||
|
- Install the binary to `/usr/bin/socktop_agent`
|
||||||
|
- Install a systemd service file (disabled by default)
|
||||||
|
- Create `/var/lib/socktop` for state files
|
||||||
|
|
||||||
|
### Enable and start the agent service
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Enable to start on boot
|
||||||
|
sudo systemctl enable socktop-agent
|
||||||
|
|
||||||
|
# Start the service
|
||||||
|
sudo systemctl start socktop-agent
|
||||||
|
|
||||||
|
# Check status
|
||||||
|
sudo systemctl status socktop-agent
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configure the agent
|
||||||
|
|
||||||
|
Edit the systemd service to customize settings:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo systemctl edit socktop-agent
|
||||||
|
```
|
||||||
|
|
||||||
|
Add configuration in the override section:
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[Service]
|
||||||
|
Environment=SOCKTOP_PORT=8080
|
||||||
|
Environment=SOCKTOP_TOKEN=your-secret-token
|
||||||
|
Environment=RUST_LOG=info
|
||||||
|
```
|
||||||
|
|
||||||
|
Then restart:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo systemctl restart socktop-agent
|
||||||
|
```
|
||||||
|
|
||||||
|
## GitHub Actions
|
||||||
|
|
||||||
|
The project includes a GitHub Actions workflow (`.github/workflows/build-deb.yml`) that automatically builds `.deb` packages for both x86_64 and ARM64 architectures on every push to master or when tags are created.
|
||||||
|
|
||||||
|
### Downloading pre-built packages
|
||||||
|
|
||||||
|
1. Go to the [Actions tab](https://github.com/jasonwitty/socktop/actions)
|
||||||
|
2. Click on the latest "Build Debian Packages" workflow run
|
||||||
|
3. Download the artifacts:
|
||||||
|
- `debian-packages-amd64` - x86_64 packages
|
||||||
|
- `debian-packages-arm64` - ARM64 packages
|
||||||
|
- `all-debian-packages` - All packages combined
|
||||||
|
- `checksums` - SHA256 checksums
|
||||||
|
|
||||||
|
### Release packages
|
||||||
|
|
||||||
|
When you create a git tag starting with `v` (e.g., `v1.50.0`), the workflow will automatically create a GitHub Release with all `.deb` packages attached.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git tag v1.50.0
|
||||||
|
git push origin v1.50.0
|
||||||
|
```
|
||||||
|
|
||||||
|
## Package Details
|
||||||
|
|
||||||
|
### socktop package
|
||||||
|
|
||||||
|
- **Binary**: `/usr/bin/socktop`
|
||||||
|
- **Documentation**: `/usr/share/doc/socktop/`
|
||||||
|
- **Size**: ~5-8 MB (depends on architecture)
|
||||||
|
|
||||||
|
### socktop_agent package
|
||||||
|
|
||||||
|
- **Binary**: `/usr/bin/socktop_agent`
|
||||||
|
- **Service**: `socktop-agent.service`
|
||||||
|
- **User/Group**: `socktop`
|
||||||
|
- **State directory**: `/var/lib/socktop`
|
||||||
|
- **Config directory**: `/etc/socktop` (created but empty by default)
|
||||||
|
- **Documentation**: `/usr/share/doc/socktop_agent/`
|
||||||
|
- **Size**: ~5-8 MB (depends on architecture)
|
||||||
|
|
||||||
|
## Uninstalling
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Remove packages but keep configuration
|
||||||
|
sudo apt remove socktop socktop_agent
|
||||||
|
|
||||||
|
# Remove packages and all configuration (purge)
|
||||||
|
sudo apt purge socktop socktop_agent
|
||||||
|
```
|
||||||
|
|
||||||
|
When purging `socktop_agent`, the following are removed:
|
||||||
|
- The `socktop` user and group
|
||||||
|
- `/var/lib/socktop` directory
|
||||||
|
- Empty `/etc/socktop` directory (if empty)
|
||||||
|
|
||||||
|
## Verifying Packages
|
||||||
|
|
||||||
|
Check package contents:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dpkg -c socktop_*.deb
|
||||||
|
dpkg -c socktop_agent_*.deb
|
||||||
|
```
|
||||||
|
|
||||||
|
Check package information:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dpkg -I socktop_*.deb
|
||||||
|
dpkg -I socktop_agent_*.deb
|
||||||
|
```
|
||||||
|
|
||||||
|
After installation, verify files:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dpkg -L socktop
|
||||||
|
dpkg -L socktop-agent
|
||||||
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Service fails to start
|
||||||
|
|
||||||
|
Check logs:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo journalctl -u socktop-agent -f
|
||||||
|
```
|
||||||
|
|
||||||
|
Verify the socktop user exists:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
id socktop
|
||||||
|
```
|
||||||
|
|
||||||
|
### Permission issues
|
||||||
|
|
||||||
|
Ensure the state directory has correct permissions:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo chown -R socktop:socktop /var/lib/socktop
|
||||||
|
sudo chmod 755 /var/lib/socktop
|
||||||
|
```
|
||||||
|
|
||||||
|
### Missing dependencies
|
||||||
|
|
||||||
|
If installation fails due to missing dependencies:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt --fix-broken install
|
||||||
|
```
|
||||||
|
|
||||||
|
## Creating a Local APT Repository (Advanced)
|
||||||
|
|
||||||
|
To create your own APT repository for easy installation:
|
||||||
|
|
||||||
|
1. Install required tools:
|
||||||
|
```bash
|
||||||
|
sudo apt install dpkg-dev
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Create repository structure:
|
||||||
|
```bash
|
||||||
|
mkdir -p ~/socktop-repo/pool/main
|
||||||
|
cp *.deb ~/socktop-repo/pool/main/
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Generate package index:
|
||||||
|
```bash
|
||||||
|
cd ~/socktop-repo
|
||||||
|
dpkg-scanpackages pool/main /dev/null | gzip -9c > pool/main/Packages.gz
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Serve via HTTP (for testing):
|
||||||
|
```bash
|
||||||
|
cd ~/socktop-repo
|
||||||
|
python3 -m http.server 8000
|
||||||
|
```
|
||||||
|
|
||||||
|
5. Add to sources on client machines:
|
||||||
|
```bash
|
||||||
|
echo "deb [trusted=yes] http://your-server:8000 pool/main/" | \
|
||||||
|
sudo tee /etc/apt/sources.list.d/socktop.list
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install socktop socktop-agent
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
When adding new features that affect packaging:
|
||||||
|
|
||||||
|
1. Update `Cargo.toml` metadata in the `[package.metadata.deb]` section
|
||||||
|
2. Add new assets to the `assets` array if needed
|
||||||
|
3. Update maintainer scripts in `socktop_agent/debian/` if needed
|
||||||
|
4. Test package building locally before committing
|
||||||
|
5. Update this documentation
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
- [cargo-deb documentation](https://github.com/kornelski/cargo-deb)
|
||||||
|
- [Debian Policy Manual](https://www.debian.org/doc/debian-policy/)
|
||||||
|
- [systemd service files](https://www.freedesktop.org/software/systemd/man/systemd.service.html)
|
||||||
+13
-10
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
This guide explains how to cross-compile the socktop_agent on various host systems and deploy it to a Raspberry Pi. Cross-compiling is particularly useful for older or resource-constrained Pi models where native compilation might be slow.
|
This guide explains how to cross-compile the socktop_agent on various host systems and deploy it to a Raspberry Pi. Cross-compiling is particularly useful for older or resource-constrained Pi models where native compilation might be slow.
|
||||||
|
|
||||||
|
**Note:** GPU monitoring support is not available on ARMv7 (32-bit) and RISC-V architectures due to library limitations. When building for these platforms, the `--no-default-features` flag must be used to disable GPU support.
|
||||||
|
|
||||||
## Cross-Compilation Host Setup
|
## Cross-Compilation Host Setup
|
||||||
|
|
||||||
Choose your host operating system:
|
Choose your host operating system:
|
||||||
@@ -23,8 +25,9 @@ sudo apt update
|
|||||||
sudo apt install gcc-aarch64-linux-gnu libc6-dev-arm64-cross libdrm-dev:arm64
|
sudo apt install gcc-aarch64-linux-gnu libc6-dev-arm64-cross libdrm-dev:arm64
|
||||||
|
|
||||||
# For 32-bit Raspberry Pi (armv7)
|
# For 32-bit Raspberry Pi (armv7)
|
||||||
|
# Note: GPU support not available on armv7
|
||||||
sudo apt update
|
sudo apt update
|
||||||
sudo apt install gcc-arm-linux-gnueabihf libc6-dev-armhf-cross libdrm-dev:armhf
|
sudo apt install gcc-arm-linux-gnueabihf libc6-dev-armhf-cross
|
||||||
```
|
```
|
||||||
|
|
||||||
### Setup Rust Cross-Compilation Targets
|
### Setup Rust Cross-Compilation Targets
|
||||||
@@ -65,9 +68,8 @@ sudo pacman -S aarch64-linux-gnu-gcc
|
|||||||
yay -S aarch64-linux-gnu-libdrm
|
yay -S aarch64-linux-gnu-libdrm
|
||||||
|
|
||||||
# For 32-bit Raspberry Pi (armv7)
|
# For 32-bit Raspberry Pi (armv7)
|
||||||
|
# Note: GPU support not available on armv7
|
||||||
sudo pacman -S arm-linux-gnueabihf-gcc
|
sudo pacman -S arm-linux-gnueabihf-gcc
|
||||||
# Install libdrm for armv7 using an AUR helper
|
|
||||||
yay -S arm-linux-gnueabihf-libdrm
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Setup Rust Cross-Compilation Targets
|
### Setup Rust Cross-Compilation Targets
|
||||||
@@ -114,8 +116,8 @@ cd path/to/socktop
|
|||||||
# For 64-bit Raspberry Pi
|
# For 64-bit Raspberry Pi
|
||||||
docker run --rm -it -v "$(pwd)":/home/rust/src messense/rust-musl-cross:aarch64-musl cargo build --release --target aarch64-unknown-linux-musl -p socktop_agent
|
docker run --rm -it -v "$(pwd)":/home/rust/src messense/rust-musl-cross:aarch64-musl cargo build --release --target aarch64-unknown-linux-musl -p socktop_agent
|
||||||
|
|
||||||
# For 32-bit Raspberry Pi
|
# For 32-bit Raspberry Pi (without GPU support)
|
||||||
docker run --rm -it -v "$(pwd)":/home/rust/src messense/rust-musl-cross:armv7-musleabihf cargo build --release --target armv7-unknown-linux-musleabihf -p socktop_agent
|
docker run --rm -it -v "$(pwd)":/home/rust/src messense/rust-musl-cross:armv7-musleabihf cargo build --release --target armv7-unknown-linux-musleabihf -p socktop_agent --no-default-features
|
||||||
```
|
```
|
||||||
|
|
||||||
The compiled binaries will be available in your local target directory.
|
The compiled binaries will be available in your local target directory.
|
||||||
@@ -133,11 +135,11 @@ The recommended approach for Windows is to use Windows Subsystem for Linux (WSL2
|
|||||||
After setting up your environment, build the socktop_agent for your target Raspberry Pi:
|
After setting up your environment, build the socktop_agent for your target Raspberry Pi:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# For 64-bit Raspberry Pi
|
# For 64-bit Raspberry Pi (with GPU support)
|
||||||
cargo build --release --target aarch64-unknown-linux-gnu -p socktop_agent
|
cargo build --release --target aarch64-unknown-linux-gnu -p socktop_agent
|
||||||
|
|
||||||
# For 32-bit Raspberry Pi
|
# For 32-bit Raspberry Pi (without GPU support)
|
||||||
cargo build --release --target armv7-unknown-linux-gnueabihf -p socktop_agent
|
cargo build --release --target armv7-unknown-linux-gnueabihf -p socktop_agent --no-default-features
|
||||||
```
|
```
|
||||||
|
|
||||||
## Transfer the Binary to Your Raspberry Pi
|
## Transfer the Binary to Your Raspberry Pi
|
||||||
@@ -161,11 +163,12 @@ SSH into your Raspberry Pi and install the required dependencies:
|
|||||||
```bash
|
```bash
|
||||||
ssh pi@raspberry-pi-ip
|
ssh pi@raspberry-pi-ip
|
||||||
|
|
||||||
# For Raspberry Pi OS (Debian-based)
|
# For Raspberry Pi OS (Debian-based) - 64-bit only
|
||||||
|
# (32-bit armv7 builds don't require these)
|
||||||
sudo apt update
|
sudo apt update
|
||||||
sudo apt install libdrm-dev libdrm-amdgpu1
|
sudo apt install libdrm-dev libdrm-amdgpu1
|
||||||
|
|
||||||
# For Arch Linux ARM
|
# For Arch Linux ARM - 64-bit only
|
||||||
sudo pacman -Syu
|
sudo pacman -Syu
|
||||||
sudo pacman -S libdrm
|
sudo pacman -S libdrm
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
socktop_agent: TLS enabled. Listening on wss://0.0.0.0:8433/ws
|
|
||||||
socktop_agent: TLS enabled. Listening on wss://0.0.0.0:8433/ws
|
|
||||||
socktop_agent: TLS enabled. Listening on wss://0.0.0.0:8433/ws
|
|
||||||
Error: Address already in use (os error 98)
|
|
||||||
socktop_agent: TLS enabled. Listening on wss://0.0.0.0:8433/ws
|
|
||||||
Error: Address already in use (os error 98)
|
|
||||||
socktop_agent: TLS enabled. Listening on wss://0.0.0.0:8443/ws
|
|
||||||
socktop_agent: TLS enabled. Listening on wss://0.0.0.0:8443/ws
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
# Sync this repo to the 'gitea' remote as a mirror.
|
||||||
|
# - Mirrors ALL refs (branches, tags) and prunes removed ones.
|
||||||
|
# - This makes the Gitea repo match GitHub exactly.
|
||||||
|
|
||||||
|
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||||
|
echo "Error: not inside a git repo" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! git remote get-url gitea >/dev/null 2>&1; then
|
||||||
|
echo "Missing 'gitea' remote. Add it with:" >&2
|
||||||
|
echo " git remote add gitea https://gt.wittyoneoff.com/jason/socktop.git" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Fetching from origin (pruning)..."
|
||||||
|
git fetch origin --prune --tags
|
||||||
|
|
||||||
|
echo "Pushing mirror to gitea..."
|
||||||
|
git push gitea --mirror
|
||||||
|
|
||||||
|
echo "Done: Gitea should now match origin (GitHub)."
|
||||||
|
|
||||||
+23
-3
@@ -1,15 +1,17 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "socktop"
|
name = "socktop"
|
||||||
version = "1.40.0"
|
version = "1.50.0"
|
||||||
authors = ["Jason Witty <jasonpwitty+socktop@proton.me>"]
|
authors = ["Jason Witty <jasonpwitty+socktop@proton.me>"]
|
||||||
description = "Remote system monitor over WebSocket, TUI like top"
|
description = "Remote system monitor over WebSocket, TUI like top"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
homepage = "https://github.com/jasonwitty/socktop"
|
||||||
|
repository = "https://github.com/jasonwitty/socktop"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# socktop connector for agent communication
|
# socktop connector for agent communication
|
||||||
socktop_connector = { path = "../socktop_connector" }
|
socktop_connector = "1.50.0"
|
||||||
|
|
||||||
tokio = { workspace = true }
|
tokio = { workspace = true }
|
||||||
futures-util = { workspace = true }
|
futures-util = { workspace = true }
|
||||||
@@ -18,10 +20,28 @@ serde_json = { workspace = true }
|
|||||||
url = { workspace = true }
|
url = { workspace = true }
|
||||||
ratatui = { workspace = true }
|
ratatui = { workspace = true }
|
||||||
crossterm = { workspace = true }
|
crossterm = { workspace = true }
|
||||||
|
unicode-width = { workspace = true }
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
dirs-next = { workspace = true }
|
dirs-next = { workspace = true }
|
||||||
sysinfo = { workspace = true }
|
sysinfo = { workspace = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
assert_cmd = "2.0"
|
assert_cmd = "2.0"
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
|
|
||||||
|
[package.metadata.deb]
|
||||||
|
maintainer = "Jason Witty <jasonpwitty+socktop@proton.me>"
|
||||||
|
copyright = "2024, Jason Witty <jasonpwitty+socktop@proton.me>"
|
||||||
|
license-file = ["../LICENSE", "4"]
|
||||||
|
extended-description = """\
|
||||||
|
socktop is a remote system monitor with a rich terminal user interface (TUI) \
|
||||||
|
that connects to remote hosts running the socktop_agent over WebSocket. \
|
||||||
|
It provides real-time monitoring of CPU, memory, processes, and more with \
|
||||||
|
an interface similar to the traditional 'top' command."""
|
||||||
|
depends = "$auto"
|
||||||
|
section = "admin"
|
||||||
|
priority = "optional"
|
||||||
|
assets = [
|
||||||
|
["target/release/socktop", "usr/bin/", "755"],
|
||||||
|
["../README.md", "usr/share/doc/socktop/", "644"],
|
||||||
|
]
|
||||||
|
|||||||
+637
-189
File diff suppressed because it is too large
Load Diff
+16
-7
@@ -2,16 +2,25 @@
|
|||||||
|
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
pub fn push_capped<T>(dq: &mut VecDeque<T>, v: T, cap: usize) {
|
/// Push a value into a capped deque. Returns the evicted front element if any.
|
||||||
if dq.len() == cap {
|
/// Callers maintaining a running sum can use this to update the sum without
|
||||||
dq.pop_front();
|
/// re-iterating the whole deque.
|
||||||
}
|
pub fn push_capped<T>(dq: &mut VecDeque<T>, v: T, cap: usize) -> Option<T> {
|
||||||
|
let evicted = if dq.len() == cap {
|
||||||
|
dq.pop_front()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
dq.push_back(v);
|
dq.push_back(v);
|
||||||
|
evicted
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keeps a history deque per core with a fixed capacity
|
// Keeps a history deque per core with a fixed capacity.
|
||||||
|
// Storage is u64 so sparkline rendering can hand the slice directly to
|
||||||
|
// ratatui's `Sparkline::data` (which takes `&[u64]`) without per-frame
|
||||||
|
// allocation or widening conversion.
|
||||||
pub struct PerCoreHistory {
|
pub struct PerCoreHistory {
|
||||||
pub deques: Vec<VecDeque<u16>>,
|
pub deques: Vec<VecDeque<u64>>,
|
||||||
cap: usize,
|
cap: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +44,7 @@ impl PerCoreHistory {
|
|||||||
pub fn push_samples(&mut self, samples: &[f32]) {
|
pub fn push_samples(&mut self, samples: &[f32]) {
|
||||||
self.ensure_cores(samples.len());
|
self.ensure_cores(samples.len());
|
||||||
for (i, v) in samples.iter().enumerate() {
|
for (i, v) in samples.iter().enumerate() {
|
||||||
let val = v.clamp(0.0, 100.0).round() as u16;
|
let val = v.clamp(0.0, 100.0).round() as u64;
|
||||||
push_capped(&mut self.deques[i], val, self.cap);
|
push_capped(&mut self.deques[i], val, self.cap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+77
-12
@@ -22,6 +22,7 @@ pub(crate) struct ParsedArgs {
|
|||||||
metrics_interval_ms: Option<u64>,
|
metrics_interval_ms: Option<u64>,
|
||||||
processes_interval_ms: Option<u64>,
|
processes_interval_ms: Option<u64>,
|
||||||
verify_hostname: bool,
|
verify_hostname: bool,
|
||||||
|
compact: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Result<ParsedArgs, String> {
|
pub(crate) fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Result<ParsedArgs, String> {
|
||||||
@@ -36,11 +37,12 @@ pub(crate) fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Result<Pars
|
|||||||
let mut metrics_interval_ms: Option<u64> = None;
|
let mut metrics_interval_ms: Option<u64> = None;
|
||||||
let mut processes_interval_ms: Option<u64> = None;
|
let mut processes_interval_ms: Option<u64> = None;
|
||||||
let mut verify_hostname = false;
|
let mut verify_hostname = false;
|
||||||
|
let mut compact = false;
|
||||||
while let Some(arg) = it.next() {
|
while let Some(arg) = it.next() {
|
||||||
match arg.as_str() {
|
match arg.as_str() {
|
||||||
"-h" | "--help" => {
|
"-h" | "--help" => {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"Usage: {prog} [--tls-ca CERT_PEM|-t CERT_PEM] [--verify-hostname] [--profile NAME|-P NAME] [--save] [--demo] [--metrics-interval-ms N] [--processes-interval-ms N] [ws://HOST:PORT/ws]\n"
|
"Usage: {prog} [--tls-ca CERT_PEM|-t CERT_PEM] [--verify-hostname] [--profile NAME|-P NAME] [--save] [--demo] [--compact] [--metrics-interval-ms N] [--processes-interval-ms N] [ws://HOST:PORT/ws]\n"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
"--tls-ca" | "-t" => {
|
"--tls-ca" | "-t" => {
|
||||||
@@ -61,6 +63,11 @@ pub(crate) fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Result<Pars
|
|||||||
"--demo" => {
|
"--demo" => {
|
||||||
demo = true;
|
demo = true;
|
||||||
}
|
}
|
||||||
|
"--compact" => {
|
||||||
|
// Force the small-window layout at any terminal size. Without it the
|
||||||
|
// layout switches on its own once the window gets too short.
|
||||||
|
compact = true;
|
||||||
|
}
|
||||||
"--dry-run" => {
|
"--dry-run" => {
|
||||||
// intentionally undocumented
|
// intentionally undocumented
|
||||||
dry_run = true;
|
dry_run = true;
|
||||||
@@ -100,7 +107,7 @@ pub(crate) fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Result<Pars
|
|||||||
url = Some(arg);
|
url = Some(arg);
|
||||||
} else {
|
} else {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"Unexpected argument. Usage: {prog} [--tls-ca CERT_PEM|-t CERT_PEM] [--verify-hostname] [--profile NAME|-P NAME] [--save] [--demo] [ws://HOST:PORT/ws]"
|
"Unexpected argument. Usage: {prog} [--tls-ca CERT_PEM|-t CERT_PEM] [--verify-hostname] [--profile NAME|-P NAME] [--save] [--demo] [--compact] [ws://HOST:PORT/ws]"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,6 +123,7 @@ pub(crate) fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Result<Pars
|
|||||||
metrics_interval_ms,
|
metrics_interval_ms,
|
||||||
processes_interval_ms,
|
processes_interval_ms,
|
||||||
verify_hostname,
|
verify_hostname,
|
||||||
|
compact,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +144,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if parsed.demo || matches!(parsed.profile.as_deref(), Some("demo")) {
|
if parsed.demo || matches!(parsed.profile.as_deref(), Some("demo")) {
|
||||||
return run_demo_mode(parsed.tls_ca.as_deref()).await;
|
return run_demo_mode(parsed.tls_ca.as_deref(), parsed.compact).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
let profiles_file = load_profiles();
|
let profiles_file = load_profiles();
|
||||||
@@ -241,7 +249,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
if (1..=names.len()).contains(&idx) {
|
if (1..=names.len()).contains(&idx) {
|
||||||
let name = &names[idx - 1];
|
let name = &names[idx - 1];
|
||||||
if name == "demo" {
|
if name == "demo" {
|
||||||
return run_demo_mode(parsed.tls_ca.as_deref()).await;
|
return run_demo_mode(parsed.tls_ca.as_deref(), parsed.compact).await;
|
||||||
}
|
}
|
||||||
if let Some(entry) = profiles_mut.profiles.get(name) {
|
if let Some(entry) = profiles_mut.profiles.get(name) {
|
||||||
(
|
(
|
||||||
@@ -301,7 +309,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
);
|
);
|
||||||
eprintln!("If you don't have an agent running, you can try the demo mode.");
|
eprintln!("If you don't have an agent running, you can try the demo mode.");
|
||||||
if prompt_yes_no("Would you like to start the demo mode now? [Y/n]: ") {
|
if prompt_yes_no("Would you like to start the demo mode now? [Y/n]: ") {
|
||||||
return run_demo_mode(parsed.tls_ca.as_deref()).await;
|
return run_demo_mode(parsed.tls_ca.as_deref(), parsed.compact).await;
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Aborting. You can run 'socktop --help' for usage information.");
|
eprintln!("Aborting. You can run 'socktop --help' for usage information.");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -315,7 +323,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let has_token = url.contains("token=");
|
let has_token = url.contains("token=");
|
||||||
let mut app = App::new()
|
let mut app = App::new()
|
||||||
.with_intervals(metrics_interval_ms, processes_interval_ms)
|
.with_intervals(metrics_interval_ms, processes_interval_ms)
|
||||||
.with_status(is_tls, has_token);
|
.with_status(is_tls, has_token)
|
||||||
|
.with_compact(parsed.compact);
|
||||||
if parsed.dry_run {
|
if parsed.dry_run {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@@ -379,11 +388,23 @@ fn gather_intervals(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Demo mode implementation
|
// Demo mode implementation
|
||||||
async fn run_demo_mode(_tls_ca: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
|
async fn run_demo_mode(
|
||||||
|
_tls_ca: Option<&str>,
|
||||||
|
compact: bool,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let port = 3231;
|
let port = 3231;
|
||||||
let url = format!("ws://127.0.0.1:{port}/ws");
|
let url = format!("ws://127.0.0.1:{port}/ws");
|
||||||
let child = spawn_demo_agent(port)?;
|
let child = match spawn_demo_agent(port) {
|
||||||
let mut app = App::new();
|
Ok(child) => child,
|
||||||
|
// The agent ships as its own binary, so a missing one is a setup problem,
|
||||||
|
// not a crash: tell the user how to fix it instead of dumping an io error.
|
||||||
|
Err(e @ DemoAgentError::NotFound(_)) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
Err(e) => return Err(e.into()),
|
||||||
|
};
|
||||||
|
let mut app = App::new().with_compact(compact);
|
||||||
// Demo mode connects to localhost, so disable hostname verification
|
// Demo mode connects to localhost, so disable hostname verification
|
||||||
tokio::select! { res=app.run(&url,None,false)=>{ drop(child); res } _=tokio::signal::ctrl_c()=>{ drop(child); Ok(()) } }
|
tokio::select! { res=app.run(&url,None,false)=>{ drop(child); res } _=tokio::signal::ctrl_c()=>{ drop(child); Ok(()) } }
|
||||||
}
|
}
|
||||||
@@ -399,9 +420,50 @@ impl Drop for DemoGuard {
|
|||||||
eprintln!("Stopped demo agent on port {}", self.port);
|
eprintln!("Stopped demo agent on port {}", self.port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn spawn_demo_agent(port: u16) -> Result<DemoGuard, Box<dyn std::error::Error>> {
|
#[derive(Debug)]
|
||||||
|
enum DemoAgentError {
|
||||||
|
/// The socktop_agent executable could not be located.
|
||||||
|
NotFound(std::path::PathBuf),
|
||||||
|
Io(std::io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for DemoAgentError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::NotFound(candidate) => write!(
|
||||||
|
f,
|
||||||
|
"Could not start demo mode: '{}' was not found{}.\n\
|
||||||
|
\n\
|
||||||
|
Demo mode runs a local agent, which is shipped as a separate binary\n\
|
||||||
|
and is not installed alongside the socktop TUI. Install it with:\n\
|
||||||
|
\n cargo install socktop_agent\n\n\
|
||||||
|
then run socktop again. See {} for other install options.",
|
||||||
|
candidate.display(),
|
||||||
|
// A bare file name means find_agent_executable() fell back to a PATH lookup.
|
||||||
|
if candidate.parent().is_none_or(|p| p.as_os_str().is_empty()) {
|
||||||
|
" on your PATH"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
|
env!("CARGO_PKG_HOMEPAGE"),
|
||||||
|
),
|
||||||
|
Self::Io(e) => write!(f, "Could not start demo mode: {e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for DemoAgentError {
|
||||||
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||||
|
match self {
|
||||||
|
Self::NotFound(_) => None,
|
||||||
|
Self::Io(e) => Some(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_demo_agent(port: u16) -> Result<DemoGuard, DemoAgentError> {
|
||||||
let candidate = find_agent_executable();
|
let candidate = find_agent_executable();
|
||||||
let mut cmd = std::process::Command::new(candidate);
|
let mut cmd = std::process::Command::new(&candidate);
|
||||||
cmd.arg("--port").arg(port.to_string());
|
cmd.arg("--port").arg(port.to_string());
|
||||||
cmd.env("SOCKTOP_ENABLE_SSL", "0");
|
cmd.env("SOCKTOP_ENABLE_SSL", "0");
|
||||||
|
|
||||||
@@ -409,7 +471,10 @@ fn spawn_demo_agent(port: u16) -> Result<DemoGuard, Box<dyn std::error::Error>>
|
|||||||
//cmd.env("SOCKTOP_AGENT_GPU", "0");
|
//cmd.env("SOCKTOP_AGENT_GPU", "0");
|
||||||
//cmd.env("SOCKTOP_AGENT_TEMP", "0");
|
//cmd.env("SOCKTOP_AGENT_TEMP", "0");
|
||||||
|
|
||||||
let child = cmd.spawn()?;
|
let child = cmd.spawn().map_err(|e| match e.kind() {
|
||||||
|
std::io::ErrorKind::NotFound => DemoAgentError::NotFound(candidate),
|
||||||
|
_ => DemoAgentError::Io(e),
|
||||||
|
})?;
|
||||||
std::thread::sleep(std::time::Duration::from_millis(300));
|
std::thread::sleep(std::time::Duration::from_millis(300));
|
||||||
Ok(DemoGuard {
|
Ok(DemoGuard {
|
||||||
port,
|
port,
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
+350
-66
@@ -7,11 +7,17 @@ use ratatui::style::{Color, Style};
|
|||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Constraint, Direction, Layout, Rect},
|
layout::{Constraint, Direction, Layout, Rect},
|
||||||
text::{Line, Span},
|
text::{Line, Span},
|
||||||
widgets::{Block, Borders, Paragraph, Sparkline},
|
widgets::{
|
||||||
|
Block, Borders, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState, Sparkline,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::history::PerCoreHistory;
|
use crate::history::PerCoreHistory;
|
||||||
use crate::types::Metrics;
|
use crate::types::Metrics;
|
||||||
|
use crate::ui::fit::{cols, pick_pair};
|
||||||
|
|
||||||
|
/// Columns kept clear between the CPU title and the temperature readout.
|
||||||
|
const TITLE_GAP: u16 = 2;
|
||||||
|
|
||||||
/// State for dragging the scrollbar thumb
|
/// State for dragging the scrollbar thumb
|
||||||
#[derive(Clone, Copy, Debug, Default)]
|
#[derive(Clone, Copy, Debug, Default)]
|
||||||
@@ -42,8 +48,8 @@ pub fn per_core_content_area(area: Rect) -> Rect {
|
|||||||
/// Handles key events for per-core CPU bars.
|
/// Handles key events for per-core CPU bars.
|
||||||
pub fn per_core_handle_key(scroll_offset: &mut usize, key: KeyEvent, page_size: usize) {
|
pub fn per_core_handle_key(scroll_offset: &mut usize, key: KeyEvent, page_size: usize) {
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Up => *scroll_offset = scroll_offset.saturating_sub(1),
|
KeyCode::Left => *scroll_offset = scroll_offset.saturating_sub(1),
|
||||||
KeyCode::Down => *scroll_offset = scroll_offset.saturating_add(1),
|
KeyCode::Right => *scroll_offset = scroll_offset.saturating_add(1),
|
||||||
KeyCode::PageUp => {
|
KeyCode::PageUp => {
|
||||||
let step = page_size.max(1);
|
let step = page_size.max(1);
|
||||||
*scroll_offset = scroll_offset.saturating_sub(step);
|
*scroll_offset = scroll_offset.saturating_sub(step);
|
||||||
@@ -133,11 +139,9 @@ pub fn per_core_handle_scrollbar_mouse(
|
|||||||
}
|
}
|
||||||
let thumb_len = (track * view).div_ceil(total).max(1).min(track);
|
let thumb_len = (track * view).div_ceil(total).max(1).min(track);
|
||||||
let top_for_offset = |off: usize| -> usize {
|
let top_for_offset = |off: usize| -> usize {
|
||||||
if max_off == 0 {
|
((track - thumb_len) * off + max_off / 2)
|
||||||
0
|
.checked_div(max_off)
|
||||||
} else {
|
.unwrap_or(0)
|
||||||
((track - thumb_len) * off + max_off / 2) / max_off
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let thumb_top = top_for_offset(offset);
|
let thumb_top = top_for_offset(offset);
|
||||||
|
|
||||||
@@ -190,11 +194,9 @@ pub fn per_core_handle_scrollbar_mouse(
|
|||||||
// Inverse mapping top -> offset
|
// Inverse mapping top -> offset
|
||||||
if track > thumb_len {
|
if track > thumb_len {
|
||||||
let denom = track - thumb_len;
|
let denom = track - thumb_len;
|
||||||
offset = if max_off == 0 {
|
offset = (new_top * max_off + denom / 2)
|
||||||
0
|
.checked_div(denom)
|
||||||
} else {
|
.unwrap_or(0);
|
||||||
(new_top * max_off + denom / 2) / denom
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
}
|
}
|
||||||
@@ -234,26 +236,117 @@ pub fn per_core_clamp(scroll_offset: &mut usize, total_rows: usize, viewport_row
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Draws the CPU average sparkline graph.
|
/// Draws the CPU average sparkline graph.
|
||||||
|
///
|
||||||
|
/// `hist_sum` is the running sum of `hist` maintained by the caller so we don't
|
||||||
|
/// fold the (up to 600-element) deque on every frame.
|
||||||
pub fn draw_cpu_avg_graph(
|
pub fn draw_cpu_avg_graph(
|
||||||
f: &mut ratatui::Frame<'_>,
|
f: &mut ratatui::Frame<'_>,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
hist: &std::collections::VecDeque<u64>,
|
hist: &mut std::collections::VecDeque<u64>,
|
||||||
|
hist_sum: u64,
|
||||||
m: Option<&Metrics>,
|
m: Option<&Metrics>,
|
||||||
) {
|
) {
|
||||||
let title = if let Some(mm) = m {
|
let avg_cpu = if hist.is_empty() {
|
||||||
format!("CPU avg (now: {:>5.1}%)", mm.cpu_total)
|
0.0
|
||||||
} else {
|
} else {
|
||||||
"CPU avg".into()
|
hist_sum as f64 / hist.len() as f64
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let (title, top_right_info) = cpu_title_for_width(
|
||||||
|
m.map(|mm| mm.cpu_total),
|
||||||
|
avg_cpu,
|
||||||
|
m.and_then(|mm| mm.cpu_temp_c),
|
||||||
|
area.width,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Hand a slice directly to Sparkline. `make_contiguous` is amortized cheap
|
||||||
|
// for our usage pattern (cap'd 600-element ring updated at 2 Hz) and lets
|
||||||
|
// us skip the per-frame Vec allocation .collect() used to do.
|
||||||
let max_points = area.width.saturating_sub(2) as usize;
|
let max_points = area.width.saturating_sub(2) as usize;
|
||||||
let start = hist.len().saturating_sub(max_points);
|
let start = hist.len().saturating_sub(max_points);
|
||||||
let data: Vec<u64> = hist.iter().skip(start).cloned().collect();
|
let slice = &hist.make_contiguous()[start..];
|
||||||
|
|
||||||
let spark = Sparkline::default()
|
let spark = Sparkline::default()
|
||||||
.block(Block::default().borders(Borders::ALL).title(title))
|
.block(Block::default().borders(Borders::ALL).title(title))
|
||||||
.data(&data)
|
.data(slice)
|
||||||
.max(100)
|
.max(100)
|
||||||
.style(Style::default().fg(Color::Cyan));
|
.style(Style::default().fg(Color::Cyan));
|
||||||
f.render_widget(spark, area);
|
f.render_widget(spark, area);
|
||||||
|
|
||||||
|
// Temperature overlays the top border, right-aligned inside the corner. The title
|
||||||
|
// above is sized so the two cannot collide.
|
||||||
|
if !top_right_info.is_empty() {
|
||||||
|
let w = cols(&top_right_info);
|
||||||
|
let info_area = Rect {
|
||||||
|
x: area.x + area.width.saturating_sub(w + 1),
|
||||||
|
y: area.y,
|
||||||
|
width: w,
|
||||||
|
height: 1,
|
||||||
|
};
|
||||||
|
let info_line = Line::from(Span::raw(top_right_info));
|
||||||
|
f.render_widget(Paragraph::new(info_line), info_area);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Health glyph for a CPU temperature.
|
||||||
|
fn temp_icon(t: f32) -> &'static str {
|
||||||
|
if t < 50.0 {
|
||||||
|
"😎"
|
||||||
|
} else if t < 85.0 {
|
||||||
|
"⚠️"
|
||||||
|
} else {
|
||||||
|
"🔥"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Chooses the CPU pane's title and its right-aligned temperature readout for a pane
|
||||||
|
/// `width` columns wide.
|
||||||
|
///
|
||||||
|
/// Both are painted onto the pane's top border, so without a shared budget the
|
||||||
|
/// temperature simply overwrites the tail of the title on a narrow pane. Detail is given
|
||||||
|
/// up in this order: the `CPU Temp:` label, then the `now:`/`avg:` labels, then the
|
||||||
|
/// average reading, then the decimal on the temperature, and only last the temperature
|
||||||
|
/// itself — the readings are what the pane is for, but a thermal warning is worth more
|
||||||
|
/// than a second decimal place.
|
||||||
|
fn cpu_title_for_width(
|
||||||
|
cpu_now: Option<f32>,
|
||||||
|
avg_cpu: f64,
|
||||||
|
temp_c: Option<f32>,
|
||||||
|
width: u16,
|
||||||
|
) -> (String, String) {
|
||||||
|
let Some(now) = cpu_now else {
|
||||||
|
return ("CPU avg".into(), String::new());
|
||||||
|
};
|
||||||
|
|
||||||
|
// Two borders, plus a column of breathing room at each end of the title.
|
||||||
|
let budget = width.saturating_sub(4);
|
||||||
|
|
||||||
|
let labelled = format!("CPU (now: {now:>5.1}% | avg: {avg_cpu:>5.1}%)");
|
||||||
|
let bare = format!("CPU ({now:.1}% | {avg_cpu:.1}%)");
|
||||||
|
let now_only = format!("CPU ({now:.1}%)");
|
||||||
|
|
||||||
|
let (temp_labelled, temp_plain, temp_coarse) = match temp_c {
|
||||||
|
Some(t) => {
|
||||||
|
let icon = temp_icon(t);
|
||||||
|
(
|
||||||
|
format!("CPU Temp: {t:.1}°C {icon}"),
|
||||||
|
format!("{t:.1}°C {icon}"),
|
||||||
|
format!("{t:.0}°C {icon}"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
None => ("CPU Temp: N/A".into(), "N/A".into(), "N/A".into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let ladder = [
|
||||||
|
(labelled.as_str(), temp_labelled.as_str()),
|
||||||
|
(labelled.as_str(), temp_plain.as_str()),
|
||||||
|
(bare.as_str(), temp_plain.as_str()),
|
||||||
|
(bare.as_str(), temp_coarse.as_str()),
|
||||||
|
(now_only.as_str(), temp_coarse.as_str()),
|
||||||
|
(now_only.as_str(), ""),
|
||||||
|
];
|
||||||
|
let (title, temp) = pick_pair(budget, TITLE_GAP, &ladder);
|
||||||
|
(title.to_string(), temp.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws the per-core CPU bars with sparklines and trends.
|
/// Draws the per-core CPU bars with sparklines and trends.
|
||||||
@@ -261,7 +354,7 @@ pub fn draw_per_core_bars(
|
|||||||
f: &mut ratatui::Frame<'_>,
|
f: &mut ratatui::Frame<'_>,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
m: Option<&Metrics>,
|
m: Option<&Metrics>,
|
||||||
per_core_hist: &PerCoreHistory,
|
per_core_hist: &mut PerCoreHistory,
|
||||||
scroll_offset: usize,
|
scroll_offset: usize,
|
||||||
) {
|
) {
|
||||||
f.render_widget(
|
f.render_widget(
|
||||||
@@ -306,7 +399,7 @@ pub fn draw_per_core_bars(
|
|||||||
let rect = vchunks[i];
|
let rect = vchunks[i];
|
||||||
let hchunks = Layout::default()
|
let hchunks = Layout::default()
|
||||||
.direction(Direction::Horizontal)
|
.direction(Direction::Horizontal)
|
||||||
.constraints([Constraint::Min(6), Constraint::Length(12)])
|
.constraints([Constraint::Min(6), Constraint::Length(13)])
|
||||||
.split(rect);
|
.split(rect);
|
||||||
|
|
||||||
let curr = mm.cpu_per_core[idx].clamp(0.0, 100.0);
|
let curr = mm.cpu_per_core[idx].clamp(0.0, 100.0);
|
||||||
@@ -317,12 +410,17 @@ pub fn draw_per_core_bars(
|
|||||||
.map(|v| v as f32)
|
.map(|v| v as f32)
|
||||||
.unwrap_or(curr);
|
.unwrap_or(curr);
|
||||||
|
|
||||||
|
// Trend indicator. Various Unicode glyphs we tried for the "flat"
|
||||||
|
// trend (╌, ·) substituted as a hyphen on terminals with narrow font
|
||||||
|
// coverage; combined with the next column being `100.0` they read as
|
||||||
|
// `cpu0 -100.0%`, a nonsensical negative percent. Use a literal space
|
||||||
|
// for the flat case — no character, no fallback, no confusion.
|
||||||
let trend = if curr > older + 0.2 {
|
let trend = if curr > older + 0.2 {
|
||||||
"↑"
|
"↑"
|
||||||
} else if curr + 0.2 < older {
|
} else if curr + 0.2 < older {
|
||||||
"↓"
|
"↓"
|
||||||
} else {
|
} else {
|
||||||
"╌"
|
" "
|
||||||
};
|
};
|
||||||
|
|
||||||
let fg = match curr {
|
let fg = match curr {
|
||||||
@@ -331,24 +429,24 @@ pub fn draw_per_core_bars(
|
|||||||
_ => Color::Red,
|
_ => Color::Red,
|
||||||
};
|
};
|
||||||
|
|
||||||
let hist: Vec<u64> = per_core_hist
|
// Borrow the per-core deque mutably so we can hand a contiguous slice
|
||||||
.deques
|
// to Sparkline without allocating a fresh Vec each frame.
|
||||||
.get(idx)
|
if let Some(d) = per_core_hist.deques.get_mut(idx) {
|
||||||
.map(|d| {
|
let max_points = hchunks[0].width as usize;
|
||||||
let max_points = hchunks[0].width as usize;
|
let start = d.len().saturating_sub(max_points);
|
||||||
let start = d.len().saturating_sub(max_points);
|
let slice = &d.make_contiguous()[start..];
|
||||||
d.iter().skip(start).map(|&v| v as u64).collect()
|
let spark = Sparkline::default()
|
||||||
})
|
.data(slice)
|
||||||
.unwrap_or_default();
|
.max(100)
|
||||||
|
.style(Style::default().fg(fg));
|
||||||
|
f.render_widget(spark, hchunks[0]);
|
||||||
|
}
|
||||||
|
|
||||||
let spark = Sparkline::default()
|
// Hard space between the trend mark and the number — even if the
|
||||||
.data(&hist)
|
// arrow glyphs (↑/↓) fall back to ASCII on a terminal that lacks
|
||||||
.max(100)
|
// them, this space prevents the trend mark from visually joining
|
||||||
.style(Style::default().fg(fg));
|
// `100.0` to look like a negative value.
|
||||||
|
let label = format!("cpu{idx:<2}{trend} {curr:>5.1}%");
|
||||||
f.render_widget(spark, hchunks[0]);
|
|
||||||
|
|
||||||
let label = format!("cpu{idx:<2}{trend}{curr:>5.1}%");
|
|
||||||
let line = Line::from(Span::styled(
|
let line = Line::from(Span::styled(
|
||||||
label,
|
label,
|
||||||
Style::default().fg(fg).add_modifier(Modifier::BOLD),
|
Style::default().fg(fg).add_modifier(Modifier::BOLD),
|
||||||
@@ -356,38 +454,224 @@ pub fn draw_per_core_bars(
|
|||||||
f.render_widget(Paragraph::new(line).right_aligned(), hchunks[1]);
|
f.render_widget(Paragraph::new(line).right_aligned(), hchunks[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Custom 1-col scrollbar with arrows, track, and exact mapping
|
// 1-col scrollbar (ratatui built-in widget). Skips drawing when the
|
||||||
|
// content fits in the viewport, matching the previous behaviour.
|
||||||
let scroll_area = Rect {
|
let scroll_area = Rect {
|
||||||
x: inner.x + inner.width.saturating_sub(1),
|
x: inner.x + inner.width.saturating_sub(1),
|
||||||
y: inner.y,
|
y: inner.y,
|
||||||
width: 1,
|
width: 1,
|
||||||
height: inner.height,
|
height: inner.height,
|
||||||
};
|
};
|
||||||
if scroll_area.height >= 3 {
|
let max_off = total_rows.saturating_sub(viewport_rows);
|
||||||
let track = (scroll_area.height - 2) as usize;
|
if scroll_area.height >= 3 && max_off > 0 {
|
||||||
let total = total_rows.max(1);
|
let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
|
||||||
let view = viewport_rows.clamp(1, total);
|
.begin_symbol(Some("▲"))
|
||||||
let max_off = total.saturating_sub(view);
|
.end_symbol(Some("▼"))
|
||||||
|
.thumb_symbol("█")
|
||||||
let thumb_len = (track * view).div_ceil(total).max(1).min(track);
|
.track_symbol(Some("│"))
|
||||||
let thumb_top = if max_off == 0 {
|
.thumb_style(Style::default().fg(SB_THUMB))
|
||||||
0
|
.track_style(Style::default().fg(SB_TRACK))
|
||||||
} else {
|
.begin_style(Style::default().fg(SB_ARROW))
|
||||||
((track - thumb_len) * offset + max_off / 2) / max_off
|
.end_style(Style::default().fg(SB_ARROW));
|
||||||
};
|
let mut state = ScrollbarState::new(max_off).position(offset);
|
||||||
|
f.render_stateful_widget(scrollbar, scroll_area, &mut state);
|
||||||
// Build lines: top arrow, track (with thumb), bottom arrow
|
}
|
||||||
let mut lines: Vec<Line> = Vec::with_capacity(scroll_area.height as usize);
|
}
|
||||||
lines.push(Line::from(Span::styled("▲", Style::default().fg(SB_ARROW))));
|
|
||||||
for i in 0..track {
|
#[cfg(test)]
|
||||||
if i >= thumb_top && i < thumb_top + thumb_len {
|
mod title_tests {
|
||||||
lines.push(Line::from(Span::styled("█", Style::default().fg(SB_THUMB))));
|
use super::*;
|
||||||
} else {
|
|
||||||
lines.push(Line::from(Span::styled("│", Style::default().fg(SB_TRACK))));
|
/// The defect this replaces: the temperature was painted over the title's tail on a
|
||||||
}
|
/// narrow pane. Whatever the width, the two must fit side by side on the border.
|
||||||
}
|
#[test]
|
||||||
lines.push(Line::from(Span::styled("▼", Style::default().fg(SB_ARROW))));
|
fn title_and_temperature_never_overlap() {
|
||||||
|
for width in 0..=200u16 {
|
||||||
f.render_widget(Paragraph::new(lines), scroll_area);
|
let (title, temp) = cpu_title_for_width(Some(3.4), 12.7, Some(43.0), width);
|
||||||
|
let budget = width.saturating_sub(4);
|
||||||
|
if temp.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
cols(&title) + cols(&temp) + TITLE_GAP <= budget,
|
||||||
|
"width {width}: {title:?} + {temp:?} do not fit in {budget} columns"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The current CPU reading is the one thing the pane must always show.
|
||||||
|
#[test]
|
||||||
|
fn the_current_reading_always_survives() {
|
||||||
|
for width in 20..=200u16 {
|
||||||
|
let (title, _) = cpu_title_for_width(Some(3.4), 12.7, Some(43.0), width);
|
||||||
|
assert!(
|
||||||
|
title.contains("3.4"),
|
||||||
|
"width {width}: lost the reading ({title:?})"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The ladder from the design: temp label, then now/avg labels, then the average,
|
||||||
|
/// then the temperature's decimal, then the temperature.
|
||||||
|
#[test]
|
||||||
|
fn detail_is_dropped_in_priority_order() {
|
||||||
|
let at = |w| cpu_title_for_width(Some(0.7), 1.3, Some(43.0), w);
|
||||||
|
|
||||||
|
let (title, temp) = at(80);
|
||||||
|
assert_eq!(title, "CPU (now: 0.7% | avg: 1.3%)");
|
||||||
|
assert_eq!(temp, "CPU Temp: 43.0°C 😎");
|
||||||
|
|
||||||
|
// The "CPU Temp:" label goes first; the readings keep their labels.
|
||||||
|
let (title, temp) = at(50);
|
||||||
|
assert_eq!(title, "CPU (now: 0.7% | avg: 1.3%)");
|
||||||
|
assert_eq!(temp, "43.0°C 😎");
|
||||||
|
|
||||||
|
// Then the now:/avg: labels.
|
||||||
|
let (title, temp) = at(40);
|
||||||
|
assert_eq!(title, "CPU (0.7% | 1.3%)");
|
||||||
|
assert_eq!(temp, "43.0°C 😎");
|
||||||
|
|
||||||
|
// Then the temperature's decimal.
|
||||||
|
let (title, temp) = at(31);
|
||||||
|
assert_eq!(title, "CPU (0.7% | 1.3%)");
|
||||||
|
assert_eq!(temp, "43°C 😎");
|
||||||
|
|
||||||
|
// Then the average reading.
|
||||||
|
let (title, temp) = at(26);
|
||||||
|
assert_eq!(title, "CPU (0.7%)");
|
||||||
|
assert_eq!(temp, "43°C 😎");
|
||||||
|
|
||||||
|
// Last of all, the temperature itself.
|
||||||
|
let (title, temp) = at(15);
|
||||||
|
assert_eq!(title, "CPU (0.7%)");
|
||||||
|
assert_eq!(temp, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A hot CPU has to stay visible as a warning, so the glyph rides along with the
|
||||||
|
/// reading at every tier that shows a temperature at all.
|
||||||
|
#[test]
|
||||||
|
fn the_thermal_glyph_tracks_the_temperature() {
|
||||||
|
for (t, icon) in [(43.0, "😎"), (70.0, "⚠️"), (92.0, "🔥")] {
|
||||||
|
for width in 26..=80u16 {
|
||||||
|
let (_, temp) = cpu_title_for_width(Some(0.7), 1.3, Some(t), width);
|
||||||
|
assert!(
|
||||||
|
temp.contains(icon),
|
||||||
|
"width {width} at {t}°C: expected {icon} in {temp:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An agent that reports no temperature must not leave a stray label behind.
|
||||||
|
#[test]
|
||||||
|
fn a_missing_temperature_degrades_to_nothing() {
|
||||||
|
let (_, temp) = cpu_title_for_width(Some(0.7), 1.3, None, 80);
|
||||||
|
assert_eq!(temp, "CPU Temp: N/A");
|
||||||
|
let (_, temp) = cpu_title_for_width(Some(0.7), 1.3, None, 14);
|
||||||
|
assert_eq!(temp, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Before the first payload arrives there are no readings to show.
|
||||||
|
#[test]
|
||||||
|
fn no_metrics_yet_shows_the_placeholder() {
|
||||||
|
let (title, temp) = cpu_title_for_width(None, 0.0, None, 80);
|
||||||
|
assert_eq!(title, "CPU avg");
|
||||||
|
assert!(temp.is_empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod render_tests {
|
||||||
|
use super::*;
|
||||||
|
use ratatui::Terminal;
|
||||||
|
use ratatui::backend::TestBackend;
|
||||||
|
use socktop_connector::Metrics;
|
||||||
|
|
||||||
|
fn fake_metrics(cores: Vec<f32>) -> Metrics {
|
||||||
|
Metrics {
|
||||||
|
cpu_total: 0.0,
|
||||||
|
cpu_per_core: cores,
|
||||||
|
mem_total: 1024,
|
||||||
|
mem_used: 0,
|
||||||
|
swap_total: 0,
|
||||||
|
swap_used: 0,
|
||||||
|
hostname: "t".into(),
|
||||||
|
cpu_temp_c: None,
|
||||||
|
disks: vec![],
|
||||||
|
networks: vec![],
|
||||||
|
top_processes: vec![],
|
||||||
|
gpus: None,
|
||||||
|
process_count: Some(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dump(terminal: &Terminal<TestBackend>) -> String {
|
||||||
|
let buf = terminal.backend().buffer();
|
||||||
|
let mut out = String::new();
|
||||||
|
for y in 0..buf.area().height {
|
||||||
|
for x in 0..buf.area().width {
|
||||||
|
out.push_str(buf[(x, y)].symbol());
|
||||||
|
}
|
||||||
|
out.push('\n');
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Regression: the "flat" trend glyph used to be `╌` (U+254C), then `·`
|
||||||
|
/// (U+00B7) — both substituted as a hyphen on terminals with narrow font
|
||||||
|
/// coverage. When a core sat at exactly 100% the label rendered as
|
||||||
|
/// `cpu3 -100.0%` (no space between trend and digits). Now we use a
|
||||||
|
/// literal space for the flat case AND insert a hard space between every
|
||||||
|
/// trend mark and the number, so no glyph substitution can produce a
|
||||||
|
/// "-100" substring. We assert that across flat AND transitioning cores.
|
||||||
|
#[test]
|
||||||
|
fn percore_label_never_renders_as_negative() {
|
||||||
|
let m = fake_metrics(vec![100.0, 100.0, 100.0, 100.0]);
|
||||||
|
let mut hist = PerCoreHistory::new(60);
|
||||||
|
hist.ensure_cores(4);
|
||||||
|
// First sample: history is empty, no trend on first frame.
|
||||||
|
hist.push_samples(&m.cpu_per_core);
|
||||||
|
// Second sample: identical values → flat trend (the user's complaint).
|
||||||
|
hist.push_samples(&m.cpu_per_core);
|
||||||
|
|
||||||
|
let backend = TestBackend::new(120, 8);
|
||||||
|
let mut terminal = Terminal::new(backend).unwrap();
|
||||||
|
terminal
|
||||||
|
.draw(|f| {
|
||||||
|
draw_per_core_bars(f, Rect::new(0, 0, 120, 8), Some(&m), &mut hist, 0);
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let out = dump(&terminal);
|
||||||
|
eprintln!("---flat 100% render---\n{out}");
|
||||||
|
assert!(!out.contains("-100"), "found '-100' in flat-trend render");
|
||||||
|
|
||||||
|
// Decreasing trend at saturation: hist was high, current drops a bit.
|
||||||
|
let mut hist2 = PerCoreHistory::new(60);
|
||||||
|
hist2.ensure_cores(4);
|
||||||
|
for _ in 0..25 {
|
||||||
|
hist2.push_samples(&[100.0, 100.0, 100.0, 100.0]);
|
||||||
|
}
|
||||||
|
let m2 = fake_metrics(vec![100.0, 100.0, 100.0, 80.0]);
|
||||||
|
hist2.push_samples(&m2.cpu_per_core);
|
||||||
|
|
||||||
|
let backend = TestBackend::new(120, 8);
|
||||||
|
let mut terminal = Terminal::new(backend).unwrap();
|
||||||
|
terminal
|
||||||
|
.draw(|f| {
|
||||||
|
draw_per_core_bars(f, Rect::new(0, 0, 120, 8), Some(&m2), &mut hist2, 0);
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let out = dump(&terminal);
|
||||||
|
eprintln!("---decreasing render---\n{out}");
|
||||||
|
assert!(
|
||||||
|
!out.contains("-100"),
|
||||||
|
"found '-100' in decreasing-trend render"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!out.contains("-80"),
|
||||||
|
"found '-80' in decreasing-trend render"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-6
@@ -24,6 +24,9 @@ pub fn draw_disks(f: &mut ratatui::Frame<'_>, area: Rect, m: Option<&Metrics>) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deduplication is performed once on the App side when fresh disk data
|
||||||
|
// arrives (disks poll cadence is 5s, draw cadence is ~500ms, so doing it
|
||||||
|
// here would rebuild a HashSet ~10x per refresh for no reason).
|
||||||
let per_disk_h = 3u16;
|
let per_disk_h = 3u16;
|
||||||
let max_cards = (inner.height / per_disk_h).min(mm.disks.len() as u16) as usize;
|
let max_cards = (inner.height / per_disk_h).min(mm.disks.len() as u16) as usize;
|
||||||
|
|
||||||
@@ -53,23 +56,43 @@ pub fn draw_disks(f: &mut ratatui::Frame<'_>, area: Rect, m: Option<&Metrics>) {
|
|||||||
ratatui::style::Color::Red
|
ratatui::style::Color::Red
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add indentation for partitions
|
||||||
|
let indent = if d.is_partition { "└─" } else { "" };
|
||||||
|
|
||||||
|
// Add temperature if available
|
||||||
|
let temp_str = d
|
||||||
|
.temperature
|
||||||
|
.map(|t| format!(" {}°C", t.round() as i32))
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
let title = format!(
|
let title = format!(
|
||||||
"{} {} {} / {} ({}%)",
|
"{}{}{}{} {} / {} ({}%)",
|
||||||
|
indent,
|
||||||
disk_icon(&d.name),
|
disk_icon(&d.name),
|
||||||
truncate_middle(&d.name, (slot.width.saturating_sub(6)) as usize / 2),
|
truncate_middle(&d.name, (slot.width.saturating_sub(6)) as usize / 2),
|
||||||
|
temp_str,
|
||||||
human(used),
|
human(used),
|
||||||
human(d.total),
|
human(d.total),
|
||||||
pct
|
pct
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Indent the entire card (block) for partitions to align with └─ prefix (4 chars)
|
||||||
|
let card_indent = if d.is_partition { 4 } else { 0 };
|
||||||
|
let card_rect = Rect {
|
||||||
|
x: slot.x + card_indent,
|
||||||
|
y: slot.y,
|
||||||
|
width: slot.width.saturating_sub(card_indent),
|
||||||
|
height: slot.height,
|
||||||
|
};
|
||||||
|
|
||||||
let card = Block::default().borders(Borders::ALL).title(title);
|
let card = Block::default().borders(Borders::ALL).title(title);
|
||||||
f.render_widget(card, *slot);
|
f.render_widget(card, card_rect);
|
||||||
|
|
||||||
let inner_card = Rect {
|
let inner_card = Rect {
|
||||||
x: slot.x + 1,
|
x: card_rect.x + 1,
|
||||||
y: slot.y + 1,
|
y: card_rect.y + 1,
|
||||||
width: slot.width.saturating_sub(2),
|
width: card_rect.width.saturating_sub(2),
|
||||||
height: slot.height.saturating_sub(2),
|
height: card_rect.height.saturating_sub(2),
|
||||||
};
|
};
|
||||||
if inner_card.height == 0 {
|
if inner_card.height == 0 {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -0,0 +1,133 @@
|
|||||||
|
//! Fitting text to the columns actually available.
|
||||||
|
//!
|
||||||
|
//! Several panes paint two independent pieces of text onto one row — a left title and a
|
||||||
|
//! right-aligned readout. Nothing reserves space for the right piece, so on a narrow
|
||||||
|
//! terminal the right one is simply painted over the tail of the left one and the title
|
||||||
|
//! is clobbered mid-word. The helpers here let a caller measure in real terminal columns
|
||||||
|
//! and pick the richest wording that still fits, so the two never overlap.
|
||||||
|
//!
|
||||||
|
//! Note that `str::len()` is a byte count and must not be used for this: `⏱` is three
|
||||||
|
//! bytes wide but one column, and `🔒` is four bytes but two columns.
|
||||||
|
|
||||||
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
|
/// Terminal columns `s` occupies, saturating at `u16::MAX`.
|
||||||
|
pub fn cols(s: &str) -> u16 {
|
||||||
|
UnicodeWidthStr::width(s).min(u16::MAX as usize) as u16
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shortens `s` to at most `max` columns, marking the cut with `…`.
|
||||||
|
///
|
||||||
|
/// Cuts on character boundaries and accounts for wide characters, so the result never
|
||||||
|
/// exceeds `max` columns and never splits a multi-byte character.
|
||||||
|
pub fn truncate_cols(s: &str, max: u16) -> String {
|
||||||
|
if cols(s) <= max {
|
||||||
|
return s.to_string();
|
||||||
|
}
|
||||||
|
if max == 0 {
|
||||||
|
return String::new();
|
||||||
|
}
|
||||||
|
// Reserve one column for the ellipsis.
|
||||||
|
let budget = max.saturating_sub(1);
|
||||||
|
let mut used = 0u16;
|
||||||
|
let mut out = String::new();
|
||||||
|
for ch in s.chars() {
|
||||||
|
let w = cols(ch.encode_utf8(&mut [0u8; 4]));
|
||||||
|
if used + w > budget {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
used += w;
|
||||||
|
out.push(ch);
|
||||||
|
}
|
||||||
|
out.push('…');
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Picks the first (richest) candidate pair that fits side by side in `width` columns
|
||||||
|
/// with at least `gap` columns between them.
|
||||||
|
///
|
||||||
|
/// Candidates are ordered most- to least-detailed; the last one is the floor and is
|
||||||
|
/// returned even if it does not fit, so callers always get something to render.
|
||||||
|
pub fn pick_pair<'a>(
|
||||||
|
width: u16,
|
||||||
|
gap: u16,
|
||||||
|
candidates: &[(&'a str, &'a str)],
|
||||||
|
) -> (&'a str, &'a str) {
|
||||||
|
let fits = |left: &str, right: &str| {
|
||||||
|
let needed = cols(left)
|
||||||
|
.saturating_add(cols(right))
|
||||||
|
.saturating_add(if right.is_empty() { 0 } else { gap });
|
||||||
|
needed <= width
|
||||||
|
};
|
||||||
|
for &(left, right) in candidates {
|
||||||
|
if fits(left, right) {
|
||||||
|
return (left, right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
candidates.last().copied().unwrap_or(("", ""))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
/// The bug these helpers exist to prevent: byte length overstates the width of the
|
||||||
|
/// glyphs socktop puts in its header, which is what pushed the right-hand text into
|
||||||
|
/// the title in the first place.
|
||||||
|
#[test]
|
||||||
|
fn cols_counts_columns_not_bytes() {
|
||||||
|
assert_eq!(cols("abc"), 3);
|
||||||
|
// Stopwatch: 3 bytes, 1 column.
|
||||||
|
assert_eq!("⏱".len(), 3);
|
||||||
|
assert_eq!(cols("⏱"), 1);
|
||||||
|
// Lock: 4 bytes, 2 columns.
|
||||||
|
assert_eq!("🔒".len(), 4);
|
||||||
|
assert_eq!(cols("🔒"), 2);
|
||||||
|
assert_eq!(cols("⏱ 500ms metrics | 2000ms procs"), 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncate_respects_the_column_budget() {
|
||||||
|
assert_eq!(truncate_cols("cachyos-gaming", 20), "cachyos-gaming");
|
||||||
|
assert_eq!(truncate_cols("cachyos-gaming", 14), "cachyos-gaming");
|
||||||
|
assert_eq!(truncate_cols("cachyos-gaming", 10), "cachyos-g…");
|
||||||
|
assert_eq!(cols(&truncate_cols("cachyos-gaming", 10)), 10);
|
||||||
|
assert_eq!(truncate_cols("cachyos-gaming", 1), "…");
|
||||||
|
assert_eq!(truncate_cols("cachyos-gaming", 0), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Truncation must never land mid-character or overrun the budget on wide glyphs.
|
||||||
|
#[test]
|
||||||
|
fn truncate_handles_wide_and_multibyte_characters() {
|
||||||
|
for max in 0..12u16 {
|
||||||
|
let out = truncate_cols("🔒🔒🔒 TLS", max);
|
||||||
|
assert!(cols(&out) <= max, "{out:?} exceeds {max} columns");
|
||||||
|
assert!(out.chars().all(|c| c != '\u{fffd}'), "{out:?} split a char");
|
||||||
|
}
|
||||||
|
// A wide glyph that cannot fit beside the ellipsis is dropped whole.
|
||||||
|
assert_eq!(truncate_cols("🔒ab", 2), "…");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pick_pair_takes_the_richest_that_fits() {
|
||||||
|
let candidates = [
|
||||||
|
("full left text", "full right text"),
|
||||||
|
("left text", "right text"),
|
||||||
|
("left", "right"),
|
||||||
|
];
|
||||||
|
assert_eq!(pick_pair(80, 2, &candidates), candidates[0]);
|
||||||
|
assert_eq!(pick_pair(24, 2, &candidates), candidates[1]);
|
||||||
|
assert_eq!(pick_pair(12, 2, &candidates), candidates[2]);
|
||||||
|
// Below the floor the last candidate is still returned.
|
||||||
|
assert_eq!(pick_pair(1, 2, &candidates), candidates[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The gap is what keeps the two pieces from touching; it must not be charged when
|
||||||
|
/// there is no right-hand piece to separate.
|
||||||
|
#[test]
|
||||||
|
fn pick_pair_only_charges_the_gap_when_both_sides_are_present() {
|
||||||
|
let candidates = [("0123456789", "x"), ("0123456789", "")];
|
||||||
|
assert_eq!(pick_pair(11, 2, &candidates), candidates[1]);
|
||||||
|
assert_eq!(pick_pair(13, 2, &candidates), candidates[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -121,3 +121,209 @@ pub fn draw_gpu(f: &mut ratatui::Frame<'_>, area: Rect, m: Option<&Metrics>) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// One-line GPU strip for compact mode: no device name (it is the first thing to lose
|
||||||
|
/// value when rows are scarce), just utilisation and VRAM on the single content row
|
||||||
|
/// between the block borders. Only the first GPU fits; the title says so when there are
|
||||||
|
/// more.
|
||||||
|
pub fn draw_gpu_compact(f: &mut ratatui::Frame<'_>, area: Rect, m: Option<&Metrics>) {
|
||||||
|
let gpus = m.and_then(|mm| mm.gpus.as_ref());
|
||||||
|
let count = gpus.map(|g| g.len()).unwrap_or(0);
|
||||||
|
let title = if count > 1 {
|
||||||
|
format!("GPU (1/{count})")
|
||||||
|
} else {
|
||||||
|
"GPU".to_string()
|
||||||
|
};
|
||||||
|
f.render_widget(Block::default().borders(Borders::ALL).title(title), area);
|
||||||
|
|
||||||
|
if area.height < 3 || area.width <= 2 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let inner = Rect {
|
||||||
|
x: area.x + 1,
|
||||||
|
y: area.y + 1,
|
||||||
|
width: area.width - 2,
|
||||||
|
height: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(g) = gpus.and_then(|v| v.first()) else {
|
||||||
|
f.render_widget(Paragraph::new("No GPUs"), inner);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let util = g.utilization.unwrap_or(0.0).clamp(0.0, 100.0) as u16;
|
||||||
|
let used = g.mem_used.unwrap_or(0);
|
||||||
|
let total = g.mem_total.unwrap_or(1);
|
||||||
|
let mem_ratio = if total > 0 {
|
||||||
|
(used as f64 / total as f64).clamp(0.0, 1.0)
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
};
|
||||||
|
let util_label = format!("util: {util}%");
|
||||||
|
let mem_label = format!(
|
||||||
|
"vram: {}/{} ({}%)",
|
||||||
|
fmt_bytes(used),
|
||||||
|
fmt_bytes(total),
|
||||||
|
(mem_ratio * 100.0).round() as u16
|
||||||
|
);
|
||||||
|
|
||||||
|
// Bars are sized explicitly rather than left to stretch: an idle bar renders as
|
||||||
|
// empty cells, so a full-width one turns into a long blank run between two labels.
|
||||||
|
const MIN_GAUGE_W: u16 = 6;
|
||||||
|
const MAX_GAUGE_W: u16 = 24;
|
||||||
|
let labels_w = util_label.len() as u16 + mem_label.len() as u16 + 4; // one space each side
|
||||||
|
let gauge_w = inner
|
||||||
|
.width
|
||||||
|
.saturating_sub(labels_w)
|
||||||
|
.min(2 * MAX_GAUGE_W)
|
||||||
|
.div_euclid(2);
|
||||||
|
|
||||||
|
// Too narrow for bars worth drawing: keep the numbers, drop the bars.
|
||||||
|
if gauge_w < MIN_GAUGE_W {
|
||||||
|
f.render_widget(
|
||||||
|
Paragraph::new(Span::raw(format!("{util_label} {mem_label}")))
|
||||||
|
.style(Style::default().fg(Color::Gray)),
|
||||||
|
inner,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Each label leads its own bar. Bar-then-label (as the tall panel does) is ambiguous
|
||||||
|
// on a single line: with an idle bar rendering empty, the next pair's fill ends up
|
||||||
|
// flush against the previous pair's text and reads as belonging to it.
|
||||||
|
let mut x = inner.x;
|
||||||
|
let mut place = |w: u16| {
|
||||||
|
let r = Rect {
|
||||||
|
x,
|
||||||
|
y: inner.y,
|
||||||
|
width: w,
|
||||||
|
height: 1,
|
||||||
|
};
|
||||||
|
x += w;
|
||||||
|
r
|
||||||
|
};
|
||||||
|
let util_rect = place(util_label.len() as u16 + 2);
|
||||||
|
let util_bar = place(gauge_w);
|
||||||
|
let mem_rect = place(mem_label.len() as u16 + 2);
|
||||||
|
let mem_bar = place(gauge_w);
|
||||||
|
|
||||||
|
let label = |text: &str| {
|
||||||
|
Paragraph::new(Span::raw(format!(" {text} "))).style(Style::default().fg(Color::Gray))
|
||||||
|
};
|
||||||
|
|
||||||
|
f.render_widget(label(&util_label), util_rect);
|
||||||
|
f.render_widget(
|
||||||
|
Gauge::default()
|
||||||
|
.gauge_style(Style::default().fg(Color::Green))
|
||||||
|
.label(Span::raw(""))
|
||||||
|
.ratio(util as f64 / 100.0),
|
||||||
|
util_bar,
|
||||||
|
);
|
||||||
|
f.render_widget(label(&mem_label), mem_rect);
|
||||||
|
f.render_widget(
|
||||||
|
Gauge::default()
|
||||||
|
.gauge_style(Style::default().fg(Color::LightMagenta))
|
||||||
|
.label(Span::raw(""))
|
||||||
|
.ratio(mem_ratio),
|
||||||
|
mem_bar,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod render_tests {
|
||||||
|
use super::*;
|
||||||
|
use ratatui::Terminal;
|
||||||
|
use ratatui::backend::TestBackend;
|
||||||
|
use socktop_connector::{GpuInfo, Metrics};
|
||||||
|
|
||||||
|
fn gpu(name: &str) -> GpuInfo {
|
||||||
|
GpuInfo {
|
||||||
|
name: Some(name.into()),
|
||||||
|
vendor: None,
|
||||||
|
utilization: Some(42.0),
|
||||||
|
mem_used: Some(4_724_464_025),
|
||||||
|
mem_total: Some(17_070_817_280),
|
||||||
|
temp: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn metrics(gpus: Option<Vec<GpuInfo>>) -> Metrics {
|
||||||
|
Metrics {
|
||||||
|
cpu_total: 0.0,
|
||||||
|
cpu_per_core: vec![],
|
||||||
|
mem_total: 1024,
|
||||||
|
mem_used: 0,
|
||||||
|
swap_total: 0,
|
||||||
|
swap_used: 0,
|
||||||
|
hostname: "t".into(),
|
||||||
|
cpu_temp_c: None,
|
||||||
|
disks: vec![],
|
||||||
|
networks: vec![],
|
||||||
|
top_processes: vec![],
|
||||||
|
gpus,
|
||||||
|
process_count: Some(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(width: u16, m: &Metrics) -> String {
|
||||||
|
let mut terminal = Terminal::new(TestBackend::new(width, 3)).unwrap();
|
||||||
|
terminal
|
||||||
|
.draw(|f| draw_gpu_compact(f, Rect::new(0, 0, width, 3), Some(m)))
|
||||||
|
.unwrap();
|
||||||
|
let buf = terminal.backend().buffer();
|
||||||
|
let mut out = String::new();
|
||||||
|
for y in 0..buf.area().height {
|
||||||
|
for x in 0..buf.area().width {
|
||||||
|
out.push_str(buf[(x, y)].symbol());
|
||||||
|
}
|
||||||
|
out.push('\n');
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compact mode drops the device name — the row is one line and the numbers are
|
||||||
|
/// what the space is for.
|
||||||
|
#[test]
|
||||||
|
fn compact_strip_omits_the_device_name() {
|
||||||
|
let m = metrics(Some(vec![gpu("NVIDIA GeForce RTX 5080")]));
|
||||||
|
let out = render(80, &m);
|
||||||
|
assert!(
|
||||||
|
!out.contains("NVIDIA"),
|
||||||
|
"name leaked into compact strip:\n{out}"
|
||||||
|
);
|
||||||
|
assert!(out.contains("util: 42%"), "{out}");
|
||||||
|
assert!(out.contains("vram: 4.4G/15.9G (28%)"), "{out}");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A second GPU cannot fit on one line, so the title has to say the strip is partial
|
||||||
|
/// rather than silently showing only the first card.
|
||||||
|
#[test]
|
||||||
|
fn multiple_gpus_are_flagged_in_the_title() {
|
||||||
|
let one = render(80, &metrics(Some(vec![gpu("a")])));
|
||||||
|
assert!(one.contains("GPU") && !one.contains("1/"), "{one}");
|
||||||
|
|
||||||
|
let two = render(80, &metrics(Some(vec![gpu("a"), gpu("b")])));
|
||||||
|
assert!(two.contains("GPU (1/2)"), "{two}");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Narrow terminals drop the gauges rather than rendering two-cell stubs, but must
|
||||||
|
/// never drop the numbers.
|
||||||
|
#[test]
|
||||||
|
fn narrow_strip_keeps_the_numbers() {
|
||||||
|
let m = metrics(Some(vec![gpu("a")]));
|
||||||
|
for width in [20u16, 30, 40, 47, 48, 80, 200] {
|
||||||
|
let out = render(width, &m);
|
||||||
|
if width >= 40 {
|
||||||
|
assert!(out.contains("util: 42%"), "width {width}:\n{out}");
|
||||||
|
}
|
||||||
|
// No panic, and the block always closes on the last row.
|
||||||
|
assert_eq!(out.lines().count(), 3, "width {width}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn missing_gpu_payload_does_not_panic() {
|
||||||
|
assert!(render(80, &metrics(None)).contains("No GPUs"));
|
||||||
|
assert!(render(80, &metrics(Some(vec![]))).contains("No GPUs"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+226
-46
@@ -1,52 +1,232 @@
|
|||||||
//! Top header with hostname and CPU temperature indicator.
|
//! Top header with hostname, connection status and polling intervals.
|
||||||
|
//!
|
||||||
|
//! The row carries two pieces of text — session identity on the left, polling intervals
|
||||||
|
//! on the right — and both matter. Rather than let the right one overwrite the left when
|
||||||
|
//! they no longer both fit, the header drops detail in priority order: the hostname and
|
||||||
|
//! the intervals are what survive longest, because they are what tells you *which* host
|
||||||
|
//! you are looking at and how fresh the numbers are.
|
||||||
|
|
||||||
use crate::types::Metrics;
|
use crate::ui::fit::{cols, pick_pair, truncate_cols};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::Rect,
|
layout::Rect,
|
||||||
widgets::{Block, Borders},
|
text::{Line, Span},
|
||||||
|
widgets::{Block, Borders, Paragraph},
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
pub fn draw_header(
|
/// Columns kept clear between the left and right halves.
|
||||||
f: &mut ratatui::Frame<'_>,
|
const GAP: u16 = 2;
|
||||||
area: Rect,
|
/// Never shorten the hostname below this before dropping the intervals instead.
|
||||||
m: Option<&Metrics>,
|
const HOSTNAME_FLOOR: u16 = 8;
|
||||||
is_tls: bool,
|
|
||||||
has_token: bool,
|
/// Session state the header renders.
|
||||||
metrics_interval: Duration,
|
#[derive(Clone, Copy)]
|
||||||
procs_interval: Duration,
|
pub struct HeaderState<'a> {
|
||||||
) {
|
pub hostname: Option<&'a str>,
|
||||||
let base = if let Some(mm) = m {
|
pub is_tls: bool,
|
||||||
let temp = mm
|
pub has_token: bool,
|
||||||
.cpu_temp_c
|
pub metrics_ms: u128,
|
||||||
.map(|t| {
|
pub procs_ms: u128,
|
||||||
let icon = if t < 50.0 {
|
}
|
||||||
"😎"
|
|
||||||
} else if t < 85.0 {
|
/// Builds the left and right halves of the header for a row `width` columns wide.
|
||||||
"⚠️"
|
///
|
||||||
} else {
|
/// Detail is dropped in this order as the row narrows: the key hints, then the TLS/token
|
||||||
"🔥"
|
/// badges, then the `socktop — host:` prefix (leaving the bare hostname), then the
|
||||||
};
|
/// `metrics`/`procs` words, and only then is the hostname itself shortened. The two
|
||||||
format!("CPU Temp: {t:.1}°C {icon}")
|
/// halves are always sized to sit side by side, so neither can paint over the other.
|
||||||
})
|
///
|
||||||
.unwrap_or_else(|| "CPU Temp: N/A".into());
|
/// Callers cache the result and rebuild it only when the state or the width changes.
|
||||||
format!("socktop — host: {} | {}", mm.hostname, temp)
|
pub fn build_header(state: HeaderState<'_>, width: u16) -> (String, String) {
|
||||||
} else {
|
let host = state.hostname.unwrap_or("connecting...");
|
||||||
"socktop — connecting...".into()
|
let tls = if state.is_tls {
|
||||||
};
|
"🔒 TLS"
|
||||||
// TLS indicator: lock vs lock with cross (using ✗). Keep explicit label for clarity.
|
} else {
|
||||||
let tls_txt = if is_tls { "🔒 TLS" } else { "🔒✗ TLS" };
|
"🔒✗ TLS"
|
||||||
// Token indicator
|
};
|
||||||
let tok_txt = if has_token { "🔑 token" } else { "" };
|
let badges = if state.has_token {
|
||||||
let mi = metrics_interval.as_millis();
|
format!("{tls} | 🔑 token")
|
||||||
let pi = procs_interval.as_millis();
|
} else {
|
||||||
let intervals = format!("⏱ {mi}ms metrics | {pi}ms procs");
|
tls.to_string()
|
||||||
let mut parts = vec![base, tls_txt.into()];
|
};
|
||||||
if !tok_txt.is_empty() {
|
|
||||||
parts.push(tok_txt.into());
|
let named = format!("socktop — host: {host}");
|
||||||
}
|
let with_badges = format!("{named} | {badges}");
|
||||||
parts.push(intervals);
|
let with_keys = format!("{with_badges} | (a: about, h: help, q: quit)");
|
||||||
parts.push("(q to quit)".into());
|
|
||||||
let title = parts.join(" | ");
|
let intervals = format!(
|
||||||
f.render_widget(Block::default().title(title).borders(Borders::BOTTOM), area);
|
"⏱ {}ms metrics | {}ms procs",
|
||||||
|
state.metrics_ms, state.procs_ms
|
||||||
|
);
|
||||||
|
let intervals_short = format!("⏱ {}ms | {}ms", state.metrics_ms, state.procs_ms);
|
||||||
|
|
||||||
|
// Richest first. The bare hostname is reached before the intervals lose their
|
||||||
|
// labels, and the hostname is only shortened once nothing else is left to give.
|
||||||
|
let ladder = [
|
||||||
|
(with_keys.as_str(), intervals.as_str()),
|
||||||
|
(with_badges.as_str(), intervals.as_str()),
|
||||||
|
(named.as_str(), intervals.as_str()),
|
||||||
|
(host, intervals.as_str()),
|
||||||
|
(host, intervals_short.as_str()),
|
||||||
|
];
|
||||||
|
let (left, right) = pick_pair(width, GAP, &ladder);
|
||||||
|
if cols(left) + cols(right) + GAP <= width {
|
||||||
|
return (left.to_string(), right.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Past the floor of the ladder: shorten the hostname, and give up the intervals only
|
||||||
|
// if even a stub of a hostname will not fit beside them.
|
||||||
|
let room = width
|
||||||
|
.saturating_sub(cols(&intervals_short))
|
||||||
|
.saturating_sub(GAP);
|
||||||
|
if room >= HOSTNAME_FLOOR {
|
||||||
|
return (truncate_cols(host, room), intervals_short);
|
||||||
|
}
|
||||||
|
(truncate_cols(host, width), String::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_header(f: &mut ratatui::Frame<'_>, area: Rect, title: &str, intervals: &str) {
|
||||||
|
f.render_widget(Block::default().title(title).borders(Borders::BOTTOM), area);
|
||||||
|
|
||||||
|
if intervals.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let intervals_width = cols(intervals);
|
||||||
|
if area.width >= intervals_width {
|
||||||
|
let right_area = Rect {
|
||||||
|
x: area.x + area.width - intervals_width,
|
||||||
|
y: area.y,
|
||||||
|
width: intervals_width,
|
||||||
|
height: 1,
|
||||||
|
};
|
||||||
|
f.render_widget(Paragraph::new(Line::from(Span::raw(intervals))), right_area);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn state(hostname: Option<&str>) -> HeaderState<'_> {
|
||||||
|
HeaderState {
|
||||||
|
hostname,
|
||||||
|
is_tls: false,
|
||||||
|
has_token: false,
|
||||||
|
metrics_ms: 500,
|
||||||
|
procs_ms: 2000,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The defect this replaces: the two halves were painted independently, so below
|
||||||
|
/// ~105 columns the right half landed on top of the title. Whatever the width, they
|
||||||
|
/// must now fit side by side.
|
||||||
|
#[test]
|
||||||
|
fn halves_never_overlap_at_any_width() {
|
||||||
|
for width in 0..=200u16 {
|
||||||
|
let (left, right) = build_header(state(Some("cachyos-gaming")), width);
|
||||||
|
let used = cols(&left) + cols(&right);
|
||||||
|
if right.is_empty() {
|
||||||
|
assert!(cols(&left) <= width, "width {width}: {left:?} overflows");
|
||||||
|
} else {
|
||||||
|
assert!(
|
||||||
|
used + GAP <= width,
|
||||||
|
"width {width}: {left:?} + {right:?} = {used} cols, no room for both"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Hostname and intervals are the two things worth keeping; everything else is
|
||||||
|
/// context that can go.
|
||||||
|
#[test]
|
||||||
|
fn hostname_and_intervals_survive_longest() {
|
||||||
|
for width in 34..=200u16 {
|
||||||
|
let (left, right) = build_header(state(Some("cachyos-gaming")), width);
|
||||||
|
assert!(
|
||||||
|
left.contains("cachyos-gaming"),
|
||||||
|
"width {width}: lost the hostname ({left:?})"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
right.contains("500ms") && right.contains("2000ms"),
|
||||||
|
"width {width}: lost the intervals ({right:?})"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The ladder from the design: key hints, then badges, then the prefix, then the
|
||||||
|
/// interval labels, then the hostname itself.
|
||||||
|
#[test]
|
||||||
|
fn detail_is_dropped_in_priority_order() {
|
||||||
|
let s = state(Some("cachyos-gaming"));
|
||||||
|
|
||||||
|
let (left, right) = build_header(s, 120);
|
||||||
|
assert_eq!(
|
||||||
|
left,
|
||||||
|
"socktop — host: cachyos-gaming | 🔒✗ TLS | (a: about, h: help, q: quit)"
|
||||||
|
);
|
||||||
|
assert_eq!(right, "⏱ 500ms metrics | 2000ms procs");
|
||||||
|
|
||||||
|
// Key hints go first.
|
||||||
|
let (left, _) = build_header(s, 80);
|
||||||
|
assert_eq!(left, "socktop — host: cachyos-gaming | 🔒✗ TLS");
|
||||||
|
|
||||||
|
// Then the badges.
|
||||||
|
let (left, _) = build_header(s, 70);
|
||||||
|
assert_eq!(left, "socktop — host: cachyos-gaming");
|
||||||
|
|
||||||
|
// Then the prefix, leaving the bare hostname.
|
||||||
|
let (left, right) = build_header(s, 50);
|
||||||
|
assert_eq!(left, "cachyos-gaming");
|
||||||
|
assert_eq!(right, "⏱ 500ms metrics | 2000ms procs");
|
||||||
|
|
||||||
|
// Then the interval labels.
|
||||||
|
let (left, right) = build_header(s, 34);
|
||||||
|
assert_eq!(left, "cachyos-gaming");
|
||||||
|
assert_eq!(right, "⏱ 500ms | 2000ms");
|
||||||
|
|
||||||
|
// Only then is the hostname itself shortened.
|
||||||
|
// 30 columns - 16 for the short intervals - 2 gap leaves 12 for the hostname.
|
||||||
|
let (left, right) = build_header(s, 30);
|
||||||
|
assert_eq!(left, "cachyos-gam…");
|
||||||
|
assert_eq!(right, "⏱ 500ms | 2000ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A long hostname must not push the intervals off the row.
|
||||||
|
#[test]
|
||||||
|
fn a_long_hostname_is_shortened_rather_than_winning_the_row() {
|
||||||
|
let long = "a-very-long-hostname-that-will-not-fit-anywhere";
|
||||||
|
for width in 30..=100u16 {
|
||||||
|
let (left, right) = build_header(state(Some(long)), width);
|
||||||
|
assert!(!right.is_empty(), "width {width}: intervals were dropped");
|
||||||
|
assert!(cols(&left) + cols(&right) + GAP <= width, "width {width}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Widths too small for both: the hostname is the last thing standing.
|
||||||
|
#[test]
|
||||||
|
fn hostname_is_the_final_survivor() {
|
||||||
|
let (left, right) = build_header(state(Some("cachyos-gaming")), 20);
|
||||||
|
assert!(right.is_empty(), "intervals should have been dropped");
|
||||||
|
assert!(!left.is_empty());
|
||||||
|
assert!(cols(&left) <= 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tls_and_token_badges_appear_when_there_is_room() {
|
||||||
|
let s = HeaderState {
|
||||||
|
hostname: Some("host"),
|
||||||
|
is_tls: true,
|
||||||
|
has_token: true,
|
||||||
|
metrics_ms: 500,
|
||||||
|
procs_ms: 2000,
|
||||||
|
};
|
||||||
|
let (left, _) = build_header(s, 200);
|
||||||
|
assert!(left.contains("🔒 TLS"), "{left}");
|
||||||
|
assert!(left.contains("🔑 token"), "{left}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_missing_hostname_reads_as_connecting() {
|
||||||
|
let (left, _) = build_header(state(None), 120);
|
||||||
|
assert!(left.contains("connecting"), "{left}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,352 @@
|
|||||||
|
//! Root layout computation, shared by the draw path and the input hit-testing paths.
|
||||||
|
//!
|
||||||
|
//! Two modes:
|
||||||
|
//!
|
||||||
|
//! * [`LayoutMode::Normal`] — the full layout. CPU graph and per-core bars on top,
|
||||||
|
//! Memory over Swap on the left with the GPU panel beside them, then Disks and the
|
||||||
|
//! network graphs next to the process table.
|
||||||
|
//!
|
||||||
|
//! * [`LayoutMode::Compact`] — entered when the window is too short for the Disks pane
|
||||||
|
//! to render even one complete disk card. Disks is dropped, Memory and Swap move side
|
||||||
|
//! by side into the space it vacated, the GPU collapses to a single full-width line
|
||||||
|
//! (and disappears entirely when the host has no GPU), and every row reclaimed goes to
|
||||||
|
//! the CPU graph and per-core bars — which in the fixed layout are squeezed to nothing
|
||||||
|
//! long before the rest of the panes stop being useful.
|
||||||
|
|
||||||
|
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||||
|
|
||||||
|
/// Which of the two layouts [`compute`] produced.
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub enum LayoutMode {
|
||||||
|
Normal,
|
||||||
|
Compact,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LayoutMode {
|
||||||
|
pub fn is_compact(self) -> bool {
|
||||||
|
matches!(self, LayoutMode::Compact)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Rows the Disks pane needs before it can show one disk card: the card itself is
|
||||||
|
/// 3 rows (`disks::draw_disks`) plus the pane's own top and bottom border.
|
||||||
|
const DISKS_MIN_H: u16 = 5;
|
||||||
|
|
||||||
|
/// Header line.
|
||||||
|
const HEADER_H: u16 = 1;
|
||||||
|
/// Memory and Swap gauges: 1 content row between borders.
|
||||||
|
const GAUGE_H: u16 = 3;
|
||||||
|
/// A network graph at its preferred height.
|
||||||
|
const NET_H: u16 = 5;
|
||||||
|
|
||||||
|
// Compact-mode budget. The top row is kept at `TOP_MIN_H` (3 content rows between
|
||||||
|
// borders) before the network graphs are allowed to shrink, because restoring the CPU
|
||||||
|
// panes is the entire point of the mode.
|
||||||
|
const TOP_MIN_H: u16 = 5;
|
||||||
|
const BOTTOM_PREF_H: u16 = GAUGE_H + 2 * NET_H;
|
||||||
|
const BOTTOM_MIN_H: u16 = GAUGE_H + 2 * 3;
|
||||||
|
|
||||||
|
/// Every pane rect for one frame. `disks` and `gpu` are `None` when the mode omits them.
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub struct AppLayout {
|
||||||
|
pub mode: LayoutMode,
|
||||||
|
pub header: Rect,
|
||||||
|
pub cpu: Rect,
|
||||||
|
pub per_core: Rect,
|
||||||
|
pub gpu: Option<Rect>,
|
||||||
|
pub mem: Rect,
|
||||||
|
pub swap: Rect,
|
||||||
|
pub disks: Option<Rect>,
|
||||||
|
pub download: Rect,
|
||||||
|
pub upload: Rect,
|
||||||
|
pub procs: Rect,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Splits `area` into pane rects.
|
||||||
|
///
|
||||||
|
/// `force_compact` comes from `--compact` and pins the compact layout at any size.
|
||||||
|
/// `has_gpu` decides whether compact mode reserves its one-line GPU strip; it is false
|
||||||
|
/// until the first metrics payload arrives, so a GPU-less host never reserves the row.
|
||||||
|
pub fn compute(area: Rect, force_compact: bool, has_gpu: bool) -> AppLayout {
|
||||||
|
if force_compact {
|
||||||
|
return compact(area, has_gpu);
|
||||||
|
}
|
||||||
|
let normal = normal(area);
|
||||||
|
match normal.disks {
|
||||||
|
Some(d) if d.height >= DISKS_MIN_H => normal,
|
||||||
|
_ => compact(area, has_gpu),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn split(area: Rect, dir: Direction, constraints: &[Constraint]) -> std::rc::Rc<[Rect]> {
|
||||||
|
Layout::default()
|
||||||
|
.direction(dir)
|
||||||
|
.constraints(constraints)
|
||||||
|
.split(area)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 66/34 split used by every full-width row in the normal layout.
|
||||||
|
fn left_right(area: Rect) -> std::rc::Rc<[Rect]> {
|
||||||
|
split(
|
||||||
|
area,
|
||||||
|
Direction::Horizontal,
|
||||||
|
&[Constraint::Percentage(66), Constraint::Percentage(34)],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn normal(area: Rect) -> AppLayout {
|
||||||
|
let rows = split(
|
||||||
|
area,
|
||||||
|
Direction::Vertical,
|
||||||
|
&[
|
||||||
|
Constraint::Length(HEADER_H), // header
|
||||||
|
Constraint::Ratio(1, 3), // top row
|
||||||
|
Constraint::Length(GAUGE_H), // memory (left) + GPU (right, part 1)
|
||||||
|
Constraint::Length(GAUGE_H), // swap (left) + GPU (right, part 2)
|
||||||
|
Constraint::Min(2 * NET_H), // bottom: disks + net (left), top procs (right)
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
let top = left_right(rows[1]);
|
||||||
|
let mem_lr = left_right(rows[2]);
|
||||||
|
let swap_lr = left_right(rows[3]);
|
||||||
|
|
||||||
|
// GPU spans the same vertical space as Memory + Swap.
|
||||||
|
let gpu = Rect {
|
||||||
|
x: mem_lr[1].x,
|
||||||
|
y: mem_lr[1].y,
|
||||||
|
width: mem_lr[1].width,
|
||||||
|
height: mem_lr[1].height + swap_lr[1].height,
|
||||||
|
};
|
||||||
|
|
||||||
|
let bottom = split(
|
||||||
|
rows[4],
|
||||||
|
Direction::Horizontal,
|
||||||
|
&[Constraint::Percentage(60), Constraint::Percentage(40)],
|
||||||
|
);
|
||||||
|
let left_stack = split(
|
||||||
|
bottom[0],
|
||||||
|
Direction::Vertical,
|
||||||
|
&[
|
||||||
|
Constraint::Min(4), // disks absorbs the slack
|
||||||
|
Constraint::Length(NET_H), // download
|
||||||
|
Constraint::Length(NET_H), // upload
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
AppLayout {
|
||||||
|
mode: LayoutMode::Normal,
|
||||||
|
header: rows[0],
|
||||||
|
cpu: top[0],
|
||||||
|
per_core: top[1],
|
||||||
|
gpu: Some(gpu),
|
||||||
|
mem: mem_lr[0],
|
||||||
|
swap: swap_lr[0],
|
||||||
|
disks: Some(left_stack[0]),
|
||||||
|
download: left_stack[1],
|
||||||
|
upload: left_stack[2],
|
||||||
|
procs: bottom[1],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compact(area: Rect, has_gpu: bool) -> AppLayout {
|
||||||
|
let gpu_h = if has_gpu { GAUGE_H } else { 0 };
|
||||||
|
let avail = area.height.saturating_sub(HEADER_H + gpu_h);
|
||||||
|
|
||||||
|
// Give the top row its floor first, then share any surplus with the bottom so the
|
||||||
|
// process table keeps growing with the window instead of staying pinned at 13 rows.
|
||||||
|
let (top_h, bottom_h) = if avail >= TOP_MIN_H + BOTTOM_PREF_H {
|
||||||
|
let top = TOP_MIN_H + (avail - TOP_MIN_H - BOTTOM_PREF_H) / 2;
|
||||||
|
(top, avail - top)
|
||||||
|
} else if avail >= TOP_MIN_H + BOTTOM_MIN_H {
|
||||||
|
(TOP_MIN_H, avail - TOP_MIN_H)
|
||||||
|
} else {
|
||||||
|
// Smaller than both floors: the network graphs are already at their minimum, so
|
||||||
|
// the top row takes what is left (panes clip below this point).
|
||||||
|
let bottom = BOTTOM_MIN_H.min(avail);
|
||||||
|
(avail - bottom, bottom)
|
||||||
|
};
|
||||||
|
|
||||||
|
let rows = split(
|
||||||
|
area,
|
||||||
|
Direction::Vertical,
|
||||||
|
&[
|
||||||
|
Constraint::Length(HEADER_H),
|
||||||
|
Constraint::Length(top_h),
|
||||||
|
Constraint::Length(gpu_h),
|
||||||
|
Constraint::Length(bottom_h),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
let top = left_right(rows[1]);
|
||||||
|
|
||||||
|
let bottom = split(
|
||||||
|
rows[3],
|
||||||
|
Direction::Horizontal,
|
||||||
|
&[Constraint::Percentage(60), Constraint::Percentage(40)],
|
||||||
|
);
|
||||||
|
// Memory + Swap take the row Disks used to occupy; the graphs share what is left.
|
||||||
|
let left_stack = split(
|
||||||
|
bottom[0],
|
||||||
|
Direction::Vertical,
|
||||||
|
&[
|
||||||
|
Constraint::Length(GAUGE_H),
|
||||||
|
Constraint::Fill(1),
|
||||||
|
Constraint::Fill(1),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
let gauges = split(
|
||||||
|
left_stack[0],
|
||||||
|
Direction::Horizontal,
|
||||||
|
&[Constraint::Percentage(50), Constraint::Percentage(50)],
|
||||||
|
);
|
||||||
|
|
||||||
|
AppLayout {
|
||||||
|
mode: LayoutMode::Compact,
|
||||||
|
header: rows[0],
|
||||||
|
cpu: top[0],
|
||||||
|
per_core: top[1],
|
||||||
|
gpu: has_gpu.then_some(rows[2]),
|
||||||
|
mem: gauges[0],
|
||||||
|
swap: gauges[1],
|
||||||
|
disks: None,
|
||||||
|
download: left_stack[1],
|
||||||
|
upload: left_stack[2],
|
||||||
|
procs: bottom[1],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn area(w: u16, h: u16) -> Rect {
|
||||||
|
Rect::new(0, 0, w, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The height where the normal layout still fits a full disk card. Below it the CPU
|
||||||
|
/// panes are the ones that collapse, which is what compact mode exists to prevent.
|
||||||
|
#[test]
|
||||||
|
fn tall_window_stays_normal() {
|
||||||
|
let l = compute(area(120, 40), false, true);
|
||||||
|
assert_eq!(l.mode, LayoutMode::Normal);
|
||||||
|
assert!(l.disks.expect("disks pane").height >= DISKS_MIN_H);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn short_window_switches_to_compact() {
|
||||||
|
let l = compute(area(120, 24), false, true);
|
||||||
|
assert_eq!(l.mode, LayoutMode::Compact);
|
||||||
|
assert!(l.disks.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The switch happens exactly when Disks can no longer show one card, and never
|
||||||
|
/// oscillates: every height above the crossover is normal, every height below is
|
||||||
|
/// compact.
|
||||||
|
#[test]
|
||||||
|
fn mode_is_monotonic_in_height() {
|
||||||
|
let mut first_normal = None;
|
||||||
|
for h in 10..=60u16 {
|
||||||
|
let mode = compute(area(120, h), false, true).mode;
|
||||||
|
match (mode, first_normal) {
|
||||||
|
(LayoutMode::Normal, None) => first_normal = Some(h),
|
||||||
|
(LayoutMode::Compact, Some(prev)) => {
|
||||||
|
panic!("height {h} went back to compact after normal at {prev}")
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(first_normal.is_some(), "never reached the normal layout");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn force_compact_overrides_a_tall_window() {
|
||||||
|
let l = compute(area(200, 80), true, true);
|
||||||
|
assert_eq!(l.mode, LayoutMode::Compact);
|
||||||
|
assert!(l.disks.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compact_drops_the_gpu_row_without_a_gpu() {
|
||||||
|
let with = compute(area(120, 24), true, true);
|
||||||
|
let without = compute(area(120, 24), true, false);
|
||||||
|
assert!(with.gpu.is_some());
|
||||||
|
assert_eq!(with.gpu.expect("gpu strip").height, GAUGE_H);
|
||||||
|
assert!(without.gpu.is_none());
|
||||||
|
// The rows a GPU-less host saves are shared between the CPU panes and the
|
||||||
|
// bottom half, and none of them are left as a gap.
|
||||||
|
assert!(without.cpu.height > with.cpu.height);
|
||||||
|
assert!(without.procs.height > with.procs.height);
|
||||||
|
assert_eq!(without.procs.y + without.procs.height, 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compact exists to keep the CPU graph and per-core bars drawable: both need
|
||||||
|
/// content rows inside their borders.
|
||||||
|
#[test]
|
||||||
|
fn compact_keeps_the_cpu_panes_drawable() {
|
||||||
|
for h in 18..=32u16 {
|
||||||
|
let l = compute(area(120, h), false, true);
|
||||||
|
assert_eq!(l.mode, LayoutMode::Compact, "height {h}");
|
||||||
|
assert!(
|
||||||
|
l.cpu.height >= TOP_MIN_H,
|
||||||
|
"height {h}: cpu pane only {} rows",
|
||||||
|
l.cpu.height
|
||||||
|
);
|
||||||
|
assert_eq!(l.per_core.height, l.cpu.height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Regression guard for the bug this mode fixes: at 18 rows the old fixed layout
|
||||||
|
/// left the top row with no drawable interior at all.
|
||||||
|
#[test]
|
||||||
|
fn compact_beats_the_fixed_layout_at_18_rows() {
|
||||||
|
let compact = compute(area(120, 18), false, true);
|
||||||
|
let fixed = normal(area(120, 18));
|
||||||
|
assert!(fixed.cpu.height <= 2, "fixed layout unexpectedly usable");
|
||||||
|
assert!(compact.cpu.height > fixed.cpu.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compact_panes_tile_the_area_without_gaps() {
|
||||||
|
for h in 16..=32u16 {
|
||||||
|
for has_gpu in [true, false] {
|
||||||
|
let l = compute(area(120, h), true, has_gpu);
|
||||||
|
assert_eq!(l.header.y, 0);
|
||||||
|
assert_eq!(l.cpu.y, l.header.y + l.header.height);
|
||||||
|
assert_eq!(l.per_core.x, l.cpu.x + l.cpu.width);
|
||||||
|
|
||||||
|
let after_cpu = l.cpu.y + l.cpu.height;
|
||||||
|
let bottom_y = match l.gpu {
|
||||||
|
Some(g) => {
|
||||||
|
assert_eq!(g.y, after_cpu);
|
||||||
|
assert_eq!(g.width, 120, "gpu strip spans the full width");
|
||||||
|
g.y + g.height
|
||||||
|
}
|
||||||
|
None => after_cpu,
|
||||||
|
};
|
||||||
|
assert_eq!(l.mem.y, bottom_y);
|
||||||
|
// Memory and Swap sit side by side on one row.
|
||||||
|
assert_eq!(l.swap.y, l.mem.y);
|
||||||
|
assert_eq!(l.swap.x, l.mem.x + l.mem.width);
|
||||||
|
assert_eq!(l.mem.height, GAUGE_H);
|
||||||
|
assert_eq!(l.download.y, l.mem.y + l.mem.height);
|
||||||
|
assert_eq!(l.upload.y, l.download.y + l.download.height);
|
||||||
|
assert_eq!(l.procs.y, bottom_y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A degenerate size must not panic or produce rects outside the frame.
|
||||||
|
#[test]
|
||||||
|
fn tiny_windows_stay_inside_the_frame() {
|
||||||
|
for h in 0..=16u16 {
|
||||||
|
for w in [0u16, 1, 20, 80] {
|
||||||
|
let l = compute(area(w, h), false, true);
|
||||||
|
for r in [l.header, l.cpu, l.per_core, l.mem, l.swap, l.procs] {
|
||||||
|
assert!(r.y + r.height <= h, "{r:?} escapes height {h}");
|
||||||
|
assert!(r.x + r.width <= w, "{r:?} escapes width {w}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,10 +2,16 @@
|
|||||||
|
|
||||||
pub mod cpu;
|
pub mod cpu;
|
||||||
pub mod disks;
|
pub mod disks;
|
||||||
|
pub mod fit;
|
||||||
pub mod gpu;
|
pub mod gpu;
|
||||||
pub mod header;
|
pub mod header;
|
||||||
|
pub mod layout;
|
||||||
pub mod mem;
|
pub mod mem;
|
||||||
pub mod modal;
|
pub mod modal;
|
||||||
|
pub mod modal_connection;
|
||||||
|
pub mod modal_format;
|
||||||
|
pub mod modal_process;
|
||||||
|
pub mod modal_types;
|
||||||
pub mod net;
|
pub mod net;
|
||||||
pub mod processes;
|
pub mod processes;
|
||||||
pub mod swap;
|
pub mod swap;
|
||||||
|
|||||||
+363
-341
@@ -1,66 +1,29 @@
|
|||||||
//! Modal window system for socktop TUI application
|
//! Modal window system for socktop TUI application
|
||||||
|
|
||||||
use std::time::{Duration, Instant};
|
use super::theme::MODAL_DIM_BG;
|
||||||
|
|
||||||
use super::theme::{
|
|
||||||
BTN_EXIT_BG_ACTIVE, BTN_EXIT_FG_ACTIVE, BTN_EXIT_FG_INACTIVE, BTN_EXIT_TEXT,
|
|
||||||
BTN_RETRY_BG_ACTIVE, BTN_RETRY_FG_ACTIVE, BTN_RETRY_FG_INACTIVE, BTN_RETRY_TEXT, ICON_CLUSTER,
|
|
||||||
ICON_COUNTDOWN_LABEL, ICON_MESSAGE, ICON_OFFLINE_LABEL, ICON_RETRY_LABEL, ICON_WARNING_TITLE,
|
|
||||||
LARGE_ERROR_ICON, MODAL_AGENT_FG, MODAL_BG, MODAL_BORDER_FG, MODAL_COUNTDOWN_LABEL_FG,
|
|
||||||
MODAL_DIM_BG, MODAL_FG, MODAL_HINT_FG, MODAL_ICON_PINK, MODAL_OFFLINE_LABEL_FG,
|
|
||||||
MODAL_RETRY_LABEL_FG, MODAL_TITLE_FG,
|
|
||||||
};
|
|
||||||
use crossterm::event::KeyCode;
|
use crossterm::event::KeyCode;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
Frame,
|
Frame,
|
||||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||||
style::{Color, Modifier, Style},
|
style::{Color, Modifier, Style},
|
||||||
text::{Line, Span, Text},
|
text::Line,
|
||||||
widgets::{Block, Borders, Clear, Paragraph, Wrap},
|
widgets::{Block, Borders, Clear, Paragraph, Wrap},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
// Re-export types from modal_types
|
||||||
pub enum ModalType {
|
pub use super::modal_types::{
|
||||||
ConnectionError {
|
ModalAction, ModalButton, ModalType, ProcessHistoryData, ProcessModalData,
|
||||||
message: String,
|
};
|
||||||
disconnected_at: Instant,
|
|
||||||
retry_count: u32,
|
|
||||||
auto_retry_countdown: Option<u64>,
|
|
||||||
},
|
|
||||||
#[allow(dead_code)]
|
|
||||||
Confirmation {
|
|
||||||
title: String,
|
|
||||||
message: String,
|
|
||||||
confirm_text: String,
|
|
||||||
cancel_text: String,
|
|
||||||
},
|
|
||||||
#[allow(dead_code)]
|
|
||||||
Info { title: String, message: String },
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub enum ModalAction {
|
|
||||||
None,
|
|
||||||
RetryConnection,
|
|
||||||
ExitApp,
|
|
||||||
Confirm,
|
|
||||||
Cancel,
|
|
||||||
Dismiss,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub enum ModalButton {
|
|
||||||
Retry,
|
|
||||||
Exit,
|
|
||||||
Confirm,
|
|
||||||
Cancel,
|
|
||||||
Ok,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ModalManager {
|
pub struct ModalManager {
|
||||||
stack: Vec<ModalType>,
|
stack: Vec<ModalType>,
|
||||||
active_button: ModalButton,
|
pub(super) active_button: ModalButton,
|
||||||
|
pub thread_scroll_offset: usize,
|
||||||
|
pub journal_scroll_offset: usize,
|
||||||
|
pub thread_scroll_max: usize,
|
||||||
|
pub journal_scroll_max: usize,
|
||||||
|
pub help_scroll_offset: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModalManager {
|
impl ModalManager {
|
||||||
@@ -68,16 +31,39 @@ impl ModalManager {
|
|||||||
Self {
|
Self {
|
||||||
stack: Vec::new(),
|
stack: Vec::new(),
|
||||||
active_button: ModalButton::Retry,
|
active_button: ModalButton::Retry,
|
||||||
|
thread_scroll_offset: 0,
|
||||||
|
journal_scroll_offset: 0,
|
||||||
|
thread_scroll_max: 0,
|
||||||
|
journal_scroll_max: 0,
|
||||||
|
help_scroll_offset: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn is_active(&self) -> bool {
|
pub fn is_active(&self) -> bool {
|
||||||
!self.stack.is_empty()
|
!self.stack.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn current_modal(&self) -> Option<&ModalType> {
|
||||||
|
self.stack.last()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn push_modal(&mut self, modal: ModalType) {
|
pub fn push_modal(&mut self, modal: ModalType) {
|
||||||
self.stack.push(modal);
|
self.stack.push(modal);
|
||||||
self.active_button = match self.stack.last() {
|
self.active_button = match self.stack.last() {
|
||||||
Some(ModalType::ConnectionError { .. }) => ModalButton::Retry,
|
Some(ModalType::ConnectionError { .. }) => ModalButton::Retry,
|
||||||
|
Some(ModalType::ProcessDetails { .. }) => {
|
||||||
|
// Reset scroll state for new process details
|
||||||
|
self.thread_scroll_offset = 0;
|
||||||
|
self.journal_scroll_offset = 0;
|
||||||
|
self.thread_scroll_max = 0;
|
||||||
|
self.journal_scroll_max = 0;
|
||||||
|
ModalButton::Ok
|
||||||
|
}
|
||||||
|
Some(ModalType::About) => ModalButton::Ok,
|
||||||
|
Some(ModalType::Help) => {
|
||||||
|
// Reset scroll state for help modal
|
||||||
|
self.help_scroll_offset = 0;
|
||||||
|
ModalButton::Ok
|
||||||
|
}
|
||||||
Some(ModalType::Confirmation { .. }) => ModalButton::Confirm,
|
Some(ModalType::Confirmation { .. }) => ModalButton::Confirm,
|
||||||
Some(ModalType::Info { .. }) => ModalButton::Ok,
|
Some(ModalType::Info { .. }) => ModalButton::Ok,
|
||||||
None => ModalButton::Ok,
|
None => ModalButton::Ok,
|
||||||
@@ -88,6 +74,9 @@ impl ModalManager {
|
|||||||
if let Some(next) = self.stack.last() {
|
if let Some(next) = self.stack.last() {
|
||||||
self.active_button = match next {
|
self.active_button = match next {
|
||||||
ModalType::ConnectionError { .. } => ModalButton::Retry,
|
ModalType::ConnectionError { .. } => ModalButton::Retry,
|
||||||
|
ModalType::ProcessDetails { .. } => ModalButton::Ok,
|
||||||
|
ModalType::About => ModalButton::Ok,
|
||||||
|
ModalType::Help => ModalButton::Ok,
|
||||||
ModalType::Confirmation { .. } => ModalButton::Confirm,
|
ModalType::Confirmation { .. } => ModalButton::Confirm,
|
||||||
ModalType::Info { .. } => ModalButton::Ok,
|
ModalType::Info { .. } => ModalButton::Ok,
|
||||||
};
|
};
|
||||||
@@ -135,6 +124,101 @@ impl ModalManager {
|
|||||||
ModalAction::None
|
ModalAction::None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
KeyCode::Char('x') | KeyCode::Char('X') => {
|
||||||
|
if matches!(self.stack.last(), Some(ModalType::ProcessDetails { .. })) {
|
||||||
|
// Close all ProcessDetails modals at once (handles parent navigation chain)
|
||||||
|
while matches!(self.stack.last(), Some(ModalType::ProcessDetails { .. })) {
|
||||||
|
self.pop_modal();
|
||||||
|
}
|
||||||
|
ModalAction::Dismiss
|
||||||
|
} else {
|
||||||
|
ModalAction::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Char('j') | KeyCode::Char('J') => {
|
||||||
|
if matches!(self.stack.last(), Some(ModalType::ProcessDetails { .. })) {
|
||||||
|
self.thread_scroll_offset = self
|
||||||
|
.thread_scroll_offset
|
||||||
|
.saturating_add(1)
|
||||||
|
.min(self.thread_scroll_max);
|
||||||
|
ModalAction::Handled
|
||||||
|
} else {
|
||||||
|
ModalAction::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Char('k') | KeyCode::Char('K') => {
|
||||||
|
if matches!(self.stack.last(), Some(ModalType::ProcessDetails { .. })) {
|
||||||
|
self.thread_scroll_offset = self.thread_scroll_offset.saturating_sub(1);
|
||||||
|
ModalAction::Handled
|
||||||
|
} else {
|
||||||
|
ModalAction::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Char('d') | KeyCode::Char('D') => {
|
||||||
|
if matches!(self.stack.last(), Some(ModalType::ProcessDetails { .. })) {
|
||||||
|
self.thread_scroll_offset = self
|
||||||
|
.thread_scroll_offset
|
||||||
|
.saturating_add(10)
|
||||||
|
.min(self.thread_scroll_max);
|
||||||
|
ModalAction::Handled
|
||||||
|
} else {
|
||||||
|
ModalAction::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Char('u') | KeyCode::Char('U') => {
|
||||||
|
if matches!(self.stack.last(), Some(ModalType::ProcessDetails { .. })) {
|
||||||
|
self.thread_scroll_offset = self.thread_scroll_offset.saturating_sub(10);
|
||||||
|
ModalAction::Handled
|
||||||
|
} else {
|
||||||
|
ModalAction::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Char('[') => {
|
||||||
|
if matches!(self.stack.last(), Some(ModalType::ProcessDetails { .. })) {
|
||||||
|
self.journal_scroll_offset = self.journal_scroll_offset.saturating_sub(1);
|
||||||
|
ModalAction::Handled
|
||||||
|
} else {
|
||||||
|
ModalAction::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Char(']') => {
|
||||||
|
if matches!(self.stack.last(), Some(ModalType::ProcessDetails { .. })) {
|
||||||
|
self.journal_scroll_offset = self
|
||||||
|
.journal_scroll_offset
|
||||||
|
.saturating_add(1)
|
||||||
|
.min(self.journal_scroll_max);
|
||||||
|
ModalAction::Handled
|
||||||
|
} else {
|
||||||
|
ModalAction::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Char('p') | KeyCode::Char('P') => {
|
||||||
|
// Switch to parent process if it exists
|
||||||
|
if let Some(ModalType::ProcessDetails { pid }) = self.stack.last() {
|
||||||
|
// We need to get the parent PID from the process details
|
||||||
|
// For now, return a special action that the app can handle
|
||||||
|
// The app has access to the process details and can extract parent_pid
|
||||||
|
ModalAction::SwitchToParentProcess(*pid)
|
||||||
|
} else {
|
||||||
|
ModalAction::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Up => {
|
||||||
|
if matches!(self.stack.last(), Some(ModalType::Help)) {
|
||||||
|
self.help_scroll_offset = self.help_scroll_offset.saturating_sub(1);
|
||||||
|
ModalAction::Handled
|
||||||
|
} else {
|
||||||
|
ModalAction::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyCode::Down => {
|
||||||
|
if matches!(self.stack.last(), Some(ModalType::Help)) {
|
||||||
|
self.help_scroll_offset = self.help_scroll_offset.saturating_add(1);
|
||||||
|
ModalAction::Handled
|
||||||
|
} else {
|
||||||
|
ModalAction::None
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => ModalAction::None,
|
_ => ModalAction::None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,6 +228,18 @@ impl ModalManager {
|
|||||||
ModalAction::RetryConnection
|
ModalAction::RetryConnection
|
||||||
}
|
}
|
||||||
(Some(ModalType::ConnectionError { .. }), ModalButton::Exit) => ModalAction::ExitApp,
|
(Some(ModalType::ConnectionError { .. }), ModalButton::Exit) => ModalAction::ExitApp,
|
||||||
|
(Some(ModalType::ProcessDetails { .. }), ModalButton::Ok) => {
|
||||||
|
self.pop_modal();
|
||||||
|
ModalAction::Dismiss
|
||||||
|
}
|
||||||
|
(Some(ModalType::About), ModalButton::Ok) => {
|
||||||
|
self.pop_modal();
|
||||||
|
ModalAction::Dismiss
|
||||||
|
}
|
||||||
|
(Some(ModalType::Help), ModalButton::Ok) => {
|
||||||
|
self.pop_modal();
|
||||||
|
ModalAction::Dismiss
|
||||||
|
}
|
||||||
(Some(ModalType::Confirmation { .. }), ModalButton::Confirm) => ModalAction::Confirm,
|
(Some(ModalType::Confirmation { .. }), ModalButton::Confirm) => ModalAction::Confirm,
|
||||||
(Some(ModalType::Confirmation { .. }), ModalButton::Cancel) => ModalAction::Cancel,
|
(Some(ModalType::Confirmation { .. }), ModalButton::Cancel) => ModalAction::Cancel,
|
||||||
(Some(ModalType::Info { .. }), ModalButton::Ok) => {
|
(Some(ModalType::Info { .. }), ModalButton::Ok) => {
|
||||||
@@ -166,10 +262,10 @@ impl ModalManager {
|
|||||||
self.next_button();
|
self.next_button();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, f: &mut Frame) {
|
pub fn render(&mut self, f: &mut Frame, data: ProcessModalData) {
|
||||||
if let Some(m) = self.stack.last() {
|
if let Some(m) = self.stack.last().cloned() {
|
||||||
self.render_background_dim(f);
|
self.render_background_dim(f);
|
||||||
self.render_modal_content(f, m);
|
self.render_modal_content(f, &m, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,9 +280,27 @@ impl ModalManager {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_modal_content(&self, f: &mut Frame, modal: &ModalType) {
|
fn render_modal_content(&mut self, f: &mut Frame, modal: &ModalType, data: ProcessModalData) {
|
||||||
let area = f.area();
|
let area = f.area();
|
||||||
let modal_area = self.centered_rect(70, 50, area);
|
// Different sizes for different modal types
|
||||||
|
let modal_area = match modal {
|
||||||
|
ModalType::ProcessDetails { .. } => {
|
||||||
|
// Process details modal uses almost full screen (95% width, 90% height)
|
||||||
|
self.centered_rect(95, 90, area)
|
||||||
|
}
|
||||||
|
ModalType::About => {
|
||||||
|
// About modal uses medium size
|
||||||
|
self.centered_rect(90, 90, area)
|
||||||
|
}
|
||||||
|
ModalType::Help => {
|
||||||
|
// Help modal uses medium size
|
||||||
|
self.centered_rect(70, 80, area)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// Other modals use smaller size
|
||||||
|
self.centered_rect(70, 50, area)
|
||||||
|
}
|
||||||
|
};
|
||||||
f.render_widget(Clear, modal_area);
|
f.render_widget(Clear, modal_area);
|
||||||
match modal {
|
match modal {
|
||||||
ModalType::ConnectionError {
|
ModalType::ConnectionError {
|
||||||
@@ -202,6 +316,11 @@ impl ModalManager {
|
|||||||
*retry_count,
|
*retry_count,
|
||||||
*auto_retry_countdown,
|
*auto_retry_countdown,
|
||||||
),
|
),
|
||||||
|
ModalType::ProcessDetails { pid } => {
|
||||||
|
self.render_process_details(f, modal_area, *pid, data)
|
||||||
|
}
|
||||||
|
ModalType::About => self.render_about(f, modal_area),
|
||||||
|
ModalType::Help => self.render_help(f, modal_area),
|
||||||
ModalType::Confirmation {
|
ModalType::Confirmation {
|
||||||
title,
|
title,
|
||||||
message,
|
message,
|
||||||
@@ -212,279 +331,6 @@ impl ModalManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_connection_error(
|
|
||||||
&self,
|
|
||||||
f: &mut Frame,
|
|
||||||
area: Rect,
|
|
||||||
message: &str,
|
|
||||||
disconnected_at: Instant,
|
|
||||||
retry_count: u32,
|
|
||||||
auto_retry_countdown: Option<u64>,
|
|
||||||
) {
|
|
||||||
let duration_text = format_duration(disconnected_at.elapsed());
|
|
||||||
let chunks = Layout::default()
|
|
||||||
.direction(Direction::Vertical)
|
|
||||||
.constraints([
|
|
||||||
Constraint::Length(3),
|
|
||||||
Constraint::Min(4),
|
|
||||||
Constraint::Length(4),
|
|
||||||
])
|
|
||||||
.split(area);
|
|
||||||
let block = Block::default()
|
|
||||||
.title(ICON_WARNING_TITLE)
|
|
||||||
.title_style(
|
|
||||||
Style::default()
|
|
||||||
.fg(MODAL_TITLE_FG)
|
|
||||||
.add_modifier(Modifier::BOLD),
|
|
||||||
)
|
|
||||||
.borders(Borders::ALL)
|
|
||||||
.border_style(Style::default().fg(MODAL_BORDER_FG))
|
|
||||||
.style(Style::default().bg(MODAL_BG).fg(MODAL_FG));
|
|
||||||
f.render_widget(block, area);
|
|
||||||
|
|
||||||
let content_area = chunks[1];
|
|
||||||
let max_w = content_area.width.saturating_sub(15) as usize;
|
|
||||||
let clean_message = if message.to_lowercase().contains("hostname verification")
|
|
||||||
|| message.contains("socktop_connector")
|
|
||||||
{
|
|
||||||
"Connection failed - hostname verification disabled".to_string()
|
|
||||||
} else if message.contains("Failed to fetch metrics:") {
|
|
||||||
if let Some(p) = message.find(':') {
|
|
||||||
let ess = message[p + 1..].trim();
|
|
||||||
if ess.len() > max_w {
|
|
||||||
format!("{}...", &ess[..max_w.saturating_sub(3)])
|
|
||||||
} else {
|
|
||||||
ess.to_string()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
"Connection error".to_string()
|
|
||||||
}
|
|
||||||
} else if message.starts_with("Retry failed:") {
|
|
||||||
if let Some(p) = message.find(':') {
|
|
||||||
let ess = message[p + 1..].trim();
|
|
||||||
if ess.len() > max_w {
|
|
||||||
format!("{}...", &ess[..max_w.saturating_sub(3)])
|
|
||||||
} else {
|
|
||||||
ess.to_string()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
"Retry failed".to_string()
|
|
||||||
}
|
|
||||||
} else if message.len() > max_w {
|
|
||||||
format!("{}...", &message[..max_w.saturating_sub(3)])
|
|
||||||
} else {
|
|
||||||
message.to_string()
|
|
||||||
};
|
|
||||||
let truncate = |s: &str| {
|
|
||||||
if s.len() > max_w {
|
|
||||||
format!("{}...", &s[..max_w.saturating_sub(3)])
|
|
||||||
} else {
|
|
||||||
s.to_string()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let agent_text = truncate("📡 Cannot connect to socktop agent");
|
|
||||||
let message_text = truncate(&clean_message);
|
|
||||||
let duration_display = truncate(&duration_text);
|
|
||||||
let retry_display = truncate(&retry_count.to_string());
|
|
||||||
let countdown_text = auto_retry_countdown.map(|c| {
|
|
||||||
if c == 0 {
|
|
||||||
"Auto retry now...".to_string()
|
|
||||||
} else {
|
|
||||||
format!("{c}s")
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Determine if we have enough space (height + width) to show large centered icon
|
|
||||||
let icon_max_width = LARGE_ERROR_ICON
|
|
||||||
.iter()
|
|
||||||
.map(|l| l.trim().chars().count())
|
|
||||||
.max()
|
|
||||||
.unwrap_or(0) as u16;
|
|
||||||
let large_allowed = content_area.height >= (LARGE_ERROR_ICON.len() as u16 + 8)
|
|
||||||
&& content_area.width >= icon_max_width + 6; // small margin for borders/padding
|
|
||||||
let mut icon_lines: Vec<Line> = Vec::new();
|
|
||||||
if large_allowed {
|
|
||||||
for &raw in LARGE_ERROR_ICON.iter() {
|
|
||||||
let trimmed = raw.trim();
|
|
||||||
icon_lines.push(Line::from(
|
|
||||||
trimmed
|
|
||||||
.chars()
|
|
||||||
.map(|ch| {
|
|
||||||
if ch == '!' {
|
|
||||||
Span::styled(
|
|
||||||
ch.to_string(),
|
|
||||||
Style::default()
|
|
||||||
.fg(Color::White)
|
|
||||||
.add_modifier(Modifier::BOLD),
|
|
||||||
)
|
|
||||||
} else if ch == '/' || ch == '\\' || ch == '_' {
|
|
||||||
// keep outline in pink
|
|
||||||
Span::styled(
|
|
||||||
ch.to_string(),
|
|
||||||
Style::default()
|
|
||||||
.fg(MODAL_ICON_PINK)
|
|
||||||
.add_modifier(Modifier::BOLD),
|
|
||||||
)
|
|
||||||
} else if ch == ' ' {
|
|
||||||
Span::raw(" ")
|
|
||||||
} else {
|
|
||||||
Span::styled(ch.to_string(), Style::default().fg(MODAL_ICON_PINK))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
icon_lines.push(Line::from("")); // blank spacer line below icon
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut info_lines: Vec<Line> = Vec::new();
|
|
||||||
if !large_allowed {
|
|
||||||
info_lines.push(Line::from(vec![Span::styled(
|
|
||||||
ICON_CLUSTER,
|
|
||||||
Style::default().fg(MODAL_ICON_PINK),
|
|
||||||
)]));
|
|
||||||
info_lines.push(Line::from(""));
|
|
||||||
}
|
|
||||||
info_lines.push(Line::from(vec![Span::styled(
|
|
||||||
&agent_text,
|
|
||||||
Style::default().fg(MODAL_AGENT_FG),
|
|
||||||
)]));
|
|
||||||
info_lines.push(Line::from(""));
|
|
||||||
info_lines.push(Line::from(vec![
|
|
||||||
Span::styled(ICON_MESSAGE, Style::default().fg(MODAL_HINT_FG)),
|
|
||||||
Span::styled(&message_text, Style::default().fg(MODAL_AGENT_FG)),
|
|
||||||
]));
|
|
||||||
info_lines.push(Line::from(""));
|
|
||||||
info_lines.push(Line::from(vec![
|
|
||||||
Span::styled(
|
|
||||||
ICON_OFFLINE_LABEL,
|
|
||||||
Style::default().fg(MODAL_OFFLINE_LABEL_FG),
|
|
||||||
),
|
|
||||||
Span::styled(
|
|
||||||
&duration_display,
|
|
||||||
Style::default()
|
|
||||||
.fg(Color::White)
|
|
||||||
.add_modifier(Modifier::BOLD),
|
|
||||||
),
|
|
||||||
]));
|
|
||||||
info_lines.push(Line::from(vec![
|
|
||||||
Span::styled(ICON_RETRY_LABEL, Style::default().fg(MODAL_RETRY_LABEL_FG)),
|
|
||||||
Span::styled(
|
|
||||||
&retry_display,
|
|
||||||
Style::default()
|
|
||||||
.fg(Color::White)
|
|
||||||
.add_modifier(Modifier::BOLD),
|
|
||||||
),
|
|
||||||
]));
|
|
||||||
if let Some(cd) = &countdown_text {
|
|
||||||
info_lines.push(Line::from(vec![
|
|
||||||
Span::styled(
|
|
||||||
ICON_COUNTDOWN_LABEL,
|
|
||||||
Style::default().fg(MODAL_COUNTDOWN_LABEL_FG),
|
|
||||||
),
|
|
||||||
Span::styled(
|
|
||||||
cd,
|
|
||||||
Style::default()
|
|
||||||
.fg(Color::White)
|
|
||||||
.add_modifier(Modifier::BOLD),
|
|
||||||
),
|
|
||||||
]));
|
|
||||||
}
|
|
||||||
|
|
||||||
let constrained = Rect {
|
|
||||||
x: content_area.x + 2,
|
|
||||||
y: content_area.y,
|
|
||||||
width: content_area.width.saturating_sub(4),
|
|
||||||
height: content_area.height,
|
|
||||||
};
|
|
||||||
if large_allowed {
|
|
||||||
let split = Layout::default()
|
|
||||||
.direction(Direction::Vertical)
|
|
||||||
.constraints([
|
|
||||||
Constraint::Length(icon_lines.len() as u16),
|
|
||||||
Constraint::Min(0),
|
|
||||||
])
|
|
||||||
.split(constrained);
|
|
||||||
// Center the icon block; each line already trimmed so per-line centering keeps shape
|
|
||||||
f.render_widget(
|
|
||||||
Paragraph::new(Text::from(icon_lines))
|
|
||||||
.alignment(Alignment::Center)
|
|
||||||
.wrap(Wrap { trim: false }),
|
|
||||||
split[0],
|
|
||||||
);
|
|
||||||
f.render_widget(
|
|
||||||
Paragraph::new(Text::from(info_lines))
|
|
||||||
.alignment(Alignment::Center)
|
|
||||||
.wrap(Wrap { trim: true }),
|
|
||||||
split[1],
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
f.render_widget(
|
|
||||||
Paragraph::new(Text::from(info_lines))
|
|
||||||
.alignment(Alignment::Center)
|
|
||||||
.wrap(Wrap { trim: true }),
|
|
||||||
constrained,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let button_area = Rect {
|
|
||||||
x: chunks[2].x,
|
|
||||||
y: chunks[2].y,
|
|
||||||
width: chunks[2].width,
|
|
||||||
height: chunks[2].height.saturating_sub(1),
|
|
||||||
};
|
|
||||||
self.render_connection_error_buttons(f, button_area);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_connection_error_buttons(&self, f: &mut Frame, area: Rect) {
|
|
||||||
let button_chunks = Layout::default()
|
|
||||||
.direction(Direction::Horizontal)
|
|
||||||
.constraints([
|
|
||||||
Constraint::Percentage(30),
|
|
||||||
Constraint::Percentage(15),
|
|
||||||
Constraint::Percentage(10),
|
|
||||||
Constraint::Percentage(15),
|
|
||||||
Constraint::Percentage(30),
|
|
||||||
])
|
|
||||||
.split(area);
|
|
||||||
let retry_style = if self.active_button == ModalButton::Retry {
|
|
||||||
Style::default()
|
|
||||||
.bg(BTN_RETRY_BG_ACTIVE)
|
|
||||||
.fg(BTN_RETRY_FG_ACTIVE)
|
|
||||||
.add_modifier(Modifier::BOLD)
|
|
||||||
} else {
|
|
||||||
Style::default()
|
|
||||||
.fg(BTN_RETRY_FG_INACTIVE)
|
|
||||||
.add_modifier(Modifier::DIM)
|
|
||||||
};
|
|
||||||
let exit_style = if self.active_button == ModalButton::Exit {
|
|
||||||
Style::default()
|
|
||||||
.bg(BTN_EXIT_BG_ACTIVE)
|
|
||||||
.fg(BTN_EXIT_FG_ACTIVE)
|
|
||||||
.add_modifier(Modifier::BOLD)
|
|
||||||
} else {
|
|
||||||
Style::default()
|
|
||||||
.fg(BTN_EXIT_FG_INACTIVE)
|
|
||||||
.add_modifier(Modifier::DIM)
|
|
||||||
};
|
|
||||||
f.render_widget(
|
|
||||||
Paragraph::new(Text::from(Line::from(vec![Span::styled(
|
|
||||||
BTN_RETRY_TEXT,
|
|
||||||
retry_style,
|
|
||||||
)])))
|
|
||||||
.alignment(Alignment::Center),
|
|
||||||
button_chunks[1],
|
|
||||||
);
|
|
||||||
f.render_widget(
|
|
||||||
Paragraph::new(Text::from(Line::from(vec![Span::styled(
|
|
||||||
BTN_EXIT_TEXT,
|
|
||||||
exit_style,
|
|
||||||
)])))
|
|
||||||
.alignment(Alignment::Center),
|
|
||||||
button_chunks[3],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_confirmation(
|
fn render_confirmation(
|
||||||
&self,
|
&self,
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
@@ -577,6 +423,196 @@ impl ModalManager {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn render_about(&self, f: &mut Frame, area: Rect) {
|
||||||
|
//get ASCII art from a constant stored in theme.rs
|
||||||
|
use super::theme::ASCII_ART;
|
||||||
|
|
||||||
|
let version = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
let about_text = format!(
|
||||||
|
"{}\n\
|
||||||
|
Version {}\n\
|
||||||
|
\n\
|
||||||
|
A terminal first remote monitoring tool\n\
|
||||||
|
\n\
|
||||||
|
Website: https://socktop.io\n\
|
||||||
|
GitHub: https://github.com/jasonwitty/socktop\n\
|
||||||
|
\n\
|
||||||
|
License: MIT License\n\
|
||||||
|
\n\
|
||||||
|
Created by Jason Witty\n\
|
||||||
|
jasonpwitty+socktop@proton.me",
|
||||||
|
ASCII_ART, version
|
||||||
|
);
|
||||||
|
|
||||||
|
// Render the border block
|
||||||
|
let block = Block::default()
|
||||||
|
.title(" About socktop ")
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.style(Style::default().bg(Color::Black).fg(Color::DarkGray));
|
||||||
|
f.render_widget(block, area);
|
||||||
|
|
||||||
|
// Calculate inner area manually to avoid any parent styling
|
||||||
|
let inner_area = Rect {
|
||||||
|
x: area.x + 1,
|
||||||
|
y: area.y + 1,
|
||||||
|
width: area.width.saturating_sub(2),
|
||||||
|
height: area.height.saturating_sub(2), // Leave room for button at bottom
|
||||||
|
};
|
||||||
|
|
||||||
|
// Render content area with explicit black background
|
||||||
|
f.render_widget(
|
||||||
|
Paragraph::new(about_text)
|
||||||
|
.style(Style::default().fg(Color::Cyan).bg(Color::Black))
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.wrap(Wrap { trim: false }),
|
||||||
|
inner_area,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Button area
|
||||||
|
let button_area = Rect {
|
||||||
|
x: area.x + 1,
|
||||||
|
y: area.y + area.height.saturating_sub(2),
|
||||||
|
width: area.width.saturating_sub(2),
|
||||||
|
height: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
let ok_style = if self.active_button == ModalButton::Ok {
|
||||||
|
Style::default()
|
||||||
|
.bg(Color::Blue)
|
||||||
|
.fg(Color::White)
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default().fg(Color::Blue).bg(Color::Black)
|
||||||
|
};
|
||||||
|
|
||||||
|
f.render_widget(
|
||||||
|
Paragraph::new("[ Enter ] Close")
|
||||||
|
.style(ok_style)
|
||||||
|
.alignment(Alignment::Center),
|
||||||
|
button_area,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_help(&self, f: &mut Frame, area: Rect) {
|
||||||
|
let help_lines = vec![
|
||||||
|
"GLOBAL",
|
||||||
|
" q/Q/Esc ........ Quit │ a/A ....... About │ h/H ....... Help",
|
||||||
|
"",
|
||||||
|
"PROCESS LIST",
|
||||||
|
" / .............. Start/edit fuzzy search",
|
||||||
|
" c/C ............ Clear search filter",
|
||||||
|
" ↑/↓ ............ Select/navigate processes",
|
||||||
|
" Enter .......... Open Process Details",
|
||||||
|
" x/X ............ Clear selection",
|
||||||
|
" Click header ... Sort by column (CPU/Mem)",
|
||||||
|
" Click row ...... Select process",
|
||||||
|
"",
|
||||||
|
"SEARCH MODE (after pressing /)",
|
||||||
|
" Type ........... Enter search query (fuzzy match)",
|
||||||
|
" ↑/↓ ............ Navigate results while typing",
|
||||||
|
" Esc ............ Cancel search and clear filter",
|
||||||
|
" Enter .......... Apply filter and select first result",
|
||||||
|
"",
|
||||||
|
"CPU PER-CORE",
|
||||||
|
" ←/→ ............ Scroll cores │ PgUp/PgDn ... Page up/down",
|
||||||
|
" Home/End ....... Jump to first/last core",
|
||||||
|
"",
|
||||||
|
"PROCESS DETAILS MODAL",
|
||||||
|
" x/X ............ Close modal (all parent modals)",
|
||||||
|
" p/P ............ Navigate to parent process",
|
||||||
|
" j/k ............ Scroll threads ↓/↑ (1 line)",
|
||||||
|
" d/u ............ Scroll threads ↓/↑ (10 lines)",
|
||||||
|
" [ / ] .......... Scroll journal ↑/↓",
|
||||||
|
" Esc/Enter ...... Close modal",
|
||||||
|
"",
|
||||||
|
"MODAL NAVIGATION",
|
||||||
|
" Tab/→ .......... Next button │ Shift+Tab/← ... Previous button",
|
||||||
|
" Enter .......... Confirm/OK │ Esc ............ Cancel/Close",
|
||||||
|
];
|
||||||
|
|
||||||
|
// Render the border block
|
||||||
|
let block = Block::default()
|
||||||
|
.title(" Hotkey Help (use ↑/↓ to scroll) ")
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.style(Style::default().bg(Color::Black).fg(Color::DarkGray));
|
||||||
|
f.render_widget(block, area);
|
||||||
|
|
||||||
|
// Split into content area and button area
|
||||||
|
let chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([Constraint::Min(1), Constraint::Length(1)])
|
||||||
|
.split(Rect {
|
||||||
|
x: area.x + 1,
|
||||||
|
y: area.y + 1,
|
||||||
|
width: area.width.saturating_sub(2),
|
||||||
|
height: area.height.saturating_sub(2),
|
||||||
|
});
|
||||||
|
|
||||||
|
let content_area = chunks[0];
|
||||||
|
let button_area = chunks[1];
|
||||||
|
|
||||||
|
// Calculate visible window
|
||||||
|
let visible_height = content_area.height as usize;
|
||||||
|
let total_lines = help_lines.len();
|
||||||
|
let max_scroll = total_lines.saturating_sub(visible_height);
|
||||||
|
let scroll_offset = self.help_scroll_offset.min(max_scroll);
|
||||||
|
|
||||||
|
// Get visible lines
|
||||||
|
let visible_lines: Vec<Line> = help_lines
|
||||||
|
.iter()
|
||||||
|
.skip(scroll_offset)
|
||||||
|
.take(visible_height)
|
||||||
|
.map(|s| Line::from(*s))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// Render scrollable content
|
||||||
|
f.render_widget(
|
||||||
|
Paragraph::new(visible_lines)
|
||||||
|
.style(Style::default().fg(Color::Cyan).bg(Color::Black))
|
||||||
|
.alignment(Alignment::Left),
|
||||||
|
content_area,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Render scrollbar if needed
|
||||||
|
if total_lines > visible_height {
|
||||||
|
use ratatui::widgets::{Scrollbar, ScrollbarOrientation, ScrollbarState};
|
||||||
|
|
||||||
|
let scrollbar_area = Rect {
|
||||||
|
x: area.x + area.width.saturating_sub(2),
|
||||||
|
y: area.y + 1,
|
||||||
|
width: 1,
|
||||||
|
height: area.height.saturating_sub(2),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut scrollbar_state = ScrollbarState::new(max_scroll).position(scroll_offset);
|
||||||
|
|
||||||
|
let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
|
||||||
|
.begin_symbol(Some("↑"))
|
||||||
|
.end_symbol(Some("↓"))
|
||||||
|
.style(Style::default().fg(Color::DarkGray));
|
||||||
|
|
||||||
|
f.render_stateful_widget(scrollbar, scrollbar_area, &mut scrollbar_state);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Button area
|
||||||
|
let ok_style = if self.active_button == ModalButton::Ok {
|
||||||
|
Style::default()
|
||||||
|
.bg(Color::Blue)
|
||||||
|
.fg(Color::White)
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default().fg(Color::Blue).bg(Color::Black)
|
||||||
|
};
|
||||||
|
|
||||||
|
f.render_widget(
|
||||||
|
Paragraph::new("[ Enter ] Close")
|
||||||
|
.style(ok_style)
|
||||||
|
.alignment(Alignment::Center),
|
||||||
|
button_area,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
fn centered_rect(&self, percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
fn centered_rect(&self, percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
||||||
let vert = Layout::default()
|
let vert = Layout::default()
|
||||||
.direction(Direction::Vertical)
|
.direction(Direction::Vertical)
|
||||||
@@ -596,17 +632,3 @@ impl ModalManager {
|
|||||||
.split(vert[1])[1]
|
.split(vert[1])[1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_duration(duration: Duration) -> String {
|
|
||||||
let total = duration.as_secs();
|
|
||||||
let h = total / 3600;
|
|
||||||
let m = (total % 3600) / 60;
|
|
||||||
let s = total % 60;
|
|
||||||
if h > 0 {
|
|
||||||
format!("{h}h {m}m {s}s")
|
|
||||||
} else if m > 0 {
|
|
||||||
format!("{m}m {s}s")
|
|
||||||
} else {
|
|
||||||
format!("{s}s")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,298 @@
|
|||||||
|
//! Connection error modal rendering
|
||||||
|
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use super::modal_format::format_duration;
|
||||||
|
use super::theme::{
|
||||||
|
BTN_EXIT_BG_ACTIVE, BTN_EXIT_FG_ACTIVE, BTN_EXIT_FG_INACTIVE, BTN_EXIT_TEXT,
|
||||||
|
BTN_RETRY_BG_ACTIVE, BTN_RETRY_FG_ACTIVE, BTN_RETRY_FG_INACTIVE, BTN_RETRY_TEXT, ICON_CLUSTER,
|
||||||
|
ICON_COUNTDOWN_LABEL, ICON_MESSAGE, ICON_OFFLINE_LABEL, ICON_RETRY_LABEL, ICON_WARNING_TITLE,
|
||||||
|
LARGE_ERROR_ICON, MODAL_AGENT_FG, MODAL_BG, MODAL_BORDER_FG, MODAL_COUNTDOWN_LABEL_FG,
|
||||||
|
MODAL_FG, MODAL_HINT_FG, MODAL_ICON_PINK, MODAL_OFFLINE_LABEL_FG, MODAL_RETRY_LABEL_FG,
|
||||||
|
MODAL_TITLE_FG,
|
||||||
|
};
|
||||||
|
use ratatui::{
|
||||||
|
Frame,
|
||||||
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||||
|
style::{Color, Modifier, Style},
|
||||||
|
text::{Line, Span, Text},
|
||||||
|
widgets::{Block, Borders, Paragraph, Wrap},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::modal::{ModalButton, ModalManager};
|
||||||
|
|
||||||
|
impl ModalManager {
|
||||||
|
pub(super) fn render_connection_error(
|
||||||
|
&self,
|
||||||
|
f: &mut Frame,
|
||||||
|
area: Rect,
|
||||||
|
message: &str,
|
||||||
|
disconnected_at: Instant,
|
||||||
|
retry_count: u32,
|
||||||
|
auto_retry_countdown: Option<u64>,
|
||||||
|
) {
|
||||||
|
let duration_text = format_duration(disconnected_at.elapsed());
|
||||||
|
let chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Length(3),
|
||||||
|
Constraint::Min(4),
|
||||||
|
Constraint::Length(4),
|
||||||
|
])
|
||||||
|
.split(area);
|
||||||
|
let block = Block::default()
|
||||||
|
.title(
|
||||||
|
Line::from(ICON_WARNING_TITLE).style(
|
||||||
|
Style::default()
|
||||||
|
.fg(MODAL_TITLE_FG)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_style(Style::default().fg(MODAL_BORDER_FG))
|
||||||
|
.style(Style::default().bg(MODAL_BG).fg(MODAL_FG));
|
||||||
|
f.render_widget(block, area);
|
||||||
|
|
||||||
|
let content_area = chunks[1];
|
||||||
|
let max_w = content_area.width.saturating_sub(15) as usize;
|
||||||
|
let clean_message = if message.to_lowercase().contains("hostname verification")
|
||||||
|
|| message.contains("socktop_connector")
|
||||||
|
{
|
||||||
|
"Connection failed - hostname verification disabled".to_string()
|
||||||
|
} else if message.contains("Failed to fetch metrics:") {
|
||||||
|
if let Some(p) = message.find(':') {
|
||||||
|
let ess = message[p + 1..].trim();
|
||||||
|
if ess.len() > max_w {
|
||||||
|
format!("{}...", &ess[..max_w.saturating_sub(3)])
|
||||||
|
} else {
|
||||||
|
ess.to_string()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
"Connection error".to_string()
|
||||||
|
}
|
||||||
|
} else if message.starts_with("Retry failed:") {
|
||||||
|
if let Some(p) = message.find(':') {
|
||||||
|
let ess = message[p + 1..].trim();
|
||||||
|
if ess.len() > max_w {
|
||||||
|
format!("{}...", &ess[..max_w.saturating_sub(3)])
|
||||||
|
} else {
|
||||||
|
ess.to_string()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
"Retry failed".to_string()
|
||||||
|
}
|
||||||
|
} else if message.len() > max_w {
|
||||||
|
format!("{}...", &message[..max_w.saturating_sub(3)])
|
||||||
|
} else {
|
||||||
|
message.to_string()
|
||||||
|
};
|
||||||
|
let truncate = |s: &str| {
|
||||||
|
if s.len() > max_w {
|
||||||
|
format!("{}...", &s[..max_w.saturating_sub(3)])
|
||||||
|
} else {
|
||||||
|
s.to_string()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let agent_text = truncate("📡 Cannot connect to socktop agent");
|
||||||
|
let message_text = truncate(&clean_message);
|
||||||
|
let duration_display = truncate(&duration_text);
|
||||||
|
let retry_display = truncate(&retry_count.to_string());
|
||||||
|
let countdown_text = auto_retry_countdown.map(|c| {
|
||||||
|
if c == 0 {
|
||||||
|
"Auto retry now...".to_string()
|
||||||
|
} else {
|
||||||
|
format!("{c}s")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Determine if we have enough space (height + width) to show large centered icon
|
||||||
|
let icon_max_width = LARGE_ERROR_ICON
|
||||||
|
.iter()
|
||||||
|
.map(|l| l.trim().chars().count())
|
||||||
|
.max()
|
||||||
|
.unwrap_or(0) as u16;
|
||||||
|
let large_allowed = content_area.height >= (LARGE_ERROR_ICON.len() as u16 + 8)
|
||||||
|
&& content_area.width >= icon_max_width + 6; // small margin for borders/padding
|
||||||
|
let mut icon_lines: Vec<Line> = Vec::new();
|
||||||
|
if large_allowed {
|
||||||
|
for &raw in LARGE_ERROR_ICON.iter() {
|
||||||
|
let trimmed = raw.trim();
|
||||||
|
icon_lines.push(Line::from(
|
||||||
|
trimmed
|
||||||
|
.chars()
|
||||||
|
.map(|ch| {
|
||||||
|
if ch == '!' {
|
||||||
|
Span::styled(
|
||||||
|
ch.to_string(),
|
||||||
|
Style::default()
|
||||||
|
.fg(Color::White)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
)
|
||||||
|
} else if ch == '/' || ch == '\\' || ch == '_' {
|
||||||
|
// keep outline in pink
|
||||||
|
Span::styled(
|
||||||
|
ch.to_string(),
|
||||||
|
Style::default()
|
||||||
|
.fg(MODAL_ICON_PINK)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
)
|
||||||
|
} else if ch == ' ' {
|
||||||
|
Span::raw(" ")
|
||||||
|
} else {
|
||||||
|
Span::styled(ch.to_string(), Style::default().fg(MODAL_ICON_PINK))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
icon_lines.push(Line::from("")); // blank spacer line below icon
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut info_lines: Vec<Line> = Vec::new();
|
||||||
|
if !large_allowed {
|
||||||
|
info_lines.push(Line::from(vec![Span::styled(
|
||||||
|
ICON_CLUSTER,
|
||||||
|
Style::default().fg(MODAL_ICON_PINK),
|
||||||
|
)]));
|
||||||
|
info_lines.push(Line::from(""));
|
||||||
|
}
|
||||||
|
info_lines.push(Line::from(vec![Span::styled(
|
||||||
|
&agent_text,
|
||||||
|
Style::default().fg(MODAL_AGENT_FG),
|
||||||
|
)]));
|
||||||
|
info_lines.push(Line::from(""));
|
||||||
|
info_lines.push(Line::from(vec![
|
||||||
|
Span::styled(ICON_MESSAGE, Style::default().fg(MODAL_HINT_FG)),
|
||||||
|
Span::styled(&message_text, Style::default().fg(MODAL_AGENT_FG)),
|
||||||
|
]));
|
||||||
|
info_lines.push(Line::from(""));
|
||||||
|
info_lines.push(Line::from(vec![
|
||||||
|
Span::styled(
|
||||||
|
ICON_OFFLINE_LABEL,
|
||||||
|
Style::default().fg(MODAL_OFFLINE_LABEL_FG),
|
||||||
|
),
|
||||||
|
Span::styled(
|
||||||
|
&duration_display,
|
||||||
|
Style::default()
|
||||||
|
.fg(Color::White)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
),
|
||||||
|
]));
|
||||||
|
info_lines.push(Line::from(vec![
|
||||||
|
Span::styled(ICON_RETRY_LABEL, Style::default().fg(MODAL_RETRY_LABEL_FG)),
|
||||||
|
Span::styled(
|
||||||
|
&retry_display,
|
||||||
|
Style::default()
|
||||||
|
.fg(Color::White)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
),
|
||||||
|
]));
|
||||||
|
if let Some(cd) = &countdown_text {
|
||||||
|
info_lines.push(Line::from(vec![
|
||||||
|
Span::styled(
|
||||||
|
ICON_COUNTDOWN_LABEL,
|
||||||
|
Style::default().fg(MODAL_COUNTDOWN_LABEL_FG),
|
||||||
|
),
|
||||||
|
Span::styled(
|
||||||
|
cd,
|
||||||
|
Style::default()
|
||||||
|
.fg(Color::White)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
),
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
|
let constrained = Rect {
|
||||||
|
x: content_area.x + 2,
|
||||||
|
y: content_area.y,
|
||||||
|
width: content_area.width.saturating_sub(4),
|
||||||
|
height: content_area.height,
|
||||||
|
};
|
||||||
|
if large_allowed {
|
||||||
|
let split = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Length(icon_lines.len() as u16),
|
||||||
|
Constraint::Min(0),
|
||||||
|
])
|
||||||
|
.split(constrained);
|
||||||
|
// Center the icon block; each line already trimmed so per-line centering keeps shape
|
||||||
|
f.render_widget(
|
||||||
|
Paragraph::new(Text::from(icon_lines))
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.wrap(Wrap { trim: false }),
|
||||||
|
split[0],
|
||||||
|
);
|
||||||
|
f.render_widget(
|
||||||
|
Paragraph::new(Text::from(info_lines))
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.wrap(Wrap { trim: true }),
|
||||||
|
split[1],
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
f.render_widget(
|
||||||
|
Paragraph::new(Text::from(info_lines))
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.wrap(Wrap { trim: true }),
|
||||||
|
constrained,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let button_area = Rect {
|
||||||
|
x: chunks[2].x,
|
||||||
|
y: chunks[2].y,
|
||||||
|
width: chunks[2].width,
|
||||||
|
height: chunks[2].height.saturating_sub(1),
|
||||||
|
};
|
||||||
|
self.render_connection_error_buttons(f, button_area);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_connection_error_buttons(&self, f: &mut Frame, area: Rect) {
|
||||||
|
let button_chunks = Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage(30),
|
||||||
|
Constraint::Percentage(15),
|
||||||
|
Constraint::Percentage(10),
|
||||||
|
Constraint::Percentage(15),
|
||||||
|
Constraint::Percentage(30),
|
||||||
|
])
|
||||||
|
.split(area);
|
||||||
|
let retry_style = if self.active_button == ModalButton::Retry {
|
||||||
|
Style::default()
|
||||||
|
.bg(BTN_RETRY_BG_ACTIVE)
|
||||||
|
.fg(BTN_RETRY_FG_ACTIVE)
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default()
|
||||||
|
.fg(BTN_RETRY_FG_INACTIVE)
|
||||||
|
.add_modifier(Modifier::DIM)
|
||||||
|
};
|
||||||
|
let exit_style = if self.active_button == ModalButton::Exit {
|
||||||
|
Style::default()
|
||||||
|
.bg(BTN_EXIT_BG_ACTIVE)
|
||||||
|
.fg(BTN_EXIT_FG_ACTIVE)
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default()
|
||||||
|
.fg(BTN_EXIT_FG_INACTIVE)
|
||||||
|
.add_modifier(Modifier::DIM)
|
||||||
|
};
|
||||||
|
f.render_widget(
|
||||||
|
Paragraph::new(Text::from(Line::from(vec![Span::styled(
|
||||||
|
BTN_RETRY_TEXT,
|
||||||
|
retry_style,
|
||||||
|
)])))
|
||||||
|
.alignment(Alignment::Center),
|
||||||
|
button_chunks[1],
|
||||||
|
);
|
||||||
|
f.render_widget(
|
||||||
|
Paragraph::new(Text::from(Line::from(vec![Span::styled(
|
||||||
|
BTN_EXIT_TEXT,
|
||||||
|
exit_style,
|
||||||
|
)])))
|
||||||
|
.alignment(Alignment::Center),
|
||||||
|
button_chunks[3],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
//! Formatting utilities for process details modal
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
/// Format uptime in human-readable form
|
||||||
|
pub fn format_uptime(secs: u64) -> String {
|
||||||
|
let days = secs / 86400;
|
||||||
|
let hours = (secs % 86400) / 3600;
|
||||||
|
let minutes = (secs % 3600) / 60;
|
||||||
|
let seconds = secs % 60;
|
||||||
|
|
||||||
|
if days > 0 {
|
||||||
|
format!("{days}d {hours}h {minutes}m")
|
||||||
|
} else if hours > 0 {
|
||||||
|
format!("{hours}h {minutes}m {seconds}s")
|
||||||
|
} else if minutes > 0 {
|
||||||
|
format!("{minutes}m {seconds}s")
|
||||||
|
} else {
|
||||||
|
format!("{seconds}s")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Format duration in human-readable form
|
||||||
|
pub fn format_duration(duration: Duration) -> String {
|
||||||
|
let total = duration.as_secs();
|
||||||
|
let h = total / 3600;
|
||||||
|
let m = (total % 3600) / 60;
|
||||||
|
let s = total % 60;
|
||||||
|
if h > 0 {
|
||||||
|
format!("{h}h {m}m {s}s")
|
||||||
|
} else if m > 0 {
|
||||||
|
format!("{m}m {s}s")
|
||||||
|
} else {
|
||||||
|
format!("{s}s")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Normalize CPU usage to 0-100% by dividing by thread count
|
||||||
|
pub fn normalize_cpu_usage(cpu_usage: f32, thread_count: u32) -> f32 {
|
||||||
|
let threads = thread_count.max(1) as f32;
|
||||||
|
(cpu_usage / threads).min(100.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calculate dynamic Y-axis maximum in 10% increments
|
||||||
|
pub fn calculate_dynamic_y_max(max_value: f64) -> f64 {
|
||||||
|
((max_value / 10.0).ceil() * 10.0).clamp(10.0, 100.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_uptime_seconds() {
|
||||||
|
assert_eq!(format_uptime(45), "45s");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_uptime_minutes() {
|
||||||
|
assert_eq!(format_uptime(125), "2m 5s");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_uptime_hours() {
|
||||||
|
assert_eq!(format_uptime(3665), "1h 1m 5s");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_uptime_days() {
|
||||||
|
assert_eq!(format_uptime(90061), "1d 1h 1m");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_normalize_cpu_single_thread() {
|
||||||
|
assert_eq!(normalize_cpu_usage(50.0, 1), 50.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_normalize_cpu_multi_thread() {
|
||||||
|
assert_eq!(normalize_cpu_usage(400.0, 4), 100.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_normalize_cpu_zero_threads() {
|
||||||
|
// Should default to 1 thread to avoid division by zero
|
||||||
|
assert_eq!(normalize_cpu_usage(100.0, 0), 100.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_normalize_cpu_caps_at_100() {
|
||||||
|
assert_eq!(normalize_cpu_usage(150.0, 1), 100.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dynamic_y_max_rounds_up() {
|
||||||
|
assert_eq!(calculate_dynamic_y_max(15.0), 20.0);
|
||||||
|
assert_eq!(calculate_dynamic_y_max(25.0), 30.0);
|
||||||
|
assert_eq!(calculate_dynamic_y_max(5.0), 10.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dynamic_y_max_minimum() {
|
||||||
|
assert_eq!(calculate_dynamic_y_max(0.0), 10.0);
|
||||||
|
assert_eq!(calculate_dynamic_y_max(3.0), 10.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_dynamic_y_max_caps_at_100() {
|
||||||
|
assert_eq!(calculate_dynamic_y_max(95.0), 100.0);
|
||||||
|
assert_eq!(calculate_dynamic_y_max(100.0), 100.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,79 @@
|
|||||||
|
//! Type definitions for modal system
|
||||||
|
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
/// History data for process metrics rendering
|
||||||
|
pub struct ProcessHistoryData<'a> {
|
||||||
|
pub cpu: &'a std::collections::VecDeque<f32>,
|
||||||
|
/// Running sum of `cpu` maintained by the caller (avoids re-summing per frame)
|
||||||
|
pub cpu_sum: f32,
|
||||||
|
pub mem: &'a std::collections::VecDeque<u64>,
|
||||||
|
pub io_read: &'a std::collections::VecDeque<u64>,
|
||||||
|
pub io_write: &'a std::collections::VecDeque<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Process data for modal rendering
|
||||||
|
pub struct ProcessModalData<'a> {
|
||||||
|
pub details: Option<&'a socktop_connector::ProcessMetricsResponse>,
|
||||||
|
pub journal: Option<&'a socktop_connector::JournalResponse>,
|
||||||
|
pub history: ProcessHistoryData<'a>,
|
||||||
|
pub max_mem_bytes: u64,
|
||||||
|
pub unsupported: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parameters for rendering scatter plot
|
||||||
|
pub(super) struct ScatterPlotParams<'a> {
|
||||||
|
pub process: &'a socktop_connector::DetailedProcessInfo,
|
||||||
|
pub main_user_ms: f64,
|
||||||
|
pub main_system_ms: f64,
|
||||||
|
pub max_user: f64,
|
||||||
|
pub max_system: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum ModalType {
|
||||||
|
ConnectionError {
|
||||||
|
message: String,
|
||||||
|
disconnected_at: Instant,
|
||||||
|
retry_count: u32,
|
||||||
|
auto_retry_countdown: Option<u64>,
|
||||||
|
},
|
||||||
|
ProcessDetails {
|
||||||
|
pid: u32,
|
||||||
|
},
|
||||||
|
About,
|
||||||
|
Help,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
Confirmation {
|
||||||
|
title: String,
|
||||||
|
message: String,
|
||||||
|
confirm_text: String,
|
||||||
|
cancel_text: String,
|
||||||
|
},
|
||||||
|
#[allow(dead_code)]
|
||||||
|
Info {
|
||||||
|
title: String,
|
||||||
|
message: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum ModalAction {
|
||||||
|
None, // Modal didn't handle the key, pass to main window
|
||||||
|
Handled, // Modal handled the key, don't pass to main window
|
||||||
|
RetryConnection,
|
||||||
|
ExitApp,
|
||||||
|
Confirm,
|
||||||
|
Cancel,
|
||||||
|
Dismiss,
|
||||||
|
SwitchToParentProcess(u32), // Switch to viewing parent process details
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum ModalButton {
|
||||||
|
Retry,
|
||||||
|
Exit,
|
||||||
|
Confirm,
|
||||||
|
Cancel,
|
||||||
|
Ok,
|
||||||
|
}
|
||||||
@@ -11,12 +11,12 @@ pub fn draw_net_spark(
|
|||||||
f: &mut ratatui::Frame<'_>,
|
f: &mut ratatui::Frame<'_>,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
title: &str,
|
title: &str,
|
||||||
hist: &VecDeque<u64>,
|
hist: &mut VecDeque<u64>,
|
||||||
color: Color,
|
color: Color,
|
||||||
) {
|
) {
|
||||||
let max_points = area.width.saturating_sub(2) as usize;
|
let max_points = area.width.saturating_sub(2) as usize;
|
||||||
let start = hist.len().saturating_sub(max_points);
|
let start = hist.len().saturating_sub(max_points);
|
||||||
let data: Vec<u64> = hist.iter().skip(start).cloned().collect();
|
let slice = &hist.make_contiguous()[start..];
|
||||||
|
|
||||||
let spark = Sparkline::default()
|
let spark = Sparkline::default()
|
||||||
.block(
|
.block(
|
||||||
@@ -24,7 +24,7 @@ pub fn draw_net_spark(
|
|||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.title(title.to_string()),
|
.title(title.to_string()),
|
||||||
)
|
)
|
||||||
.data(&data)
|
.data(slice)
|
||||||
.style(Style::default().fg(color));
|
.style(Style::default().fg(color));
|
||||||
f.render_widget(spark, area);
|
f.render_widget(spark, area);
|
||||||
}
|
}
|
||||||
|
|||||||
+892
-94
File diff suppressed because it is too large
Load Diff
+36
-1
@@ -30,6 +30,15 @@ pub const BTN_EXIT_BG_ACTIVE: Color = Color::Rgb(255, 255, 255); // modern red
|
|||||||
pub const BTN_EXIT_FG_ACTIVE: Color = Color::Rgb(26, 26, 46);
|
pub const BTN_EXIT_FG_ACTIVE: Color = Color::Rgb(26, 26, 46);
|
||||||
pub const BTN_EXIT_FG_INACTIVE: Color = Color::Rgb(255, 255, 255);
|
pub const BTN_EXIT_FG_INACTIVE: Color = Color::Rgb(255, 255, 255);
|
||||||
|
|
||||||
|
// Process selection colors
|
||||||
|
pub const PROCESS_SELECTION_BG: Color = Color::Rgb(147, 112, 219); // Medium slate blue (purple)
|
||||||
|
pub const PROCESS_SELECTION_FG: Color = Color::Rgb(255, 255, 255); // White text for contrast
|
||||||
|
pub const PROCESS_TOOLTIP_BG: Color = Color::Rgb(147, 112, 219); // Same purple as selection
|
||||||
|
pub const PROCESS_TOOLTIP_FG: Color = Color::Rgb(255, 255, 255); // White text for contrast
|
||||||
|
|
||||||
|
// Process details modal colors (matches main UI aesthetic - no custom colors, terminal defaults)
|
||||||
|
pub const PROCESS_DETAILS_ACCENT: Color = Color::Rgb(147, 112, 219); // Purple accent for highlights
|
||||||
|
|
||||||
// Emoji / icon strings (centralized so they can be themed/swapped later)
|
// Emoji / icon strings (centralized so they can be themed/swapped later)
|
||||||
pub const ICON_WARNING_TITLE: &str = " 🔌 CONNECTION ERROR ";
|
pub const ICON_WARNING_TITLE: &str = " 🔌 CONNECTION ERROR ";
|
||||||
pub const ICON_CLUSTER: &str = "⚠️";
|
pub const ICON_CLUSTER: &str = "⚠️";
|
||||||
@@ -40,7 +49,7 @@ pub const ICON_COUNTDOWN_LABEL: &str = "⏰ Next auto retry: ";
|
|||||||
pub const BTN_RETRY_TEXT: &str = " 🔄 Retry ";
|
pub const BTN_RETRY_TEXT: &str = " 🔄 Retry ";
|
||||||
pub const BTN_EXIT_TEXT: &str = " ❌ Exit ";
|
pub const BTN_EXIT_TEXT: &str = " ❌ Exit ";
|
||||||
|
|
||||||
// Large multi-line warning icon
|
// warning icon
|
||||||
pub const LARGE_ERROR_ICON: &[&str] = &[
|
pub const LARGE_ERROR_ICON: &[&str] = &[
|
||||||
" /\\ ",
|
" /\\ ",
|
||||||
" / \\ ",
|
" / \\ ",
|
||||||
@@ -51,3 +60,29 @@ pub const LARGE_ERROR_ICON: &[&str] = &[
|
|||||||
" / !! \\ ",
|
" / !! \\ ",
|
||||||
" /______________\\ ",
|
" /______________\\ ",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
//about logo
|
||||||
|
pub const ASCII_ART: &str = r#"
|
||||||
|
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
||||||
|
⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣶⣾⠿⠿⠛⠃⠀⠀⠀⠀⠀⣀⣀⣠⡄⠀⠀⠀⠀⠀
|
||||||
|
⠀⠀⠀⠀⠀⠀⠀⠀⠘⠛⢉⣠⣴⣾⣿⠿⠆⢰⣾⡿⠿⠛⠛⠋⠁⠀⠀⠀⠀⠀
|
||||||
|
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⠟⠋⣁⣤⣤⣶⠀⣠⣤⣶⣾⣿⣿⡿⠀⠀⠀⠀⠀
|
||||||
|
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣶⣿⣿⣿⣿⣿⡆⠘⠛⢉⣁⣤⣤⣤⡀⠀⠀⠀⠀
|
||||||
|
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀
|
||||||
|
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣧⠈⢿⣿⣿⣿⣿⣷⠀⠀⠀⠀
|
||||||
|
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣧⠈⢿⣿⣿⣿⣿⡄⠀⠀⠀
|
||||||
|
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⠿⠋⣁⠀⢿⣿⣿⣿⣷⡀⠀⠀
|
||||||
|
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣴⣿⣿⣿⣿⡟⢁⣴⣿⣿⡇⢸⣿⣿⡿⠟⠃⠀⠀
|
||||||
|
⠀⠀⠀⠀⠀⠀⢀⣠⣴⣿⣿⣿⣿⣿⣿⡟⢀⣿⣿⣿⡟⢀⣾⠟⢁⣤⣶⣿⠀⠀
|
||||||
|
⠀⠀⠀⠀⣠⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠸⡿⠟⢋⣠⣾⠃⣰⣿⣿⣿⡟⠀⠀
|
||||||
|
⠀⠀⣴⣄⠙⣿⣿⣿⣿⣿⡿⠿⠛⠋⣉⣁⣤⣴⣶⣿⣿⣿⠀⣿⡿⠟⠋⠀⠀⠀
|
||||||
|
⠀⠀⣿⣿⡆⠹⠟⠋⣁⣤⡄⢰⣿⠿⠟⠛⠋⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
||||||
|
⠀⠀⠈⠉⠁⠀⠀⠀⠙⠛⠃⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
||||||
|
|
||||||
|
███████╗ ██████╗ ██████╗████████╗ ██████╗ ██████╗
|
||||||
|
██╔════╝██╔═══██╗██╔════╝╚══██╔══╝██╔═══██╗██╔══██╗
|
||||||
|
███████╗██║ ██║██║ ██║ ██║ ██║██████╔╝
|
||||||
|
╚════██║██║ ██║██║ ██║ ██║ ██║██╔═══╝
|
||||||
|
███████║╚██████╔╝╚██████╗ ██║ ╚██████╔╝██║
|
||||||
|
╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝
|
||||||
|
"#;
|
||||||
|
|||||||
@@ -73,3 +73,36 @@ fn test_tlc_ca_arg_long_and_short_parsed() {
|
|||||||
);
|
);
|
||||||
assert!(text3.contains("Usage:"));
|
assert!(text3.contains("Usage:"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_compact_flag_documented_and_accepted() {
|
||||||
|
let exe = env!("CARGO_BIN_EXE_socktop");
|
||||||
|
let out = Command::new(exe)
|
||||||
|
.args(["--compact", "--help"])
|
||||||
|
.output()
|
||||||
|
.expect("run socktop --compact --help");
|
||||||
|
assert!(
|
||||||
|
out.status.success(),
|
||||||
|
"socktop --compact --help did not succeed"
|
||||||
|
);
|
||||||
|
let text = format!(
|
||||||
|
"{}{}",
|
||||||
|
String::from_utf8_lossy(&out.stdout),
|
||||||
|
String::from_utf8_lossy(&out.stderr)
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
text.contains("--compact"),
|
||||||
|
"help text missing --compact\n{text}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// The flag must not be mistaken for the positional URL argument.
|
||||||
|
let out2 = Command::new(exe)
|
||||||
|
.args(["--compact", "--dry-run", "ws://127.0.0.1:3000/ws"])
|
||||||
|
.output()
|
||||||
|
.expect("run socktop --compact --dry-run");
|
||||||
|
assert!(
|
||||||
|
out2.status.success(),
|
||||||
|
"socktop --compact with a URL was rejected: {}",
|
||||||
|
String::from_utf8_lossy(&out2.stderr)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,39 +1,68 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "socktop_agent"
|
name = "socktop_agent"
|
||||||
version = "1.40.70"
|
version = "1.50.2"
|
||||||
authors = ["Jason Witty <jasonpwitty+socktop@proton.me>"]
|
authors = ["Jason Witty <jasonpwitty+socktop@proton.me>"]
|
||||||
description = "Socktop agent daemon. Serves host metrics over WebSocket."
|
description = "Socktop agent daemon. Serves host metrics over WebSocket."
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
homepage = "https://github.com/jasonwitty/socktop"
|
||||||
|
repository = "https://github.com/jasonwitty/socktop"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "1", features = ["full"] }
|
# Tokio: Use minimal features instead of "full" to reduce binary size
|
||||||
|
# Only include: rt-multi-thread (async runtime), net (WebSocket), sync (Mutex/RwLock), macros (#[tokio::test])
|
||||||
|
# Excluded: io, fs, process, signal, time (not needed for this workload)
|
||||||
|
# Savings: ~200-300KB binary size, faster compile times
|
||||||
|
tokio = { version = "1", features = ["rt-multi-thread", "net", "sync", "macros"] }
|
||||||
axum = { version = "0.7", features = ["ws", "macros"] }
|
axum = { version = "0.7", features = ["ws", "macros"] }
|
||||||
sysinfo = { version = "0.37", features = ["network", "disk", "component"] }
|
sysinfo = { version = "0.37", features = ["network", "disk", "component"] }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
|
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
|
||||||
futures-util = "0.3.31"
|
futures-util = "0.3.31"
|
||||||
tracing = "0.1"
|
tracing = { version = "0.1", optional = true }
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"], optional = true }
|
||||||
# nvml-wrapper removed (unused; GPU metrics via gfxinfo only now)
|
gfxinfo = { version = "0.1.2", optional = true }
|
||||||
gfxinfo = "0.1.2"
|
|
||||||
once_cell = "1.19"
|
once_cell = "1.19"
|
||||||
axum-server = { version = "0.6", features = ["tls-rustls"] }
|
axum-server = { version = "0.7", features = ["tls-rustls"] }
|
||||||
rustls = "0.23"
|
rustls = { version = "0.23", features = ["aws-lc-rs"] }
|
||||||
rustls-pemfile = "2.1"
|
rustls-pemfile = "2.1"
|
||||||
rcgen = "0.13" # pure-Rust self-signed cert generation (replaces openssl vendored build)
|
rcgen = "0.13"
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
hostname = "0.3"
|
hostname = "0.3"
|
||||||
prost = { workspace = true }
|
prost = { workspace = true }
|
||||||
time = { version = "0.3", default-features = false, features = ["formatting", "macros", "parsing" ] }
|
time = { version = "0.3", default-features = false, features = ["formatting", "macros", "parsing" ] }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["gpu"]
|
||||||
|
gpu = ["gfxinfo"]
|
||||||
|
logging = ["tracing", "tracing-subscriber"]
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
prost-build = "0.13"
|
prost-build = "0.13"
|
||||||
tonic-build = { version = "0.12", default-features = false, optional = true }
|
tonic-build = { version = "0.12", default-features = false, optional = true }
|
||||||
protoc-bin-vendored = "3"
|
protoc-bin-vendored = "3"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
assert_cmd = "2.0"
|
assert_cmd = "2.0"
|
||||||
tempfile = "3.10"
|
tempfile = "3.10"
|
||||||
tokio-tungstenite = "0.21"
|
tokio-tungstenite = "0.21"
|
||||||
|
|
||||||
|
[package.metadata.deb]
|
||||||
|
maintainer = "Jason Witty <jasonpwitty+socktop@proton.me>"
|
||||||
|
copyright = "2024, Jason Witty <jasonpwitty+socktop@proton.me>"
|
||||||
|
license-file = ["../LICENSE", "4"]
|
||||||
|
extended-description = """\
|
||||||
|
socktop_agent is the daemon component that runs on remote hosts to collect \
|
||||||
|
and serve system metrics over WebSocket. It gathers CPU, memory, disk, network, \
|
||||||
|
GPU, and process information that can be monitored remotely by the socktop TUI client."""
|
||||||
|
depends = "$auto"
|
||||||
|
section = "admin"
|
||||||
|
priority = "optional"
|
||||||
|
assets = [
|
||||||
|
["target/release/socktop_agent", "usr/bin/", "755"],
|
||||||
|
["../README.md", "usr/share/doc/socktop_agent/", "644"],
|
||||||
|
]
|
||||||
|
maintainer-scripts = "debian/"
|
||||||
|
systemd-units = { unit-name = "socktop-agent", unit-scripts = ".", enable = false }
|
||||||
|
|||||||
Executable
+57
@@ -0,0 +1,57 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Create socktop user and group if they don't exist
|
||||||
|
if ! getent group socktop >/dev/null; then
|
||||||
|
addgroup --system socktop
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! getent passwd socktop >/dev/null; then
|
||||||
|
adduser --system --ingroup socktop --home /var/lib/socktop \
|
||||||
|
--no-create-home --disabled-password --disabled-login \
|
||||||
|
--gecos "Socktop Agent" socktop
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create state directory
|
||||||
|
mkdir -p /var/lib/socktop
|
||||||
|
chown socktop:socktop /var/lib/socktop
|
||||||
|
chmod 755 /var/lib/socktop
|
||||||
|
|
||||||
|
# Create config directory if it doesn't exist
|
||||||
|
mkdir -p /etc/socktop
|
||||||
|
chmod 755 /etc/socktop
|
||||||
|
|
||||||
|
#DEBHELPER#
|
||||||
|
|
||||||
|
# Print helpful message to the user
|
||||||
|
cat <<EOF
|
||||||
|
|
||||||
|
┌─────────────────────────────────────────────────────────────────────┐
|
||||||
|
│ socktop-agent has been installed successfully! │
|
||||||
|
├─────────────────────────────────────────────────────────────────────┤
|
||||||
|
│ │
|
||||||
|
│ The systemd service has been installed but is NOT enabled by │
|
||||||
|
│ default. To enable and start the service: │
|
||||||
|
│ │
|
||||||
|
│ sudo systemctl enable --now socktop-agent │
|
||||||
|
│ │
|
||||||
|
│ To start without enabling on boot: │
|
||||||
|
│ │
|
||||||
|
│ sudo systemctl start socktop-agent │
|
||||||
|
│ │
|
||||||
|
│ To check service status: │
|
||||||
|
│ │
|
||||||
|
│ sudo systemctl status socktop-agent │
|
||||||
|
│ │
|
||||||
|
│ Default settings: │
|
||||||
|
│ - Port: 3000 (use -p or --port to change) │
|
||||||
|
│ - SSL/TLS: disabled (use --enableSSL to enable) │
|
||||||
|
│ │
|
||||||
|
│ For more information, see: │
|
||||||
|
│ /usr/share/doc/socktop_agent/README.md │
|
||||||
|
│ │
|
||||||
|
└─────────────────────────────────────────────────────────────────────┘
|
||||||
|
|
||||||
|
EOF
|
||||||
|
|
||||||
|
exit 0
|
||||||
Executable
+34
@@ -0,0 +1,34 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
purge)
|
||||||
|
# Remove user and group on purge
|
||||||
|
if getent passwd socktop >/dev/null; then
|
||||||
|
deluser --quiet socktop || true
|
||||||
|
fi
|
||||||
|
|
||||||
|
if getent group socktop >/dev/null; then
|
||||||
|
delgroup --quiet socktop || true
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Remove state directory on purge
|
||||||
|
rm -rf /var/lib/socktop
|
||||||
|
|
||||||
|
# Remove config directory if empty
|
||||||
|
rmdir --ignore-fail-on-non-empty /etc/socktop 2>/dev/null || true
|
||||||
|
;;
|
||||||
|
|
||||||
|
remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
|
||||||
|
# Do nothing on remove/upgrade
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
echo "postrm called with unknown argument \`$1'" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
#DEBHELPER#
|
||||||
|
|
||||||
|
exit 0
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Socktop Agent - Remote System Monitor
|
||||||
|
Documentation=https://github.com/jasonwitty/socktop
|
||||||
|
After=network-online.target
|
||||||
|
Wants=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
ExecStart=/usr/bin/socktop_agent --port 3000
|
||||||
|
Environment=RUST_LOG=info
|
||||||
|
# Optional authentication token:
|
||||||
|
# Environment=SOCKTOP_TOKEN=changeme
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=5
|
||||||
|
User=socktop
|
||||||
|
Group=socktop
|
||||||
|
NoNewPrivileges=true
|
||||||
|
|
||||||
|
# Security hardening
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectSystem=strict
|
||||||
|
ProtectHome=true
|
||||||
|
ReadWritePaths=/var/lib/socktop
|
||||||
|
StateDirectory=socktop
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
// gpu.rs
|
// gpu.rs
|
||||||
|
#[cfg(feature = "gpu")]
|
||||||
use gfxinfo::active_gpu;
|
use gfxinfo::active_gpu;
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize)]
|
#[derive(Debug, Clone, serde::Serialize)]
|
||||||
@@ -9,6 +10,7 @@ pub struct GpuMetrics {
|
|||||||
pub mem_total_bytes: u64,
|
pub mem_total_bytes: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gpu")]
|
||||||
pub fn collect_all_gpus() -> Result<Vec<GpuMetrics>, Box<dyn std::error::Error>> {
|
pub fn collect_all_gpus() -> Result<Vec<GpuMetrics>, Box<dyn std::error::Error>> {
|
||||||
let gpu = active_gpu()?; // Use ? to unwrap Result
|
let gpu = active_gpu()?; // Use ? to unwrap Result
|
||||||
let info = gpu.info();
|
let info = gpu.info();
|
||||||
@@ -22,3 +24,9 @@ pub fn collect_all_gpus() -> Result<Vec<GpuMetrics>, Box<dyn std::error::Error>>
|
|||||||
|
|
||||||
Ok(vec![metrics])
|
Ok(vec![metrics])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "gpu"))]
|
||||||
|
pub fn collect_all_gpus() -> Result<Vec<GpuMetrics>, Box<dyn std::error::Error>> {
|
||||||
|
// GPU support not available on this platform
|
||||||
|
Ok(vec![])
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
//! Library interface for socktop_agent functionality
|
||||||
|
//! This allows testing of agent functions.
|
||||||
|
|
||||||
|
pub mod gpu;
|
||||||
|
pub mod metrics;
|
||||||
|
pub mod proto;
|
||||||
|
pub mod state;
|
||||||
|
pub mod tls;
|
||||||
|
pub mod types;
|
||||||
|
pub mod ws;
|
||||||
|
|
||||||
|
// Re-export commonly used types and functions for testing
|
||||||
|
pub use metrics::{collect_journal_entries, collect_process_metrics};
|
||||||
|
pub use state::{AppState, CacheEntry};
|
||||||
|
pub use types::{
|
||||||
|
DetailedProcessInfo, JournalEntry, JournalResponse, LogLevel, ProcessMetricsResponse,
|
||||||
|
};
|
||||||
@@ -29,10 +29,53 @@ fn arg_value(name: &str) -> Option<String> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
fn main() -> anyhow::Result<()> {
|
||||||
async fn main() -> anyhow::Result<()> {
|
// Install rustls crypto provider before any TLS operations
|
||||||
|
// This is required when using axum-server's tls-rustls feature
|
||||||
|
rustls::crypto::aws_lc_rs::default_provider()
|
||||||
|
.install_default()
|
||||||
|
.ok(); // Ignore error if already installed
|
||||||
|
|
||||||
|
#[cfg(feature = "logging")]
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
// Configure Tokio runtime with optimized thread pool for reduced overhead.
|
||||||
|
//
|
||||||
|
// The agent is primarily I/O-bound (WebSocket, /proc file reads, sysinfo)
|
||||||
|
// with no CPU-intensive or blocking operations, so a smaller thread pool
|
||||||
|
// is beneficial:
|
||||||
|
//
|
||||||
|
// Benefits:
|
||||||
|
// - Lower memory footprint (~1-2MB per thread saved)
|
||||||
|
// - Reduced context switching overhead
|
||||||
|
// - Fewer idle threads consuming resources
|
||||||
|
// - Better for resource-constrained systems
|
||||||
|
//
|
||||||
|
// Trade-offs:
|
||||||
|
// - Slightly reduced throughput under very high concurrent connections
|
||||||
|
// - Could introduce latency if blocking operations are added (don't do this!)
|
||||||
|
//
|
||||||
|
// Default: 2 threads (sufficient for typical workloads with 1-10 clients)
|
||||||
|
// Override: Set SOCKTOP_WORKER_THREADS=4 to use more threads if needed
|
||||||
|
//
|
||||||
|
// Note: Default Tokio uses num_cpus threads which is excessive for this workload.
|
||||||
|
|
||||||
|
let worker_threads = std::env::var("SOCKTOP_WORKER_THREADS")
|
||||||
|
.ok()
|
||||||
|
.and_then(|s| s.parse::<usize>().ok())
|
||||||
|
.unwrap_or(2)
|
||||||
|
.clamp(1, 16); // Ensure 1-16 threads
|
||||||
|
|
||||||
|
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||||
|
.worker_threads(worker_threads)
|
||||||
|
.thread_name("socktop-agent")
|
||||||
|
.enable_all()
|
||||||
|
.build()?;
|
||||||
|
|
||||||
|
runtime.block_on(async_main())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn async_main() -> anyhow::Result<()> {
|
||||||
// Version flag (print and exit). Keep before heavy initialization.
|
// Version flag (print and exit). Keep before heavy initialization.
|
||||||
if arg_flag("--version") || arg_flag("-V") {
|
if arg_flag("--version") || arg_flag("-V") {
|
||||||
println!("socktop_agent {}", env!("CARGO_PKG_VERSION"));
|
println!("socktop_agent {}", env!("CARGO_PKG_VERSION"));
|
||||||
|
|||||||
+1046
-85
File diff suppressed because it is too large
Load Diff
@@ -17,6 +17,10 @@ pub type SharedNetworks = Arc<Mutex<Networks>>;
|
|||||||
pub struct ProcCpuTracker {
|
pub struct ProcCpuTracker {
|
||||||
pub last_total: u64,
|
pub last_total: u64,
|
||||||
pub last_per_pid: HashMap<u32, u64>,
|
pub last_per_pid: HashMap<u32, u64>,
|
||||||
|
/// PID → process name cache. Mirrors the non-Linux `ProcessCache.names`.
|
||||||
|
/// On a Pi with ~150-300 mostly-stable processes this avoids re-allocating
|
||||||
|
/// the same `String`s on every processes poll (~once per 1.5s).
|
||||||
|
pub names: HashMap<u32, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
@@ -63,6 +67,11 @@ pub struct AppState {
|
|||||||
pub cache_metrics: Arc<Mutex<CacheEntry<crate::types::Metrics>>>,
|
pub cache_metrics: Arc<Mutex<CacheEntry<crate::types::Metrics>>>,
|
||||||
pub cache_disks: Arc<Mutex<CacheEntry<Vec<crate::types::DiskInfo>>>>,
|
pub cache_disks: Arc<Mutex<CacheEntry<Vec<crate::types::DiskInfo>>>>,
|
||||||
pub cache_processes: Arc<Mutex<CacheEntry<crate::types::ProcessesPayload>>>,
|
pub cache_processes: Arc<Mutex<CacheEntry<crate::types::ProcessesPayload>>>,
|
||||||
|
|
||||||
|
// Process detail caches (per-PID)
|
||||||
|
pub cache_process_metrics:
|
||||||
|
Arc<Mutex<HashMap<u32, CacheEntry<crate::types::ProcessMetricsResponse>>>>,
|
||||||
|
pub cache_journal_entries: Arc<Mutex<HashMap<u32, CacheEntry<crate::types::JournalResponse>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@@ -71,6 +80,12 @@ pub struct CacheEntry<T> {
|
|||||||
pub value: Option<T>,
|
pub value: Option<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Default for CacheEntry<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> CacheEntry<T> {
|
impl<T> CacheEntry<T> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -90,6 +105,12 @@ impl<T> CacheEntry<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for AppState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let sys = System::new();
|
let sys = System::new();
|
||||||
@@ -116,6 +137,8 @@ impl AppState {
|
|||||||
cache_metrics: Arc::new(Mutex::new(CacheEntry::new())),
|
cache_metrics: Arc::new(Mutex::new(CacheEntry::new())),
|
||||||
cache_disks: Arc::new(Mutex::new(CacheEntry::new())),
|
cache_disks: Arc::new(Mutex::new(CacheEntry::new())),
|
||||||
cache_processes: Arc::new(Mutex::new(CacheEntry::new())),
|
cache_processes: Arc::new(Mutex::new(CacheEntry::new())),
|
||||||
|
cache_process_metrics: Arc::new(Mutex::new(HashMap::new())),
|
||||||
|
cache_journal_entries: Arc::new(Mutex::new(HashMap::new())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ pub struct DiskInfo {
|
|||||||
pub name: String,
|
pub name: String,
|
||||||
pub total: u64,
|
pub total: u64,
|
||||||
pub available: u64,
|
pub available: u64,
|
||||||
|
pub temperature: Option<f32>,
|
||||||
|
pub is_partition: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
@@ -47,3 +49,76 @@ pub struct ProcessesPayload {
|
|||||||
pub process_count: usize,
|
pub process_count: usize,
|
||||||
pub top_processes: Vec<ProcessInfo>,
|
pub top_processes: Vec<ProcessInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub struct ThreadInfo {
|
||||||
|
pub tid: u32, // Thread ID
|
||||||
|
pub name: String, // Thread name (from /proc/{pid}/task/{tid}/comm)
|
||||||
|
pub cpu_time_user: u64, // User CPU time in microseconds
|
||||||
|
pub cpu_time_system: u64, // System CPU time in microseconds
|
||||||
|
pub status: String, // Thread status (Running, Sleeping, etc.)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub struct DetailedProcessInfo {
|
||||||
|
pub pid: u32,
|
||||||
|
pub name: String,
|
||||||
|
pub command: String,
|
||||||
|
pub cpu_usage: f32,
|
||||||
|
pub mem_bytes: u64,
|
||||||
|
pub virtual_mem_bytes: u64,
|
||||||
|
pub shared_mem_bytes: Option<u64>,
|
||||||
|
pub thread_count: u32,
|
||||||
|
pub fd_count: Option<u32>,
|
||||||
|
pub status: String,
|
||||||
|
pub parent_pid: Option<u32>,
|
||||||
|
pub user_id: u32,
|
||||||
|
pub group_id: u32,
|
||||||
|
pub start_time: u64, // Unix timestamp
|
||||||
|
pub cpu_time_user: u64, // Microseconds
|
||||||
|
pub cpu_time_system: u64, // Microseconds
|
||||||
|
pub read_bytes: Option<u64>,
|
||||||
|
pub write_bytes: Option<u64>,
|
||||||
|
pub working_directory: Option<String>,
|
||||||
|
pub executable_path: Option<String>,
|
||||||
|
pub child_processes: Vec<DetailedProcessInfo>,
|
||||||
|
pub threads: Vec<ThreadInfo>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub struct ProcessMetricsResponse {
|
||||||
|
pub process: DetailedProcessInfo,
|
||||||
|
pub cached_at: u64, // Unix timestamp when this data was cached
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub struct JournalEntry {
|
||||||
|
pub timestamp: String, // ISO 8601 formatted timestamp
|
||||||
|
pub priority: LogLevel,
|
||||||
|
pub message: String,
|
||||||
|
pub unit: Option<String>, // systemd unit name
|
||||||
|
pub pid: Option<u32>,
|
||||||
|
pub comm: Option<String>, // process command name
|
||||||
|
pub uid: Option<u32>,
|
||||||
|
pub gid: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub enum LogLevel {
|
||||||
|
Emergency = 0,
|
||||||
|
Alert = 1,
|
||||||
|
Critical = 2,
|
||||||
|
Error = 3,
|
||||||
|
Warning = 4,
|
||||||
|
Notice = 5,
|
||||||
|
Info = 6,
|
||||||
|
Debug = 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub struct JournalResponse {
|
||||||
|
pub entries: Vec<JournalEntry>,
|
||||||
|
pub total_count: u32,
|
||||||
|
pub truncated: bool,
|
||||||
|
pub cached_at: u64, // Unix timestamp when this data was cached
|
||||||
|
}
|
||||||
|
|||||||
+114
-19
@@ -17,6 +17,8 @@ use crate::proto::pb;
|
|||||||
use crate::state::AppState;
|
use crate::state::AppState;
|
||||||
|
|
||||||
// Compression threshold based on typical payload size
|
// Compression threshold based on typical payload size
|
||||||
|
// Temporarily increased for testing - revert to 768 for production
|
||||||
|
//const COMPRESSION_THRESHOLD: usize = 50_000;
|
||||||
const COMPRESSION_THRESHOLD: usize = 768;
|
const COMPRESSION_THRESHOLD: usize = 768;
|
||||||
|
|
||||||
// Reusable buffer for compression to avoid allocations
|
// Reusable buffer for compression to avoid allocations
|
||||||
@@ -67,12 +69,12 @@ async fn handle_socket(mut socket: WebSocket, state: AppState) {
|
|||||||
Message::Text(ref text) if text == "get_processes" => {
|
Message::Text(ref text) if text == "get_processes" => {
|
||||||
let payload = collect_processes_all(&state).await;
|
let payload = collect_processes_all(&state).await;
|
||||||
|
|
||||||
// Map to protobuf message
|
// Get cached buffers. The Vec capacity is preserved across
|
||||||
// Get cached buffers
|
// calls (with_capacity(512) seeds it, then we swap-back after
|
||||||
|
// encode so the allocation outlives any single request).
|
||||||
let cache = COMPRESSION_CACHE.get_or_init(|| Mutex::new(CompressionCache::new()));
|
let cache = COMPRESSION_CACHE.get_or_init(|| Mutex::new(CompressionCache::new()));
|
||||||
let mut cache = cache.lock().await;
|
let mut cache = cache.lock().await;
|
||||||
|
|
||||||
// Reuse process vector to build the list
|
|
||||||
cache.processes_vec.clear();
|
cache.processes_vec.clear();
|
||||||
cache
|
cache
|
||||||
.processes_vec
|
.processes_vec
|
||||||
@@ -83,34 +85,127 @@ async fn handle_socket(mut socket: WebSocket, state: AppState) {
|
|||||||
mem_bytes: p.mem_bytes,
|
mem_bytes: p.mem_bytes,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let pb = pb::Processes {
|
// Move the populated Vec into the proto, encode, then move it
|
||||||
|
// BACK into the cache so the next call reuses the same heap
|
||||||
|
// allocation. The previous code did `mem::take(...)` here but
|
||||||
|
// then dropped `pb` (and the Vec along with it), leaving the
|
||||||
|
// cache holding an empty zero-capacity Vec — defeating the
|
||||||
|
// whole point of `with_capacity(512)`.
|
||||||
|
let mut pb = pb::Processes {
|
||||||
process_count: payload.process_count as u64,
|
process_count: payload.process_count as u64,
|
||||||
rows: std::mem::take(&mut cache.processes_vec),
|
rows: std::mem::take(&mut cache.processes_vec),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut buf = Vec::with_capacity(8 * 1024);
|
let mut buf = Vec::with_capacity(8 * 1024);
|
||||||
if prost::Message::encode(&pb, &mut buf).is_err() {
|
let encode_result = prost::Message::encode(&pb, &mut buf);
|
||||||
|
// Restore the (now-encoded-from) Vec to the cache before pb is
|
||||||
|
// dropped. We `take` it out of pb to leave that field empty,
|
||||||
|
// and the next request will `.clear()` before refilling.
|
||||||
|
cache.processes_vec = std::mem::take(&mut pb.rows);
|
||||||
|
|
||||||
|
if encode_result.is_err() {
|
||||||
let _ = socket.send(Message::Close(None)).await;
|
let _ = socket.send(Message::Close(None)).await;
|
||||||
|
} else if buf.len() <= COMPRESSION_THRESHOLD {
|
||||||
|
let _ = socket.send(Message::Binary(buf)).await;
|
||||||
} else {
|
} else {
|
||||||
// compress if large
|
// Create a new encoder for each message to ensure proper gzip headers
|
||||||
if buf.len() <= COMPRESSION_THRESHOLD {
|
let mut encoder =
|
||||||
let _ = socket.send(Message::Binary(buf)).await;
|
GzEncoder::new(Vec::with_capacity(buf.len()), Compression::fast());
|
||||||
} else {
|
match encoder.write_all(&buf).and_then(|_| encoder.finish()) {
|
||||||
// Create a new encoder for each message to ensure proper gzip headers
|
Ok(compressed) => {
|
||||||
let mut encoder =
|
let _ = socket.send(Message::Binary(compressed)).await;
|
||||||
GzEncoder::new(Vec::with_capacity(buf.len()), Compression::fast());
|
}
|
||||||
match encoder.write_all(&buf).and_then(|_| encoder.finish()) {
|
Err(_) => {
|
||||||
Ok(compressed) => {
|
let _ = socket.send(Message::Binary(buf)).await;
|
||||||
let _ = socket.send(Message::Binary(compressed)).await;
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
let _ = socket.send(Message::Binary(buf)).await;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
drop(cache); // Explicit drop to release mutex early
|
drop(cache); // Explicit drop to release mutex early
|
||||||
}
|
}
|
||||||
|
Message::Text(ref text) if text.starts_with("get_process_metrics:") => {
|
||||||
|
if let Some(pid_str) = text.strip_prefix("get_process_metrics:")
|
||||||
|
&& let Ok(pid) = pid_str.parse::<u32>()
|
||||||
|
{
|
||||||
|
let ttl = std::time::Duration::from_millis(250); // 250ms TTL
|
||||||
|
|
||||||
|
// Check cache first
|
||||||
|
{
|
||||||
|
let cache = state.cache_process_metrics.lock().await;
|
||||||
|
if let Some(entry) = cache.get(&pid)
|
||||||
|
&& entry.is_fresh(ttl)
|
||||||
|
&& let Some(cached_response) = entry.get()
|
||||||
|
{
|
||||||
|
let _ = send_json(&mut socket, cached_response).await;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect fresh data
|
||||||
|
match crate::metrics::collect_process_metrics(pid, &state).await {
|
||||||
|
Ok(response) => {
|
||||||
|
// Cache the response
|
||||||
|
{
|
||||||
|
let mut cache = state.cache_process_metrics.lock().await;
|
||||||
|
cache
|
||||||
|
.entry(pid)
|
||||||
|
.or_insert_with(crate::state::CacheEntry::new)
|
||||||
|
.set(response.clone());
|
||||||
|
}
|
||||||
|
let _ = send_json(&mut socket, &response).await;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
let error_response = serde_json::json!({
|
||||||
|
"error": err,
|
||||||
|
"request": "get_process_metrics",
|
||||||
|
"pid": pid
|
||||||
|
});
|
||||||
|
let _ = send_json(&mut socket, &error_response).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Message::Text(ref text) if text.starts_with("get_journal_entries:") => {
|
||||||
|
if let Some(pid_str) = text.strip_prefix("get_journal_entries:")
|
||||||
|
&& let Ok(pid) = pid_str.parse::<u32>()
|
||||||
|
{
|
||||||
|
let ttl = std::time::Duration::from_secs(1); // 1s TTL
|
||||||
|
|
||||||
|
// Check cache first
|
||||||
|
{
|
||||||
|
let cache = state.cache_journal_entries.lock().await;
|
||||||
|
if let Some(entry) = cache.get(&pid)
|
||||||
|
&& entry.is_fresh(ttl)
|
||||||
|
&& let Some(cached_response) = entry.get()
|
||||||
|
{
|
||||||
|
let _ = send_json(&mut socket, cached_response).await;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect fresh data
|
||||||
|
match crate::metrics::collect_journal_entries(pid) {
|
||||||
|
Ok(response) => {
|
||||||
|
// Cache the response
|
||||||
|
{
|
||||||
|
let mut cache = state.cache_journal_entries.lock().await;
|
||||||
|
cache
|
||||||
|
.entry(pid)
|
||||||
|
.or_insert_with(crate::state::CacheEntry::new)
|
||||||
|
.set(response.clone());
|
||||||
|
}
|
||||||
|
let _ = send_json(&mut socket, &response).await;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
let error_response = serde_json::json!({
|
||||||
|
"error": err,
|
||||||
|
"request": "get_journal_entries",
|
||||||
|
"pid": pid
|
||||||
|
});
|
||||||
|
let _ = send_json(&mut socket, &error_response).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Message::Close(_) => break,
|
Message::Close(_) => break,
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,132 @@
|
|||||||
|
//! Tests for the process cache functionality
|
||||||
|
|
||||||
|
use socktop_agent::state::{AppState, CacheEntry};
|
||||||
|
use socktop_agent::types::{DetailedProcessInfo, JournalResponse, ProcessMetricsResponse};
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::time::sleep;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_process_cache_ttl() {
|
||||||
|
let state = AppState::new();
|
||||||
|
let pid = 12345;
|
||||||
|
|
||||||
|
// Create mock data
|
||||||
|
let process_info = DetailedProcessInfo {
|
||||||
|
pid,
|
||||||
|
name: "test_process".to_string(),
|
||||||
|
command: "test command".to_string(),
|
||||||
|
cpu_usage: 50.0,
|
||||||
|
mem_bytes: 1024 * 1024,
|
||||||
|
virtual_mem_bytes: 2048 * 1024,
|
||||||
|
shared_mem_bytes: Some(512 * 1024),
|
||||||
|
thread_count: 4,
|
||||||
|
fd_count: Some(10),
|
||||||
|
status: "Running".to_string(),
|
||||||
|
parent_pid: Some(1),
|
||||||
|
user_id: 1000,
|
||||||
|
group_id: 1000,
|
||||||
|
start_time: 1234567890,
|
||||||
|
cpu_time_user: 100000,
|
||||||
|
cpu_time_system: 50000,
|
||||||
|
read_bytes: Some(1024),
|
||||||
|
write_bytes: Some(2048),
|
||||||
|
working_directory: Some("/tmp".to_string()),
|
||||||
|
executable_path: Some("/usr/bin/test".to_string()),
|
||||||
|
child_processes: vec![],
|
||||||
|
threads: vec![],
|
||||||
|
};
|
||||||
|
|
||||||
|
let metrics_response = ProcessMetricsResponse {
|
||||||
|
process: process_info,
|
||||||
|
cached_at: 1234567890,
|
||||||
|
};
|
||||||
|
|
||||||
|
let journal_response = JournalResponse {
|
||||||
|
entries: vec![],
|
||||||
|
total_count: 0,
|
||||||
|
truncated: false,
|
||||||
|
cached_at: 1234567890,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test process metrics caching
|
||||||
|
{
|
||||||
|
let mut cache = state.cache_process_metrics.lock().await;
|
||||||
|
cache
|
||||||
|
.entry(pid)
|
||||||
|
.or_insert_with(CacheEntry::new)
|
||||||
|
.set(metrics_response.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should get cached value immediately
|
||||||
|
{
|
||||||
|
let cache = state.cache_process_metrics.lock().await;
|
||||||
|
let ttl = Duration::from_millis(250);
|
||||||
|
if let Some(entry) = cache.get(&pid) {
|
||||||
|
assert!(entry.is_fresh(ttl));
|
||||||
|
assert!(entry.get().is_some());
|
||||||
|
assert_eq!(entry.get().unwrap().process.pid, pid);
|
||||||
|
} else {
|
||||||
|
panic!("Expected cached entry");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("✓ Process metrics cached and retrieved successfully");
|
||||||
|
|
||||||
|
// Test journal entries caching
|
||||||
|
{
|
||||||
|
let mut cache = state.cache_journal_entries.lock().await;
|
||||||
|
cache
|
||||||
|
.entry(pid)
|
||||||
|
.or_insert_with(CacheEntry::new)
|
||||||
|
.set(journal_response.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should get cached value immediately
|
||||||
|
{
|
||||||
|
let cache = state.cache_journal_entries.lock().await;
|
||||||
|
let ttl = Duration::from_secs(1);
|
||||||
|
if let Some(entry) = cache.get(&pid) {
|
||||||
|
assert!(entry.is_fresh(ttl));
|
||||||
|
assert!(entry.get().is_some());
|
||||||
|
assert_eq!(entry.get().unwrap().total_count, 0);
|
||||||
|
} else {
|
||||||
|
panic!("Expected cached entry");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("✓ Journal entries cached and retrieved successfully");
|
||||||
|
|
||||||
|
// Wait for process metrics to expire (250ms + buffer)
|
||||||
|
sleep(Duration::from_millis(300)).await;
|
||||||
|
|
||||||
|
// Process metrics should be expired now
|
||||||
|
{
|
||||||
|
let cache = state.cache_process_metrics.lock().await;
|
||||||
|
let ttl = Duration::from_millis(250);
|
||||||
|
if let Some(entry) = cache.get(&pid) {
|
||||||
|
assert!(!entry.is_fresh(ttl));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("✓ Process metrics correctly expired after TTL");
|
||||||
|
|
||||||
|
// Journal entries should still be valid (1s TTL)
|
||||||
|
{
|
||||||
|
let cache = state.cache_journal_entries.lock().await;
|
||||||
|
let ttl = Duration::from_secs(1);
|
||||||
|
if let Some(entry) = cache.get(&pid) {
|
||||||
|
assert!(entry.is_fresh(ttl));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("✓ Journal entries still valid within TTL");
|
||||||
|
|
||||||
|
// Wait for journal entries to expire (additional 800ms to reach 1s total)
|
||||||
|
sleep(Duration::from_millis(800)).await;
|
||||||
|
|
||||||
|
// Journal entries should be expired now
|
||||||
|
{
|
||||||
|
let cache = state.cache_journal_entries.lock().await;
|
||||||
|
let ttl = Duration::from_secs(1);
|
||||||
|
if let Some(entry) = cache.get(&pid) {
|
||||||
|
assert!(!entry.is_fresh(ttl));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("✓ Journal entries correctly expired after TTL");
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
//! Tests for process detail collection functionality
|
||||||
|
|
||||||
|
use socktop_agent::metrics::{collect_journal_entries, collect_process_metrics};
|
||||||
|
use socktop_agent::state::AppState;
|
||||||
|
use std::process;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_collect_process_metrics_self() {
|
||||||
|
// Test collecting metrics for our own process
|
||||||
|
let pid = process::id();
|
||||||
|
let state = AppState::new();
|
||||||
|
|
||||||
|
match collect_process_metrics(pid, &state).await {
|
||||||
|
Ok(response) => {
|
||||||
|
assert_eq!(response.process.pid, pid);
|
||||||
|
assert!(!response.process.name.is_empty());
|
||||||
|
// Command might be empty on some systems, so don't assert on it
|
||||||
|
assert!(response.cached_at > 0);
|
||||||
|
println!(
|
||||||
|
"✓ Process metrics collected for PID {}: {} ({})",
|
||||||
|
pid, response.process.name, response.process.command
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
// This might fail if sysinfo can't find the process, which is possible
|
||||||
|
println!("⚠ Warning: Failed to collect process metrics for self: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_collect_journal_entries_self() {
|
||||||
|
// Test collecting journal entries for our own process
|
||||||
|
let pid = process::id();
|
||||||
|
|
||||||
|
match collect_journal_entries(pid) {
|
||||||
|
Ok(response) => {
|
||||||
|
assert!(response.cached_at > 0);
|
||||||
|
println!(
|
||||||
|
"✓ Journal entries collected for PID {}: {} entries",
|
||||||
|
pid, response.total_count
|
||||||
|
);
|
||||||
|
if !response.entries.is_empty() {
|
||||||
|
let entry = &response.entries[0];
|
||||||
|
println!(" Latest entry: {}", entry.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
// This might fail if journalctl is not available or restricted
|
||||||
|
println!("⚠ Warning: Failed to collect journal entries for self: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_collect_process_metrics_invalid_pid() {
|
||||||
|
// Test with an invalid PID
|
||||||
|
let invalid_pid = 999999;
|
||||||
|
let state = AppState::new();
|
||||||
|
|
||||||
|
match collect_process_metrics(invalid_pid, &state).await {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("⚠ Warning: Unexpectedly found process for invalid PID {invalid_pid}");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("✓ Correctly failed for invalid PID {invalid_pid}: {e}");
|
||||||
|
assert!(e.contains("not found"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_collect_journal_entries_invalid_pid() {
|
||||||
|
// Test with an invalid PID - journalctl might still return empty results
|
||||||
|
let invalid_pid = 999999;
|
||||||
|
|
||||||
|
match collect_journal_entries(invalid_pid) {
|
||||||
|
Ok(response) => {
|
||||||
|
println!(
|
||||||
|
"✓ Journal query completed for invalid PID {} (empty result expected): {} entries",
|
||||||
|
invalid_pid, response.total_count
|
||||||
|
);
|
||||||
|
// Should be empty or very few entries
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("✓ Journal query failed for invalid PID {invalid_pid}: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
use assert_cmd::prelude::*;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
@@ -17,7 +16,7 @@ fn generates_self_signed_cert_and_key_in_xdg_path() {
|
|||||||
let xdg = tmpdir.path().to_path_buf();
|
let xdg = tmpdir.path().to_path_buf();
|
||||||
|
|
||||||
// Run the agent once with --enableSSL, short timeout so it exits quickly when killed
|
// Run the agent once with --enableSSL, short timeout so it exits quickly when killed
|
||||||
let mut cmd = Command::cargo_bin("socktop_agent").expect("binary exists");
|
let mut cmd = Command::new(assert_cmd::cargo::cargo_bin!("socktop_agent"));
|
||||||
// Bind to an ephemeral port (-p 0) to avoid conflicts/flakes
|
// Bind to an ephemeral port (-p 0) to avoid conflicts/flakes
|
||||||
cmd.env("XDG_CONFIG_HOME", &xdg)
|
cmd.env("XDG_CONFIG_HOME", &xdg)
|
||||||
.arg("--enableSSL")
|
.arg("--enableSSL")
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "socktop_connector"
|
name = "socktop_connector"
|
||||||
version = "0.1.6"
|
version = "1.50.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "WebSocket connector library for socktop agent communication"
|
description = "WebSocket connector library for socktop agent communication"
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ use tokio_tungstenite::{Connector, connect_async_tls_with_config};
|
|||||||
use crate::error::{ConnectorError, Result};
|
use crate::error::{ConnectorError, Result};
|
||||||
use crate::types::{AgentRequest, AgentResponse};
|
use crate::types::{AgentRequest, AgentResponse};
|
||||||
#[cfg(any(feature = "networking", feature = "wasm"))]
|
#[cfg(any(feature = "networking", feature = "wasm"))]
|
||||||
use crate::types::{DiskInfo, Metrics, ProcessInfo, ProcessesPayload};
|
use crate::types::{DiskInfo, Metrics, ProcessInfo, ProcessesPayload, ProcessMetricsResponse, JournalResponse};
|
||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
fn ensure_crypto_provider() {
|
fn ensure_crypto_provider() {
|
||||||
use std::sync::Once;
|
use std::sync::Once;
|
||||||
@@ -186,6 +186,18 @@ impl SocktopConnector {
|
|||||||
.ok_or_else(|| ConnectorError::invalid_response("Failed to get processes"))?;
|
.ok_or_else(|| ConnectorError::invalid_response("Failed to get processes"))?;
|
||||||
Ok(AgentResponse::Processes(processes))
|
Ok(AgentResponse::Processes(processes))
|
||||||
}
|
}
|
||||||
|
AgentRequest::ProcessMetrics { pid } => {
|
||||||
|
let process_metrics = request_process_metrics(stream, pid)
|
||||||
|
.await
|
||||||
|
.ok_or_else(|| ConnectorError::invalid_response("Failed to get process metrics"))?;
|
||||||
|
Ok(AgentResponse::ProcessMetrics(process_metrics))
|
||||||
|
}
|
||||||
|
AgentRequest::JournalEntries { pid } => {
|
||||||
|
let journal_entries = request_journal_entries(stream, pid)
|
||||||
|
.await
|
||||||
|
.ok_or_else(|| ConnectorError::invalid_response("Failed to get journal entries"))?;
|
||||||
|
Ok(AgentResponse::JournalEntries(journal_entries))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -437,6 +449,38 @@ async fn request_processes(ws: &mut WsStream) -> Option<ProcessesPayload> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send a "get_process_metrics:{pid}" request and await a JSON ProcessMetricsResponse
|
||||||
|
#[cfg(feature = "networking")]
|
||||||
|
async fn request_process_metrics(ws: &mut WsStream, pid: u32) -> Option<ProcessMetricsResponse> {
|
||||||
|
let request = format!("get_process_metrics:{}", pid);
|
||||||
|
if ws.send(Message::Text(request)).await.is_err() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
match ws.next().await {
|
||||||
|
Some(Ok(Message::Binary(b))) => {
|
||||||
|
gunzip_to_string(&b).ok().and_then(|s| serde_json::from_str::<ProcessMetricsResponse>(&s).ok())
|
||||||
|
}
|
||||||
|
Some(Ok(Message::Text(json))) => serde_json::from_str::<ProcessMetricsResponse>(&json).ok(),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send a "get_journal_entries:{pid}" request and await a JSON JournalResponse
|
||||||
|
#[cfg(feature = "networking")]
|
||||||
|
async fn request_journal_entries(ws: &mut WsStream, pid: u32) -> Option<JournalResponse> {
|
||||||
|
let request = format!("get_journal_entries:{}", pid);
|
||||||
|
if ws.send(Message::Text(request)).await.is_err() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
match ws.next().await {
|
||||||
|
Some(Ok(Message::Binary(b))) => {
|
||||||
|
gunzip_to_string(&b).ok().and_then(|s| serde_json::from_str::<JournalResponse>(&s).ok())
|
||||||
|
}
|
||||||
|
Some(Ok(Message::Text(json))) => serde_json::from_str::<JournalResponse>(&json).ok(),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Decompress a gzip-compressed binary frame into a String.
|
// Decompress a gzip-compressed binary frame into a String.
|
||||||
/// Unified gzip decompression to string for both networking and WASM
|
/// Unified gzip decompression to string for both networking and WASM
|
||||||
#[cfg(any(feature = "networking", feature = "wasm"))]
|
#[cfg(any(feature = "networking", feature = "wasm"))]
|
||||||
@@ -805,6 +849,20 @@ impl SocktopConnector {
|
|||||||
Ok(AgentResponse::Processes(processes))
|
Ok(AgentResponse::Processes(processes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
AgentRequest::ProcessMetrics { pid: _ } => {
|
||||||
|
// Parse JSON response for process metrics
|
||||||
|
let process_metrics: ProcessMetricsResponse = serde_json::from_str(&response).map_err(|e| {
|
||||||
|
ConnectorError::serialization_error(format!("Failed to parse process metrics: {e}"))
|
||||||
|
})?;
|
||||||
|
Ok(AgentResponse::ProcessMetrics(process_metrics))
|
||||||
|
}
|
||||||
|
AgentRequest::JournalEntries { pid: _ } => {
|
||||||
|
// Parse JSON response for journal entries
|
||||||
|
let journal_entries: JournalResponse = serde_json::from_str(&response).map_err(|e| {
|
||||||
|
ConnectorError::serialization_error(format!("Failed to parse journal entries: {e}"))
|
||||||
|
})?;
|
||||||
|
Ok(AgentResponse::JournalEntries(journal_entries))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ use crate::{AgentRequest, AgentResponse};
|
|||||||
|
|
||||||
#[cfg(feature = "networking")]
|
#[cfg(feature = "networking")]
|
||||||
use crate::networking::{
|
use crate::networking::{
|
||||||
WsStream, connect_to_agent, request_disks, request_metrics, request_processes,
|
WsStream, connect_to_agent, request_disks, request_journal_entries, request_metrics,
|
||||||
|
request_process_metrics, request_processes,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(all(feature = "wasm", not(feature = "networking")))]
|
#[cfg(all(feature = "wasm", not(feature = "networking")))]
|
||||||
@@ -72,6 +73,20 @@ impl SocktopConnector {
|
|||||||
.ok_or_else(|| ConnectorError::invalid_response("Failed to get processes"))?;
|
.ok_or_else(|| ConnectorError::invalid_response("Failed to get processes"))?;
|
||||||
Ok(AgentResponse::Processes(processes))
|
Ok(AgentResponse::Processes(processes))
|
||||||
}
|
}
|
||||||
|
AgentRequest::ProcessMetrics { pid } => {
|
||||||
|
let process_metrics =
|
||||||
|
request_process_metrics(stream, pid).await.ok_or_else(|| {
|
||||||
|
ConnectorError::invalid_response("Failed to get process metrics")
|
||||||
|
})?;
|
||||||
|
Ok(AgentResponse::ProcessMetrics(process_metrics))
|
||||||
|
}
|
||||||
|
AgentRequest::JournalEntries { pid } => {
|
||||||
|
let journal_entries =
|
||||||
|
request_journal_entries(stream, pid).await.ok_or_else(|| {
|
||||||
|
ConnectorError::invalid_response("Failed to get journal entries")
|
||||||
|
})?;
|
||||||
|
Ok(AgentResponse::JournalEntries(journal_entries))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -161,7 +161,8 @@ pub use config::ConnectorConfig;
|
|||||||
pub use connector_impl::SocktopConnector;
|
pub use connector_impl::SocktopConnector;
|
||||||
pub use error::{ConnectorError, Result};
|
pub use error::{ConnectorError, Result};
|
||||||
pub use types::{
|
pub use types::{
|
||||||
AgentRequest, AgentResponse, DiskInfo, GpuInfo, Metrics, NetworkInfo, ProcessInfo,
|
AgentRequest, AgentResponse, DetailedProcessInfo, DiskInfo, GpuInfo, JournalEntry,
|
||||||
|
JournalResponse, LogLevel, Metrics, NetworkInfo, ProcessInfo, ProcessMetricsResponse,
|
||||||
ProcessesPayload,
|
ProcessesPayload,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
//! WebSocket request handlers for native (non-WASM) environments.
|
//! WebSocket request handlers for native (non-WASM) environments.
|
||||||
|
|
||||||
use crate::networking::WsStream;
|
use crate::networking::WsStream;
|
||||||
|
use crate::types::{JournalResponse, ProcessMetricsResponse};
|
||||||
use crate::utils::{gunzip_to_string, gunzip_to_vec, is_gzip};
|
use crate::utils::{gunzip_to_string, gunzip_to_vec, is_gzip};
|
||||||
use crate::{DiskInfo, Metrics, ProcessInfo, ProcessesPayload, pb};
|
use crate::{DiskInfo, Metrics, ProcessInfo, ProcessesPayload, pb};
|
||||||
|
|
||||||
@@ -82,3 +83,36 @@ pub async fn request_processes(ws: &mut WsStream) -> Option<ProcessesPayload> {
|
|||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send a "get_process_metrics:{pid}" request and await a JSON ProcessMetricsResponse
|
||||||
|
pub async fn request_process_metrics(
|
||||||
|
ws: &mut WsStream,
|
||||||
|
pid: u32,
|
||||||
|
) -> Option<ProcessMetricsResponse> {
|
||||||
|
let request = format!("get_process_metrics:{pid}");
|
||||||
|
if ws.send(Message::Text(request)).await.is_err() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
match ws.next().await {
|
||||||
|
Some(Ok(Message::Binary(b))) => gunzip_to_string(&b)
|
||||||
|
.ok()
|
||||||
|
.and_then(|s| serde_json::from_str::<ProcessMetricsResponse>(&s).ok()),
|
||||||
|
Some(Ok(Message::Text(json))) => serde_json::from_str::<ProcessMetricsResponse>(&json).ok(),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send a "get_journal_entries:{pid}" request and await a JSON JournalResponse
|
||||||
|
pub async fn request_journal_entries(ws: &mut WsStream, pid: u32) -> Option<JournalResponse> {
|
||||||
|
let request = format!("get_journal_entries:{pid}");
|
||||||
|
if ws.send(Message::Text(request)).await.is_err() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
match ws.next().await {
|
||||||
|
Some(Ok(Message::Binary(b))) => gunzip_to_string(&b)
|
||||||
|
.ok()
|
||||||
|
.and_then(|s| serde_json::from_str::<JournalResponse>(&s).ok()),
|
||||||
|
Some(Ok(Message::Text(json))) => serde_json::from_str::<JournalResponse>(&json).ok(),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ pub struct DiskInfo {
|
|||||||
pub name: String,
|
pub name: String,
|
||||||
pub total: u64,
|
pub total: u64,
|
||||||
pub available: u64,
|
pub available: u64,
|
||||||
|
#[serde(default)]
|
||||||
|
pub temperature: Option<f32>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub is_partition: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
@@ -73,6 +77,79 @@ pub struct ProcessesPayload {
|
|||||||
pub top_processes: Vec<ProcessInfo>,
|
pub top_processes: Vec<ProcessInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct ThreadInfo {
|
||||||
|
pub tid: u32, // Thread ID
|
||||||
|
pub name: String, // Thread name (from /proc/{pid}/task/{tid}/comm)
|
||||||
|
pub cpu_time_user: u64, // User CPU time in microseconds
|
||||||
|
pub cpu_time_system: u64, // System CPU time in microseconds
|
||||||
|
pub status: String, // Thread status (Running, Sleeping, etc.)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct DetailedProcessInfo {
|
||||||
|
pub pid: u32,
|
||||||
|
pub name: String,
|
||||||
|
pub command: String,
|
||||||
|
pub cpu_usage: f32,
|
||||||
|
pub mem_bytes: u64,
|
||||||
|
pub virtual_mem_bytes: u64,
|
||||||
|
pub shared_mem_bytes: Option<u64>,
|
||||||
|
pub thread_count: u32,
|
||||||
|
pub fd_count: Option<u32>,
|
||||||
|
pub status: String,
|
||||||
|
pub parent_pid: Option<u32>,
|
||||||
|
pub user_id: u32,
|
||||||
|
pub group_id: u32,
|
||||||
|
pub start_time: u64, // Unix timestamp
|
||||||
|
pub cpu_time_user: u64, // Microseconds
|
||||||
|
pub cpu_time_system: u64, // Microseconds
|
||||||
|
pub read_bytes: Option<u64>,
|
||||||
|
pub write_bytes: Option<u64>,
|
||||||
|
pub working_directory: Option<String>,
|
||||||
|
pub executable_path: Option<String>,
|
||||||
|
pub child_processes: Vec<DetailedProcessInfo>,
|
||||||
|
pub threads: Vec<ThreadInfo>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct ProcessMetricsResponse {
|
||||||
|
pub process: DetailedProcessInfo,
|
||||||
|
pub cached_at: u64, // Unix timestamp when this data was cached
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct JournalEntry {
|
||||||
|
pub timestamp: String, // ISO 8601 formatted timestamp
|
||||||
|
pub priority: LogLevel,
|
||||||
|
pub message: String,
|
||||||
|
pub unit: Option<String>, // systemd unit name
|
||||||
|
pub pid: Option<u32>,
|
||||||
|
pub comm: Option<String>, // process command name
|
||||||
|
pub uid: Option<u32>,
|
||||||
|
pub gid: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub enum LogLevel {
|
||||||
|
Emergency = 0,
|
||||||
|
Alert = 1,
|
||||||
|
Critical = 2,
|
||||||
|
Error = 3,
|
||||||
|
Warning = 4,
|
||||||
|
Notice = 5,
|
||||||
|
Info = 6,
|
||||||
|
Debug = 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct JournalResponse {
|
||||||
|
pub entries: Vec<JournalEntry>,
|
||||||
|
pub total_count: u32,
|
||||||
|
pub truncated: bool,
|
||||||
|
pub cached_at: u64, // Unix timestamp when this data was cached
|
||||||
|
}
|
||||||
|
|
||||||
/// Request types that can be sent to the agent
|
/// Request types that can be sent to the agent
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
@@ -83,6 +160,10 @@ pub enum AgentRequest {
|
|||||||
Disks,
|
Disks,
|
||||||
#[serde(rename = "processes")]
|
#[serde(rename = "processes")]
|
||||||
Processes,
|
Processes,
|
||||||
|
#[serde(rename = "process_metrics")]
|
||||||
|
ProcessMetrics { pid: u32 },
|
||||||
|
#[serde(rename = "journal_entries")]
|
||||||
|
JournalEntries { pid: u32 },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AgentRequest {
|
impl AgentRequest {
|
||||||
@@ -92,6 +173,8 @@ impl AgentRequest {
|
|||||||
AgentRequest::Metrics => "get_metrics".to_string(),
|
AgentRequest::Metrics => "get_metrics".to_string(),
|
||||||
AgentRequest::Disks => "get_disks".to_string(),
|
AgentRequest::Disks => "get_disks".to_string(),
|
||||||
AgentRequest::Processes => "get_processes".to_string(),
|
AgentRequest::Processes => "get_processes".to_string(),
|
||||||
|
AgentRequest::ProcessMetrics { pid } => format!("get_process_metrics:{pid}"),
|
||||||
|
AgentRequest::JournalEntries { pid } => format!("get_journal_entries:{pid}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,4 +189,8 @@ pub enum AgentResponse {
|
|||||||
Disks(Vec<DiskInfo>),
|
Disks(Vec<DiskInfo>),
|
||||||
#[serde(rename = "processes")]
|
#[serde(rename = "processes")]
|
||||||
Processes(ProcessesPayload),
|
Processes(ProcessesPayload),
|
||||||
|
#[serde(rename = "process_metrics")]
|
||||||
|
ProcessMetrics(ProcessMetricsResponse),
|
||||||
|
#[serde(rename = "journal_entries")]
|
||||||
|
JournalEntries(JournalResponse),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,10 @@
|
|||||||
use crate::error::{ConnectorError, Result};
|
use crate::error::{ConnectorError, Result};
|
||||||
use crate::pb::Processes;
|
use crate::pb::Processes;
|
||||||
use crate::utils::{gunzip_to_string, gunzip_to_vec, is_gzip, log_debug};
|
use crate::utils::{gunzip_to_string, gunzip_to_vec, is_gzip, log_debug};
|
||||||
use crate::{AgentRequest, AgentResponse, DiskInfo, Metrics, ProcessInfo, ProcessesPayload};
|
use crate::{
|
||||||
|
AgentRequest, AgentResponse, DiskInfo, JournalResponse, Metrics, ProcessInfo,
|
||||||
|
ProcessMetricsResponse, ProcessesPayload,
|
||||||
|
};
|
||||||
|
|
||||||
use prost::Message as ProstMessage;
|
use prost::Message as ProstMessage;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@@ -206,6 +209,26 @@ pub async fn send_request_and_wait(
|
|||||||
Ok(AgentResponse::Processes(processes))
|
Ok(AgentResponse::Processes(processes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
AgentRequest::ProcessMetrics { pid: _ } => {
|
||||||
|
// Parse JSON response for process metrics
|
||||||
|
let process_metrics: ProcessMetricsResponse =
|
||||||
|
serde_json::from_str(&response).map_err(|e| {
|
||||||
|
ConnectorError::serialization_error(format!(
|
||||||
|
"Failed to parse process metrics: {e}"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
Ok(AgentResponse::ProcessMetrics(process_metrics))
|
||||||
|
}
|
||||||
|
AgentRequest::JournalEntries { pid: _ } => {
|
||||||
|
// Parse JSON response for journal entries
|
||||||
|
let journal_entries: JournalResponse =
|
||||||
|
serde_json::from_str(&response).map_err(|e| {
|
||||||
|
ConnectorError::serialization_error(format!(
|
||||||
|
"Failed to parse journal entries: {e}"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
Ok(AgentResponse::JournalEntries(journal_entries))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Generated
+2
-2
@@ -37,9 +37,9 @@ checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bytes"
|
name = "bytes"
|
||||||
version = "1.10.1"
|
version = "1.11.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"
|
checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
|
|||||||
Reference in New Issue
Block a user