feat(api, bll): IMqService - добавлен метод получения статистики по очереди. StatRabbitMqController - статистика по RabbitMq
This commit is contained in:
@@ -212,6 +212,11 @@
|
||||
public const string GetStatusesLastDay = BaseStat + "/orders/statuses/last-day/{statusCode}";
|
||||
}
|
||||
|
||||
public static class StatRabbitMq
|
||||
{
|
||||
public const string GetQueueStats = BaseStat + "/rabbitmq-queues/";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Наряды
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using PARR.DAL.Services.Interfaces;
|
||||
|
||||
namespace PARR.API.Contracts.V1.Responses.Statistics
|
||||
{
|
||||
public class StatRabbitMqQueueResponse
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
|
||||
public int Consumers { get; set; }
|
||||
|
||||
public int MessagesTotal { get; set; }
|
||||
}
|
||||
}
|
||||
56
PARR.API/Controllers/V1/Statistics/StatRabbitMqController.cs
Normal file
56
PARR.API/Controllers/V1/Statistics/StatRabbitMqController.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using PARR.API.Contracts.V1;
|
||||
using PARR.API.Contracts.V1.Responses.Base;
|
||||
using PARR.API.Contracts.V1.Responses.Statistics;
|
||||
using PARR.API.Controllers.V1.Base;
|
||||
using PARR.API.Settings;
|
||||
using PARR.BLL.Services.Interfaces;
|
||||
using PARR.Constants;
|
||||
|
||||
namespace PARR.API.Controllers.V1.Statistics
|
||||
{
|
||||
/// <summary>
|
||||
/// Статистика RabbitMQ
|
||||
/// </summary>
|
||||
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
||||
public class StatRabbitMqController : BaseApiController
|
||||
{
|
||||
private readonly IMqService mqService;
|
||||
private readonly MqSettings mqSettings;
|
||||
private readonly ILogger<StatRabbitMqController> logger;
|
||||
|
||||
public StatRabbitMqController(IMqService mqService, MqSettings mqSettings, ILogger<StatRabbitMqController> logger)
|
||||
{
|
||||
this.mqService = mqService;
|
||||
this.mqSettings = mqSettings;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Статистика очередей в RabbitMQ
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet(ApiRoutes.StatRabbitMq.GetQueueStats)]
|
||||
public IActionResult GetQueueStats()
|
||||
{
|
||||
var response = new List<StatRabbitMqQueueResponse>();
|
||||
|
||||
// список очередей
|
||||
foreach (var queue in mqSettings.Statistics.QueueList)
|
||||
{
|
||||
var result = mqService.GetQueueCount(mqSettings.Statistics.StatMqAuth, queue);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
logger.LogError(result.Exception, $"Ошибка при запросе статистики из RabbitMQ для очереди: ${queue}");
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"" } }));
|
||||
}
|
||||
|
||||
response.Add(new StatRabbitMqQueueResponse { Name = queue, Consumers = result.ConsumerCount, MessagesTotal = result.Count });
|
||||
}
|
||||
|
||||
return Ok(new Response<List<StatRabbitMqQueueResponse>>(response.OrderBy(t => t.Name).ToList(), true));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace PARR.API.Settings
|
||||
public class MqSettings
|
||||
{
|
||||
public MqGenerateTemplates GenerateTemplates { get; set; } = new MqGenerateTemplates();
|
||||
public MqStatistics Statistics { get; set; } = new MqStatistics();
|
||||
}
|
||||
|
||||
public class MqGenerateTemplates : IMqSettings
|
||||
@@ -14,4 +15,18 @@ namespace PARR.API.Settings
|
||||
public string User { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class MqStatistics
|
||||
{
|
||||
public List<string> QueueList { get; set; } = new List<string>();
|
||||
public StatMqAuth StatMqAuth { get; set; } = new StatMqAuth();
|
||||
}
|
||||
|
||||
public class StatMqAuth : IMqSettings
|
||||
{
|
||||
public string HostName { get; set; } = string.Empty;
|
||||
public string QueueName { get; set; } = string.Empty;
|
||||
public string User { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,12 @@
|
||||
"MqSettings": {
|
||||
"GenerateTemplates": {
|
||||
"HostName": "10.99.253.216"
|
||||
},
|
||||
"Statistics": {
|
||||
"StatMqAuth": {
|
||||
"HostName": "10.99.253.216"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,14 @@
|
||||
"QueueName": "parr-generate-templates",
|
||||
"User": "generate_templates_api",
|
||||
"Password": "DHhjgdsf*&%95"
|
||||
},
|
||||
"Statistics": {
|
||||
"StatMqAuth": {
|
||||
"HostName": "parr-rabbitmq",
|
||||
"User": "rabbit_stats",
|
||||
"Password": "KJHdgfjHFsdy^&$!ff331"
|
||||
},
|
||||
"QueueList": [ "parr-aihit-data", "parr-espp-schedulers", "parr-espp-templates", "parr-generate-templates", "parr-orders" ]
|
||||
}
|
||||
},
|
||||
"UserCacheSettings": {
|
||||
|
||||
16
PARR.BLL/Domain/Mq/MqQueueCountResult.cs
Normal file
16
PARR.BLL/Domain/Mq/MqQueueCountResult.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace PARR.BLL.Domain.Mq
|
||||
{
|
||||
/// <summary>
|
||||
/// Результат запроса кол-ва сообщений в очереди RabbitMq
|
||||
/// </summary>
|
||||
public class MqQueueCountResult
|
||||
{
|
||||
public bool IsSuccess { get;set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
|
||||
public int ConsumerCount { get; set; }
|
||||
|
||||
public Exception? Exception { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using PARR.BLL.Domain.Mq;
|
||||
using PARR.BLL.Services.Interfaces;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
using System.Runtime;
|
||||
using System.Text;
|
||||
|
||||
namespace PARR.BLL.Services.Implementations
|
||||
@@ -154,6 +155,34 @@ 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();
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace PARR.BLL.Services.Interfaces
|
||||
|
||||
public interface IMqService : IDisposable
|
||||
{
|
||||
MqQueueCountResult GetQueueCount(IMqSettings mqSettings, string queueName);
|
||||
bool InitConsumer(IMqSettings mqSettings, MqMessageHandlerDelegate messageHandler);
|
||||
MqSendResult Send(IMqSettings mqSettings, string[] msgList);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user