Example #1
0
        public ICollection <MeteoReport> GetMeteoReportData(DateTime from, DateTime to,
                                                            decimal normTemperature, decimal normPrecipitation)
        {
            // getting all stations that are for the selected period
            var stations = dbContext.Stations.Where(x => x.StationAvailabilityPeriods.Any(period =>
                                                                                          (period.From <= from && from <= period.To) || (from <= period.From && period.From <= to)));

            List <MeteoReport> reportList = new List <MeteoReport>();

            // get days data for that stations for from/to
            foreach (var station in stations)
            {
                var daysData = dbContext.DaysData
                               .Where(dd => dd.StationId == station.Id)
                               .Where(x => from <= x.Date && x.Date <= to)
                               .ToList();

                if (daysData.Count() > 0)
                {
                    decimal        averageTemperature  = daysData.Average(x => x.Temperature);
                    DayWeatherData dayMaxTemperature   = daysData.Aggregate((agg, next) => next.Temperature > agg.Temperature ? next : agg);
                    DayWeatherData dayMinTemperature   = daysData.Aggregate((agg, next) => next.Temperature < agg.Temperature ? next : agg);
                    decimal        precipitationSum    = daysData.Sum(x => x.Precipitation);
                    DayWeatherData dayMaxPrecipitation = daysData.Aggregate((agg, next) => next.Precipitation > agg.Precipitation ? next : agg);

                    reportList.Add(new MeteoReport(
                                       station.Id,
                                       station.Name,
                                       averageTemperature,
                                       Math.Abs(averageTemperature - normTemperature),
                                       dayMaxTemperature.Temperature,
                                       dayMaxTemperature.Date,
                                       dayMinTemperature.Temperature,
                                       dayMinTemperature.Date,
                                       precipitationSum,
                                       Math.Abs(precipitationSum - normPrecipitation),
                                       dayMaxPrecipitation.Precipitation,
                                       dayMaxPrecipitation.Date,
                                       daysData.Where(x => x.Precipitation >= 1M).Count(),
                                       daysData.Where(x => x.Precipitation >= 10M).Count(),
                                       daysData.Where(x => x.Wind >= 14M).Count(),
                                       daysData.Count(x => x.ThunderCount > 0)
                                       ));
                }
            }

            return(reportList);
        }
        public IActionResult AddStationData(NewStationDataViewModel stationData)
        {
            var meteoData = new MeteoDataDBContext();

            var station = meteoData
                          .Stations
                          .FirstOrDefault(x => x.Name.ToLower() == stationData.StationName.ToLower());

            if (station == null)
            {
                ModelState.AddModelError(string.Empty, STATION_DOES_NOT_EXIST);
                stationData.StationNames = meteoData
                                           .Stations
                                           .Select(x => new SelectListItem
                {
                    Text  = x.Name,
                    Value = x.Name
                });

                return(View(stationData));
            }

            // TODO: validate those values
            var stationDataToAdd = new DayWeatherData
            {
                Station       = station,
                Date          = stationData.Date,
                Precipitation = stationData.Precipitation,
                Temperature   = stationData.Temperature,
                ThunderCount  = stationData.ThunderCount,
                Wind          = stationData.Wind
            };

            meteoData.DaysData.Add(stationDataToAdd);
            meteoData.SaveChanges();

            return(RedirectToAction("Index", "Home"));
        }
        public static void Initialize(MeteoDataDBContext context)
        {
            context.Database.EnsureCreated();
            if (context.Stations.Any())
            {
                return; // DB has been seeded
            }
            var stations = new Station[]
            {
                new Station {
                    Id = 1, Name = "София"
                },
                new Station {
                    Id = 2, Name = "Видин"
                },
                new Station {
                    Id = 3, Name = "Монтана"
                },
                new Station {
                    Id = 4, Name = "Враца"
                },
                new Station {
                    Id = 5, Name = "Плевен"
                },
                new Station {
                    Id = 6, Name = "В. Търново"
                },
                new Station {
                    Id = 7, Name = "Русе"
                },
                new Station {
                    Id = 8, Name = "Разград"
                },
                new Station {
                    Id = 9, Name = "Добрич"
                },
                new Station {
                    Id = 10, Name = "Варна"
                },
                new Station {
                    Id = 11, Name = "Бургас"
                },
                new Station {
                    Id = 12, Name = "Сливен"
                },
                new Station {
                    Id = 13, Name = "Кърджали"
                },
                new Station {
                    Id = 14, Name = "Пловдив"
                },
                new Station {
                    Id = 15, Name = "Благоевград"
                },
                new Station {
                    Id = 16, Name = "Сандански"
                },
                new Station {
                    Id = 17, Name = "Кюстендил"
                }
            };

            foreach (Station st in stations)
            {
                context.Stations.Add(st);
            }
            context.Database.OpenConnection();
            try
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Stations ON");
                context.SaveChanges();
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Stations OFF");
            }
            finally
            {
                context.Database.CloseConnection();
            }


            var daysData = new DayWeatherData[]
            {
                CreateDayData(1, new DateTime(2018, 6, 14), 30, 0.2M, 2.4M, 0),
                CreateDayData(1, new DateTime(2018, 6, 13), 28, 2.0M, 4.8M, 0),
                CreateDayData(1, new DateTime(2018, 6, 12), 29, 0.0M, 4.0M, 0),
            };

            foreach (DayWeatherData eachData in daysData)
            {
                context.DaysData.Add(eachData);
            }
            context.SaveChanges();
        }