public IHttpActionResult Get([FromUri] string id, [FromUri] DateTime?startDateTime         = null, [FromUri] DateTime?endDateTime = null,
                                     [FromUri] bool returnOnlyNonNullSystemWideEmissionsDataPoints = false, [FromUri] bool returnOnlyNonNullMarginalEmissionsDataPoints = false,
                                     [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.FindCarbonEmissionsDataPoints(regionId, startDateTimeProcessed, endDateTimeProcessed, dateTimeFlexabilityInMinutes).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 TestMineHistoricMarginalCarbonResults()
        {
            // Arrange
            var startDateTime = DateTime.Now.AddDays(-10);
            var endDateTime   = DateTime.Now.AddDays(-9);

            var wattTimeApiUrl            = CloudConfigurationManager.GetSetting("WattTimeApiUrl");
            var wattTimeApiKey            = CloudConfigurationManager.GetSetting("WattTimeApiKey");
            var selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var maxNumberOfCallsPerMinute = 9;

            List <WattTimeBalancingAuthorityInformation> regionsToMine =
                new List <WattTimeBalancingAuthorityInformation>
            {
                new WattTimeBalancingAuthorityInformation(
                    "PJM",
                    "US_PJM",
                    "Eastern Standard Time",
                    40.348444276169,
                    -74.6428556442261)
            };

            foreach (var region in regionsToMine)
            {
                int regionId;
                using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                {
                    regionId =
                        _objectModel.AddEmissionsRegion(region.smartGridRegionName, region.timeZone, region.regionLat, region.regionLong)
                        .EmissionsRegionID;

                    _objectModel.AddMarketWeatherEmissionsRegionMapping(
                        region.smartGridRegionName,
                        null,
                        null,
                        regionId);
                }


                var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

                CarbonEmissionsMiner carbonEmissionsMiner = new CarbonEmissionsMiner(
                    wattTimeApiUrl,
                    wattTimeApiKey,
                    selfThrottlingMethod,
                    databaseConnectionString,
                    maxNumberOfCallsPerMinute,
                    wattTimeInteraction);

                // Act
                carbonEmissionsMiner.MineHistoricMarginalCarbonResults(
                    startDateTime,
                    endDateTime,
                    region.regionWattTimeName,
                    regionId);

                // Assert
                // Verify that each data point has been recorded in the database
                var results = wattTimeInteraction.GetObservedMarginalCarbonResults(
                    wattTimeApiUrl,
                    region.regionWattTimeName,
                    startDateTime,
                    endDateTime,
                    null,
                    wattTimeApiKey);

                using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                {
                    foreach (var result in results)
                    {
                        var dataPoint = _objectModel.FindCarbonEmissionsDataPoints(regionId, result.timestamp);
                        Assert.IsNotNull(dataPoint);
                    }
                }
            }
        }
        public void TestParseMinerSettingsFileAndMineData()
        {
            // Arrange
            var ConfigPath = @".\ApiDataMinerConfigs\ApiDataMinerConfigs.xml";

            // Act
            var apiDataMiner = new ApiDataMiner(databaseConnectionString);

            apiDataMiner.ParseMinerSettingsFileAndMineData(ConfigPath);

            // Assert
            using (var streamReader = new StreamReader(ConfigPath))
            {
                var xmlSerializer = new XmlSerializer(typeof(ApiMinerConfigLayout));
                var minerConfigs  = (ApiMinerConfigLayout)xmlSerializer.Deserialize(streamReader);

                foreach (var regionConfiguration in minerConfigs.Regions)
                {
                    // Verify emissions were mined successfully for each region in the Config File
                    if (regionConfiguration.EmissionsMiningRegion != null)
                    {
                        var emissionsRegionName       = regionConfiguration.EmissionsMiningRegion.friendlyName;
                        var timeZone                  = regionConfiguration.EmissionsMiningRegion.TimeZone;
                        var regionLat                 = regionConfiguration.EmissionsMiningRegion.Latitude;
                        var regionLong                = regionConfiguration.EmissionsMiningRegion.Longitude;
                        var regionWattTimeName        = regionConfiguration.EmissionsMiningRegion.EmissionsWattTimeAbbreviation;
                        var wattTimeApiUrl            = regionConfiguration.EmissionsMiningRegion.ApiUrl;
                        var wattTimeApiKey            = regionConfiguration.EmissionsMiningRegion.ApiKey;
                        var selfThrottlingMethod      = regionConfiguration.WeatherMiningRegion.SelfThrottlingMethod;
                        var maxNumberOfCallsPerMinute =
                            regionConfiguration.WeatherMiningRegion.MaxNumberOfCallsPerMinute;
                        var startDateTime = DateTime.UtcNow.AddDays(-2);
                        var endDateTime   = DateTime.UtcNow.AddDays(10);

                        var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);
                        var results             = wattTimeInteraction.GetObservedMarginalCarbonResults(
                            wattTimeApiUrl,
                            regionWattTimeName,
                            startDateTime,
                            endDateTime,
                            null,
                            wattTimeApiKey);

                        int regionId;
                        using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                        {
                            regionId =
                                _objectModel.AddEmissionsRegion(emissionsRegionName, timeZone, regionLat, regionLong)
                                .EmissionsRegionID;
                        }

                        using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                        {
                            foreach (var result in results)
                            {
                                var dataPoint = _objectModel.FindCarbonEmissionsDataPoints(regionId, result.timestamp);
                                Assert.IsNotNull(dataPoint);
                            }
                        }
                    }

                    // Verify weather was mined successfully for each region in the Config File
                    if (regionConfiguration.WeatherMiningRegion != null)
                    {
                        var emissionsRegionName             = regionConfiguration.WeatherMiningRegion.friendlyName;
                        var timeZone                        = regionConfiguration.WeatherMiningRegion.TimeZone;
                        var regionLat                       = regionConfiguration.WeatherMiningRegion.Latitude;
                        var regionLong                      = regionConfiguration.WeatherMiningRegion.Longitude;
                        var weatherRegionWundergroundSubUrl =
                            regionConfiguration.WeatherMiningRegion.weatherRegionWundergroundSubUrl;
                        var wundergroundApiUrl        = regionConfiguration.WeatherMiningRegion.ApiUrl;
                        var wundergroundApiKey        = regionConfiguration.WeatherMiningRegion.ApiKey;
                        var selfThrottlingMethod      = regionConfiguration.WeatherMiningRegion.SelfThrottlingMethod;
                        var maxNumberOfCallsPerMinute =
                            regionConfiguration.WeatherMiningRegion.MaxNumberOfCallsPerMinute;

                        var wundergroundWeatherInteraction = new WundergroundWeatherInteraction(
                            selfThrottlingMethod,
                            maxNumberOfCallsPerMinute);
                        var results =
                            wundergroundWeatherInteraction.GetTenDayHourlyForecastWeatherData(
                                wundergroundApiUrl,
                                weatherRegionWundergroundSubUrl,
                                wundergroundApiKey);

                        int regionId;
                        using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                        {
                            regionId =
                                _objectModel.AddWeatherRegion(emissionsRegionName, timeZone, regionLat, regionLong)
                                .WeatherRegionID;
                        }

                        using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
                        {
                            foreach (var result in results)
                            {
                                var dataPoint = _objectModel.FindWeatherDataPoints(
                                    regionId,
                                    result.observationDateTime);
                                Assert.IsNotNull(dataPoint);
                            }
                        }
                    }
                }
            }
        }
        public void TestMineForecastMarginalCarbonResults()
        {
            // Arrange
            var regionWattTimeName  = "PJM";
            var smartGridRegionName = "PJM";
            var timeZone            = "Eastern Standard Time";
            var regionLat           = 40.348444276169;
            var regionLong          = -74.6428556442261;

            var startDateTime = DateTime.UtcNow.AddDays(-2);
            var endDateTime   = DateTime.UtcNow.AddDays(10);

            var wattTimeApiUrl            = CloudConfigurationManager.GetSetting("WattTimeApiUrl");
            var wattTimeApiKey            = CloudConfigurationManager.GetSetting("WattTimeApiKey");
            var selfThrottlingMethod      = "AzureTableStorageCallRecollection";
            var maxNumberOfCallsPerMinute = 9;

            int regionId;

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                regionId =
                    _objectModel.AddEmissionsRegion(smartGridRegionName, timeZone, regionLat, regionLong)
                    .EmissionsRegionID;
            }


            var wattTimeInteraction = new EmissionsApiInteraction(selfThrottlingMethod, maxNumberOfCallsPerMinute);

            CarbonEmissionsMiner carbonEmissionsMiner = new CarbonEmissionsMiner(
                wattTimeApiUrl,
                wattTimeApiKey,
                selfThrottlingMethod,
                databaseConnectionString,
                maxNumberOfCallsPerMinute,
                wattTimeInteraction);

            // Act
            carbonEmissionsMiner.MineForecastMarginalCarbonResults(
                startDateTime,
                endDateTime,
                regionWattTimeName,
                regionId);

            // Assert
            // Verify that each data point has been recorded in the database
            var results = wattTimeInteraction.GetForecastMarginalCarbonResults(
                wattTimeApiUrl,
                regionWattTimeName,
                startDateTime,
                endDateTime,
                null,
                wattTimeApiKey);

            using (var _objectModel = new SmartEnergyOM(databaseConnectionString))
            {
                foreach (var result in results)
                {
                    if (result.marginal_carbon.value != null)
                    {
                        var dataPoint = _objectModel.FindCarbonEmissionsDataPoints(regionId, result.timestamp);
                        Assert.IsNotNull(dataPoint);
                        Assert.AreEqual(
                            wattTimeInteraction.ConvertLbsPerMWhTo_GCo2PerkWh((double)result.marginal_carbon.value),
                            dataPoint.MarginalCO2Intensity_gCO2kWh);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Calculate the Relative Merit of Marginal Carbon Emissions Values which are present in the database between the given startDateTime and endDateTime.
        /// The Relative Merit is a value between 0 (meaning best) and 1 (being worst).
        /// </summary>
        /// <param name="regionId"></param>
        /// <param name="startDateTime"></param>
        /// <param name="endDateTime"></param>
        /// <returns>List of MarginalCarbonResult.Result representing the Relative Merit of the corresponding Marginal Carbon Emissions Values</returns>
        public List <EmissionsRelativeMeritDatapoint> CalculateHistoricRelativeMeritDataResults(int regionId, DateTime startDateTime, DateTime endDateTime)
        {
            /*** Implement your custom logic here to calculate relative merit data ***/

            /* Here is a sample method provided here which simply calculates it's comparison to a one week rolling average for the data */
            var results = new List <EmissionsRelativeMeritDatapoint>();

            using (var objectModel = new SmartEnergyOM(this.DatabaseConnectionString))
            {
                // Get datapoints on which to calculate relative merit
                var carbonResultsOnWhichToCalculateRelativeMetit =
                    objectModel.FindCarbonEmissionsDataPoints(regionId, startDateTime, endDateTime);

                foreach (var datapoint in carbonResultsOnWhichToCalculateRelativeMetit)
                {
                    try
                    {
                        // Get the last week of data
                        var startDateOfOneWeekRollingAverage = datapoint.DateTimeUTC.AddDays(-7);
                        var endDateOfOneWeekRollingAverage   = datapoint.DateTimeUTC.AddHours(1);
                        var emissionsDataPointsWithinWindow  =
                            objectModel.FindCarbonEmissionsDataPoints(regionId, startDateOfOneWeekRollingAverage,
                                                                      endDateOfOneWeekRollingAverage).Where(a => a.MarginalCO2Intensity_gCO2kWh != null);

                        // Calculate where this datapoints falls within the range of the last week
                        if (emissionsDataPointsWithinWindow.Any())
                        {
                            double?relativeMerit          = null;
                            double?relativeMerit_Forecast = null;
                            if (datapoint.MarginalCO2Intensity_gCO2kWh != null)
                            {
                                var maxValue = emissionsDataPointsWithinWindow.Max(a => a.MarginalCO2Intensity_gCO2kWh);
                                var minValue = emissionsDataPointsWithinWindow.Min(a => a.MarginalCO2Intensity_gCO2kWh);
                                var range    = (double)(maxValue - minValue);
                                relativeMerit =
                                    (datapoint.MarginalCO2Intensity_gCO2kWh - minValue) / range;

                                // One specicial check: 0 should be reserved for zero emissions. Set anything above zero to .2
                                if ((relativeMerit < .2) && (datapoint.MarginalCO2Intensity_gCO2kWh > 0))
                                {
                                    relativeMerit = .2;
                                }
                            }
                            if (datapoint.MarginalCO2Intensity_Forcast_gCO2kWh != null)
                            {
                                var maxValue = emissionsDataPointsWithinWindow.Max(a => a.MarginalCO2Intensity_Forcast_gCO2kWh);
                                var minValue = emissionsDataPointsWithinWindow.Min(a => a.MarginalCO2Intensity_Forcast_gCO2kWh);
                                var range    = (double)(maxValue - minValue);
                                relativeMerit_Forecast =
                                    (datapoint.MarginalCO2Intensity_Forcast_gCO2kWh - minValue) / range;

                                // One specicial check: 0 should be reserved for zero emissions. Set anything above zero to .2
                                if ((relativeMerit_Forecast < .2) && (datapoint.MarginalCO2Intensity_Forcast_gCO2kWh > 0))
                                {
                                    relativeMerit_Forecast = .2;
                                }
                            }
                            if ((relativeMerit != null) || (relativeMerit_Forecast != null))
                            {
                                var emissionsRelativeMeritDataResult =
                                    new EmissionsRelativeMeritDatapoint
                                {
                                    EmissionsRegionID              = regionId,
                                    Timestamp                      = datapoint.DateTimeUTC,
                                    EmissionsRelativeMerit         = relativeMerit,
                                    EmissionsRelativeMerit_Forcast = relativeMerit_Forecast
                                };
                                results.Add(emissionsRelativeMeritDataResult);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(
                            $"CarbonEmissionsMiner: CalculateHistoricRelativeMeritDataResults(): Exception encountered Calculate the Merit of datapoint at {datapoint.DateTimeUTC} for RegionId {datapoint.EmissionsRegionID}.",
                            "CarbonEmissionsMiner.CalculateHistoricRelativeMeritDataResults()",
                            null,
                            e);
                    }
                }
            }

            return(results);
        }