Skip to content

Commit

Permalink
Merge pull request #555 from YaSuenag/pr/update-json-emissiondata
Browse files Browse the repository at this point in the history
Add a configuration for disabling to cache JSON emission data
  • Loading branch information
danuw committed Aug 27, 2024
2 parents 89a5ba3 + ecda90d commit e67716d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
5 changes: 5 additions & 0 deletions casdk-docs/docs/tutorial-extras/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ where the assembly `CarbonAware.WebApi.dll` is located under `data-sources/json`
directory. For instance, if the application is installed under `/app`, copy the
file to `/app/data-sources/json`.

Emission data in JSON is cached by default, it means original data would be handled
even if JSON is updated.
You need to set `DataSources__Configurations__JSON__CacheJsonData=false`
if you want to handle updated data.

```sh
cp <mydir>/mycustomfile.json /app/data-sources/json
export DataSources__Configurations=Json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text.Json;

namespace CarbonAware.DataSources.Json.Mocks;
internal class JsonDataSourceMocker : IDataSourceMocker
public class JsonDataSourceMocker : IDataSourceMocker
{
public JsonDataSourceMocker() { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ public string DataFileLocation
}
}

public bool CacheJsonData { get; set; }

public JsonDataSourceConfiguration()
{
var assemblyPath = Assembly.GetExecutingAssembly().Location;
assemblyDirectory = Path.GetDirectoryName(assemblyPath)!;
DataFileLocation = DefaultDataFile;
CacheJsonData = true;
}

private static bool IsValidDirPath(string fileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ private IEnumerable<EmissionsData> FilterByLocation(IEnumerable<EmissionsData> d
}
using Stream stream = GetStreamFromFileLocation();
var jsonObject = await JsonSerializer.DeserializeAsync<EmissionsJsonFile>(stream);
if (_emissionsData is null || !_emissionsData.Any())
if (_configuration.CacheJsonData && (_emissionsData is null || !_emissionsData.Any()))
{
_emissionsData = jsonObject?.Emissions;
}
return _emissionsData;
return jsonObject?.Emissions;
}

private Stream GetStreamFromFileLocation()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -8,10 +8,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="Moq"/>
<PackageReference Include="NUnit"/>
<PackageReference Include="NUnit3TestAdapter"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="coverlet.collector">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -23,6 +23,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\mock\CarbonAware.DataSources.Json.Mocks.csproj" />
<ProjectReference Include="..\src\CarbonAware.DataSources.Json.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Moq.Protected;
using Microsoft.Extensions.Options;
using CarbonAware.DataSources.Json.Configuration;
using CarbonAware.DataSources.Json.Mocks;

namespace CarbonAware.DataSources.Json.Tests;

Expand Down Expand Up @@ -105,6 +106,56 @@ public async Task GetCarbonIntensityAsync_ReturnsEmptyEmissionData()
Assert.That(!result.Any(), Is.True);
}

[TestCase(true, TestName = "Test JsonDataSource with caching")]
[TestCase(false, TestName = "Test JsonDataSource without caching")]
public async Task GetCarbonIntensityAsync_CacheEmissionData(bool cache)
{
var logger = Mock.Of<ILogger<JsonDataSource>>();

var monitor = new Mock<IOptionsMonitor<JsonDataSourceConfiguration>>();
var config = new JsonDataSourceConfiguration
{
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" };
dsMocker.SetupDataMock(today, todayEnd, todayLocation.Name);
var result1 = await dataSource.GetCarbonIntensityAsync(todayLocation, today, todayEnd);
Assert.AreEqual(1, result1.Count());
foreach (var r in result1)
{
Assert.AreEqual(r.Location, todayLocation.Name);
}

var yesterday = today.AddDays(-1);
var yesterdayEnd = yesterday.AddMinutes(30);
var yesterdayLocation = new Location() { Name = "uk" };
dsMocker.SetupDataMock(yesterday, yesterdayEnd, yesterdayLocation.Name);
if (cache)
{
var result2 = await dataSource.GetCarbonIntensityAsync(todayLocation, today, todayEnd);
Assert.AreEqual(1, result2.Count());
foreach (var r in result2)
{
Assert.AreEqual(r.Location, todayLocation.Name);
}
} else
{
var result2 = await dataSource.GetCarbonIntensityAsync(yesterdayLocation, yesterday, yesterdayEnd);
Assert.AreEqual(1, result2.Count());
foreach (var r in result2)
{
Assert.AreEqual(r.Location, yesterdayLocation.Name);
}
}
}

private Mock<JsonDataSource> SetupMockDataSource() {
var logger = Mock.Of<ILogger<JsonDataSource>>();
var monitor = Mock.Of<IOptionsMonitor<JsonDataSourceConfiguration>>();
Expand Down

0 comments on commit e67716d

Please sign in to comment.