Sign inSign up

martitoci/sonarr-analyzer

By martitoci

โ€ขUpdated 4 months ago

Analyze and visualize your Sonarr library storage with secure historical insights.

Image
4

9.1K

martitoci/sonarr-analyzer repository overview

โ ๐Ÿ“Š Sonarr Analyzer

Docker Hub Docker Image Size Python Version License

A web application built with Streamlit for analyzing and monitoring your Sonarr library. Track episode file sizes, detect outliers, compare historical analyses, forecast storage growth, and manage multiple Sonarr instances โ€” all from a single, multi-user interface with role-based access control and encrypted credential storage.


โ ๐Ÿ“‹ Table of Contents


โ ๐Ÿ†• What's New in v0.4

  • ๐Ÿ“Š Dashboard โ€” Overview with key metrics and comparisons from the last two analyses
  • ๐ŸŽฌ Episode Breakdown โ€” Per-episode file detail within the current analysis result
  • ๐Ÿ“ˆ Storage Forecast โ€” Linear regression trend to project future storage needs
  • ๐ŸŽž๏ธ Codec Distribution โ€” Historical view of storage usage grouped by quality profile
  • ๐Ÿ  Multi-Instance Sonarr โ€” Manage multiple Sonarr servers per user with separate history
  • โฐ Scheduled Analysis โ€” Configure automatic analyses per instance (hourly/daily/weekly)
  • ๐Ÿ”’ Profile & Password Change โ€” Each user can update their own password

โ โœ… Prerequisites

Before you start, make sure you have:

  • Docker v20.10+ installed (Get Dockerโ )
  • Docker Compose v2.0+ (included with Docker Desktop)
  • A running Sonarr instance with API access
  • Your Sonarr API key (Settings โ†’ General โ†’ Security)

โ ๐Ÿš€ Quick Start with Docker

# Pull the latest image
docker pull martitoci/sonarr-analyzer:latest

# Run with persistent data
docker run -d \
  --name sonarr-analyzer \
  -p 8501:8501 \
  -v sonarr-data:/app/data \
  --restart unless-stopped \
  martitoci/sonarr-analyzer:latest

Access the application: http://localhost:8501โ 

โ Using Docker Compose

Save this as docker-compose.yml and run docker compose up -d:

services:
  sonarr-analyzer:
    image: martitoci/sonarr-analyzer:latest
    container_name: sonarr-analyzer
    ports:
      - "8501:8501"
    volumes:
      - sonarr-data:/app/data
    restart: unless-stopped

volumes:
  sonarr-data:

โ ๐Ÿ“ธ Screenshots

โ Dashboard & Analysis

Sonarr Analyzer Dashboard Main dashboard showing series analysis with file sizes, outliers, and detailed metrics

Historical Data Analysis Historical data view with storage evolution, trends over time, and date comparisons


โ ๐Ÿ“– Usage Guide

โ First Time Setup
  1. Start the container (see Quick Start above)
  2. Open your browser: http://localhost:8501โ 
  3. Create Admin Account:
    • On first launch you'll see the "Create Admin Account" screen
    • Enter username (min 3 characters) and password (min 8 characters)
    • Click "Create Admin Account"
  4. Login with your new admin credentials
  5. Configure Sonarr:
    • Go to "๐Ÿ”‘ Configuration" in the sidebar
    • Add your first Sonarr instance (URL + API Key)
    • Give it a name (e.g., home, remote)
  6. Activate the instance and run your first analysis

โ ๐Ÿ” Authentication Flow

โ First Run (No Admin Exists)
Start App โ†’ Create Admin Screen โ†’ Create Admin โ†’ Login Screen โ†’ Dashboard
โ Subsequent Runs (Admin Exists)
Start App โ†’ Login Screen โ†’ Enter Credentials โ†’ Dashboard

โ ๐Ÿ‘ค User Roles

