Ejemplo n.º 1
0
        private List <DataValuesModel> MapApiToDatabase(ApiModel apiModel, DataSourcesModel dataSource)
        {
            List <DataValuesModel> output = new List <DataValuesModel>();

            foreach (var item in apiModel.Results)
            {
                output.Add(new DataValuesModel()
                {
                    Data_source_id = dataSource.Data_source_id,
                    Data_Value     = item.Consumption,
                    Data_interval_start_datetime = item.Interval_start.DateTime,
                    Data_interval_start_offset   = item.Interval_start.Offset,
                    Data_interval_end_datetime   = item.Interval_end.DateTime,
                    Data_interval_end_offset     = item.Interval_end.Offset
                });
            }

            return(output);
        }
Ejemplo n.º 2
0
        private async Task <List <List <DataValuesModel> > > AllHistory(string energySource)
        {
            // Retrieve config values
            string energySourceMPAN         = _config.GetValue <string>($"OctopusApi:{energySource}:mpan");
            string energySourceSerialNumber = _config.GetValue <string>($"OctopusApi:{energySource}:serial_number");

            // Call api to retrieve count or records to retrieve
            ApiModel tempUse = await _octopusHelper.GetConsumption(energySource, energySourceMPAN, energySourceSerialNumber);

            DataSourcesModel energySourceModel = await _dataSources.GetDataSourceIdFromName(energySource);

            double energySourcePages = Math.Ceiling(tempUse.Count / 100d);

            List <List <DataValuesModel> > energySourceHistory = new List <List <DataValuesModel> >();
            List <Task <ApiModel> >        tasks = new List <Task <ApiModel> >();

            for (int page = 1; page < (energySourcePages + 1); page++)
            {
                tasks.Add(_octopusHelper.GetConsumptionPage(energySource, page, energySourceMPAN, energySourceSerialNumber));
            }

            var results = await Task.WhenAll(tasks);

            int countOfRetrievedRecords = 0;

            foreach (var item in results)
            {
                List <DataValuesModel> tempList = MapApiToDatabase(item, energySourceModel);
                countOfRetrievedRecords += tempList.Count;
                energySourceHistory.Add(tempList);
            }

            if (tempUse.Count == countOfRetrievedRecords)
            {
                return(energySourceHistory);
            }
            else
            {
                // TODO: Find a more elegant way to handle the error
                throw new Exception();
            }
        }
Ejemplo n.º 3
0
        // GET: DataSources
        public ActionResult Index(string dataSourceName = "", string dateFrom = "", string dateTo = "", string orderBy = "", byte dataTypeId = 0, int page = 1)
        {
            int rowCount = 0;
            var model    = new DataSourcesModel
            {
                ListDataSources = new DataSources {
                    DataSourceName = dataSourceName, DataTypeId = dataTypeId
                }.GetPage(dateFrom, dateTo, orderBy, CmsConstants.RowAmount20, page > 0?page - 1:page, ref rowCount),
                RowCount   = rowCount,
                Pagination = new PaginationModel
                {
                    TotalPage = rowCount,
                    PageSize  = CmsConstants.RowAmount20,
                    LinkLimit = 5,
                    PageIndex = page
                }
            };

            return(View(model));
        }
Ejemplo n.º 4
0
        private async Task <List <DataValuesModel> > NewHistory(string energySource)
        {
            // Retrieve config values
            string energySourceMPAN         = _config.GetValue <string>($"OctopusApi:{energySource}:mpan");
            string energySourceSerialNumber = _config.GetValue <string>($"OctopusApi:{energySource}:serial_number");

            // Retrieve latest record from database and the data source id
            DataValuesModel lastRecordEnergySource = await _dataValues.RetrieveLastRecordForSourceAsync(energySource);

            DataSourcesModel energySourceModel = await _dataSources.GetDataSourceIdFromName(energySource);

            // Variables
            List <DataValuesModel> returnList = new List <DataValuesModel>();
            int page     = 1;
            int useCount = 0;

            while (true)
            {
                // Get a page of values
                ApiModel temp = await _octopusHelper.GetConsumptionPage(energySource, page, energySourceMPAN, energySourceSerialNumber);

                if (useCount == 0)
                {
                    useCount = temp.Count;
                }
                else if (useCount != temp.Count)
                {
                    // Throw an error if the ammount of records get changed as the program is making subsequent calls
                    // Look if there is a more gracefull way of handling this error in the future
                    throw new Exception();
                }

                // Parse from ApiModel to List<DataValuesModel>
                List <DataValuesModel> tempList = MapApiToDatabase(temp, energySourceModel);

                // Look at each record and determine if the last record in the database has been reached
                foreach (var item in tempList)
                {
                    // TODO: look at a way to incorporate a way to determine if two instances of a class hold the same values determining them as "equal"
                    //  even if they are different instances of a class.
                    bool tempBoolTest = item.Data_source_id == lastRecordEnergySource.Data_source_id &
                                        item.Data_Value == lastRecordEnergySource.Data_Value &
                                        item.Data_interval_start_datetime == lastRecordEnergySource.Data_interval_start_datetime &
                                        item.Data_interval_start_offset == lastRecordEnergySource.Data_interval_start_offset &
                                        item.Data_interval_end_datetime == lastRecordEnergySource.Data_interval_end_datetime &
                                        item.Data_interval_end_offset == lastRecordEnergySource.Data_interval_end_offset;

                    // Record match found
                    if (tempBoolTest)
                    {
                        return(returnList);
                    }
                    // Record not found
                    else
                    {
                        returnList.Add(item);
                    }
                }

                page++;
            }
        }
