153 lines
5.2 KiB
C#
153 lines
5.2 KiB
C#
|
|
|
|
using CMSMicroservice.Protobuf.Protos.ClubMembership;
|
|
using CMSMicroservice.Protobuf.Protos.Commission;
|
|
using CMSMicroservice.Protobuf.Protos.Configuration;
|
|
using CMSMicroservice.Protobuf.Protos.NetworkMembership;
|
|
using Foursat.BackOffice.BFF.Health.Protobuf;
|
|
|
|
namespace BackOffice.BFF.Application.HealthCQ.Queries.GetSystemHealth;
|
|
|
|
public class GetSystemHealthQueryHandler : IRequestHandler<GetSystemHealthQuery, GetSystemHealthResponse>
|
|
{
|
|
private readonly IApplicationContractContext _context;
|
|
|
|
public GetSystemHealthQueryHandler(IApplicationContractContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<GetSystemHealthResponse> Handle(GetSystemHealthQuery request, CancellationToken cancellationToken)
|
|
{
|
|
var services = new List<ServiceHealthModel>();
|
|
var overallHealthy = true;
|
|
|
|
// Check CMS Commission Service
|
|
var commissionHealthy = false;
|
|
long commissionResponseTime = 0;
|
|
try
|
|
{
|
|
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
|
await _context.Commissions.GetAllWeeklyPoolsAsync(
|
|
new GetAllWeeklyPoolsRequest
|
|
{
|
|
PageIndex = 1,
|
|
PageSize = 1
|
|
},
|
|
cancellationToken: cancellationToken);
|
|
stopwatch.Stop();
|
|
commissionHealthy = true;
|
|
commissionResponseTime = stopwatch.ElapsedMilliseconds;
|
|
}
|
|
catch
|
|
{
|
|
// Service is down
|
|
}
|
|
|
|
services.Add(new ServiceHealthModel
|
|
{
|
|
ServiceName = "CMS Commission Service",
|
|
Status = commissionHealthy ? "Healthy" : "Unhealthy",
|
|
Description = commissionHealthy ? "Connected" : "Connection failed",
|
|
ResponseTimeMs = commissionResponseTime,
|
|
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
|
|
});
|
|
if (!commissionHealthy) overallHealthy = false;
|
|
|
|
// Check CMS Configuration Service
|
|
var configHealthy = false;
|
|
long configResponseTime = 0;
|
|
try
|
|
{
|
|
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
|
await _context.Configurations.GetAllConfigurationsAsync(
|
|
new GetAllConfigurationsRequest
|
|
{
|
|
PageIndex = 1,
|
|
PageSize = 1
|
|
},
|
|
cancellationToken: cancellationToken);
|
|
stopwatch.Stop();
|
|
configHealthy = true;
|
|
configResponseTime = stopwatch.ElapsedMilliseconds;
|
|
}
|
|
catch
|
|
{
|
|
// Service is down
|
|
}
|
|
|
|
services.Add(new ServiceHealthModel
|
|
{
|
|
ServiceName = "CMS Configuration Service",
|
|
Status = configHealthy ? "Healthy" : "Unhealthy",
|
|
Description = configHealthy ? "Connected" : "Connection failed",
|
|
ResponseTimeMs = configResponseTime,
|
|
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
|
|
});
|
|
if (!configHealthy) overallHealthy = false;
|
|
|
|
// Check Network Membership Service
|
|
var networkHealthy = false;
|
|
long networkResponseTime = 0;
|
|
try
|
|
{
|
|
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
|
await _context.NetworkMemberships.GetNetworkStatisticsAsync(
|
|
new GetNetworkStatisticsRequest(),
|
|
cancellationToken: cancellationToken);
|
|
stopwatch.Stop();
|
|
networkHealthy = true;
|
|
networkResponseTime = stopwatch.ElapsedMilliseconds;
|
|
}
|
|
catch
|
|
{
|
|
// Service is down
|
|
}
|
|
|
|
services.Add(new ServiceHealthModel
|
|
{
|
|
ServiceName = "Network Membership Service",
|
|
Status = networkHealthy ? "Healthy" : "Unhealthy",
|
|
Description = networkHealthy ? "Connected" : "Connection failed",
|
|
ResponseTimeMs = networkResponseTime,
|
|
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
|
|
});
|
|
if (!networkHealthy) overallHealthy = false;
|
|
|
|
// Check Club Membership Service
|
|
var clubHealthy = false;
|
|
long clubResponseTime = 0;
|
|
try
|
|
{
|
|
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
|
await _context.ClubMemberships.GetClubStatisticsAsync(
|
|
new GetClubStatisticsRequest(),
|
|
cancellationToken: cancellationToken);
|
|
stopwatch.Stop();
|
|
clubHealthy = true;
|
|
clubResponseTime = stopwatch.ElapsedMilliseconds;
|
|
}
|
|
catch
|
|
{
|
|
// Service is down
|
|
}
|
|
|
|
services.Add(new ServiceHealthModel
|
|
{
|
|
ServiceName = "Club Membership Service",
|
|
Status = clubHealthy ? "Healthy" : "Unhealthy",
|
|
Description = clubHealthy ? "Connected" : "Connection failed",
|
|
ResponseTimeMs = clubResponseTime,
|
|
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
|
|
});
|
|
if (!clubHealthy) overallHealthy = false;
|
|
|
|
return new GetSystemHealthResponse
|
|
{
|
|
OverallHealthy = overallHealthy,
|
|
Services = { services },
|
|
CheckedAt = Timestamp.FromDateTime(DateTime.UtcNow)
|
|
};
|
|
}
|
|
}
|