public async Task <IEnumerable <Forecast> > RetrieveForcastWeatherData() { var outcome = await PollyUtility.ExecuteWebRequest(async() => { var url = _configuration.Settings["forcastDataUrl"]; var request = WebRequest.CreateHttp(url); var response = await request.GetResponseAsync(); using (var stream = response.GetResponseStream()) using (var reader = new StreamReader(stream)) { var data = JsonConvert.DeserializeObject <WeatherForecast>(await reader.ReadToEndAsync()); return(data.list.Select(f => new Forecast { Temperature = f.main.temp, MinTemperature = f.main.temp_min, MaxTemperature = f.main.temp_max, WeatherTypeId = f.weather.FirstOrDefault()?.id, Date = UnixTimeStampUtility.UnixTimeStampToDateTime(f.dt) })); } }); if (outcome.Result == null) { return(new List <Forecast>()); } return(outcome.Result); }
private void UpdateCurrentWeatherData(CurrentWeatherConditions currentWeatherData) { _dawn = UnixTimeStampUtility.UnixTimeStampToDateTime(currentWeatherData?.sys?.sunrise ?? 0).TimeOfDay; _dusk = UnixTimeStampUtility.UnixTimeStampToDateTime(currentWeatherData?.sys?.sunset ?? 0).TimeOfDay; var icons = GetIcons(DateTime.Now.TimeOfDay, _dawn, _dusk); CurrentWeather = new ForecastViewModel(new Forecast { Temperature = currentWeatherData?.main?.temp ?? 0, WeatherTypeId = currentWeatherData.weather.FirstOrDefault()?.id }, icons); }
private bool CheckIfDateIsAroundNoon(int dt) { var hour = UnixTimeStampUtility.UnixTimeStampToDateTime(dt).Hour; return(hour >= 12 && hour <= 15); }
private void GetMicroServiceReportForCheck(string checkAlias, int CheckId) { DateTime startingTime = DateTime.Now.AddHours(-LastNhour); List <Tuple <string, string> > summaryValues = new List <Tuple <string, string> >(); string serviceStatus = "up"; int overallTime = 60 * 60 * LastNhour; // in sec int downtimeSum = 0; // in secs long fromTime = UnixTimeStampUtility.GetUnixTimestampSeconds(startingTime.ToUniversalTime()); long toTime = UnixTimeStampUtility.GetUnixTimestampSeconds(DateTime.Now.ToUniversalTime()); NetworkCredential nc = new NetworkCredential(UserName, Password); WebRequest request = WebRequest.Create(string.Format("https://api.pingdom.com/api/2.0/summary.outage/{0}?from={1}&to={2}", CheckId, fromTime, toTime)); request.Credentials = nc; request.Headers.Add(AppKey); request.PreAuthenticate = true; request.Method = "GET"; WebResponse respose = request.GetResponse(); List <Tuple <int, DateTime> > downRecord = new List <Tuple <int, DateTime> >(); AlertThresholds thresholdValues = new JavaScriptSerializer().Deserialize <AlertThresholds>(ReportHelpers.Load(StorageAccount, "Configuration.AlertThresholds.json", ContainerName)); using (var reader = new StreamReader(respose.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var summaryObject = js.Deserialize <dynamic>(reader.ReadToEnd()); foreach (var summary in summaryObject["summary"]) { foreach (var states in summary.Value) { if (states["status"] == "down") { DateTime start = UnixTimeStampUtility.DateTimeFromUnixTimestampSeconds(states["timefrom"]).ToLocalTime(); DateTime end = UnixTimeStampUtility.DateTimeFromUnixTimestampSeconds(states["timeto"]).ToLocalTime(); int downtime = (int)end.Subtract(start).TotalSeconds; if (downtime > thresholdValues.PingdomServiceDistruptionErrorThresholdInSeconds) { serviceStatus = "down"; downRecord.Add(new Tuple <int, DateTime>(downtime, DateTime.Now)); } } } } } if (serviceStatus.Equals("down")) { StringBuilder sb = new StringBuilder(); foreach (Tuple <int, DateTime> each in downRecord) { sb.Append(string.Format("at {0}, there is {1} second down.", each.Item2.ToString(), each.Item1)); downtimeSum = downtimeSum + each.Item1; // in secs } new SendAlertMailTask { AlertSubject = string.Format("Error: Alert for {0} pingdom service Down", checkAlias), Details = string.Format("Pingdom service {0} down time exceeded threshold: {1} second, in last {2} hours, there are {3} down happened, detail is {4}", checkAlias, thresholdValues.PingdomServiceDistruptionErrorThresholdInSeconds, LastNhour, downRecord.Count, sb.ToString()), AlertName = string.Format("Error: Pingdom Micro Service: {0}", checkAlias), Component = "Pingdom Service", Level = "Error" }.ExecuteCommand(); } int serviceUpTime = overallTime - downtimeSum; // in secs ReportHelpers.AppendDatatoBlob(StorageAccount, checkAlias + string.Format("{0:MMdd}", DateTime.Now) + "outageReport.json", new Tuple <string, string>(string.Format("{0:HH-mm}", DateTime.Now), serviceUpTime.ToString()), 24, ContainerName); }