Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

80 electricity service #109

Merged
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
33 changes: 33 additions & 0 deletions DSL/DMapper/hbs/electricity/get_time_period.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--
### Input example:
{
"response": [
{
"timestamp": 1727730000,
"price": 5.01
},
{
"timestamp": 1727733600,
"price": 3.21
},
...
]
}

### Output example:
{
"result" : "Electricity prices from 2024-10-01 til 2024-10-14
* 2024-10-01 00:00 5.01 €/MWh
* 2024-10-01 01:00 3.21 €/MWh
* 2024-10-01 02:00 0.07 €/MWh
...
}
-->

{
"response" : "Elektrihinnad alates {{startDate response}} kuni {{endDate response}}
€/MWh
{{#each response}}
* {{formatTimestamp this.timestamp}} {{addVAT this.price}}
{{/each}}"
}
25 changes: 25 additions & 0 deletions DSL/DMapper/js/electricity-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Helper function to handle date conversion and timezone adjustment
function convertToEEST(timestamp) {
const date = new Date(timestamp * 1000); // Convert to milliseconds
date.setHours(date.getHours() + 3); // Adjust for EEST (UTC+3)
return date;
}

Handlebars.registerHelper('startDate', function(response) {
const firstEntry = response[0];
return convertToEEST(firstEntry.timestamp).toISOString().slice(0, 10); // Format YYYY-MM-DD
});

Handlebars.registerHelper('endDate', function(response) {
const lastEntry = response[response.length - 1];
return convertToEEST(lastEntry.timestamp).toISOString().slice(0, 10); // Format YYYY-MM-DD
});

Handlebars.registerHelper('formatTimestamp', function(timestamp) {
return convertToEEST(timestamp).toISOString().slice(0, 16).replace('T', ' '); // Format "YYYY-MM-DD HH:mm"
});

Handlebars.registerHelper('addVAT', function(price) {
const total = price * 1.22;
return Math.round(total * 100) / 100; // Round to 2 decimal places
});
12 changes: 6 additions & 6 deletions DSL/Ruuter.public/electricity/highest-price.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ declaration:
This service fetches the Nordpool electricity prices for a user-specified date and returns the highest price.
method: get
params:
userTime: string # yyyy-mm-dd format
userDate: string # yyyy-mm-dd format
returns: json

# Prepare Dates based on userTime
# Prepare Dates based on userDate
prepareDates:
assign:
userTime: ${incoming.params.userTime || new Date().toISOString().split('T')[0]} # yyyy-mm-dd
startDateTime: ${new Date(new Date(userTime).getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0] + "T21:00:00.000Z"}
endDateTime: ${userTime + "T20:59:59.000Z"}
userDate: ${incoming.params.userDate || new Date().toISOString().split('T')[0]} # yyyy-mm-dd
startDateTime: ${new Date(new Date(userDate).getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0] + "T21:00:00.000Z"}
endDateTime: ${userDate + "T20:59:59.000Z"}
next: getPriceForPeriod

getPriceForPeriod:
Expand All @@ -39,4 +39,4 @@ assignVariables:
next: returnResult

returnResult:
return: ${[userTime, timeVar, maxPrice]}
return: ${[userDate, timeVar, maxPrice]}
12 changes: 6 additions & 6 deletions DSL/Ruuter.public/electricity/lowest-price.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ declaration:
This service fetches the Nordpool electricity prices for a user-specified date.
method: get
params:
userTime: string # yyyy-mm-dd format
userDate: string # yyyy-mm-dd format
returns: json

# Prepare Dates based on userTime
# Prepare Dates based on userDate
prepareDates:
assign:
userTime: ${incoming.params.userTime || new Date().toISOString().split('T')[0]} # yyyy-mm-dd
startDateTime: ${new Date(new Date(userTime).getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0] + "T21:00:00.000Z"}
endDateTime: ${userTime + "T20:59:59.000Z"}
userDate: ${incoming.params.userDate || new Date().toISOString().split('T')[0]} # yyyy-mm-dd
startDateTime: ${new Date(new Date(userDate).getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0] + "T21:00:00.000Z"}
endDateTime: ${userDate + "T20:59:59.000Z"}
next: getPriceForPeriod

getPriceForPeriod:
Expand All @@ -40,4 +40,4 @@ assignVariables:
next: returnResult

returnResult:
return: ${[userTime, timeVar, minPrice]}
return: ${[userDate, timeVar, minPrice]}
8 changes: 4 additions & 4 deletions DSL/Ruuter.public/electricity/mock/highest-price.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ declaration:

prepareDates:
assign:
userTime: ${incoming.params.userTime || new Date().toISOString().split('T')[0]} # yyyy-mm-dd
startDateTime: ${new Date(new Date(userTime).getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0] + "T21:00:00.000Z"} # day before at 9am
endDateTime: ${userTime + "T20:59:59.000Z"} # chosen date at 9pm - default time format
userDate: ${incoming.params.userDate || new Date().toISOString().split('T')[0]} # yyyy-mm-dd
startDateTime: ${new Date(new Date(userDate).getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0] + "T21:00:00.000Z"} # day before at 9am
endDateTime: ${userDate + "T20:59:59.000Z"} # chosen date at 9pm - default time format
next: getPriceForPeriod

getPriceForPeriod:
Expand Down Expand Up @@ -90,7 +90,7 @@ assignVariables:
next: returnResult

returnResult:
return: ${[userTime, timeVar, maxPrice]}
return: ${[userDate, timeVar, maxPrice]}

# expected mock response
# {
Expand Down
12 changes: 6 additions & 6 deletions DSL/Ruuter.public/electricity/mock/lowest-price.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ declaration:
This service fetches the Nordpool electricity prices for a user-specified date.
method: get
params:
userTime: string # yyyy-mm-dd format
userDate: string # yyyy-mm-dd format
returns: json

# Prepare Dates based on userTime
# Prepare Dates based on userDate
prepareDates:
assign:
userTime: ${incoming.params.userTime || new Date().toISOString().split('T')[0]} # yyyy-mm-dd
startDateTime: ${new Date(new Date(userTime).getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0] + "T21:00:00.000Z"} # day before at 9am
endDateTime: ${userTime + "T20:59:59.000Z"} # chosen date at 9pm - default time format
userDate: ${incoming.params.userDate || new Date().toISOString().split('T')[0]} # yyyy-mm-dd
startDateTime: ${new Date(new Date(userDate).getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0] + "T21:00:00.000Z"} # day before at 9am
endDateTime: ${userDate + "T20:59:59.000Z"} # chosen date at 9pm - default time format
next: getPriceForPeriod

getPriceForPeriod:
Expand Down Expand Up @@ -91,4 +91,4 @@ assignVariables:
next: returnResult

returnResult:
return: ${[userTime, timeVar, minPrice]}
return: ${[userDate, timeVar, minPrice]}
94 changes: 94 additions & 0 deletions DSL/Ruuter.public/electricity/mock/time-period.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
declaration:
call: declare
version: 0.1
name: "MOCK - Electricity Price Service"
description: |
This service fetches the Nordpool electricity prices for a user-specified date.
method: get
params:
userTime: string # yyyy-mm-dd format
returns: json

# Prepare Dates based on userTime
prepareDates:
assign:
userTime: ${incoming.params.userTime || new Date().toISOString().split('T')[0]} # yyyy-mm-dd
startDateTime: ${new Date(new Date(userTime).getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0] + "T21:00:00.000Z"} # day before at 9am
endDateTime: ${userTime + "T20:59:59.000Z"} # chosen date at 9pm - default time format
next: getPriceForPeriod

getPriceForPeriod:
call: reflect.mock
args:
request:
url: https://dashboard.elering.ee/api/nps/price
query:
start: ${startDateTime}
end: ${endDateTime}
response:
data:
ee: # mock data
- timestamp: 1728507600
price: 6.9500
- timestamp: 1728511200
price: 58.6900
- timestamp: 1728514800
price: 45.6700
- timestamp: 1728518400
price: 24.9100
- timestamp: 1728522000
price: 12.8900
- timestamp: 1728525600
price: 14.2400
- timestamp: 1728529200
price: 38.8700
- timestamp: 1728532800
price: 61.6000
- timestamp: 1728536400
price: 86.7100
- timestamp: 1728540000
price: 80.4600
- timestamp: 1728543600
price: 67.5000
- timestamp: 1728547200
price: 24.9800
- timestamp: 1728550800
price: 24.7400
- timestamp: 1728554400
price: 21.8300
- timestamp: 1728558000
price: 15.9400
- timestamp: 1728561600
price: 19.7800
- timestamp: 1728565200
price: 20.1100
- timestamp: 1728568800
price: 17.9100
- timestamp: 1728572400
price: 70.0200
- timestamp: 1728576000
price: 70.0600
- timestamp: 1728579600
price: 27.0300
- timestamp: 1728583200
price: 6.1600
- timestamp: 1728586800
price: 3.3200
- timestamp: 1728590400
price: 0.0000
result: eleringToday
next: assignVariables

assignVariables:
assign:
minPrice: ${eleringToday.response.body.data.ee.sort((a, b) => a.price - b.price)[0].price} # get lowest price
minPriceTimestamp: ${eleringToday.response.body.data.ee.sort((a, b) => a.price - b.price)[0].timestamp} # get lowest timestamp

tallinnOffset: 10800 # Tallinn is UTC+3 during DST (3 hours in seconds)
adjustedTimestamp: ${minPriceTimestamp + tallinnOffset}
adjustedHour: ${Math.floor((adjustedTimestamp % 86400) / 3600)}
timeVar: ${("0" + adjustedHour).slice(-2) + ":00"}
next: returnResult

returnResult:
return: ${[userTime, timeVar, minPrice]}
67 changes: 67 additions & 0 deletions DSL/Ruuter.public/electricity/time-period.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
declaration:
call: declare
version: 0.2
name: "Electricity Price Service"
description: |
This service fetches Nordpool electricity prices for a specified date range.
method: get
params:
startDate: string # yyyy-mm-dd format (start of the period)
endDate: string # yyyy-mm-dd format (end of the period)
returns: json

# Prepare startDate, endDate
getDates:
assign:
startDate: ${incoming.params.startDate || new Date().toISOString().split('T')[0]} # yyyy-mm-dd
endDate: ${incoming.params.endDate || new Date().toISOString().split('T')[0]} # default to today
next: formatDates

# make dates conform to API standard
formatDates:
assign:
startDateTime: ${new Date(new Date(startDate).getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0] + "T21:00:00.000Z"}
endDateTime: ${endDate + "T20:59:59.000Z"}
next: dateCheck

dateCheck:
switch:
- condition: ${new Date(endDateTime) < new Date(startDateTime)}
next: startDateError
next: logStep

logStep:
log: "Start date: ${startDateTime}, End date: ${endDateTime}"
next: getPriceForPeriod

getPriceForPeriod:
call: http.get
args:
url: https://dashboard.elering.ee/api/nps/price
query:
start: ${startDateTime}
end: ${endDateTime}
result: eleringPrices
next: assignResult

assignResult:
assign:
periodPrices: ${eleringPrices.response.body.data.ee}
responseLength: ${(eleringPrices.response.body.data.ee).length}
next: logResult

logResult:
log: "RESPONSE LENGTH: ${responseLength}"
next: returnResult

returnResult:
return: ${periodPrices}
next: end

startDateError:
return: "Error: start date cannot be later than end date"
next: end

dateRangeError:
return: "Error: interval cannot be longer than 1 year"
next: end
6 changes: 3 additions & 3 deletions docs/example-queries/electricity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ curl localhost:8080/electricity//highest-price
}
```

**Sample query - with a specific date (userTime parameter)**
**Sample query - with a specific date parameter (userDate)**
```
curl localhost:8080/electricity/mock/lowest-price?userTime=2024-05-05
curl localhost:8080/electricity/lowest-price?userTime=2024-05-05
curl localhost:8080/electricity/mock/lowest-price?userDate=2024-05-05
curl localhost:8080/electricity/lowest-price?userDate=2024-05-05
```

**Expected outcome**
Expand Down