FeatureAdminRead-Only
Loginโœ…โœ…
Dashboardโœ…โœ…
Configure Sonarr Instancesโœ…โœ…
Run Analysisโœ…โŒ
View Current Resultsโœ…โœ…
Episode Breakdownโœ…โœ…
View Historical Dataโœ…โœ…
Storage Forecastโœ…โœ…
Codec Distributionโœ…โœ…
Configure Schedulerโœ…โŒ
Change Own Passwordโœ…โœ…
Create / Manage Usersโœ…โŒ
Delete Analysis Dataโœ…โŒ

โ ๐Ÿ‘ฅ User Management (Admin Only)

  1. Login as admin
  2. Go to "๐Ÿ‘ฅ User Management" in the sidebar
  3. Create users with username, password, and role (admin or readonly)
  4. View all users (created date, last login)
  5. Delete users (cannot delete yourself or the last admin)

โ ๐ŸŒ Multi-Instance Support

Each user can add and manage multiple Sonarr instances independently.

โ Adding an Instance
  1. Go to "๐Ÿ”‘ Configuration"
  2. Under Add / Update Instance, enter:
    • Instance name (e.g., home, 4k, remote)
    • Sonarr URL
    • API Key
  3. Click Save Instance
โ Switching Instances
  • Use the Active Instance selector at the top of the Configuration page
  • All analyses, history, and schedules are stored separately per instance
โ Deleting an Instance
  • Select the instance and click Delete Instance
  • This removes the saved credentials; historical data is preserved

โ ๐Ÿ”‘ Token Security

  • Each user's Sonarr URL and API key are encrypted with Fernet (AES-128 CBC) before being stored in SQLite
  • A master encryption key is generated on first startup and stored in /app/data/.master.key
  • Tokens are decrypted in-session only โ€” never stored in plain text
โ Best Practices

โœ… Use strong passwords ยท Back up your data volume ยท Use HTTPS behind a reverse proxy
โŒ Don't share the master key ยท Don't expose port 8501 directly to the internet


โ ๐Ÿ“Š Running Your First Analysis

  1. Login and ensure an instance is active (see Configuration)
  2. Go to "๐Ÿ” Analysis" in the sidebar
  3. Configure options (optional):
    • Request timeout
    • Z-Score threshold
    • Absolute threshold (MB)
    • Enable "Save to history"
  4. Click "๐Ÿš€ Run Analysis"
  5. Explore results across 4 tabs:
    • Table โ€” All series with detailed metrics
    • Charts โ€” Visual representations (size distribution, top series)
    • Episode Breakdown โ€” Per-episode file detail for each series
    • Outliers โ€” Series with unusually large files

โ ๐Ÿ“ˆ Using Historical Data

Navigate to "๐Ÿ“ˆ Historical Data" in the sidebar. Five tabs are available:

โ All Analyses

Overview table of every saved analysis with storage evolution and average-size trend charts.

โ Compare Dates

Select two past dates to compare. See new/removed series, storage delta, and a top-changers chart. Download as CSV.

Select a series and view its total size, episode count, and average size over time.

โ Codec Distribution

Stacked area chart showing storage breakdown by quality profile (codec) over time.

โ Storage Forecast

Linear regression projection of total library size. Estimates when you'll hit storage milestones.


โ โฐ Scheduled Analysis

Sonarr Analyzer supports passive scheduling: analyses run automatically when the app is loaded (no background process needed).

โ Configuring a Schedule
  1. Go to "โฐ Scheduler" in the sidebar
  2. Select a frequency: every 1 h, 6 h, 12 h, 24 h, 48 h, or 168 h (weekly)
  3. Click Save Schedule
  4. The next run time is displayed; the analysis triggers on the next page load after that time
โ Notes
  • Each instance has its own schedule
  • Results are saved to history automatically
  • The scheduler tab shows the last run time and current status

โ ๐Ÿงช Running Tests

# Install dependencies
pip install -r requirements.txt

# Run all tests
pytest -v

