Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MonitorAlert Changes (Environment added) #42

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
MonitorAlert Changes (Environment added)
  • Loading branch information
thiagoloureiro committed May 9, 2024
commit 51518d8d6412f58f362281592ce630b85374d5d8
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class MonitorAlert
public string? Message { get; set; }
public string ScreenShotUrl { get; set; }
public string MonitorName { get; set; }
public MonitorEnvironment Environment { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public interface IMonitorAlertRepository
{
Task<IEnumerable<MonitorAlert>> GetMonitorAlerts(int? monitorId, int? days, List<int>? groupIds);
Task<MemoryStream> CreateExcelFileAsync(IEnumerable<MonitorAlert> alerts);
Task SaveMonitorAlert(MonitorHistory monitorHistory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public interface IMonitorRepository
Task DeleteMonitorHistory(int days);
Task PauseMonitor(int id, bool paused);
Task<int> CreateMonitorHttp(MonitorHttp monitorHttp);
Task SaveMonitorAlert(MonitorHistory monitorHistory);
Task<IEnumerable<MonitorFailureCount>> GetMonitorFailureCount(int days);
Task<IEnumerable<Monitor>?> GetMonitorListByMonitorGroupIds(List<int> groupMonitorIds,
MonitorEnvironment environment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ public class HttpClientRunner : IHttpClientRunner
private readonly IMonitorRepository _monitorRepository;
private readonly IHttpClientScreenshot _httpClientScreenshot;
private readonly INotificationProducer _notificationProducer;
private readonly IMonitorAlertRepository _monitorAlertRepository;
private int _daysToExpireCert;
private readonly ICustomLogClient<HttpClientRunner> _logClient;
public int _retryIntervalMilliseconds = 6000;

public HttpClientRunner(IMonitorRepository monitorRepository, IHttpClientScreenshot httpClientScreenshot,
INotificationProducer notificationProducer, ICustomLogClient<HttpClientRunner> logClient)
INotificationProducer notificationProducer, ICustomLogClient<HttpClientRunner> logClient, IMonitorAlertRepository monitorAlertRepository)
{
_monitorRepository = monitorRepository;
_httpClientScreenshot = httpClientScreenshot;
_notificationProducer = notificationProducer;
_logClient = logClient;
_monitorAlertRepository = monitorAlertRepository;
_retryIntervalMilliseconds = Environment.GetEnvironmentVariable("HTTP_RETRY_INTERVAL_MS") != null
? int.Parse(Environment.GetEnvironmentVariable("HTTP_RETRY_INTERVAL_MS"))
: 6000;
Expand Down Expand Up @@ -72,7 +74,7 @@ await _monitorRepository.UpdateMonitorStatus(monitorHttp.MonitorId, succeeded,
if (!monitorHttp.LastStatus)
{
await _notificationProducer.HandleSuccessNotifications(monitorHttp, response.ReasonPhrase);
await _monitorRepository.SaveMonitorAlert(monitorHistory);
await _monitorAlertRepository.SaveMonitorAlert(monitorHistory);
}

break;
Expand Down Expand Up @@ -102,7 +104,7 @@ await _notificationProducer.HandleFailedNotifications(monitorHttp,
monitorHttp.UrlToCheck,
monitorHttp.MonitorId, monitorHttp.Name);
monitorHistory.ScreenShotUrl = screenshotUrl;
await _monitorRepository.SaveMonitorAlert(monitorHistory);
await _monitorAlertRepository.SaveMonitorAlert(monitorHistory);

break;
}
Expand Down Expand Up @@ -138,7 +140,7 @@ await _notificationProducer.HandleFailedNotifications(monitorHttp,
.LastStatus) // only send notification when goes from online into offline to avoid flood
{
await _notificationProducer.HandleFailedNotifications(monitorHttp, err.Message);
await _monitorRepository.SaveMonitorAlert(monitorHistory);
await _monitorAlertRepository.SaveMonitorAlert(monitorHistory);
await _httpClientScreenshot.TakeScreenshotAsync(monitorHttp.UrlToCheck,
monitorHttp.MonitorId, monitorHttp.Name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task<IEnumerable<MonitorAlert>> GetMonitorAlerts(int? monitorId, in
if (monitorId > 0)
{
sql =
@$"SELECT M.Name as MonitorName, MA.Id, MA.MonitorId, MA.TimeStamp, MA.Status, MA.Message, MA.ScreenShotUrl
@$"SELECT M.Name as MonitorName, MA.Id, MA.MonitorId, MA.TimeStamp, MA.Status, MA.Message, MA.ScreenShotUrl, MA.Environment
FROM MonitorAlert MA
INNER JOIN Monitor M on M.Id = MA.MonitorId
WHERE MA.MonitorId = {monitorId} AND MA.TimeStamp >= DATEADD(day, -{days}, GETDATE()) AND MA.[Status] = 0
Expand All @@ -34,7 +34,7 @@ FROM MonitorAlert MA
}

sql =
@$"SELECT M.Name as MonitorName, MA.Id, MA.MonitorId, MA.TimeStamp, MA.Status, MA.Message, MA.ScreenShotUrl
@$"SELECT M.Name as MonitorName, MA.Id, MA.MonitorId, MA.TimeStamp, MA.Status, MA.Message, MA.ScreenShotUrl, MA.Environment
FROM MonitorAlert MA
INNER JOIN Monitor M on M.Id = MA.MonitorId
INNER JOIN MonitorGroupItems MGI on MGI.MonitorId = M.Id
Expand All @@ -59,6 +59,7 @@ public async Task<MemoryStream> CreateExcelFileAsync(IEnumerable<MonitorAlert> a

worksheet.Cells[1, col++].Value = "Timestamp";
worksheet.Cells[1, col++].Value = "Monitor Name";
worksheet.Cells[1, col++].Value = "Environment";
worksheet.Cells[1, col++].Value = "Message";
worksheet.Cells[1, col].Value = "Screenshot URL";

Expand All @@ -69,6 +70,7 @@ public async Task<MemoryStream> CreateExcelFileAsync(IEnumerable<MonitorAlert> a
col = 1;
worksheet.Cells[row, col++].Value = alert.TimeStamp.ToString("dd/MM/yyyy HH:mm:ss");
worksheet.Cells[row, col++].Value = alert.MonitorName;
worksheet.Cells[row, col++].Value = alert.Environment.ToString();
worksheet.Cells[row, col++].Value = alert.Message;
worksheet.Cells[row, col].Value = alert.ScreenShotUrl;

Expand All @@ -81,4 +83,20 @@ public async Task<MemoryStream> CreateExcelFileAsync(IEnumerable<MonitorAlert> a
stream.Position = 0;
return stream;
}

public async Task SaveMonitorAlert(MonitorHistory monitorHistory)
{
await using var db = new SqlConnection(_connstring);
string sql =
@"INSERT INTO [MonitorAlert] (MonitorId, TimeStamp, Status, Message, ScreenShotUrl, Environment) VALUES (@MonitorId, @TimeStamp, @Status, @Message, @ScreenShotUrl, @Environment)";
await db.ExecuteAsync(sql,
new
{
monitorHistory.MonitorId,
monitorHistory.TimeStamp,
monitorHistory.Status,
Message = monitorHistory.ResponseMessage,
monitorHistory.ScreenShotUrl
}, commandType: CommandType.Text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,21 +413,7 @@ public async Task<IEnumerable<MonitorHttp>> GetHttpMonitorByIds(List<int> ids)
return await db.QueryAsync<MonitorHttp>(sql, commandType: CommandType.Text);
}

public async Task SaveMonitorAlert(MonitorHistory monitorHistory)
{
await using var db = new SqlConnection(_connstring);
string sql =
@"INSERT INTO [MonitorAlert] (MonitorId, TimeStamp, Status, Message, ScreenShotUrl) VALUES (@MonitorId, @TimeStamp, @Status, @Message, @ScreenShotUrl)";
await db.ExecuteAsync(sql,
new
{
monitorHistory.MonitorId,
monitorHistory.TimeStamp,
monitorHistory.Status,
Message = monitorHistory.ResponseMessage,
monitorHistory.ScreenShotUrl
}, commandType: CommandType.Text);
}


public async Task<IEnumerable<MonitorFailureCount>> GetMonitorFailureCount(int days)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public async Task<IActionResult> DeleteMonitorGroup(int id)
{
return BadRequest("monitorGroups.monitorNotFound");
}
else if(monitorGroup.Monitors.Any())
else if(monitorGroup.Monitors != null && monitorGroup.Monitors.Any())
{
return BadRequest("monitorGroups.hasItemsFound");
}
Expand Down