itfx/src/InterfaceForward.Application/Services/App/LogService.cs
2024-05-21 00:20:28 +09:00

107 lines
3.9 KiB
C#

using InterfaceForward.Domain.Shared.Dtos;
using InterfaceForward.Repositories.Log.Entitys;
using InterfaceForward.Repositories.Log.Services;
using InterfaceForward.Repositories.Log.ValueObjects;
using Microsoft.AspNetCore.Mvc;
using SqlSugar.Extensions;
namespace InterfaceForward.Application.Services.App
{
/// <summary>
/// 日志服务
/// </summary>
[Route("Log")]
[ApiExplorerSettings(GroupName = "应用服务")]
public class LogService : BaseService
{
private readonly ILogRepository _logRepository;
/// <summary>
///
/// </summary>
public LogService(
ILogRepository logRepository)
{
_logRepository = logRepository;
}
#region Get
/// <summary>
/// 列表
/// </summary>
/// <param name="input">入参</param>
/// <returns></returns>
[HttpGet("GetList")]
public async Task<PagedListDto<LogIndex>> GetList(LogGetListInputValueObject input)
{
var res = await _logRepository.GetListAsync(input);
return new PagedListDto<LogIndex>(res.list, (int)res.total);
}
/// <summary>
/// 调用统计
/// </summary>
/// <param name="input">入参</param>
/// <returns></returns>
[HttpGet("GetLogsStatistics")]
public async Task<PagedListDto<LogGetListOutputValueObject>> GetLogsStatistics(LogStatisticsInputValueObject input)
{
var esData = await _logRepository.GetLogsStatisticsAsync(input);
var list = new List<LogGetListOutputValueObject>();
var time_aggs = esData.Aggregations.DateHistogram("time_aggs").Buckets;
foreach (var _time_aggs in time_aggs)
{
string time_date = _time_aggs.KeyAsString;
var name_aggs = _time_aggs.Terms("name_aggs").Buckets;
foreach (var _name_aggs in name_aggs)
{
string name = _name_aggs.Key;
var interfacename_aggs = _name_aggs.Terms("interfacename_aggs").Buckets;
foreach (var _interfacename_aggs in interfacename_aggs)
{
string interfaceName = _interfacename_aggs.Key;
int interfaceName_docCount = _interfacename_aggs.DocCount.GetValueOrDefault().ObjToInt();
var isSuccess_count = _interfacename_aggs.Terms("isSuccess_count").Buckets;
var dic = isSuccess_count.ToDictionary(b => b.KeyAsString, b => b.DocCount.ObjToInt());
int succeedCount = dic.GetValueOrDefault("true");
list.Add(new LogGetListOutputValueObject
{
Date = time_date,
Name = name,
InterfaceName = interfaceName,
Total = interfaceName_docCount,
SucceedCount = succeedCount,
FailCount = dic.GetValueOrDefault("false"),
SuccessRate = $"{Math.Round((decimal)succeedCount / interfaceName_docCount * 100, 2)}%"
});
}
}
}
var res = list.OrderByDescending(x => x.Date)
.Skip((input.PageIndex - 1) * input.PageSize)
.Take(input.PageSize).ToList();
return new PagedListDto<LogGetListOutputValueObject>(res, list.Count);
}
/// <summary>
/// 查询详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("GetById")]
public async Task<LogIndex?> GetByIdAsync(string id)
{
return await _logRepository.GetDetailsByIdAsync(id);
}
#endregion
}
}