public IHttpActionResult Get([FromUri] string id, [FromUri] DateTime?startDateTime = null, [FromUri] DateTime?endDateTime = null, [FromUri] double dateTimeFlexabilityInMinutes = 0) { try { // Set default start and end datetimes string emissionsRegionFriendlyName = id.IsNullOrWhiteSpace() ? "US_PJM" : id; var startDateTimeProcessed = startDateTime ?? DateTime.UtcNow.AddHours(-3); var endDateTimeProcessed = endDateTime ?? DateTime.UtcNow.AddHours(1); // Query database var databaseConnectionString = CloudConfigurationManager.GetSetting("SQLAzureDatabaseEntityFrameworkConnectionString"); using (var _objectModel = new SmartEnergyOM(databaseConnectionString)) { var regionId = _objectModel.FindEmissionsRegion(emissionsRegionFriendlyName).EmissionsRegionID; var result = _objectModel.FindCarbonEmissionsRelativeMeritDataPoints(regionId, startDateTimeProcessed, endDateTimeProcessed, dateTimeFlexabilityInMinutes).ToList(); // Convert the final list to objects of the common data type in the SmartEnergyAzureDataTypes NuGet package var webResult = this.ConvertToCarbonEmissionsRelativeMeritWebDataPoints(result); if (result == null) { return(this.NotFound()); } return(Ok(webResult)); } } catch (Exception e) { throw new Exception("Sorry - an exception occured executing the request"); } }
public IHttpActionResult Get([FromUri] string id, [FromUri] DateTime?startDateTime = null, [FromUri] DateTime?endDateTime = null, [FromUri] bool returnOnlyNonNullSystemWideEmissionsDataPoints = false, [FromUri] bool returnOnlyNonNullMarginalEmissionsDataPoints = false) { try { // Set default start and end datetimes string emissionsRegionFriendlyName = id.IsNullOrWhiteSpace() ? "US_PJM" : id; var startDateTimeProcessed = startDateTime ?? DateTime.UtcNow.AddHours(-3); var endDateTimeProcessed = endDateTime ?? DateTime.UtcNow.AddHours(1); // Query database var databaseConnectionString = CloudConfigurationManager.GetSetting("SQLAzureDatabaseEntityFrameworkConnectionString"); using (var _objectModel = new SmartEnergyOM(databaseConnectionString)) { var regionId = _objectModel.FindEmissionsRegion(emissionsRegionFriendlyName).EmissionsRegionID; var result = _objectModel.FindCarbonEmissionsDataPoints(regionId, startDateTimeProcessed, endDateTimeProcessed).ToList(); // Remove any relavent results as per search parameters passed in - System Wide and Marginal Emissions if (returnOnlyNonNullSystemWideEmissionsDataPoints) { result.RemoveAll(x => x.SystemWideCO2Intensity_gCO2kWh == null); } if (returnOnlyNonNullMarginalEmissionsDataPoints) { result.RemoveAll(x => x.MarginalCO2Intensity_gCO2kWh == null); } // Convert the final list to objects of the common data type in the SmartEnergyAzureDataTypes NuGet package var webResult = this.ConvertToCarbonEmissionsWebDataPoints(result); if (result == null) { return(this.NotFound()); } return(Ok(webResult)); } } catch (Exception e) { throw new Exception("Sorry - an exception occured executing the request"); } }
public void TestMineOrCalculateCarbonEmissionsRelativeMerit_CustomInternalCalculation() { // Arrange var regionWattTimeName = "PJM"; var smartGridRegionName = "US_PJM"; var startDateTime = DateTime.UtcNow.AddDays(-10); var endDateTime = DateTime.UtcNow.AddDays(-9); var wattTimeApiUrl = CloudConfigurationManager.GetSetting("WattTimeApiUrl"); var wattTimeApiKey = CloudConfigurationManager.GetSetting("WattTimeApiKey"); string wattTimeApiV2Url = CloudConfigurationManager.GetSetting("WattTimeApiV2Url"); string WattTimeUsername = CloudConfigurationManager.GetSetting("WattTimeUsername"); string WattTimePassword = CloudConfigurationManager.GetSetting("WattTimePassword"); string WattTimeEmail = CloudConfigurationManager.GetSetting("WattTimeEmail"); string WattTimeOrganization = CloudConfigurationManager.GetSetting("WattTimeOrganization"); var selfThrottlingMethod = "AzureTableStorageCallRecollection"; var maxNumberOfCallsPerMinute = 200; var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute); CarbonEmissionsMiner carbonEmissionsMiner = new CarbonEmissionsMiner( wattTimeApiUrl, wattTimeApiKey, wattTimeApiV2Url, WattTimeUsername, WattTimePassword, WattTimeEmail, WattTimeOrganization, selfThrottlingMethod, databaseConnectionString, maxNumberOfCallsPerMinute, wattTimeInteraction, "CustomInternalCalculation"); int regionId; using (var _objectModel = new SmartEnergyOM(databaseConnectionString)) { regionId = _objectModel.FindEmissionsRegion(smartGridRegionName).EmissionsRegionID; } Assert.IsNotNull(regionId, "Couldn't find specified emissions region in the database"); // Act carbonEmissionsMiner.MineOrCalculateCarbonEmissionsRelativeMerit( regionWattTimeName, regionId); // Assert // Verify that each data point has been recorded in the database var results = carbonEmissionsMiner.CalculateHistoricRelativeMeritDataResults( regionId, startDateTime, endDateTime); foreach (var result in results) { using (var _objectModel = new SmartEnergyOM(databaseConnectionString)) { var dataPoint = _objectModel.FindCarbonEmissionsRelativeMeritDataPoints(regionId, result.Timestamp.AddMinutes(-5), result.Timestamp.AddMinutes(15)); Assert.IsNotNull(dataPoint); } } }
public void TestCalculateHistoricRelativeMeritDataResults() { // Arrange var smartGridRegionName = "US_PJM"; var startDateTime = DateTime.UtcNow.AddDays(-10); var endDateTime = DateTime.UtcNow.AddDays(-9); var wattTimeApiUrl = CloudConfigurationManager.GetSetting("WattTimeApiUrl"); var wattTimeApiKey = CloudConfigurationManager.GetSetting("WattTimeApiKey"); string wattTimeApiV2Url = CloudConfigurationManager.GetSetting("WattTimeApiV2Url"); string WattTimeUsername = CloudConfigurationManager.GetSetting("WattTimeUsername"); string WattTimePassword = CloudConfigurationManager.GetSetting("WattTimePassword"); string WattTimeEmail = CloudConfigurationManager.GetSetting("WattTimeEmail"); string WattTimeOrganization = CloudConfigurationManager.GetSetting("WattTimeOrganization"); var selfThrottlingMethod = "AzureTableStorageCallRecollection"; var maxNumberOfCallsPerMinute = 200; var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute); var carbonEmissionsMiner = new CarbonEmissionsMiner( wattTimeApiUrl, wattTimeApiKey, wattTimeApiV2Url, WattTimeUsername, WattTimePassword, WattTimeEmail, WattTimeOrganization, selfThrottlingMethod, databaseConnectionString, maxNumberOfCallsPerMinute, wattTimeInteraction); int regionId; using (var _objectModel = new SmartEnergyOM(databaseConnectionString)) { regionId = _objectModel.FindEmissionsRegion(smartGridRegionName).EmissionsRegionID; } Assert.IsNotNull(regionId, "Couldn't find specified emissions region in the database"); // Act var results = carbonEmissionsMiner.CalculateHistoricRelativeMeritDataResults( regionId, startDateTime, endDateTime ); // Assert foreach (var item in results) { if (item.EmissionsRelativeMerit != null) { Assert.IsTrue(item.EmissionsRelativeMerit >= 0, "item.EmissionsRelativeMerit >= 0"); Assert.IsTrue(item.EmissionsRelativeMerit <= 1, "item.EmissionsRelativeMerit <= 1"); } if (item.EmissionsRelativeMerit_Forcast != null) { Assert.IsTrue(item.EmissionsRelativeMerit_Forcast >= 0, "item.EmissionsRelativeMerit_Forcast >= 0"); Assert.IsTrue(item.EmissionsRelativeMerit_Forcast <= 1, "item.EmissionsRelativeMerit_Forcast <= 1"); } } }