# Automated Hourly Notebook Reports

Runs `eda_aws_v4_t6-dev.ipynb` every hour, extracts preference plots as JPEGs, and posts to Slack.

## Features

- ✅ Executes notebook automatically on a schedule
- ✅ Extracts two key AB test plots from notebook output
- ✅ Sends lightweight JPEG images (~200-500KB each) to Slack
- ✅ Optional `--skip-notebook` flag to extract plots without re-running notebook
- ✅ Automatic cleanup of old reports and logs

## Setup

### 1. Install Python Dependencies

```bash
pip install -r requirements.txt
```

### 2. Configure Slack

Get your Slack channel ID: Right-click channel → View channel details → Copy ID

Edit `config.yaml`:
```yaml
slack_webhook_url: "https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
slack_bot_token: "xoxb-your-bot-token"
slack_channel: "C01234567"  # Your channel ID
```

**Get webhook:** https://api.slack.com/messaging/webhooks

**Get bot token:** https://api.slack.com/apps → Create app → OAuth & Permissions → Add scopes: `files:write`, `chat:write` → Install to workspace

### 3. Install

```bash
# Install dependencies first
pip install -r requirements.txt

# Then setup the cron job
./setup.sh
```

### 4. Test

```bash
# Run full notebook execution and plot extraction
./run_hourly.sh

# Or skip notebook execution and use existing plots
./run_hourly.sh --skip-notebook
```

Check Slack for the JPEG plots!

## Monitoring

```bash
# View logs
tail -f logs/notebook_automation_$(date +%Y%m%d).log

# View cron jobs
crontab -l

# Check recent reports
ls -lth reports/ | head
```

## Configuration

Edit `config.yaml`:
- `notebook_path`: Notebook to execute
- `slack_channel`: Use channel ID (starts with C) or name with #
- `slack_webhook_url`: Slack webhook URL for notifications
- `slack_bot_token`: Bot token for file uploads (requires `files:write` and `chat:write` scopes)
- `reports_dir`: JPEG output directory (default: reports/)
- `logs_dir`: Log directory (default: logs/)

## Command Line Options

```bash
# Default: Execute notebook and extract plots
./run_hourly.sh

# Skip notebook execution, extract plots from last run
./run_hourly.sh --skip-notebook
```

The `--skip-notebook` flag is useful for:
- Testing Slack upload without waiting for notebook execution
- Re-sending plots from a previous run
- Debugging plot extraction independently

## Generated Plots

The automation extracts and sends **two plots** to Slack:

1. **`preference_plot_interesting_clips_{timestamp}.jpg`**
   - AB test summary for all models
   - Generated from `get_preference_counts(interesting_clips)`
   - No play count filtering

2. **`preference_plot_user_intersting_clips_3p5_{timestamp}.jpg`**
   - AB test summary for v3.5 models
   - Generated from `get_preference_counts(user_intersting_clips_3p5)`
   - Filtered dataset for specific model comparisons

## Troubleshooting

**Notebook fails:**
- Check Tailscale: `tailscale status`
- Verify AWS credentials: `aws sts get-caller-identity`
- Review logs: `tail logs/notebook_automation_*.log`
- Ensure conda environment is activated: `conda activate suno_env_dev`

**Plot extraction fails:**
- Verify notebook executed successfully and has outputs
- Check that target cells exist: `get_preference_counts(interesting_clips)` and `get_preference_counts(user_intersting_clips_3p5)`
- Use `--skip-notebook` to test extraction independently

**Slack upload fails:**
- Verify bot token has `files:write` and `chat:write` scopes
- Ensure bot is invited to channel: `/invite @BotName`
- Check channel ID is correct (should start with 'C')
- Test webhook URL: `curl -X POST -H 'Content-type: application/json' --data '{"text":"Test"}' YOUR_WEBHOOK_URL`

**Cron not running:**
- Check crontab: `crontab -l`
- Run manually: `./run_hourly.sh`
- Check logs: `tail logs/cron.log`
- Verify conda path in `run_hourly.sh`

## File Structure

```
Preference_auto_job/
├── eda_aws_v4_t6-dev.ipynb     # Notebook with analysis
├── run_and_notify.py            # Main automation script
├── config.yaml                  # Configuration file
├── setup.sh                     # Setup script
├── run_hourly.sh               # Cron wrapper with conda activation
├── requirements.txt             # Python dependencies (PyYAML, requests, nbconvert, Pillow)
├── logs/                        # Execution logs (kept for 7 days)
└── reports/                     # Generated JPEG plots (kept for 1 day)
```

## How It Works

1. **Execution**: `run_hourly.sh` activates conda environment and calls `run_and_notify.py`
2. **Notebook Run**: Executes `eda_aws_v4_t6-dev.ipynb` in-place (unless `--skip-notebook`)
3. **Plot Extraction**: Reads executed notebook's output cells, extracts PNG images
4. **Conversion**: Converts PNG to JPEG format for smaller file size
5. **Upload**: Sends both plots to Slack with descriptive messages
6. **Cleanup**: Removes old reports (>1 day) and logs (>7 days)

## Uninstall

```bash
crontab -l | grep -v "run_hourly.sh" | crontab -
```
