feat(api, bll): переделал сервис получения статистики из RabbitMQ. StatRabbitMqController - статистика по очередям и соединениям
This commit is contained in:
78
PARR.BLL/Services/Implementations/MqAdminService.cs
Normal file
78
PARR.BLL/Services/Implementations/MqAdminService.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.BLL.Contracts.Interfaces;
|
||||
using PARR.BLL.Domain.Mq;
|
||||
using PARR.BLL.Services.Interfaces;
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PARR.BLL.Services.Implementations
|
||||
{
|
||||
internal class MqAdminService : IMqAdminService
|
||||
{
|
||||
private readonly HttpClient httpClient;
|
||||
private readonly ILogger<MqAdminService> logger;
|
||||
|
||||
public MqAdminService(HttpClient httpClient, ILogger<MqAdminService> logger)
|
||||
{
|
||||
this.httpClient = httpClient;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public async Task<MqApiResult> GetConnectionsAsync(IMqSettings mqSettings)
|
||||
{
|
||||
var url = $"http://{mqSettings.HostName}:15672/api/connections";
|
||||
|
||||
return await SendRequestAsync(mqSettings, url);
|
||||
}
|
||||
|
||||
|
||||
public async Task<MqApiResult> GetQueuesAsync(IMqSettings mqSettings)
|
||||
{
|
||||
var url = $"http://{mqSettings.HostName}:15672/api/queues";
|
||||
|
||||
return await SendRequestAsync(mqSettings, url);
|
||||
}
|
||||
|
||||
|
||||
private async Task<MqApiResult> SendRequestAsync(IMqSettings mqSettings, string url)
|
||||
{
|
||||
SetAuthorizationHeaders(mqSettings);
|
||||
|
||||
try
|
||||
{
|
||||
var result = await httpClient.GetAsync(url);
|
||||
|
||||
if (result.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
var content = await result.Content.ReadAsStringAsync();
|
||||
var json = JsonSerializer.Deserialize<object>(content);
|
||||
|
||||
return new MqApiResult { IsSuccess = true, Response = json };
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogError($"Ошибка при запросе к RabbitMq, url: {url}, StatusCode: {result.StatusCode}");
|
||||
|
||||
return new MqApiResult { IsSuccess = false };
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"Ошибка при запросе к RabbitMQ, url: {url}");
|
||||
return new MqApiResult { IsSuccess = false, Exception = ex };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void SetAuthorizationHeaders(IMqSettings mqSettings)
|
||||
{
|
||||
httpClient.DefaultRequestHeaders.Clear();
|
||||
httpClient.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{mqSettings.User}:{mqSettings.Password}")));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -155,34 +155,6 @@ namespace PARR.BLL.Services.Implementations
|
||||
}
|
||||
}
|
||||
|
||||
public MqQueueCountResult GetQueueCount(IMqSettings mqSettings, string queueName)
|
||||
{
|
||||
var factory = new ConnectionFactory
|
||||
{
|
||||
HostName = mqSettings.HostName,
|
||||
UserName = mqSettings.User,
|
||||
Password = mqSettings.Password,
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using (var connection = factory.CreateConnection())
|
||||
using (var channel = connection.CreateModel())
|
||||
{
|
||||
var messageCount = channel.MessageCount(queueName);
|
||||
var consumerCount = channel.ConsumerCount(queueName);
|
||||
|
||||
return new MqQueueCountResult { Count = (int)messageCount, ConsumerCount = (int)consumerCount, IsSuccess = true };
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"Ошибка при запросе кол-ва сообщений в очереди {queueName}");
|
||||
|
||||
return new MqQueueCountResult { Count = 0, IsSuccess = false, Exception = ex };
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
channel?.Close();
|
||||
|
||||
Reference in New Issue
Block a user