Skip to content

Commit

Permalink
Merge pull request #237 from microsoft/219/CarbonAwareNullable
Browse files Browse the repository at this point in the history
Enable "Nullable" for CarbonAware Project
  • Loading branch information
vaughanknight authored Jan 10, 2023
2 parents 23d2e63 + c22c748 commit 9f75054
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace CarbonAware.DataSources.ElectricityMaps.Mocks;

public class ElectricityMapsDataSourceMocker : IDataSourceMocker
{
private WireMockServer _server;
private readonly WireMockServer _server;
private const string ZONE_NAME = "eastus";
private const string ZONE_KEY = "US-NE-ISNE";
private static readonly JsonSerializerOptions _options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static EmissionsForecast ToEmissionsForecast(Location location, Forecast
emissionsForecast.RequestedAt = requestedAt;
emissionsForecast.ForecastData = emissionsForecast.ForecastData.Select(d =>
{
d.Location = location.Name;
d.Location = location.Name ?? string.Empty;
d.Duration = duration;
return d;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public void ClientInstantiation_FailsForInvalidConfig(string baseUrl)
CreateBasicClient(TestData.GetZonesAllowedJsonString(), "{}");
this.Configuration = new ElectricityMapsClientConfiguration()
{
APITokenHeader = "",
APIToken = "",
APITokenHeader = string.Empty,
APIToken = string.Empty,
BaseUrl = baseUrl,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public List<EmissionsData> GenerateTestData(List<AwsRegionData> regionData)
var e = new EmissionsData()
{
Time = DateTime.Now + TimeSpan.FromHours(i),
Location = region.code,
Location = region.code ?? string.Empty,
Rating = ran.Next(100)
};
emData.Add(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public List<EmissionsData> GenerateTestData(List<AzureRegionData> regionData)
{
// 3 times per day (8 hours apart), 365 days per year
Time = DateTime.Now + TimeSpan.FromHours(8 * hours) + TimeSpan.FromDays(days),
Location = region.name,
Location = region.name ?? string.Empty,
Rating = ran.Next(100)
};
emData.Add(e);
Expand Down
1 change: 1 addition & 0 deletions src/CarbonAware/src/CarbonAware.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<IsPackable>false</IsPackable>
Expand Down
6 changes: 3 additions & 3 deletions src/CarbonAware/src/CarbonAwareVariablesConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public class CarbonAwareVariablesConfiguration
/// <summary>
/// Gets or sets the forecast data source to use.
/// </summary>
public string ForecastDataSource { get; set; }
public string ForecastDataSource { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the emissions data source to use.
/// </summary>
public string EmissionsDataSource { get; set; }
public string EmissionsDataSource { get; set; } = string.Empty;

#nullable enable
#nullable enable
/// <summary>
/// Gets or sets proxy information for making calls to the internet.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions src/CarbonAware/src/Extensions/EmissionsDataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static IEnumerable<EmissionsData> RollingAverage(this IEnumerable<Emissio
var _data = data.GetEnumerator();
_data.MoveNext();
EmissionsData current = _data.Current;
EmissionsData last = null;
EmissionsData? last = null;

if (tickSize == default)
{
Expand Down Expand Up @@ -151,7 +151,7 @@ public static double AverageOverPeriod(this IEnumerable<EmissionsData> data, Dat
{
double rating = 0.0;
TimeSpan totalDuration = endTime - startTime;
EmissionsData previous = null;
EmissionsData? previous = null;
(bool reverseChronology, bool emptyEnumerable) = GetChronologyDetails(data);
bool startTimeCoverage = false;
bool endTimeCoverage = false;
Expand Down Expand Up @@ -193,8 +193,8 @@ public static double AverageOverPeriod(this IEnumerable<EmissionsData> data, Dat

private static (bool reverseChronology, bool emptyEnumerable) GetChronologyDetails(IEnumerable<EmissionsData> data)
{
EmissionsData current = null;
EmissionsData next = null;
EmissionsData? current = null;
EmissionsData? next = null;
var _data = data.GetEnumerator();

if (_data.MoveNext())
Expand Down
2 changes: 1 addition & 1 deletion src/CarbonAware/src/Model/EmissionsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
public record EmissionsData
{
///<example> eastus </example>
public string Location { get; set; }
public string Location { get; set; } = string.Empty;
///<example> 01-01-2022 </example>
public DateTimeOffset Time { get; set; }
///<example> 140.5 </example>
Expand Down
2 changes: 1 addition & 1 deletion src/CarbonAware/src/Model/EmissionsForecast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public record EmissionsForecast
/// <summary>
/// Gets or sets the optimal data points within the ForecastData set.
/// </summary>
public IEnumerable<EmissionsData> OptimalDataPoints { get; set; }
public IEnumerable<EmissionsData> OptimalDataPoints { get; set; } = new List<EmissionsData>();


public void Validate()
Expand Down
6 changes: 3 additions & 3 deletions src/CarbonAware/src/WebProxyConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public class WebProxyConfiguration
/// <summary>
/// Gets or sets the proxy url
/// </summary>
public string Url { get; set; }
public string Url { get; set; } = string.Empty;

/// <summary>
/// Sets the proxy username
/// </summary>
public string Username { get; set; }
public string Username { get; set; } = string.Empty;

/// <summary>
/// Sets the proxy password
/// </summary>
public string Password { get; set; }
public string Password { get; set; } = string.Empty;
}

0 comments on commit 9f75054

Please sign in to comment.