# Run with coverage
pytest --cov=. --cov-report=html tests/

Coverage includes: Authentication, token encryption, storage operations, integration workflows, role enforcement, and schema migrations.


โ ๐Ÿ’พ Data Persistence

โ Files Stored in /app/data
FilePurposeEncrypted
users.dbUser accounts and hashed passwordsPasswords bcrypt-hashed
tokens.dbPer-user, per-instance Sonarr credentialsโœ… Fernet encrypted
.master.keyMaster encryption keyKeep this safe!
sonarr_history.dbHistorical analyses and schedulesNo
โ Backup
# Backup named volume
docker run --rm \
  -v sonarr-data:/data \
  -v $(pwd):/backup \
  alpine tar czf /backup/sonarr-backup.tar.gz -C /data .

# Restore
docker run --rm \
  -v sonarr-data:/data \
  -v $(pwd):/backup \
  alpine tar xzf /backup/sonarr-backup.tar.gz -C /data
โ Bind Mount (Host Directory)
mkdir -p ./sonarr-data
# Linux/macOS: fix ownership for UID 1000 (container user)
sudo chown -R 1000:1000 ./sonarr-data

docker run -d \
  --name sonarr-analyzer \
  -p 8501:8501 \
  -v $(pwd)/sonarr-data:/app/data \
  martitoci/sonarr-analyzer:latest

โ ๐Ÿ“Š Understanding the Results

โ Metrics Explained
MetricDescription
EpisodesNumber of downloaded episode files
Total Size (GB)Combined size of all episodes
Avg Size (MB)Average file size per episode
Z-ScoreStatistical deviation from the mean across all series
OutlierYes if Z-Score exceeds the configured threshold (default 2.0)
โ Z-Score Interpretation
Z-Score = (Value โˆ’ Mean) / Std Deviation
Z-ScoreStatusSuggested Action
0 โ€“ 1Normal โœ…No action needed
1 โ€“ 2Slightly high โš ๏ธMonitor
> 2Outlier ๐ŸšจReview and consider re-encoding
> 3Extreme ๐Ÿ”ฅHigh priority

โ ๐Ÿ”ง Configuration

โ Environment Variables
VariableDefaultDescription
STREAMLIT_SERVER_PORT8501Web interface port
STREAMLIT_SERVER_ADDRESS0.0.0.0Bind address
STREAMLIT_SERVER_HEADLESStrueHeadless mode
STREAMLIT_BROWSER_GATHER_USAGE_STATSfalseDisable telemetry
โ Custom Port
docker run -d --name sonarr-analyzer -p 9000:8501 \
  -v sonarr-data:/app/data \
  martitoci/sonarr-analyzer:latest
# Access at http://localhost:9000

โ ๐Ÿณ Docker Image Details

  • Base Image: python:3.11-slim
  • Architecture: linux/amd64, linux/arm64
  • User: Non-root (UID 1000, appuser)
  • Health Check: Built-in Python-based check (no curl required)
  • Privilege Drop: gosu (GPG-verified, official Docker pattern)
โ Tags
TagDescription
latestLatest stable release
v0.4.0This release
v0.3.0Previous release
docker pull martitoci/sonarr-analyzer:latest
docker pull martitoci/sonarr-analyzer:v0.4.0

โ ๐Ÿ” Troubleshooting

โ Cannot Login
  • Verify credentials (case-sensitive)
  • Check logs: docker logs sonarr-analyzer
  • To reset all user accounts (tokens and history preserved):
    docker exec sonarr-analyzer rm /app/data/users.db
    docker restart sonarr-analyzer
    
โ Database Permission Error (Bind Mounts)
# Fix ownership on the host
sudo chown -R 1000:1000 /path/to/your/data

The entrypoint script attempts to fix this automatically when the container runs as root. For persistent issues, switch to a named volume.

