Skip to content

Commit

Permalink
Adding build stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
daithihearn committed Apr 15, 2023
1 parent 397c2d0 commit 8a686ab
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 60 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/build-docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Build Docker image

on:
workflow_dispatch:
pull_request:
branches: ["main"]

jobs:
build:
name: Build docker image
runs-on: self-hosted

steps:
- uses: actions/checkout@v3
- name: Build the Docker image
run: docker build . --file Dockerfile
29 changes: 29 additions & 0 deletions .github/workflows/publish-to-dockerhub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Publish to Dockerhub

on:
release:
types: [published]

jobs:
publish:
name: Build and Publish docker image to dockerhub
permissions:
contents: write
pull-requests: write
packages: read
runs-on: self-hosted

steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: daithihearn/electricity-prices-dashboard
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
platforms: linux/amd64,linux/arm64/v8
tags: "latest,${{ github.ref_name }}"
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM python:3.11.3

# Set the working directory to /app
WORKDIR /app

# Install dependencies
COPY requirements.txt /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Copy source code
COPY . /app

# Run the app
CMD ["streamlit", "run", "Electricity.py"]
111 changes: 52 additions & 59 deletions Electricity.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
import streamlit as st
import time
import os
import logging
import requests
import numpy as np
import pandas as pd
import json
from babel.numbers import format_decimal
from datetime import date, datetime, timedelta
from dateutil.parser import parse
from dotenv import load_dotenv
from streamlit_timeline import st_timeline

st.set_page_config(layout="wide")




def format_euro(amount) -> str:
return f'{format_decimal(amount, locale="en_GB", format="#,##0.000")}'

PRICES_API= 'https://elec.daithiapp.com/api/v1/price'


PRICES_API = 'https://elec.daithiapp.com/api/v1/price'


class Price:
Expand All @@ -45,25 +36,26 @@ def get_prices(start: date, end: date) -> list[Price]:
return [Price(x.get('price'), parse(x.get('dateTime'))) for x in price_data]



def get_current_price(prices: list[Price]) -> Price:
now = datetime.now()
hour = now.strftime('%H')
return next((x for x in prices if x.datetime.strftime('%H') == hour), None)


def get_tomorrow():
today = date.today()
tomorrow = today + timedelta(days=1)
return get_prices(tomorrow, tomorrow)


def get_today():
today = date.today()
return get_prices(today, today)

def get_date_days_ago(days:int):
ago=date.today() - timedelta(days=days)
return get_prices(ago, ago)

def get_date_days_ago(days: int):
ago = date.today() - timedelta(days=days)
return get_prices(ago, ago)


def get_min_price(prices: list[Price]):
Expand All @@ -74,8 +66,6 @@ def get_max_price(prices: list[Price]):
return max(prices, key=lambda x: x.value)




def get_cheapest_period(prices: list[Price], n: int):

prices_sorted = sorted(prices, key=lambda x: x.hour)
Expand All @@ -90,6 +80,7 @@ def get_cheapest_period(prices: list[Price], n: int):

return min_window


def get_cheap_period_recent_average(days: int) -> float:
today = date.today()

Expand All @@ -104,81 +95,83 @@ def get_cheap_period_recent_average(days: int) -> float:

return total / days


def calculate_average(prices: list[Price]) -> float:
return sum(x.value for x in prices) / len(prices)



#get data
# get data
# price_data_v1=get_prices()

st.title('Electricity prices')

start_date_average = date.today() - timedelta(days=30)
elecAverage= round(calculate_average(get_prices ( start_date_average,date.today())),2)
currentPrice=round(get_current_price(get_today()).value,2)
difElec=round(currentPrice - elecAverage,2)
elecAverage = round(calculate_average(
get_prices(start_date_average, date.today())), 2)
currentPrice = round(get_current_price(get_today()).value, 2)
difElec = round(currentPrice - elecAverage, 2)
col1, col2, col3, col4 = st.columns(4)
col1.metric("Average Price (30 days)" , elecAverage,delta_color="inverse")
col2.metric("Current Price" , currentPrice,difElec,delta_color="inverse")
labelminhour= "Min Price at " +get_min_price(get_today()).hour + ":00"
labelmaxhour= "Max Price at " +get_max_price(get_today()).hour + ":00"

minToday=round(get_min_price(get_today()).value,2)
difElecMin=round(minToday - elecAverage,2)
col1.metric("Average Price (30 days)", elecAverage, delta_color="inverse")
col2.metric("Current Price", currentPrice, difElec, delta_color="inverse")
labelminhour = "Min Price at " + get_min_price(get_today()).hour + ":00"
labelmaxhour = "Max Price at " + get_max_price(get_today()).hour + ":00"

