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 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); } } }