โ Cannot Connect to Sonarr
  • Use the full URL including protocol: http://192.168.1.10:8989
  • Use the host's LAN IP or container name, not localhost
  • Verify the API key in Sonarr โ†’ Settings โ†’ General โ†’ Security
โ Database Locked
docker restart sonarr-analyzer
โ Master Key Lost

All encrypted tokens become unrecoverable โ€” users must re-enter their Sonarr credentials. Always back up /app/data.


โ ๐Ÿ“ˆ Use Cases

Monthly Storage Audit: Run on the 1st of each month, compare with the previous run to identify growth outliers.

Pre/Post Re-encoding: Run before and after re-encoding high-Z-score series to measure savings.

Multi-Server Monitoring: Each team member or household user manages their own Sonarr instance credentials and sees only their own history.

Proactive Planning: Use the Storage Forecast tab to know when to add drives before you run out of space.


โ ๐Ÿ› ๏ธ Building from Source

git clone https://github.com/chmedinap/sonarr-analyzer.git
cd sonarr-analyzer

docker build -t sonarr-analyzer:local .

docker run -d \
  --name sonarr-analyzer \
  -p 8501:8501 \
  -v sonarr-data:/app/data \
  sonarr-analyzer:local

โ ๐Ÿ“ Version History

โ v0.4.0 (Current)
  • โž• Dashboard with key metrics and last-analysis comparison
  • โž• Episode breakdown tab in analysis results
  • โž• Storage forecast (linear regression) in historical view
  • โž• Codec/quality distribution chart in historical view
  • โž• Multi-instance Sonarr support (separate history per instance)
  • โž• Passive scheduler (hourly โ†’ weekly, per instance)
  • โž• Profile page with password change
  • ๐Ÿ”ง Fixed @st.cache_data incompatibility with progress UI
  • ๐Ÿ”ง Fixed requests.Session resource leak
  • ๐Ÿ”ง Fixed rowcount capturing wrong table in delete operations
  • ๐Ÿ”ง Schema migrations for existing databases (no data loss on upgrade)
โ v0.3.0
  • โž• User authentication with bcrypt
  • โž• Role-based access control (admin / read-only)
  • โž• Per-user encrypted token storage (Fernet)
  • โž• First-run admin creation flow
  • โž• User management (admin only)
  • โž• 60+ comprehensive unit tests
โ v0.2.0
  • โž• Credential encryption (AES-256)
  • โž• SQLite historical database
  • โž• Date comparison feature
  • โž• Trend visualizations
โ v0.1.0
  • Initial release: current analysis, Z-score detection, outlier identification

โ ๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new features
  4. Run pytest -v and verify all pass
  5. Submit a pull request

โ ๐Ÿ“„ License

MIT License โ€” see LICENSE file for details.


โ ๐Ÿ“ž Support


โ โšก Quick Commands Reference

# Pull latest image
docker pull martitoci/sonarr-analyzer:latest

# Run with persistent data
docker run -d --name sonarr-analyzer -p 8501:8501 \
  -v sonarr-data:/app/data --restart unless-stopped \
  martitoci/sonarr-analyzer:latest

# View logs
docker logs -f sonarr-analyzer

# Stop / remove
docker stop sonarr-analyzer
docker rm sonarr-analyzer

# Backup data volume
docker run --rm -v sonarr-data:/data -v $(pwd):/backup \
  alpine tar czf /backup/sonarr-backup.tar.gz -C /data .

# Update to latest
docker pull martitoci/sonarr-analyzer:latest
docker stop sonarr-analyzer && docker rm sonarr-analyzer
docker run -d --name sonarr-analyzer -p 8501:8501 \
  -v sonarr-data:/app/data --restart unless-stopped \
  martitoci/sonarr-analyzer:latest

Made with โค๏ธ for the Sonarr community

Tag summary

Content type

Image

Digest

sha256:1537b4cb0โ€ฆ

Size

220.6 MB

Last updated

4 months ago

docker pull martitoci/sonarr-analyzer