From 4e48ea23561bd413fc508bd2780249b318451d70 Mon Sep 17 00:00:00 2001 From: Mati Kljukin Date: Thu, 17 Oct 2024 08:48:37 +0300 Subject: [PATCH] added highest-price mock, service, readme --- .../electricity/highest-price.yml | 42 ++++++++ .../electricity/mock/highest-price.yml | 102 ++++++++++++++++++ docs/example-queries/electricity/README.md | 47 ++++++++ 3 files changed, 191 insertions(+) create mode 100644 DSL/Ruuter.public/electricity/highest-price.yml create mode 100644 DSL/Ruuter.public/electricity/mock/highest-price.yml create mode 100644 docs/example-queries/electricity/README.md diff --git a/DSL/Ruuter.public/electricity/highest-price.yml b/DSL/Ruuter.public/electricity/highest-price.yml new file mode 100644 index 0000000..5785503 --- /dev/null +++ b/DSL/Ruuter.public/electricity/highest-price.yml @@ -0,0 +1,42 @@ +declaration: + call: declare + version: 0.1 + name: "Electricity Price Service" + description: | + This service fetches the Nordpool electricity prices for a user-specified date and returns the highest price. + method: get + params: + userDate: string # yyyy-mm-dd format + returns: json + +# Prepare Dates based on userDate +prepareDates: + assign: + 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: + call: http.get + args: + url: https://dashboard.elering.ee/api/nps/price + query: + start: ${startDateTime} + end: ${endDateTime} + result: eleringToday + next: assignVariables + +assignVariables: + assign: + maxPrice: ${eleringToday.response.body.data.ee.sort((a, b) => b.price - a.price)[0].price} # get highest price + maxPriceTimestamp: ${eleringToday.response.body.data.ee.sort((a, b) => b.price - a.price)[0].timestamp} # get timestamp for highest price + + tallinnOffset: 10800 # Tallinn is UTC+3 during DST (3 hours in seconds) + adjustedTimestamp: ${maxPriceTimestamp + tallinnOffset} + adjustedHour: ${Math.floor((adjustedTimestamp % 86400) / 3600)} + timeVar: ${("0" + adjustedHour).slice(-2) + ":00"} + next: returnResult + +returnResult: + return: ${[userDate, timeVar, maxPrice]} \ No newline at end of file diff --git a/DSL/Ruuter.public/electricity/mock/highest-price.yml b/DSL/Ruuter.public/electricity/mock/highest-price.yml new file mode 100644 index 0000000..4b6c390 --- /dev/null +++ b/DSL/Ruuter.public/electricity/mock/highest-price.yml @@ -0,0 +1,102 @@ +declaration: + call: declare + version: 0.1 + name: "MOCK - Get Highest Electricity Price" + description: | + "This mock service fetches the highest electricity price for a specified day." + method: "get" + parameters: + - date: "The date (YYYY-MM-DD) for which to retrieve the highest electricity price." + return: "json" + +prepareDates: + assign: + 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: + 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: + maxPrice: ${eleringToday.response.body.data.ee.sort((a, b) => b.price - a.price)[0].price} # get highest price + maxPriceTimestamp: ${eleringToday.response.body.data.ee.sort((a, b) => b.price - a.price)[0].timestamp} # get highest timestamp + + tallinnOffset: 10800 # Tallinn is UTC+3 during DST (3 hours in seconds) + adjustedTimestamp: ${maxPriceTimestamp + tallinnOffset} + adjustedHour: ${Math.floor((adjustedTimestamp % 86400) / 3600)} + timeVar: ${("0" + adjustedHour).slice(-2) + ":00"} + next: returnResult + +returnResult: + return: ${[userDate, timeVar, maxPrice]} + +# expected mock response +# { +# "response": [ +# "2024-10-11", +# "08:00", +# 86.71 +# ] +# } \ No newline at end of file diff --git a/docs/example-queries/electricity/README.md b/docs/example-queries/electricity/README.md new file mode 100644 index 0000000..cfbd233 --- /dev/null +++ b/docs/example-queries/electricity/README.md @@ -0,0 +1,47 @@ +# Electricity service API + +## Fetch the lowest or highest electricity price for a specific date (default is today) +**Endpoints** +``` +electricity/mock/lowest-price +electricity/lowest-price +electricity/mock/highest-price +electricity/highest-price +``` + +**Sample query - without date** +``` +curl localhost:8080/electricity/mock/lowest-price +curl localhost:8080/electricity//lowest-price + +curl localhost:8080/electricity/mock/highest-price +curl localhost:8080/electricity//highest-price +``` + +**Expected outcome** +``` +{ + "response": [ + "2024-10-10", + "23:00", + 0.0 + ] +} +``` + +**Sample query - with a specific date parameter (userDate)** +``` +curl localhost:8080/electricity/mock/lowest-price?userDate=2024-05-05 +curl localhost:8080/electricity/lowest-price?userDate=2024-05-05 +``` + +**Expected outcome** +``` +{ + "response": [ + "2024-05-05", + "15:00", + 1.9 + ] +} +```