MittWatt is a web application designed to provide insights into electricity prices in the Finnish market. It offers real-time data on current electricity prices, historical data for the past two weeks, and predictions for the next two weeks.
- Current Prices: View the current electricity prices in Finland.
- Historical Data: Access statistics on electricity prices for the past 14 days.
- Future Predictions: Get predictions for electricity prices for the next 14 days.
The MittWatt project is divided into five main parts:
- Prediction Model: Utilizes machine learning algorithms to forecast electricity prices for the next 14 days.
- Data Retrieval Script: A Python script that fetches historical electricity prices for the past two years and the current prices.
- Web Application: Developed using the T3 stack (TRPC, Tailwind CSS, Next.js) for a seamless user experience.
- API Tests: Ensures the reliability and accuracy of the API endpoints.
- Deployment on AWS: Preparing the application for deployment on AWS.
Before you begin, ensure you have met the following requirements:
- Node.js (version 14.x or higher)
- PostgreSQL (version 12.x or higher)
- Python (version 3.7 or higher)
To set up the project locally, follow these steps:
-
Install pnpm:
npm install -g pnpm
-
Clone the repository:
git clone https://github.com/ahmedeleven/mittwatt.git cd mittwatt
-
Install dependencies:
pnpm install
-
Set up the database:
- Ensure you have PostgreSQL installed and running.
- Create a new database and update the .env file with your database credentials as shown below:
DATABASE_URL="postgresql://[username]:[password]@[host_name]:[port_number]/[database_name]"
where
[username]
is the username of the database user.[password]
is the password of the database user.[host_name]
is the host name of the database.[port_number]
is the port number of the database.[database_name]
is the name of the database.
-
Run database migrations:
pnpm prisma migrate dev
-
Run data retrieval script: Run the scripts for the prediction model and historical data retrieval. Please check the README.md under the
prisma
folder and README.md under theml_models
folder for more details. -
Start the development server:
pnpm dev
Once the development server is running, you can access the web application at http://localhost:3000
. The application consists of three main tabs:
- Current: Displays the current electricity prices.
- Past: Shows historical data for the past two weeks.
- Future: Provides predictions for the next two weeks.
This README file provides detailed steps for deploying a web application on AWS using an EC2 instance and RDS PostgreSQL for the database. The deployment process includes setting up the necessary environment, cloning the repository, configuring the application, installing required software, and connecting the application to the database.
- Launching an EC2 Instance
- Setting Up the EC2 Instance
- Cloning the Repository
- Configuring the Application
- Installing Dependencies
- Setting Up the Database
- Connecting EC2 to RDS
- Running the Application
- Setting Up a Reverse Proxy with Caddy
- Setting Up DuckDNS
- Setting Up Scheduled Tasks
- Log in to AWS Management Console.
- Navigate to the EC2 Dashboard and click on "Launch Instance".
- Configure the instance:
- Choose an Ubuntu Amazon Machine Image (AMI).
- Select an instance type (e.g., t2.micro for the free tier).
- Configure instance details and add storage:
- Set the volume size to 20 GB (default is 8 GB).
- Configure security groups:
- Ensure the security group allows inbound traffic on ports 22 (SSH), 80 (HTTP), and 3000 (application port).
- Launch the instance and connect via SSH.
-
Connect to your EC2 instance via SSH.
-
Update the system and install necessary software:
sudo apt update sudo apt upgrade -y
-
Install Node.js, npm, and pnpm:
sudo apt install -y nodejs npm curl -fsSL https://get.pnpm.io/install.sh | sh - sudo npm install -g pnpm
-
Install Git:
sudo apt install -y git
- Clone the project repository:
git clone https://github.com/ahmedeleven/mittwatt.git cd mittwatt
-
Copy the example environment file and edit it:
cp .env.example .env vim .env
-
Add the necessary environment variables to .env.
-
Install the project dependencies:
pnpm install
-
If you encounter network issues:
pnpm install --network-concurrency 1
- Log in to AWS Management Console.
- Navigate to the RDS Dashboard:
- In the AWS Management Console, search for and select RDS to open the RDS Dashboard.
- Create a New Database:
- Click on Create database.
- Choose a Database Creation Method:
- Select Standard create for more customization options.
- Engine Options:
- Under Engine options, select PostgreSQL.
- Templates:
- Choose the Free tier template to ensure that the instance is eligible for the free tier.
- Specify Database Details:
- DB Instance Identifier: Enter a unique name for your database instance (e.g., mydatabase).
- Master Username: Enter the master username (e.g., mydatabaseuser).
- Master Password: Set and confirm a strong password (e.g., your-password).
- Initial Database Name: Provide a name for your initial database (e.g., mydatabase).
- Connectivity:
- VPC: Choose the VPC where your EC2 instance is located.
- Public Accessibility: Set to Yes if you need to migrate the database from a local environment to RDS. After migration, change it to No to ensure that the RDS instance is not publicly accessible.
- Additional Configuration:
- Security Groups: Select or create a security group that allows inbound traffic on port 5432 from your EC2 instance’s IP address.
- Review and Create:
- Review all the settings you’ve configured.
- Click Create database to launch the RDS instance.
- Obtain the Endpoint:
- Once the RDS instance is created, go to the Databases section in the RDS Dashboard.
- Select your database instance to view its details and find the Endpoint.
- Update EC2 Configuration:
- Ensure that your EC2 instance’s security group allows outbound traffic on port 5432.
- Update your
.env
file on the EC2 instance with the RDS connection details:DATABASE_URL="postgresql://[username]:[password]@[host_name]:[port_number]/[database_name]"
- Replace
[RDS-endpoint]
with the endpoint URL from the RDS dashboard.
- Migrate Data (if needed):
- If you set Public Accessibility to Yes, you can connect to the RDS instance from your local environment to migrate data. After migration, set Public Accessibility to No for better security.
-
Activate the virtual environment and install Python dependencies:
python3 -m venv venv source venv/bin/activate pip install -r requirements.txt
-
Navigate to the
prisma
directory and runmain.py
:cd ../prisma/ python3 main.py
-
Navigate to the
ml_models
directory and runml_main.py
:cd ../ml_models/ python3 ml_main.py
-
Start the development server:
pnpm dev
-
Install Caddy:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list sudo apt update sudo apt install caddy
-
Configure Caddy for reverse proxy: Edit the Caddyfile (/etc/caddy/Caddyfile):
:80 { reverse_proxy localhost:3000 } your-domain.duckdns.org { reverse_proxy localhost:3000 }
-
Create an account and obtain your token:
- Go to the DuckDNS website.
- Create an account and log in.
- Add your desired domain (e.g., your-domain.duckdns.org) and obtain your token.
-
Create and configure the DuckDNS update script:
mkdir ~/duckdns cd ~/duckdns vim duck.sh
Add the following content to duck.sh:
echo url="https://www.duckdns.org/update?domains=your-domain&token=your-token&ip=" | curl -k -o ~/duckdns/duck.log -K -
Replace
your-domain
with your DuckDNS domain andyour-token
with the token you obtained.Make the script executable and run it:
chmod +x duck.sh ./duck.sh
Set up a cron job to update DuckDNS automatically:
crontab -e
Add the following line to the crontab file:
*/5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1
This will run the DuckDNS update script every 5 minutes.
Open the crontab file:
crontab -e
Add the following lines to schedule the scripts:
# Run /prisma/main.py every weekday at 15:00
00 15 * * 1-5 /usr/bin/python3 /path/to/your/project/prisma/main.py >/dev/null 2>&1
# Run /ml_models/ml_main.py every weekday at 18:00
00 18 * * 1-5 /usr/bin/python3 /path/to/your/project/ml_models/ml_main.py >/dev/null 2>&1