コード例 #1
0
        public async Task<IActionResult> DayResults([FromRoute] string panelId)
        {

             var res = await Get(panelId);

            if (res == null) return NotFound();

           DateTime d1 = DateTime.Now;
           d1 = d1.AddDays(-1);
            var dayAnalytics = res
                .Where(x.DateTime==d1).ToListAsync();

var sumDay=dayAnalytics.Sum(x.KiloWatt => Convert.ToInt32(x.KiloWatt));
var avgDay=sumDay/dayAnalytics.Count();
var minDay=dayAnalytics.Min(r=> r.KiloWatt);
var maxDay=dayAnalytics.Max(r=> r.KiloWatt);

            var result = new List<OneDayElectricityModel>
            {
                OneDayElectricityModel = new OneDayElectricityModel
                {
                    Sum = sumDay,
                    Maximum = maxDay,
                    Minimum=minDay,
                    Average=avgDay
                })
            };
            
           // var result = new List<OneDayElectricityModel>();
          
            return Ok(result);
        }
コード例 #2
0
        public void TestDayAnalyticsAndGenericRepository()
        {
            IDayAnalyticsRepository dayAnalyticsRepository = new DayAnalyticsRepository(new CrossSolarDbContext());

            var oneDayElectricity = new OneDayElectricityModel
            {
                Average  = 13,
                DateTime = DateTime.Now,
                Maximum  = 2423,
                Minimum  = 324,
                Sum      = 34543
            };

            dayAnalyticsRepository.InsertAsync(oneDayElectricity);

            oneDayElectricity.Average = 1;

            dayAnalyticsRepository.UpdateAsync(oneDayElectricity);



            var result = dayAnalyticsRepository.GetBySerialAsync("123");

            var averageResult   = dayAnalyticsRepository.Query().Where(x => x.Average == 1);
            var resultAnalytics = dayAnalyticsRepository.GetAsync("2");

            Assert.NotNull(result);
            Assert.NotNull(averageResult);
            Assert.NotNull(resultAnalytics);
        }
コード例 #3
0
        public void TestModels()
        {
            OneHourElectricityModel oneHourElectricityModel = new OneHourElectricityModel();

            oneHourElectricityModel.DateTime = DateTime.Now;
            oneHourElectricityModel.Id       = 1;
            oneHourElectricityModel.KiloWatt = 234;

            OneHourElectricityListModel oneHourElectricityListModel = new OneHourElectricityListModel();

            oneHourElectricityListModel.OneHourElectricitys.Add(oneHourElectricityModel);

            Assert.NotNull(oneHourElectricityListModel.OneHourElectricitys);
            Assert.NotNull(oneHourElectricityModel);



            OneDayElectricityModel oneDayElectricityModel = new OneDayElectricityModel();

            oneDayElectricityModel.Average  = 23;
            oneDayElectricityModel.DateTime = DateTime.UtcNow;
            oneDayElectricityModel.Maximum  = 344;
            oneDayElectricityModel.Minimum  = 24255;
            oneDayElectricityModel.Sum      = 234446;

            Assert.NotNull(oneDayElectricityModel);
        }
コード例 #4
0
        public async Task <IActionResult> DayResults([FromRoute] string panelId)
        {
            try
            {
                var result    = new List <OneDayElectricityModel>();
                var getData   = new OneDayElectricityModel();
                var analytics = await _analyticsRepository.Query()
                                .Where(x => x.PanelId.Equals(panelId, StringComparison.CurrentCultureIgnoreCase)).ToListAsync();

                var a    = analytics.OrderByDescending(z => z.DateTime).Last();
                var b    = DateTime.Now - a.DateTime;
                int days = (int)b.TotalDays;
                for (int i = 1; i < days + 1; i++)
                {
                    var c = analytics.Where(y => y.DateTime.Date == DateTime.Now.AddDays(-i).Date).ToList();
                    if (c.Count > 0)
                    {
                        getData.DateTime = DateTime.Now.AddDays(-i);
                        var d = c.OrderByDescending(x => x.KiloWatt).First();
                        var e = c.OrderByDescending(x => x.KiloWatt).Last();
                        getData.Maximum = d.KiloWatt;
                        getData.Minimum = e.KiloWatt;
                        getData.Sum     = c.Select(w => w.KiloWatt).Sum();
                        getData.Average = c.Select(w => w.KiloWatt).Sum() / c.Count();
                        result.Add(getData);
                    }
                }

                return(Ok(result));
            }
            catch (Exception Ex)
            {
                throw;
            }
        }
