diff --git a/.github/workflows/1-pr.yaml b/.github/workflows/1-pr.yaml index 581c168af..81d0df645 100644 --- a/.github/workflows/1-pr.yaml +++ b/.github/workflows/1-pr.yaml @@ -122,9 +122,11 @@ jobs: directory: ${{ env.DOTNET_SRC_DIR }} - name: Generate Open API - run: dotnet tool run swagger tofile --output ./wwwroot/api/v1/swagger.yaml --yaml ${{ env.DLL_FILE_PATH }} v1 env: + DataSources__EmissionsDataSource: Json + DataSources__Configurations__Json__Type: JSON DOTNET_ROLL_FORWARD: LatestMajor + run: dotnet tool run swagger tofile --output ./wwwroot/api/v1/swagger.yaml --yaml ${{ env.DLL_FILE_PATH }} v1 working-directory: ./src/CarbonAware.WebApi/src - name: Upload swagger artifact @@ -146,7 +148,7 @@ jobs: - name: Docker Run Container run: | - docker run -d --name runnable-container -p 8080:8080 ca-api + docker run -d --name runnable-container -e DataSources__EmissionsDataSource=Json -e DataSources__Configurations__Json__Type=JSON -p 8080:8080 ca-api docker container ls - name: Docker WGET Health Endpoint diff --git a/casdk-docs/docs/overview/enablement.md b/casdk-docs/docs/overview/enablement.md index 8489c421a..0053f859a 100644 --- a/casdk-docs/docs/overview/enablement.md +++ b/casdk-docs/docs/overview/enablement.md @@ -147,6 +147,8 @@ parameters and short explanations. To get a list of all locations supported, you can use the Locations API, referenced in `src/CarbonAware.CLI/src/Commands/Location` and the command `.\caw locations`. +Note that you have to configure `LocationDataSourcesConfiguration` +into `appsettings.json` before running the command. Expected output: @@ -438,6 +440,9 @@ endpoints, full endpoint description can be found [here](https://github.com/Gree To get a list of all locations supported, you can use the Locations API endpoint `/locations` referenced in `src/CarbonAware.WebApi/src/Controllers/LocationsController.cs`. +Note that you have to configure `LocationDataSourcesConfiguration` +into `appsettings.json` before launching WebAPI, otherwise WebAPI returns +HTTP 204 (No Content). Expected Output: @@ -460,12 +465,16 @@ Expected Output: ##### Calling the `/emissions/bylocation` endpoint In console, we can run the below command, to request data for a single location -(currently Azure region names supported) in a particular timeframe: +in a particular timeframe: ```bash curl "http://localhost:5073/emissions/bylocation?location=westus&time=2022-08-23T14%3A00&toTime=2022-08-23T14%3A30" | jq ``` +Note that region names in this example (e.g. `westus`) are defined in +[azure-regions.json](https://github.com/Green-Software-Foundation/carbon-aware-sdk/blob/dev/src/data/location-sources/azure-regions.json). +AWS region is also available in [aws-regions.json](https://github.com/Green-Software-Foundation/carbon-aware-sdk/blob/dev/src/data/data-files/aws-regions.json). + You can omit the `| jq` to get the JSON data raw and unparsed. This is a request for data in the `westus` region from the date `2022-08-23 at 14:00` to `2022-08-23 at 14:30`. (Note: semicolons `:` are encoded as `%3A` in URLs). diff --git a/casdk-docs/docs/quickstart.md b/casdk-docs/docs/quickstart.md index 29b8f788f..73de56591 100644 --- a/casdk-docs/docs/quickstart.md +++ b/casdk-docs/docs/quickstart.md @@ -75,7 +75,8 @@ parameters and short explanations. To get a list of all locations supported, you can use the Locations API, referenced in `src/CarbonAware.CLI/src/Commands/Location` -and the command `.\caw locations`. +and the command `.\caw locations`. Note that you have to configure `LocationDataSourcesConfiguration` +into `appsettings.json` before running the command. Expected output: @@ -234,6 +235,9 @@ endpoints, full endpoint description can be found here: To get a list of all locations supported, you can use the Locations API endpoint `/locations` referenced in `src/CarbonAware.WebApi/src/Controllers/LocationsController.cs`. +Note that you have to configure `LocationDataSourcesConfiguration` +into `appsettings.json` before launching WebAPI, otherwise WebAPI returns +HTTP 204 (No Content). Expected Output: @@ -257,12 +261,16 @@ Expected Output: #### Calling the `/emissions/bylocation` endpoint In console, we can run the below command, to request data for a single location -(currently Azure region names supported) in a particular timeframe: +in a particular timeframe: ```bash curl "http://localhost:5073/emissions/bylocation?location=westus&time=2022-08-23T14%3A00&toTime=2022-08-23T14%3A30" | jq ``` +Note that region names in this example (e.g. `westus`) are defined in +[azure-regions.json](https://github.com/Green-Software-Foundation/carbon-aware-sdk/blob/dev/src/data/location-sources/azure-regions.json). +AWS region is also available in [aws-regions.json](https://github.com/Green-Software-Foundation/carbon-aware-sdk/blob/dev/src/data/data-files/aws-regions.json). + You can omit the `| jq` to get the JSON data raw and unparsed. This is a request for data in the `westus` region from the date `2022-08-23 at 14:00` to `2022-08-23 at 14:30`. (Note: semicolons `:` are encoded as `%3A` in URLs). diff --git a/src/CarbonAware.CLI/test/integrationTests/CarbonAware.CLI.IntegrationTests.csproj b/src/CarbonAware.CLI/test/integrationTests/CarbonAware.CLI.IntegrationTests.csproj index 8c68ddd6d..38ea8dfd8 100644 --- a/src/CarbonAware.CLI/test/integrationTests/CarbonAware.CLI.IntegrationTests.csproj +++ b/src/CarbonAware.CLI/test/integrationTests/CarbonAware.CLI.IntegrationTests.csproj @@ -23,4 +23,12 @@ Include="..\..\..\CarbonAware.DataSources\CarbonAware.DataSources.ElectricityMaps\mock\CarbonAware.DataSources.ElectricityMaps.Mocks.csproj" /> - \ No newline at end of file + + + + + + + diff --git a/src/CarbonAware.CLI/test/integrationTests/IntegrationTestingBase.cs b/src/CarbonAware.CLI/test/integrationTests/IntegrationTestingBase.cs index 4818c7f0d..fb4a07345 100644 --- a/src/CarbonAware.CLI/test/integrationTests/IntegrationTestingBase.cs +++ b/src/CarbonAware.CLI/test/integrationTests/IntegrationTestingBase.cs @@ -99,6 +99,7 @@ public void Setup() { Environment.SetEnvironmentVariable("DataSources__EmissionsDataSource", "Json"); Environment.SetEnvironmentVariable("DataSources__Configurations__Json__Type", "JSON"); + Environment.SetEnvironmentVariable("DataSources__Configurations__Json__DataFileLocation", "test-data-azure-emissions.json"); _dataSourceMocker = new JsonDataSourceMocker(); break; } diff --git a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/mock/JsonDataSourceMocker.cs b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/mock/JsonDataSourceMocker.cs index 808e3df9b..b46fac141 100644 --- a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/mock/JsonDataSourceMocker.cs +++ b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/mock/JsonDataSourceMocker.cs @@ -6,11 +6,18 @@ namespace CarbonAware.DataSources.Json.Mocks; public class JsonDataSourceMocker : IDataSourceMocker { - public JsonDataSourceMocker() { } + + public string DataFileName { get; set; } + + public JsonDataSourceMocker() { + DataFileName = "test-data-azure-emissions.json"; + } public void SetupDataMock(DateTimeOffset start, DateTimeOffset end, string location) { - string path = new JsonDataSourceConfiguration().DataFileLocation; + var config = new JsonDataSourceConfiguration(); + config.DataFileLocation = DataFileName; + string path = config.DataFileLocation; var data = new List(); DateTimeOffset pointTime = start; diff --git a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/src/CarbonAware.DataSources.Json.csproj b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/src/CarbonAware.DataSources.Json.csproj index 7bba6e61d..aa6cf0e00 100644 --- a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/src/CarbonAware.DataSources.Json.csproj +++ b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/src/CarbonAware.DataSources.Json.csproj @@ -7,13 +7,6 @@ false - - - Always - - - @@ -24,4 +17,8 @@ - \ No newline at end of file + + + + + diff --git a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/src/Configuration/JsonDataSourceConfiguration.cs b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/src/Configuration/JsonDataSourceConfiguration.cs index b54a3116d..f34387a77 100644 --- a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/src/Configuration/JsonDataSourceConfiguration.cs +++ b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/src/Configuration/JsonDataSourceConfiguration.cs @@ -9,7 +9,6 @@ namespace CarbonAware.DataSources.Json.Configuration; internal class JsonDataSourceConfiguration { private const string BaseDirectory = "data-sources/json"; - private const string DefaultDataFile = "test-data-azure-emissions.json"; private const string DirectoryRegExPattern = @"^(?!\.{2})[-\\/a-zA-Z_\d\.: ]*$"; private string assemblyDirectory; private string? dataFileLocation; @@ -36,7 +35,6 @@ public JsonDataSourceConfiguration() { var assemblyPath = Assembly.GetExecutingAssembly().Location; assemblyDirectory = Path.GetDirectoryName(assemblyPath)!; - DataFileLocation = DefaultDataFile; CacheJsonData = true; } diff --git a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/CarbonAware.DataSources.Json.Tests.csproj b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/CarbonAware.DataSources.Json.Tests.csproj index c54355685..31cb83429 100644 --- a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/CarbonAware.DataSources.Json.Tests.csproj +++ b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/CarbonAware.DataSources.Json.Tests.csproj @@ -27,4 +27,7 @@ + + + diff --git a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/JsonDataSourceConfigurationTests.cs b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/JsonDataSourceConfigurationTests.cs index f63565a73..e8d0f0cc7 100644 --- a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/JsonDataSourceConfigurationTests.cs +++ b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/JsonDataSourceConfigurationTests.cs @@ -24,14 +24,6 @@ public void Setup() AssemblyPath = Assembly.GetExecutingAssembly().Location; } - [Test] - public void GetDefaultDataFileLocation_IsNotNull_ExpectedBaseDir() - { - Assert.That(_configuration.DataFileLocation, Is.Not.Null); - var expectedDir = Path.Combine(Path.GetDirectoryName(AssemblyPath)!, BaseDir); - Assert.That(_configuration.DataFileLocation, Contains.Substring(expectedDir)); - } - [TestCase("../newfile.json", TestName = "setting parent's dir")] [TestCase("~/newfile.json", TestName = "setting user's home dir")] [TestCase(null, TestName = "setting null filepath")] diff --git a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/JsonDataSourceTests.cs b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/JsonDataSourceTests.cs index 68ea73e24..798d58a3c 100644 --- a/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/JsonDataSourceTests.cs +++ b/src/CarbonAware.DataSources/CarbonAware.DataSources.Json/test/JsonDataSourceTests.cs @@ -111,17 +111,16 @@ public async Task GetCarbonIntensityAsync_ReturnsEmptyEmissionData() public async Task GetCarbonIntensityAsync_CacheEmissionData(bool cache) { var logger = Mock.Of>(); - var monitor = new Mock>(); + JsonDataSourceMocker dsMocker = new(); var config = new JsonDataSourceConfiguration { + DataFileLocation = dsMocker.DataFileName, CacheJsonData = cache }; monitor.Setup(m => m.CurrentValue).Returns(config); var dataSource = new JsonDataSource(logger, monitor.Object); - JsonDataSourceMocker dsMocker = new(); - var today = DateTimeOffset.Now; var todayEnd = today.AddMinutes(30); var todayLocation = new Location() { Name = "japan" }; diff --git a/src/CarbonAware.LocationSources/src/CarbonAware.LocationSources.csproj b/src/CarbonAware.LocationSources/src/CarbonAware.LocationSources.csproj index 50211d65d..fabd8f96e 100644 --- a/src/CarbonAware.LocationSources/src/CarbonAware.LocationSources.csproj +++ b/src/CarbonAware.LocationSources/src/CarbonAware.LocationSources.csproj @@ -12,16 +12,13 @@ false - - - Always - - - - \ No newline at end of file + + + + + diff --git a/src/CarbonAware.LocationSources/test/CarbonAware.LocationSources.Test.csproj b/src/CarbonAware.LocationSources/test/CarbonAware.LocationSources.Test.csproj index 28dc668ef..98f8cad33 100644 --- a/src/CarbonAware.LocationSources/test/CarbonAware.LocationSources.Test.csproj +++ b/src/CarbonAware.LocationSources/test/CarbonAware.LocationSources.Test.csproj @@ -24,4 +24,8 @@ + + + + diff --git a/src/CarbonAware.WebApi/src/Dockerfile b/src/CarbonAware.WebApi/src/Dockerfile index 068357d4a..f0bfed06f 100644 --- a/src/CarbonAware.WebApi/src/Dockerfile +++ b/src/CarbonAware.WebApi/src/Dockerfile @@ -3,8 +3,11 @@ FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS openapi-env WORKDIR /app ENV DOTNET_ROLL_FORWARD LatestMajor COPY . ./ -RUN dotnet build CarbonAware.WebApi/src/CarbonAware.WebApi.csproj -o build +RUN dotnet build CarbonAware.WebApi/src/CarbonAware.WebApi.csproj -c Release -o build WORKDIR /app/CarbonAware.WebApi/src +# Set environment variables for dummy datasource to start "dotnet tool run" +ENV DataSources__EmissionsDataSource=Json +ENV DataSources__Configurations__Json__Type=JSON RUN dotnet tool restore && \ dotnet tool run swagger tofile --output /app/build/swagger.yaml --yaml /app/build/CarbonAware.WebApi.dll v1 @@ -17,7 +20,7 @@ ENV DOTNET_ROLL_FORWARD LatestMajor # Copy everything from source COPY . ./ # Use implicit restore to build and publish -RUN dotnet publish CarbonAware.WebApi/src/CarbonAware.WebApi.csproj -a $TARGETARCH -o publish +RUN dotnet publish CarbonAware.WebApi/src/CarbonAware.WebApi.csproj -a $TARGETARCH -c Release -o publish # Build runtime image diff --git a/src/CarbonAware.WebApi/src/appsettings.json b/src/CarbonAware.WebApi/src/appsettings.json index 8208b3d19..10f68b8c8 100644 --- a/src/CarbonAware.WebApi/src/appsettings.json +++ b/src/CarbonAware.WebApi/src/appsettings.json @@ -1,14 +1,4 @@ { - "DataSources": { - "EmissionsDataSource": "test-json", - "ForecastDataSource": "", // We don't currently publish a sample test data source for forecasts. - "Configurations": { - "test-json": { - "Type": "JSON", - "DataFileLocation": "test-data-azure-emissions.json" - } - } - }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/src/CarbonAware.WebApi/test/integrationTests/CarbonAware.WebApi.IntegrationTests.csproj b/src/CarbonAware.WebApi/test/integrationTests/CarbonAware.WebApi.IntegrationTests.csproj index fd9179089..c98dc4189 100644 --- a/src/CarbonAware.WebApi/test/integrationTests/CarbonAware.WebApi.IntegrationTests.csproj +++ b/src/CarbonAware.WebApi/test/integrationTests/CarbonAware.WebApi.IntegrationTests.csproj @@ -22,20 +22,16 @@ - - - + + + - + - + + - \ No newline at end of file + diff --git a/src/CarbonAware.WebApi/test/integrationTests/CarbonAwareControllerTests.cs b/src/CarbonAware.WebApi/test/integrationTests/CarbonAwareControllerTests.cs index fb1c149f9..daf63cf0b 100644 --- a/src/CarbonAware.WebApi/test/integrationTests/CarbonAwareControllerTests.cs +++ b/src/CarbonAware.WebApi/test/integrationTests/CarbonAwareControllerTests.cs @@ -212,8 +212,6 @@ public async Task EmissionsForecastsCurrent_SupportedDataSources_ReturnsOk() [Test] public async Task EmissionsForecastsCurrent_StartAndEndOutsideWindow_ReturnsBadRequest() { - IgnoreTestForDataSource("data source does not implement '/emissions/forecasts/current'", DataSourceType.JSON); - _dataSourceMocker?.SetupForecastMock(); var queryStrings = new Dictionary(); @@ -235,8 +233,6 @@ public async Task EmissionsForecastsCurrent_StartAndEndOutsideWindow_ReturnsBadR public async Task EmissionsForecastsCurrent_InvalidLocationQueryString_ReturnsBadRequest(string queryString, string value) { - IgnoreTestForDataSource("data source does not implement '/emissions/forecasts/current'", DataSourceType.JSON); - _dataSourceMocker?.SetupForecastMock(); var queryStrings = new Dictionary(); @@ -255,8 +251,6 @@ public async Task EmissionsForecastsCurrent_InvalidLocationQueryString_ReturnsBa [TestCase("eastus", "2021-9-1T08:30:00Z", TestName = "EmissionsForecastsBatch returns BadRequest for wrong date format")] public async Task EmissionsForecastsBatch_MissingRequiredParams_ReturnsBadRequest(string location, string requestedAt) { - IgnoreTestForDataSource("data source does not implement '/emissions/forecasts/batch'", DataSourceType.JSON); - _dataSourceMocker?.SetupForecastMock(); var forecastData = Enumerable.Range(0, 1).Select(x => new { diff --git a/src/CarbonAware.WebApi/test/integrationTests/UnconfiguredWebApiTests.cs b/src/CarbonAware.WebApi/test/integrationTests/WebApiEndpointTests.cs similarity index 77% rename from src/CarbonAware.WebApi/test/integrationTests/UnconfiguredWebApiTests.cs rename to src/CarbonAware.WebApi/test/integrationTests/WebApiEndpointTests.cs index 665b3884a..6bb55dd74 100644 --- a/src/CarbonAware.WebApi/test/integrationTests/UnconfiguredWebApiTests.cs +++ b/src/CarbonAware.WebApi/test/integrationTests/WebApiEndpointTests.cs @@ -5,15 +5,15 @@ namespace CarbonAware.WebApi.IntegrationTests; /// -/// Tests that the Web API starts without configuration. +/// Tests that static Web API endpoints. /// -[TestFixture(DataSourceType.None)] -class UnconfiguredWebApiTests : IntegrationTestingBase +[TestFixture(DataSourceType.JSON)] +class WebApiEndpointTests : IntegrationTestingBase { private readonly string healthURI = "/health"; private readonly string fakeURI = "/fake-endpoint"; - public UnconfiguredWebApiTests(DataSourceType dataSource) : base(dataSource) { } + public WebApiEndpointTests(DataSourceType dataSource) : base(dataSource) { } [Test] public async Task HealthCheck_ReturnsOK() diff --git a/src/GSF.CarbonAware/src/GSF.CarbonAware.csproj b/src/GSF.CarbonAware/src/GSF.CarbonAware.csproj index 6bb9fac4b..c7ad608aa 100644 --- a/src/GSF.CarbonAware/src/GSF.CarbonAware.csproj +++ b/src/GSF.CarbonAware/src/GSF.CarbonAware.csproj @@ -50,12 +50,13 @@ - - + + + + diff --git a/src/GSF.CarbonAware/src/GSF.CarbonAware.targets b/src/GSF.CarbonAware/src/GSF.CarbonAware.targets index 752a668a6..8e17e6cf1 100644 --- a/src/GSF.CarbonAware/src/GSF.CarbonAware.targets +++ b/src/GSF.CarbonAware/src/GSF.CarbonAware.targets @@ -1,24 +1,11 @@ - - - - - - - - - - - - - - - - + + +