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

Development #182

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
More unit tests
  • Loading branch information
thiagoloureiro committed Sep 30, 2024
commit 35b86aba8d6310dcbc9f0a17928538abed7ed8d5
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class MonitorService : IMonitorService

public MonitorService(IMonitorRepository monitorRepository, ICaching caching,
IMonitorGroupService monitorGroupService, IHttpClientFactory httpClientFactory,
IHttpClientRunner httpClientRunner, IMonitorHistoryRepository monitorHistoryRepository, ILogger<MonitorService> logger)
IHttpClientRunner httpClientRunner, IMonitorHistoryRepository monitorHistoryRepository,
ILogger<MonitorService> logger)
{
_monitorRepository = monitorRepository;
_caching = caching;
Expand Down Expand Up @@ -187,48 +188,40 @@ public async Task PauseMonitor(int id, bool paused)

public async Task SetMonitorDashboardDataCacheList()
{
try
if (GlobalVariables.MasterNode)
{
if (GlobalVariables.MasterNode)
_logger.LogInformation("Started Caching Monitor Dashboard Data List");
var lstMonitorDashboard = new List<MonitorDashboard?>();
var lstMonitor = await GetMonitorList();
int maxDegreeOfParallelism =
Convert.ToInt32(Environment.GetEnvironmentVariable("CACHE_PARALLEL_TASKS") ??
"10"); // Adjust this value based on your environment

using (var semaphore = new SemaphoreSlim(maxDegreeOfParallelism))
{
_logger.LogInformation("Started Caching Monitor Dashboard Data List");
var lstMonitorDashboard = new List<MonitorDashboard?>();
var lstMonitor = await GetMonitorList();
int maxDegreeOfParallelism =
Convert.ToInt32(Environment.GetEnvironmentVariable("CACHE_PARALLEL_TASKS") ??
"10"); // Adjust this value based on your environment

using (var semaphore = new SemaphoreSlim(maxDegreeOfParallelism))
{
var tasks = lstMonitor
.Where(monitor => monitor != null)
.Select(async monitor =>
var tasks = lstMonitor
.Where(monitor => monitor != null)
.Select(async monitor =>
{
await semaphore.WaitAsync();
try
{
await semaphore.WaitAsync();
try
{
return await GetMonitorDashboardData(monitor.Id, monitor);
}
finally
{
semaphore.Release();
}
});

var results = await Task.WhenAll(tasks);
lstMonitorDashboard.AddRange(results);
}
return await GetMonitorDashboardData(monitor.Id, monitor);
}
finally
{
semaphore.Release();
}
});

_logger.LogInformation("Writing Cache to Redis");
await _caching.SetValueToCacheAsync(_cacheKeyDashboardList, lstMonitorDashboard, 20,
CacheTimeInterval.Minutes);
_logger.LogInformation("Finished writing Cache to Redis and ended Caching activity");
var results = await Task.WhenAll(tasks);
lstMonitorDashboard.AddRange(results);
}
}
catch (Exception e)
{
SentrySdk.CaptureException(e);
throw;

_logger.LogInformation("Writing Cache to Redis");
await _caching.SetValueToCacheAsync(_cacheKeyDashboardList, lstMonitorDashboard, 20,
CacheTimeInterval.Minutes);
_logger.LogInformation("Finished writing Cache to Redis and ended Caching activity");
}
}

Expand Down Expand Up @@ -532,21 +525,14 @@ private async Task CreateAction(UserAction action, string token)

public async Task<UserDto?> GetUserDetailsByToken(string token)
{
try
{
var client = CreateHttpClient(token);
var client = CreateHttpClient(token);

var authApi = Environment.GetEnvironmentVariable("AUTH_API_URL");
var content = await client.GetAsync($"{authApi}api/User/GetUserDetailsByToken");
var authApi = Environment.GetEnvironmentVariable("AUTH_API_URL");
var content = await client.GetAsync($"{authApi}api/User/GetUserDetailsByToken");

var result = await content.Content.ReadAsStringAsync();
var user = JsonConvert.DeserializeObject<UserDto>(result);
return user;
}
catch (Exception)
{
return null;
}
var result = await content.Content.ReadAsStringAsync();
var user = JsonConvert.DeserializeObject<UserDto>(result);
return user;
}

public async Task<Monitor> GetMonitorById(int id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ public async Task Should_Make_HttpClient_Call_OK_Result(string url, MonitorHttpM
Assert.NotNull(result.ReasonPhrase);
Assert.NotNull(result.Content);
}

[Theory]
[InlineData("https://postman-echo.com/get", MonitorHttpMethod.Get)]
[InlineData("https://postman-echo.com/post", MonitorHttpMethod.Post)]
[InlineData("https://postman-echo.com/put", MonitorHttpMethod.Put)]
public async Task Should_Make_HttpClient_Call_IgnoreTlsSsl_OK_Result(string url, MonitorHttpMethod method)
{
// Arrange
var monitorHttp = new MonitorHttp
{
UrlToCheck = url,
MonitorId = 1,
Name = "Test",
Id = 1,
CheckCertExpiry = true,
IgnoreTlsSsl = true,
Timeout = 10,
MonitorHttpMethod = method,
MaxRedirects = 5,
HeartBeatInterval = 1,
Retries = 0,
LastStatus = true,
ResponseTime = 10
};

// Act
var result = await _httpClientRunner.MakeHttpClientCall(monitorHttp);

// Assert
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.NotNull(result.ReasonPhrase);
Assert.NotNull(result.Content);
}

[Theory]
[InlineData("https://httpbin.org/get1", MonitorHttpMethod.Get)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task GetMonitorDashboardData_ReturnsMonitorDashboard()
var monitorId = 1;
var monitorHistory = new List<MonitorHistory>
{
new MonitorHistory { TimeStamp = DateTime.UtcNow.AddDays(-1), Status = true, ResponseTime = 100 },
new MonitorHistory { TimeStamp = DateTime.UtcNow.AddMinutes(-10), Status = true, ResponseTime = 100 },
new MonitorHistory { TimeStamp = DateTime.UtcNow.AddDays(-2), Status = false, ResponseTime = 200 },
};
var monitor = new Monitor()
Expand Down