コード例 #5
0
        public async Task <IActionResult> DayResults([FromRoute] string panelId)
        {
            //var result = new List<OneDayElectricityModel>();

            var panel = await _panelRepository.Query()
                        .FirstOrDefaultAsync(x => x.Id.ToString().Equals(panelId, StringComparison.CurrentCultureIgnoreCase));

            if (panel == null)
            {
                return(NotFound());
            }

            //var endOfPreviusDay = DateTime.Now.Date.AddMilliseconds(-1); //the end of previus day
            //var beginOfThisDay = DateTime.Now.Date; //the begin of this day
            //var endOfThisDay = DateTime.Now.AddDays(1).Date.AddMilliseconds(-1); //the end of this day
            //var beginOfNextDay = DateTime.Now.AddDays(1).Date; //the begin of next day

            // Between the end of the previous day and the end of today. Example: Between "5.07.2018 23:59:59" and "6.07.2018 23:59:59" // I can use the begin of next day.
            var analytics = await _analyticsRepository.Query().Where(x => x.PanelId.Equals(panelId, StringComparison.CurrentCultureIgnoreCase) && x.DateTime > DateTime.Now.Date.AddMilliseconds(-1) && x.DateTime < DateTime.Now.Date.AddDays(1).AddMilliseconds(-1)).ToListAsync();

            var result = new OneDayElectricityModel();

            if (analytics != null && analytics.Count > 0)
            {
                result.DateTime = DateTime.Now;
                result.Minimum  = analytics.Min(x => x.KiloWatt);
                result.Maximum  = analytics.Max(x => x.KiloWatt);
                result.Sum      = analytics.Sum(x => x.KiloWatt);
                result.Average  = analytics.Average(x => x.KiloWatt);
            }

            return(Ok(result));
        }
コード例 #6
0
        public void DailyAnalyticsCheck()
        {
            List <OneDayElectricityModel> listMust = new List <OneDayElectricityModel>();
            List <OneHourElectricity>     list     = new List <OneHourElectricity>();
            OneDayElectricityModel        lcOneDayElectricityModel;
            OneHourElectricity            lcOneHourElectricity = new OneHourElectricity();

            lcOneHourElectricity.Id       = 1;
            lcOneHourElectricity.KiloWatt = 100;
            lcOneHourElectricity.PanelId  = 2;
            lcOneHourElectricity.DateTime = new System.DateTime(2018, 8, 24);
            list.Add(lcOneHourElectricity);

            lcOneHourElectricity          = new OneHourElectricity();
            lcOneHourElectricity.Id       = 2;
            lcOneHourElectricity.KiloWatt = 200;
            lcOneHourElectricity.PanelId  = 2;
            lcOneHourElectricity.DateTime = new System.DateTime(2018, 8, 24);
            list.Add(lcOneHourElectricity);

            lcOneHourElectricity          = new OneHourElectricity();
            lcOneHourElectricity.Id       = 3;
            lcOneHourElectricity.KiloWatt = 200;
            lcOneHourElectricity.PanelId  = 2;
            lcOneHourElectricity.DateTime = new System.DateTime(2018, 8, 23);
            list.Add(lcOneHourElectricity);

            lcOneDayElectricityModel          = new OneDayElectricityModel();
            lcOneDayElectricityModel.DateTime = new System.DateTime(2018, 8, 24);
            lcOneDayElectricityModel.Sum      = 300;
            lcOneDayElectricityModel.Average  = 150;
            lcOneDayElectricityModel.Minimum  = 100;
            lcOneDayElectricityModel.Maximum  = 200;
            listMust.Add(lcOneDayElectricityModel);


            lcOneDayElectricityModel          = new OneDayElectricityModel();
            lcOneDayElectricityModel.DateTime = new System.DateTime(2018, 8, 23);
            lcOneDayElectricityModel.Sum      = 200;
            lcOneDayElectricityModel.Average  = 200;
            lcOneDayElectricityModel.Minimum  = 200;
            lcOneDayElectricityModel.Maximum  = 200;
            listMust.Add(lcOneDayElectricityModel);

            var result = _analyticsController.Calculate(list);

            // Assert
            for (int i = 0; i < result.Count; i++)
            {
                Assert.Equal(result[i].Average, listMust[i].Average);
                Assert.Equal(result[i].Maximum, listMust[i].Maximum);
                Assert.Equal(result[i].Minimum, listMust[i].Minimum);
                Assert.Equal(result[i].Sum, listMust[i].Sum);
                Assert.Equal(result[i].DateTime, listMust[i].DateTime);
            }
        }