Ejemplo n.º 5
0
        private async Task InitialChartTrackerFill(string chartsTarget)
        {
            // Get first record
            DataValuesModel firstRecordEnergySource = await _dataValues.RetrieveFirstRecordForSourceAsync(chartsTarget);

            DataSourcesModel energySourceModel = await _dataSources.GetDataSourceIdFromName(chartsTarget);

            List <ChartTrackerModel> chartsToMake = new List <ChartTrackerModel>();

            // Add daily chart
            chartsToMake.Add(new ChartTrackerModel
            {
                Data_source_id  = energySourceModel.Data_source_id,
                Chart_type      = "Daily",
                Chart_last_from = firstRecordEnergySource.Data_interval_start_datetime.Date - TimeSpan.FromDays(1),
                Chart_last_to   = firstRecordEnergySource.Data_interval_start_datetime.Date,
                Chart_next_from = firstRecordEnergySource.Data_interval_start_datetime.Date,
                Chart_next_to   = firstRecordEnergySource.Data_interval_start_datetime.Date + TimeSpan.FromDays(1)
            });

            // Add weekly chart
            DateTime firstMonday = firstRecordEnergySource.Data_interval_start_datetime.Date;

            while (firstMonday.DayOfWeek != DayOfWeek.Monday)
            {
                firstMonday -= TimeSpan.FromDays(1);
            }
            chartsToMake.Add(new ChartTrackerModel
            {
                Data_source_id  = energySourceModel.Data_source_id,
                Chart_type      = "Weekly",
                Chart_last_from = firstMonday - TimeSpan.FromDays(7),
                Chart_last_to   = firstMonday,
                Chart_next_from = firstMonday,
                Chart_next_to   = firstMonday + TimeSpan.FromDays(7)
            });

            // Add monthly chart
            DateTime firstMonth = firstRecordEnergySource.Data_interval_start_datetime.Date;

            firstMonth -= TimeSpan.FromDays(firstMonth.Day - 1);
            chartsToMake.Add(new ChartTrackerModel
            {
                Data_source_id  = energySourceModel.Data_source_id,
                Chart_type      = "Monthly",
                Chart_last_from = firstMonth.AddMonths(-1),
                Chart_last_to   = firstMonth,
                Chart_next_from = firstMonth,
                Chart_next_to   = firstMonth.AddMonths(1)
            });
            // Add quaterly chart

            DateTime quarterStart = firstMonth;

            while (quarterStart.Month != 1 && quarterStart.Month != 4 && quarterStart.Month != 7 && quarterStart.Month != 10)
            {
                quarterStart = quarterStart.AddMonths(-1);
            }
            chartsToMake.Add(new ChartTrackerModel
            {
                Data_source_id  = energySourceModel.Data_source_id,
                Chart_type      = "Quarterly",
                Chart_last_from = quarterStart.AddMonths(-3),
                Chart_last_to   = quarterStart,
                Chart_next_from = quarterStart,
                Chart_next_to   = quarterStart.AddMonths(3)
            });
            // Add yearly chart

            DateTime yearStart = quarterStart.AddMonths(1 - quarterStart.Month);

            chartsToMake.Add(new ChartTrackerModel
            {
                Data_source_id  = energySourceModel.Data_source_id,
                Chart_type      = "Yearly",
                Chart_last_from = yearStart.AddMonths(-12),
                Chart_last_to   = yearStart,
                Chart_next_from = yearStart,
                Chart_next_to   = yearStart.AddMonths(12)
            });
            // Add rolling yearly chart


            // Save charts to make
            await _chartTracker.SaveChartsToTrackerAsync(chartsToMake);
        }