maxToday=round(get_max_price(get_today()).value,2)
difElecMax=round(maxToday - elecAverage,2)
minToday = round(get_min_price(get_today()).value, 2)
difElecMin = round(minToday - elecAverage, 2)

col3.metric(labelminhour, minToday,difElecMin,delta_color="inverse")
col4.metric(labelmaxhour, maxToday,difElecMax,delta_color="inverse")
maxToday = round(get_max_price(get_today()).value, 2)
difElecMax = round(maxToday - elecAverage, 2)

col3.metric(labelminhour, minToday, difElecMin, delta_color="inverse")
col4.metric(labelmaxhour, maxToday, difElecMax, delta_color="inverse")


if get_tomorrow():
if get_tomorrow():
st.write('TOMORROW')
labelminhourT= "Min Price at " + get_min_price(get_tomorrow()).hour + ":00"
labelmaxhourT= "Max Price at " +get_max_price(get_tomorrow()).hour + ":00"

labelminhourT = "Min Price at " + \
get_min_price(get_tomorrow()).hour + ":00"
labelmaxhourT = "Max Price at " + \
get_max_price(get_tomorrow()).hour + ":00"

col1, col2, col3 = st.columns(3)
col2.metric(labelminhourT, round(get_min_price(get_tomorrow()).value,2),delta_color="inverse")
col3.metric(labelmaxhourT, round(get_max_price(get_tomorrow()).value,2),delta_color="inverse")
col2.metric(labelminhourT, round(get_min_price(
get_tomorrow()).value, 2), delta_color="inverse")
col3.metric(labelmaxhourT, round(get_max_price(
get_tomorrow()).value, 2), delta_color="inverse")
else:
st.subheader("Tomorrow's prices are not available yet")
st.subheader("Tomorrow's prices are not available yet")


st.header("Last 30 days evolutions")
#Plot timeline
ago_days=30
i=0;
# Plot timeline
ago_days = 30
i = 0
# initialize list for plot
date_arr = []
avg_arr=[]
avg_arr = []

for x in range(ago_days, 0, -1):
i=i+1
i = i+1
date_average = date.today() - timedelta(x)
date_average_str = str(date.today() - timedelta(x))
average_str=str( round(calculate_average(get_date_days_ago(x)),2))
average=round(calculate_average(get_date_days_ago(x)),2)
average_str = str(round(calculate_average(get_date_days_ago(x)), 2))
average = round(calculate_average(get_date_days_ago(x)), 2)
date_arr.append(date_average_str)
avg_arr.append(average)

df = pd.DataFrame({
'date': date_arr,
'Average electricity prices': avg_arr
'date': date_arr,
'Average electricity prices': avg_arr
})

df = df.rename(columns={'date':'index'}).set_index('index')
df = df.rename(columns={'date': 'index'}).set_index('index')

# Create the pandas DataFrame

#dateList.append( {"id": str(i), "content": "100", "start": "100"})
#st.write(calculate_average(get_date_days_ago(x)))
chart= st.line_chart(df)
#timeline = st_timeline(dateList, groups=[], options={}, height="300px")
# dateList.append( {"id": str(i), "content": "100", "start": "100"})
# st.write(calculate_average(get_date_days_ago(x)))

chart = st.line_chart(df)
# timeline = st_timeline(dateList, groups=[], options={}, height="300px")


# start_day = '2023-03-01'
Expand All @@ -196,8 +189,8 @@ def calculate_average(prices: list[Price]) -> float:
# for x in price_data:
# new_rows = [x.get('price'), x.get('dateTime')]
# #status_text.text("%x%% Complete" % x)
# chart.add_rows(new_rows)
# chart.add_rows(new_rows)

# # progress_bar.progress(x)
# #last_rows = new_rows
# time.sleep(0.000005)
Expand All @@ -211,9 +204,9 @@ def calculate_average(prices: list[Price]) -> float:
# last_rows = new_rows
# time.sleep(0.05)

#progress_bar.empty()
# progress_bar.empty()

# Streamlit widgets automatically run the script from top to bottom. Since
# this button is not connected to any other logic, it just causes a plain
# rerun.
#st.button("Re-run")
# st.button("Re-run")
10 changes: 10 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3.5"

services:

electricity-prices-dashboard:
image: electricity-prices-dashboard
container_name: electricity-prices-dashboard
restart: always
ports:
- 8501:8501
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
babel
streamlit
python-dotenv
asyncio
meross_iot==0.4.5.9
python-i18n[YAML]
python-dateutil
python-dateutil

0 comments on commit 8a686ab

Please sign in to comment.