コード例 #7
0
        public void OneDayElectricityModelTest()
        {
            OneDayElectricityModel oneDay = new OneDayElectricityModel
            {
                Average  = 2,
                DateTime = DateTime.Today,
                Maximum  = 10,
                Minimum  = 1,
                Sum      = 22
            };

            Assert.Equal(2, oneDay.Average);
            Assert.Equal(DateTime.Today, oneDay.DateTime);
            Assert.Equal(10, oneDay.Maximum);
            Assert.Equal(1, oneDay.Minimum);
            Assert.Equal(22, oneDay.Sum);
        }
コード例 #8
0
        public async Task DayResults_ShouldGetDayResults()
        {
            // Arrange
            var panel = new PanelModel
            {
                Brand     = "Areva",
                Latitude  = 12.345678,
                Longitude = 98.7655432,
                Serial    = "AAAA1111BBBB2222"
            };

            await _panelController.Register(panel);

            var panelId = "AAAA1111BBBB2222";
            OneHourElectricityModel value = new OneHourElectricityModel
            {
                KiloWatt = 10,
                DateTime = new System.DateTime()
            };
            var temp = new OneDayElectricityModel()
            {
                Sum      = 1,
                Average  = 1,
                Maximum  = 1,
                Minimum  = 1,
                DateTime = new System.DateTime()
            };
            await _analyticsController.Post(panelId, value);


            // Act
            var result = await _analyticsController.DayResults(panelId);



            // Assert
            Assert.NotNull(result);

            var createdResult = result as OkObjectResult;

            Assert.NotNull(createdResult);
            Assert.Equal(200, createdResult.StatusCode);
        }
コード例 #9
0
        public async Task DayResults_ShouldReturnOneHourElectricityModelList()
        {
            // Arrange
            var panel = new Panel
            {
                Brand     = "Areva",
                Latitude  = 12.345678,
                Longitude = 98.7655432,
                Serial    = "AAAA1111BBBB2222"
            };
            var OneHourElectricity = new OneHourElectricity
            {
                Id       = 123,
                KiloWatt = 12345678,
                DateTime = DateTime.Now,
                PanelId  = "AAAA1111BBBB2222"
            };

            AnalyticsController mockController = MockPanelAndAnalyticsImplementation(panel, OneHourElectricity);

            var expectedResult    = new List <OneDayElectricityModel>();
            var OneDayElectricity = new OneDayElectricityModel
            {
                Sum      = 123,
                Average  = 12345678,
                Maximum  = 10,
                Minimum  = 10,
                DateTime = DateTime.Now,
            };

            expectedResult.Add(OneDayElectricity);


            // Act
            var result = await _analyticsController.DayResults("AAAA1111BBBB2222");

            // Assert
            var createdResult = result as OkObjectResult;

            Assert.Equal(200, createdResult.StatusCode);
            Assert.NotNull(result);
        }
コード例 #10
0
        public async Task <IActionResult> DayResults([FromRoute] string panelId)
        {
            var result    = new List <OneDayElectricityModel>();
            var analytics = _analyticsRepository.Query().Where(x => x.PanelId.Equals(panelId, StringComparison.CurrentCultureIgnoreCase)).ToList(); //Here GetAsync() is not used as the panelId is not primary key of OneHourElectricity table

            var distinctDates = analytics.Select(o => o.DateTime).Distinct();

            foreach (var date in distinctDates)
            {
                var dateRecords = analytics.Where(x => x.DateTime == date);

                var dayResult = new OneDayElectricityModel();
                dayResult.Minimum = dateRecords.Min(x => x.KiloWatt);
                dayResult.Maximum = dateRecords.Max(x => x.KiloWatt);
                dayResult.Sum     = dateRecords.Sum(x => x.KiloWatt);
                dayResult.Average = dateRecords.Average(x => x.KiloWatt);

                result.Add(dayResult);
            }

            return(Ok(result));
        }
