forked from Green-Software-Foundation/carbon-aware-sdk
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into 219/CarbonAwareNullable
- Loading branch information
Showing
61 changed files
with
1,223 additions
and
316 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
.github/workflows/verify-azure-function-with-packages.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
name: Verify Sample Azure Functions with GSF Packages | ||
|
||
on: | ||
push: | ||
branches: [ dev, main ] | ||
pull_request: | ||
branches: [ dev, main ] | ||
paths: | ||
- 'src/**' | ||
- '.github/workflows/**' | ||
- 'samples/azure-function/**' | ||
|
||
env: | ||
DOCKERFILE_PATH: samples/azure-function/Dockerfile | ||
CONTAINER_IMAGE_NAME: runnable-container | ||
CONTAINER_RUNTIME_NAME: az-func | ||
|
||
jobs: | ||
container_azure_function: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Docker RM container name | ||
continue-on-error: true | ||
run: docker rm -f ${{ env.CONTAINER_RUNTIME_NAME }} | ||
|
||
- name: Docker Target Final | ||
run: | | ||
docker build --no-cache . -f ${{ env.DOCKERFILE_PATH }} -t ${{ env.CONTAINER_IMAGE_NAME }} | ||
docker rmi -f $(docker images -f "dangling=true" -q) | ||
- name: Docker Run Container | ||
run: docker run -d --name ${{ env.CONTAINER_RUNTIME_NAME }} -p 8080:80 ${{ env.CONTAINER_IMAGE_NAME }} | ||
|
||
# Request fails with authentication error. Expected | ||
- name: Get Average Carbon Intensity | ||
run: | | ||
set +e | ||
ret_code=$(wget -S -t 5 --waitretry=5 "http://0.0.0.0:8080/api/GetAverageCarbonIntensity?startDate=2022-03-01T15:30:00Z&endDate=2022-03-01T18:30:00Z&location=eastus" 2>&1 | grep "HTTP/" | awk '{print $2}') | ||
set -e | ||
[ "401" == $ret_code ] | ||
# Request fails with authentication error. Expected | ||
- name: Get Current Forecast | ||
run: | | ||
set +e | ||
ret_code=$(wget -S -t 5 --waitretry=5 "http://0.0.0.0:8080/api/GetCurrentForecast" --header "Content-Type: application/json" --post-data '{"startDate":"2022-11-02T15:30:00Z","endDate":"2022-11-02T18:30:00Z","location":"eastus","duration":"15"}' 2>&1 | grep "HTTP/" | awk '{print $2}') | ||
set -e | ||
[ "401" == $ret_code ] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Selecting a Data Source | ||
|
||
The Carbon Aware SDK includes access to various data sources of carbon aware data, including WattTime, ElectricityMaps, and a custom JSON source. These matrices are an attempt to track what features of the Carbon Aware SDK are enabled for which data sources. | ||
|
||
## Contents | ||
|
||
- [Type of Data Sources and Configuration](#type-of-data-sources-and-configuration) | ||
- [Data Source Methods Available](#data-source-methods-available) | ||
- [Location Coverage](#location-coverage) | ||
|
||
## Type of Data Sources and Configuration | ||
|
||
In the CarbonAware SDK configuration, you can set what data source to use as the `EmissionsDataSource` and the `ForecastDataSource`. There are also certain configuration fields that must be set in order to access the raw data. | ||
| Type | WattTime | ElectricityMaps | JSON | | ||
|--------------------------|-----------|-----------------|------| | ||
| Is Emissions DataSource | ✅ | ✅ | ✅ | | ||
| Is Forecast DataSource | ✅ | ✅ | ❌ | | ||
| Makes HTTP(s) call | ✅ | ✅ | ❌ | | ||
| Can Use Custom Data | ❌ | ❌ | ✅ | | ||
| Supports Trial + Full Account | ✅ | ✅ (*[different config required](./configuration.md#electricitymaps-configuration)) | N/A | | ||
|
||
## Data Source Methods Available | ||
|
||
Not all data sources support all the routes provided in the interfaces (`IEmissionsDataSource`/`IForecastDataSource`). | ||
|
||
| Methods | WattTime | ElectricityMaps | JSON | CLI Usage | Web Api Usage | SDK Usage | ||
|--------------|:-----------:|:-----------------:|:------:|:-:|:-:|:-:| | ||
| GetCarbonIntensityAsync | ✅ | ✅ | ✅ |`emissions`|`emissions/bylocation` or `emissions/bylocations` or `emissions/bylocations/best` or `emissions/average`‑`carbon`‑`intensity` or `emissions/average`‑`carbon`‑`intensity/batch`|`GetEmissionsDataAsync(...)` or `GetBestEmissionsDataAsync(...)` or `GetAverageCarbonIntensityDataAsync(...)`| | ||
| GetCurrentForecastAsync | ✅ | ✅ | ❌ |`emissions`‑`forecasts`|`forecasts/current`|`GetCurrentForecastAsync(...)`| | ||
| GetForecastByDateAsync | ✅ | ❌ | ❌ |`emissions`‑`forecasts` ‑‑`requested`‑`at`|`forecasts/batch` with `requestedAt` field|`GetForecastByDateAsync(...)`| | ||
|
||
## Location Coverage | ||
|
||
Different data sources provide both different features (as outlined above) but also coverage of different geographic areas. It's important to note that each data source may have different region names, which are handled through the location config. | ||
|
||
- For `WattTime`, see their [interactive coverage map](https://www.watttime.org/explorer) to find the relevant zone. | ||
- For `ElectricityMaps`, see their [live map app](https://app.electricitymaps.com/map?utm_source=electricitymaps.com&utm_medium=website&utm_campaign=banner) to find the relevant zone and see current data coming in. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "Azure Functions & C# - .NET 6 (In-Process)", | ||
"dockerFile": "../Dockerfile", | ||
"forwardPorts": [ 7071 ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Find the Dockerfile at this URL | ||
# https://github.com/Azure/azure-functions-docker/blob/dev/host/4/bullseye/amd64/dotnet/dotnet-inproc/dotnet.Dockerfile | ||
|
||
FROM mcr.microsoft.com/azure-functions/dotnet:4.0 AS base | ||
WORKDIR /home/site/wwwroot | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build | ||
COPY ["src/", "data/src/"] | ||
COPY ["scripts/", "data/scripts/"] | ||
COPY ["samples/", "data/samples/"] | ||
|
||
WORKDIR /data | ||
RUN scripts/package/create_packages.sh src/CarbonAwareSDK.sln /packages && \ | ||
dotnet restore "samples/azure-function/function.csproj" && \ | ||
scripts/package/add_packages.sh samples/azure-function/function.csproj /packages && \ | ||
dotnet build "samples/azure-function/function.csproj" -c Release -o /app/build | ||
|
||
FROM build AS publish | ||
RUN dotnet publish "samples/azure-function/function.csproj" -c Release -o /app/publish | ||
|
||
FROM base AS final | ||
WORKDIR /home/site/wwwroot | ||
COPY --from=publish /app/publish . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using GSF.CarbonAware.Handlers; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Azure.WebJobs.Extensions.Http; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Text.Json; | ||
|
||
namespace function | ||
{ | ||
public class GetCarbonIntensity | ||
{ | ||
private readonly IEmissionsHandler _handler; | ||
|
||
public GetCarbonIntensity(IEmissionsHandler handler) | ||
{ | ||
this._handler = handler; | ||
} | ||
|
||
|
||
[FunctionName("GetAverageCarbonIntensity")] | ||
public async Task<IActionResult> Run( | ||
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, | ||
ILogger log) | ||
{ | ||
//Get the startDate, endDate, and location from the request query if the values are present in the query | ||
string startDate = req.Query["startdate"]; | ||
string endDate = req.Query["enddate"]; | ||
string location = req.Query["location"]; | ||
|
||
//If the parameters are includes in the body of the request, read the values from the request body | ||
string requestBody = String.Empty; | ||
using (StreamReader streamReader = new(req.Body)) | ||
{ | ||
requestBody = await streamReader.ReadToEndAsync(); | ||
} | ||
dynamic data = JsonSerializer.Deserialize<object>(requestBody); | ||
|
||
startDate ??= data?.startDate; | ||
endDate ??= data?.endDate; | ||
location ??= data?.location; | ||
|
||
|
||
try | ||
{ | ||
var result = await _handler.GetAverageCarbonIntensityAsync(location, DateTimeOffset.Parse(startDate), DateTimeOffset.Parse(endDate)); | ||
log.LogInformation($"For location {location} Starting at: {startDate} Ending at: {endDate} the Average Emissions Rating is: {result}."); | ||
|
||
return new OkObjectResult(result); | ||
} | ||
catch (Exception e) | ||
{ | ||
//Messages related to incorrect parameter values (ie dates outside of range) are returned in the data section | ||
//Otherwise send the returned error messages | ||
if (e.Data.Count>0) | ||
return new BadRequestObjectResult(e.Data); | ||
else | ||
return new BadRequestObjectResult(e.Message); | ||
} | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.