public static WeatherLocatioinModel LoadLocationWeatherData( DateTime now, Location location, WeatherData[] locationData) { var minDate = now.AddMinutes(-90); var maxDate = now.AddMinutes(90); // погода на текущее время var current = locationData.FirstOrDefault(d => d.Date > minDate && d.Date <= maxDate); // погода на ближайшие сутки var xxx = current != null ? current.Date : now; var day = locationData .Where(d => d.Date > xxx && d.Date <= xxx.AddDays(1)) .Where(FilterByHours) .OrderBy(d => d.Date) .Take(3) .ToArray(); // прогноз на несколько дней var forecast = locationData .Where(d => d.Date.Date > now.Date) .GroupBy(d => d.Date.Date) .Take(3); var model = new WeatherLocatioinModel { LocationId = location.Id, LocationName = location.DisplayName, Now = CreateModel(current), Today = day.Select(CreateModel).ToArray(), Forecast = forecast.Select(CreateDailyModel).ToArray() }; return model; }
public object AddLocation(HttpRequestParams request) { var displayName = request.GetRequiredString("displayName"); var query = request.GetRequiredString("query"); using (var session = Context.OpenSession()) { var location = new Location { Id = Guid.NewGuid(), DisplayName = displayName, Query = query }; session.Save(location); session.Flush(); } return null; }
private void UpdateOneLocation(Location location, ISession session) { try { var forecast = LoadForecast(location.Query, Logger); UpdateWeatherData(session, location, forecast, Logger); } catch (Exception ex) { string msg = string.Format("loading error (location {0})", location); Logger.ErrorException(msg, ex); } }
private static WeatherData GetWeatherDataItem(long seconds, ISession session, Location location) { var date = DateTimeFromUnixTimestampSeconds(seconds); var dataItem = session .Query<WeatherData>() .FirstOrDefault(obj => obj.Date == date && obj.Location.Id == location.Id); if (dataItem == null) { dataItem = new WeatherData { Id = Guid.NewGuid(), Date = date, Location = location }; session.Save(dataItem); } return dataItem; }
private static void UpdateWeatherData(ISession session, Location location, dynamic forecast, Logger logger) { var list = forecast.list as IEnumerable; int count = 0; if (list != null) { foreach (dynamic item in list) { long seconds = item.dt; var dataItem = GetWeatherDataItem(seconds, session, location); UpdateWeatherDataItem(dataItem, item); session.Flush(); count++; } } logger.Info("updated {0} items", count); }