public async Task <IActionResult> Post([FromBody] MVDMonitor model) { if (string.IsNullOrEmpty(model.Name)) { return(Error("Name is required.")); } var dataObj = new MVDMonitor { CreatedDate = DateTime.UtcNow, Name = model.Name }; DB.Monitors.Add(dataObj); var result = await DB.SaveChangesAsync(); if (result > 0) { return(Success("Monitoring saved successfully.", new { Id = dataObj.MonitorId })); } else { return(Error("Something is wrong with your model.")); } }
public async Task <IActionResult> Get(MVDMonitor monitor) { var list = await db.Monitors.Where(w => w.UserId.Equals(_userId)).ToListAsync(); var clientList = new List <object>(); foreach (var monitorItem in list) { clientList.Add(await GetMonitorClientModel(monitorItem)); } return(Success(data: list)); }
public async Task <IActionResult> Post([FromBody] MVMMonitorSave value) { if (string.IsNullOrEmpty(value.Name)) { return(Error("Name is required")); } var monitorObject = new MVDMonitor { Id = Guid.NewGuid(), Name = value.Name, CreatedDate = DateTime.UtcNow, UserId = _userId }; db.Add(monitorObject); var monitorStepData = new MVDSMonitorStepSettingsRequest { Url = value.Url }; var monitorStep = new MVDMonitorStep { Id = Guid.NewGuid(), MonitorId = monitorObject.Id, Type = MVDMonitorStepTypes.Request, Settings = JsonConvert.SerializeObject(monitorStepData) //gelen stringi JSON' a çevir }; db.MonitorSteps.Add(monitorStep); var result = await db.SaveChangesAsync(); if (result > 0) { return(Success("Monitoring Save successfully", new { Id = monitorObject.Id })); } else { return(Error("something went wrong")); } }
public async Task <object> GetMonitorClientModel(MVDMonitor monitor) { var url = string.Empty; var monitorStepRequest = await db.MonitorSteps.FirstOrDefaultAsync(x => x.MonitorId == monitor.Id && x.Type == MVDMonitorStepTypes.Request); var upTime = 0.00; var downTime = 0.00; var loadTime = 0.00; var totalMonitorTime = 0.00; var downTimePercent = 0.00; var loadTimes = new List <double>(); var upTimes = new List <double>(); if (monitorStepRequest != null) { var requestSettings = monitorStepRequest.SettingsAsRequest(); if (requestSettings != null) { url = requestSettings.Url; } } var week = DateTime.UtcNow.AddDays(-14); // son 14 gününde yapılan requestler var logs = await db.MonitorStepLogs .Where(x => x.MonitorStepId == monitorStepRequest.Id && x.StartDate >= week) .OrderByDescending(x => x.StartDate) .Take(50) .ToListAsync(); // Loadtime sadece başarılı requestler üzerinden hesaplanıyor if (logs.Any(x => x.Status == MVDMonitorStepStatusType.Success)) { loadTime = logs .Where(x => x.Status == MVDMonitorStepStatusType.Success) .Average(x => x.EndDate.Subtract(x.StartDate).TotalMilliseconds); } foreach (var log in logs) { totalMonitorTime += log.Interval; if (log.Status == MVDMonitorStepStatusType.Success) { loadTimes.Add(log.EndDate.Subtract(log.StartDate).TotalMilliseconds); } if (log.Status == MVDMonitorStepStatusType.Fail) { downTime += log.Interval; } } downTimePercent = 100 - (downTime / totalMonitorTime) * 100; return (new { monitor.Id, monitor.LastCheckDate, monitor.LoadTime, monitor.Name, monitor.TestStatus, Url, upTime, upTimes, downTime, downTimePercent, loadTime, loadTimes, totalMonitorTime }); }
private async Task <object> GetMonitorClientModel(MVDMonitor monitor) { var url = string.Empty; var loadTime = 0.00; var loadTimes = new List <double>(); var upTime = 0.00; var downTime = 0.00; var downTimePercent = 0.00; var totalMonitoredTime = 0; var upTimes = new List <double>(); var stepStatus = MVDMonitorStepStatusTypes.Unknown; var monitorStepRequest = await Db.MonitorSteps.FirstOrDefaultAsync(x => x.MonitorId == monitor.MonitorId && x.Type == MVDMonitorStepTypes.Request); if (monitorStepRequest != null) { var requestSettings = monitorStepRequest.SettingsAsRequest(); if (requestSettings != null) { url = requestSettings.Url; } var week = DateTime.UtcNow.AddDays(-14); var logs = await Db.MonitorStepLogs .Where(x => x.MonitorStepId == monitorStepRequest.MonitorStepId && x.StartDate >= week) .OrderByDescending(x => x.StartDate) .Take(50) .ToListAsync(); logs = logs.OrderBy(x => x.StartDate).ToList(); if (logs.Any(x => x.Status == MVDMonitorStepStatusTypes.Success)) { loadTime = logs .Where(x => x.Status == MVDMonitorStepStatusTypes.Success) .Average(x => x.EndDate.Subtract(x.StartDate).TotalMilliseconds); } foreach (var log in logs) { totalMonitoredTime += log.Interval; if (log.Status == MVDMonitorStepStatusTypes.Success) { loadTimes.Add(log.EndDate.Subtract(log.StartDate).TotalMilliseconds); } if (log.Status == MVDMonitorStepStatusTypes.Fail) { downTime += log.Interval; } var currentDowntimePercent = (downTime / totalMonitoredTime) * 100; var currentUptimePercent = 100 - currentDowntimePercent; upTimes.Add(double.IsNaN(currentUptimePercent) ? 0 : currentUptimePercent); } var lastLog = logs.LastOrDefault(); if (lastLog != null) { stepStatus = lastLog.Status; } downTimePercent = (downTime / totalMonitoredTime) * 100; upTime = 100 - downTimePercent; } if (double.IsNaN(upTime)) { upTime = 0; } return(new { monitor.MonitorId, monitor.CreatedDate, monitor.LastCheckDate, monitor.MonitorStatus, monitor.Name, monitor.TestStatus, monitor.UpdatedDate, url, upTime, upTimes, downTime, downTimePercent, loadTime, loadTimes, totalMonitoredTime, stepStatus, stepStatusText = $"{stepStatus}" }); }
public async Task <IActionResult> Post([FromBody] MVMMonitorSave value) { if (string.IsNullOrEmpty(value.Name)) { return(Error("Name is required.")); } var monitorCheck = await Db.Monitors.AnyAsync( x => x.MonitorId != value.Id && x.Name.Equals(value.Name) && x.UserId == UserId); if (monitorCheck) { return(Error("This project name is already in use. Please choose a different name.")); } MVDMonitor data = null; if (value.Id != Guid.Empty) { data = await Db.Monitors.FirstOrDefaultAsync(x => x.MonitorId == value.Id && x.UserId == UserId); if (data == null) { return(Error("Monitor not found.")); } data.UpdatedDate = DateTime.UtcNow; data.Name = value.Name; } else { data = new MVDMonitor { MonitorId = Guid.NewGuid(), CreatedDate = DateTime.UtcNow, Name = value.Name, UserId = UserId }; Db.Monitors.Add(data); } var monitorStepData = new MVDSMonitorStepSettingsRequest { Url = value.Url }; var step = await Db.MonitorSteps.FirstOrDefaultAsync(x => x.MonitorId == data.MonitorId && x.Type == MVDMonitorStepTypes.Request); if (step != null) { var requestSettings = step.SettingsAsRequest() ?? new MVDSMonitorStepSettingsRequest(); requestSettings.Url = value.Url; step.Settings = JsonConvert.SerializeObject(requestSettings); } else { step = new MVDMonitorStep { MonitorStepId = Guid.NewGuid(), Type = MVDMonitorStepTypes.Request, MonitorId = data.MonitorId, Settings = JsonConvert.SerializeObject(monitorStepData), Interval = 10 }; Db.MonitorSteps.Add(step); }; var result = await Db.SaveChangesAsync(); if (result > 0) { return(Success("Monitoring saved successfully.", new { Id = data.MonitorId })); } else { return(Error("Something is wrong with your model.")); } }