Skip to content

Commit

Permalink
Fix nord pool prices timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
jurna committed Dec 12, 2023
1 parent 9fa0a20 commit 02868a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
18 changes: 14 additions & 4 deletions internal/nordpool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"io"
"log"
"net/http"
"time"
)
Expand Down Expand Up @@ -37,6 +38,7 @@ type TransmissionCostConfig struct {
type NordPoolConfig struct {
MaxPrice float64 `yaml:"max-price"`
Vat float64 `yaml:"vat"`
Timezone string `yaml:"timezone"`
TransmissionCost TransmissionCostConfig `yaml:"transmission-cost"`
}

Expand All @@ -53,18 +55,23 @@ var (
)

func GetPrice(s3svc *s3.S3, awsS3Bucket string, date time.Time, config NordPoolConfig) (price float64, err error) {
prices, err := readPrices(s3svc, awsS3Bucket, date)
location, err := time.LoadLocation(config.Timezone)
if err != nil {
return
}
locationDate := date.In(location)
prices, err := readPrices(s3svc, awsS3Bucket, locationDate)
if err != nil {
if !errors.Is(err, errPricesFileDoesNotExist) {
return
}
prices, err = fetchDates(s3svc, awsS3Bucket, date)
prices, err = fetchDates(s3svc, awsS3Bucket, locationDate)
if err != nil {
return
}
}
poolPrice, err := findPrice(prices.Data.Lt, date)
price, err = calculatePrice(date, poolPrice, config)
poolPrice, err := findPrice(prices.Data.Lt, locationDate)
price, err = calculatePrice(locationDate, poolPrice, config)
return
}

Expand All @@ -87,6 +94,7 @@ func calculatePrice(date time.Time, poolPrice float64, config NordPoolConfig) (p

func findPrice(prices []Price, date time.Time) (price float64, err error) {
timestamp := date.Truncate(time.Hour).Unix()
log.Printf("Looking for price at %d, date %s", timestamp, date)
for _, p := range prices {
if p.Timestamp == timestamp {
price = p.Price
Expand All @@ -105,6 +113,7 @@ func fetchDates(s3svc *s3.S3, awsS3Bucket string, date time.Time) (prices Prices
trunc := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
q.Add("start", trunc.Format(time.RFC3339))
q.Add("end", trunc.AddDate(0, 0, 1).Format(time.RFC3339))
log.Printf("Fetching prices from %s to %s", q.Get("start"), q.Get("end"))
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down Expand Up @@ -140,6 +149,7 @@ func pricesFileName(date time.Time) string {

func readPrices(s3svc *s3.S3, awsS3Bucket string, date time.Time) (prices Prices, err error) {
fileName := pricesFileName(date)
log.Printf("Reading prices from %s", fileName)
input := &s3.GetObjectInput{Bucket: aws.String(awsS3Bucket),
Key: &fileName,
}
Expand Down
3 changes: 2 additions & 1 deletion internal/nordpool/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
)

func TestCalculatePrice(t *testing.T) {
location, _ := time.LoadLocation("Europe/Vilnius")
config := NordPoolConfig{
MaxPrice: 0,
Vat: 0,
Timezone: "Europe/Vilnius",
TransmissionCost: TransmissionCostConfig{Day: 0.1, Night: 0.05, DayStartsAt: 7, NightStartsAt: 23, Timezone: "Etc/GMT-2"},
}
location, _ := time.LoadLocation(config.Timezone)
tests := []struct {
name string
currentTime time.Time
Expand Down

0 comments on commit 02868a0

Please sign in to comment.