private List <Tuple <string, string> > GetCheckSummaryAvgForSpecifiedTimeSpan(int checkId) { int i = 7; List <Tuple <string, string> > summaryValues = new List <Tuple <string, string> >(); while (i >= 1) { //Get the average response time for the past 7 hours/weeks based on the frequency. long fromTime = 0; long toTime = 0; if (Frequency.Equals("Hourly", StringComparison.OrdinalIgnoreCase)) { fromTime = UnixTimeStampUtility.GetUnixTimestampSeconds(DateTime.UtcNow.Subtract(new TimeSpan(0, i, 0, 0))); toTime = UnixTimeStampUtility.GetUnixTimestampSeconds(DateTime.UtcNow.Subtract(new TimeSpan(0, i - 1, 0, 0))); } else { fromTime = UnixTimeStampUtility.GetUnixTimestampSeconds(DateTime.UtcNow.Subtract(new TimeSpan(i, 0, 0, 0))); toTime = UnixTimeStampUtility.GetUnixTimestampSeconds(DateTime.UtcNow.Subtract(new TimeSpan(i - 1, 0, 0, 0))); } NetworkCredential nc = new NetworkCredential(UserName, Password); WebRequest request = WebRequest.Create(string.Format("https://api.pingdom.com/api/2.0/summary.average/{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(); 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 status in summary.Value) { //Get the average response time and store it to the JSON object. if (status.Key == "avgresponse") { if (Frequency.Equals("Hourly", StringComparison.OrdinalIgnoreCase)) { summaryValues.Add(new Tuple <string, string>(String.Format("{0:HH:mm}", UnixTimeStampUtility.DateTimeFromUnixTimestampSeconds(fromTime).ToLocalTime()), status.Value.ToString())); } else { summaryValues.Add(new Tuple <string, string>(String.Format("{0:MM/dd}", UnixTimeStampUtility.DateTimeFromUnixTimestampSeconds(fromTime).ToLocalTime()), status.Value.ToString())); } } } } } i--; } return(summaryValues); }
private void GetDetailedReportForCheck(string checkAlias) { DateTime startingTime = DateTime.Now.AddHours(DateTime.Now.Hour * -1).AddDays(NoOfDays * -1); //get the midnight time for today to create separate report for each day. for (int j = 0; j <= NoOfDays; j++) { int i = 0; List <Tuple <string, string> > summaryValues = new List <Tuple <string, string> >(); while (i <= 23) //get th values for each hour. TBD : For today, there may not be values for all 24 hours and they are being filled as zero.Need to fix it. { long fromTime = 0; long toTime = 0; fromTime = UnixTimeStampUtility.GetUnixTimestampSeconds(startingTime.ToUniversalTime()); toTime = UnixTimeStampUtility.GetUnixTimestampSeconds(startingTime.AddHours(1).ToUniversalTime()); NetworkCredential nc = new NetworkCredential(UserName, Password); WebRequest request = WebRequest.Create(string.Format("https://api.pingdom.com/api/2.0/summary.average/{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(); 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 status in summary.Value) { //Get the average response time and store it to the JSON object. if (status.Key == "avgresponse") { summaryValues.Add(new Tuple <string, string>(String.Format("{0:HH:mm}", UnixTimeStampUtility.DateTimeFromUnixTimestampSeconds(fromTime).ToLocalTime()), status.Value.ToString())); } } } } i++; startingTime = startingTime.AddHours(1); } JArray reportObject = ReportHelpers.GetJson(summaryValues); ReportHelpers.CreateBlob(StorageAccount, checkAlias + string.Format("{0:MMdd}", startingTime.AddHours(-1)) + "DetailedReport.json", ContainerName, "application/json", ReportHelpers.ToStream(reportObject)); } }