public async Task <IActionResult> Uptime(string source) { var metrics = await _metricService.GetMetricsAsync(Metrics.Ping, source); if (metrics.Count() == 0) { return(NotFound()); } var metric = metrics.First(); if (!_auth.IsAuthenticated() && !metric.Public) { return(Unauthorized()); } return(new BadgeResult( _badge.GetUptimeBadge( metric.Source, await _uptime.ComputeUptimeAsync(metric.Source) ) )); }
public async Task <IActionResult> Metric(string type, string source, string start = null, string end = null) { Metrics metricType; try { metricType = type.ToEnum <Metrics>(); } catch (System.Exception) { return(BadRequest("Bad type. Needs to be one of Metrics type.")); } if (start != null) { try { DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc).AddMilliseconds(Convert.ToInt64(start)); } catch (System.Exception) { return(BadRequest("Bad start date. Needs to be the number of milliseconds since Epoch.")); } } if (end != null) { try { DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc).AddMilliseconds(Convert.ToInt64(end)); } catch (System.Exception) { return(BadRequest("Bad end date. Needs to be the number of milliseconds since Epoch.")); } } if (start != null && end != null) { if ( DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc).AddMilliseconds(Convert.ToInt64(start)) >= DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc).AddMilliseconds(Convert.ToInt64(end)) ) { return(BadRequest("Bad dates. End date needs to be greater than the start date.")); } } var model = await _metricService.GetMetricsAsync(metricType, source); if (model.Count() == 0) { return(NotFound()); } if (!_auth.IsAuthenticated() && !model.First().Public) { return(Unauthorized()); } ViewBag.ManualLabels = await _context.ManualLabels.ToListAsync(); ViewBag.Max = 100; ViewBag.Min = 0; if (metricType == Metrics.Ping) { var pingSetting = await _context .PingSettings .FirstOrDefaultAsync(setting => new Uri(setting.ServerUrl).Host == source); ViewBag.Max = pingSetting.MaxResponseTime.TotalMilliseconds; ViewBag.Uptime = await _uptime.ComputeUptimeAsync(source); } ViewBag.Start = start ?? 0.ToString(); ViewBag.End = end ?? 0.ToString(); return(View(model.First())); }