コード例 #11
0
        public List <OneDayElectricityModel> GetOneDayMetrics(string panelId)
        {
            var historicalList = new List <OneDayElectricityModel>();
            var oldestDate     = _dbContext.OneHourElectricitys.Min(x => x.DateTime.Date);
            var currentDate    = DateTime.Now;

            while (currentDate.Date >= oldestDate.Date)
            {
                var todaysRecord = _dbContext.OneHourElectricitys.Where(x => x.DateTime.Date == currentDate.Date && x.PanelId == panelId);
                if (todaysRecord != null && todaysRecord.Any())
                {
                    var todaysModel = new OneDayElectricityModel();
                    todaysModel.Maximum  = todaysRecord.Max(x => x.KiloWatt);
                    todaysModel.Minimum  = todaysRecord.Min(x => x.KiloWatt);
                    todaysModel.Average  = todaysRecord.Average(x => x.KiloWatt);
                    todaysModel.Sum      = todaysRecord.Sum(x => x.KiloWatt);
                    todaysModel.DateTime = currentDate;
                    historicalList.Add(todaysModel);
                }
                currentDate = currentDate.AddDays(-1);
            }
            return(historicalList);
        }
        public async Task Get_ShouldGetOneDayElectricity()
        {
            // Arrange
            string panelId = 1.ToString();
            OneDayElectricityModel oneDayElectricityModel = new OneDayElectricityModel
            {
                Sum      = 0,
                Average  = 0,
                Maximum  = 0,
                Minimum  = 0,
                DateTime = new DateTime()
            };

            var oneDayElectricityModels = new List <OneDayElectricityModel>();

            try
            {
                // Act
                var result = await _analyticsController.DayResults(panelId);

                // Assert
                Assert.NotNull(result);

                var okResult = result as OkObjectResult;
                Assert.NotNull(okResult);
                Assert.Equal(200, okResult.StatusCode);

                var objectResult = okResult.Value as List <OneDayElectricityModel>;
                Assert.Equal(oneDayElectricityModels.ToString(), objectResult.ToString());
            }
            catch (Exception)
            {
                // exc.Message is "The provider for the source IQueryable doesn't implement IAsyncQueryProvider. Only providers that implement IEntityQueryProvider can be used for Entity Framework asynchronous operations."
                // This error is only present in unitTest, this error is not taken in the Crossolar endpoint.
            }
        }
コード例 #13
0
 public Task UpdateAsync(OneDayElectricityModel entity)
 {
     throw new System.NotImplementedException();
 }
コード例 #14
0
 public Task <int> InsertAsync(OneDayElectricityModel entity)
 {
     _dayAnalyticsData.Add(entity);
     return(Task.FromResult(1));
 }
コード例 #15
0
        public async Task <IActionResult> DayResults([FromRoute] string panelId)
        {
            var result = new List <OneDayElectricityModel>();

            var analytics = await _analyticsRepository.Query()
                            .Where(x => x.PanelId.Equals(panelId, StringComparison.CurrentCultureIgnoreCase)).ToListAsync();

            analytics.Sort((x, y) => DateTime.Compare(x.DateTime, y.DateTime));

            //The data for a single day, will use it to build up the List
            OneDayElectricityModel data = new OneDayElectricityModel();

            //start out with the first, then cycle through
            int currentDay = analytics[0].DateTime.DayOfYear;

            data.Maximum = analytics[0].KiloWatt;
            data.Minimum = analytics[0].KiloWatt;
            int count = 0;
            int total = 0;

            foreach (OneHourElectricity e in analytics)
            {
                if (e.DateTime.DayOfYear == currentDay)
                {
                    data.Sum += e.KiloWatt;
                    if (e.KiloWatt < data.Minimum)
                    {
                        data.Minimum = e.KiloWatt;
                    }
                    if (e.KiloWatt > data.Maximum)
                    {
                        data.Maximum = e.KiloWatt;
                    }
                    ++count;
                    ++total;
                    data.DateTime = e.DateTime;

                    if (total == analytics.Count)
                    {
                        data.Average = data.Sum / count;
                        result.Add(data);
                    }
                }

                else if (e.DateTime.DayOfYear != currentDay)
                {
                    data.Average = data.Sum / count;
                    result.Add(data);

                    //reset the values
                    data         = new OneDayElectricityModel();
                    data.Maximum = e.KiloWatt;
                    data.Minimum = e.KiloWatt;
                    data.Sum    += e.KiloWatt;
                    count        = 1;
                    ++total;
                    currentDay    = e.DateTime.DayOfYear;
                    data.DateTime = e.DateTime;
                }
            }
            return(Ok(result));
        }
コード例 #16
0
 Task IGenericRepository <OneDayElectricityModel> .InsertAsync(OneDayElectricityModel entity)
 {
     throw new NotImplementedException();
 }
コード例 #17
0
 public Task <int> InsertAsync(OneDayElectricityModel entity)
 {
     throw new NotImplementedException();
 }