Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ PERSISTENCE_ADAPTER=sqlalchemy
POLICIES_PERSISTENCE_ADAPTER=yaml

# Database configuration
# For SQLite: sqlite:///path/to/file.db
# For SQLite: sqlite:///data/db/edgemining.db (default)
# For PostgreSQL: postgresql://user:password@localhost:5432/dbname
# For MySQL: mysql+pymysql://user:password@localhost:3306/dbname
DB_PATH=sqlite:///edgemining.db
DB_PATH=sqlite:///data/db/edgemining.db

# Alembic Migrations
# Automatically run database migrations on startup (recommended)
RUN_MIGRATIONS_ON_STARTUP=true


# Create a backup of the database before running migrations (SQLite only)
# Backups are stored in data/db/backups/
BACKUP_BEFORE_MIGRATION=true

# Optional: Path to the directory for storing optimization policies (when using YAML)
YAML_POLICIES_DIR=optimization_policies
# Path to the directory for storing optimization policies (when using YAML)
YAML_POLICIES_DIR=data/policies

# API Settings
API_PORT=8001
Expand Down
15 changes: 13 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,19 @@ htmlcov/
.pytest_cache/
*.log

# Edge Mining stuffs
edgemining.db
# User data directory
data/db/

# Track example templates but ignore any custom files added by users
data/examples/*
data/policies/*
!data/examples/rules/
!data/examples/rules/start/
!data/examples/rules/stop/
!data/examples/rules/start/*.yaml
!data/examples/rules/stop/*.yaml
!data/examples/policies/
!data/examples/policies/*.yaml

# SQLAlchemy database files
*.db
Expand Down
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ RUN if [ -f requirements.txt ]; then \
# Copy the rest of the source code
COPY . .

# Create data directories for user files
RUN mkdir -p /app/data/db/backups \
&& mkdir -p /app/data/policies \
&& mkdir -p /app/data/examples

# Execute the application
CMD ["python", "-m", "edge_mining", "standard"]
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@ The project uses **Hexagonal Architecture (Ports and Adapters)** to clearly sepa

Key settings:
- `PERSISTENCE_ADAPTER`: Choose between `in_memory`, `sqlite`, or `sqlalchemy` (recommended)
- `DB_PATH`: Database URL (e.g., `sqlite:///edgemining.db` or PostgreSQL URL)
- `DB_PATH`: Database URL (e.g., `sqlite:///data/db/edgemining.db` or PostgreSQL URL)
- `RUN_MIGRATIONS_ON_STARTUP`: Set to `true` to automatically apply database migrations

5. **Initialize the database (SQLAlchemy only):**
5. **Create data directory structure:**

The application stores all user data in the `data/` directory:
```bash
mkdir -p data/db/backups data/policies data/examples
```

**Directory structure:**
- `data/db/` - Database file and automatic backups
- `data/policies/` - Your optimization policy YAML files
- `data/examples/` - Example rules for reference (templates only)

6. **Initialize the database (SQLAlchemy only):**
If using SQLAlchemy persistence, migrations **will run automatically on startup**.

See [docs/ALEMBIC_MIGRATIONS.md](docs/ALEMBIC_MIGRATIONS.md) for detailed migration management.
Expand Down Expand Up @@ -90,6 +102,19 @@ The API will be available at `http://localhost:8001` (or the configured port). Y

You can run Edge Mining Core using `docker compose` in daemon mode using the provided `compose.yaml`.

**Important:** The `data/` directory is mounted as a volume, ensuring your database, policies, and backups persist even when the container is removed.

Before first run, create the data directory structure:
```bash
mkdir -p data/db/backups data/policies data/examples
```

You can:
- Place your optimization policy YAML files in `data/policies/`
- Find rule examples in `data/examples/start/` and `data/examples/stop/`
- Access the database at `data/db/edgemining.db`
- Find automatic backups in `data/db/backups/`

### 1. Build the image (first run or when `requirements.txt` changes)

From the project root:
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ services:
ports:
- "8001:8001"
volumes:
- ./:/app
- ./data:/app/data
141 changes: 141 additions & 0 deletions data/examples/policies/550e8400-e29b-41d4-a716-446655440000.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
id: 550e8400-e29b-41d4-a716-446655440000
name: Advanced Solar Mining Policy
description: Comprehensive policy for solar-based mining optimization with intelligent start and stop conditions
start_rules:
- id: 550e8400-e29b-41d4-a716-446655440001
name: Priority Solar Mining
description: 'High priority: Start mining when solar production is very high'
conditions:
all_of:
- field: energy_state.production
operator: gt
value: 3000
- field: energy_state.battery.state_of_charge
operator: gt
value: 40
priority: 20
enabled: true
- id: 550e8400-e29b-41d4-a716-446655440002
name: Smart Weekend Mining
description: Weekend mining with intelligent conditions
conditions:
all_of:
- field: timestamp.weekday
operator: in
value: [5, 6]
- any_of:
- all_of:
- field: forecast.avg_next_4_hours_power
operator: gt
value: 2000
- field: energy_state.battery.state_of_charge
operator: gt
value: 70
- all_of:
- field: energy_state.grid.exporting_power
operator: lt
value: 800
- field: energy_state.battery.state_of_charge
operator: gt
value: 50
- not_:
field: timestamp.hour
operator: in
value: [2, 3, 4]
priority: 15
enabled: true
- id: 550e8400-e29b-41d4-a716-446655440003
name: Economic Opportunity Mining
description: Start mining when economic conditions are favorable
conditions:
all_of:
- field: energy_state.battery.state_of_charge
operator: gte
value: 85
- field: energy_state.production
operator: gt
value: 1800
- any_of:
- field: timestamp.weekday
operator: in
value: [5, 6]
- all_of:
- field: timestamp.weekday
operator: in
value: [0, 1, 2, 3, 4]
- field: timestamp.hour
operator: in
value: [11, 12, 13, 14]
priority: 5
enabled: true
stop_rules:
- id: 550e8400-e29b-41d4-a716-446655440004
name: Critical Battery Protection
description: 'CRITICAL: Stop mining immediately when battery is critically low'
conditions:
any_of:
- field: energy_state.battery.state_of_charge
operator: lt
value: 15
- field: energy_state.battery.remaining_capacity
operator: lt
value: 800
- all_of:
- field: energy_state.battery.current_power
operator: lt
value: -1000
- field: energy_state.battery.state_of_charge
operator: lt
value: 25
priority: 100
enabled: true
- id: 550e8400-e29b-41d4-a716-446655440005
name: Grid Import Limit Protection
description: Stop mining when importing too much from grid
conditions:
all_of:
- field: energy_state.grid.importing_power
operator: gt
value: 2500
- any_of:
- field: forecast.next_hour_power
operator: lt
value: 1000
- field: energy_state.battery.state_of_charge
operator: lt
value: 60
- not_:
all_of:
- field: timestamp.hour
operator: in
value: [1, 2, 3, 4, 5]
- field: energy_state.battery.state_of_charge
operator: lt
value: 30
priority: 80
enabled: true
- id: 550e8400-e29b-41d4-a716-446655440006
name: Peak Home Consumption Stop
description: Stop mining during high home energy demand periods
conditions:
all_of:
- field: home_load_forecast
operator: gt
value: 2800
- field: timestamp.hour
operator: in
value: [17, 18, 19, 20, 21, 22]
- any_of:
- field: energy_state.battery.state_of_charge
operator: lt
value: 70
- field: energy_state.production
operator: lt
value: 1500
priority: 60
enabled: true
metadata:
author: Edge Mining User
version: 5
created: '2025-08-04'
last_modified: '2026-01-18'
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rules:
value: 800 # Less than 800 WattHours remaining
- all_of: # Battery discharging fast + low SOC
- field: "energy_state.battery.discharging_power"
operator: "lt"
operator: "gt"
value: 1000 # Discharging more than 1kW
- field: "energy_state.battery.state_of_charge"
operator: "lt"
Expand Down
Loading