feat(api): статистика за период о выполнении заданий роботами
This commit is contained in:
@@ -184,6 +184,7 @@
|
|||||||
public static class StatRobotTask
|
public static class StatRobotTask
|
||||||
{
|
{
|
||||||
public const string Get = BaseStat + "/robot-tasks/";
|
public const string Get = BaseStat + "/robot-tasks/";
|
||||||
|
public const string GetPeriodStatistics = BaseStat + "/robot-tasks/{robot}/period/";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class StatRobotStatus
|
public static class StatRobotStatus
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
namespace PARR.API.Contracts.V1.Responses.Statistics
|
||||||
|
{
|
||||||
|
public class StatRobotTaskPeriodResponse
|
||||||
|
{
|
||||||
|
public DateTimeOffset Date { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Робот взял заданий на создание
|
||||||
|
/// </summary>
|
||||||
|
public int Creating { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Взял заданий на обновление
|
||||||
|
/// </summary>
|
||||||
|
public int Updating { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Робот успешно выполнил заданий
|
||||||
|
/// </summary>
|
||||||
|
public int Ok { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ошибок
|
||||||
|
/// </summary>
|
||||||
|
public int Errors { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,12 +2,14 @@
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using NodaTime.Extensions;
|
||||||
using PARR.API.Contracts.V1;
|
using PARR.API.Contracts.V1;
|
||||||
using PARR.API.Contracts.V1.Responses;
|
using PARR.API.Contracts.V1.Responses;
|
||||||
using PARR.API.Contracts.V1.Responses.Base;
|
using PARR.API.Contracts.V1.Responses.Base;
|
||||||
using PARR.API.Contracts.V1.Responses.Statistics;
|
using PARR.API.Contracts.V1.Responses.Statistics;
|
||||||
using PARR.API.Controllers.V1.Base;
|
using PARR.API.Controllers.V1.Base;
|
||||||
using PARR.Constants;
|
using PARR.Constants;
|
||||||
|
using PARR.DAL.Contracts;
|
||||||
using PARR.DAL.Services.Interfaces;
|
using PARR.DAL.Services.Interfaces;
|
||||||
|
|
||||||
namespace PARR.API.Controllers.V1.Statistics
|
namespace PARR.API.Controllers.V1.Statistics
|
||||||
@@ -21,18 +23,21 @@ namespace PARR.API.Controllers.V1.Statistics
|
|||||||
private readonly IRobotService robotService;
|
private readonly IRobotService robotService;
|
||||||
private readonly IRobotConfigurationService robotConfigurationService;
|
private readonly IRobotConfigurationService robotConfigurationService;
|
||||||
private readonly ITaskStatusService taskStatusService;
|
private readonly ITaskStatusService taskStatusService;
|
||||||
|
private readonly IRobotHistoryService robotHistoryService;
|
||||||
private readonly IMapper mapper;
|
private readonly IMapper mapper;
|
||||||
|
|
||||||
public StatRobotTaskController(
|
public StatRobotTaskController(
|
||||||
IRobotService robotService,
|
IRobotService robotService,
|
||||||
IRobotConfigurationService robotConfigurationService,
|
IRobotConfigurationService robotConfigurationService,
|
||||||
ITaskStatusService taskStatusService,
|
ITaskStatusService taskStatusService,
|
||||||
|
IRobotHistoryService robotHistoryService,
|
||||||
IMapper mapper
|
IMapper mapper
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
this.robotService = robotService;
|
this.robotService = robotService;
|
||||||
this.robotConfigurationService = robotConfigurationService;
|
this.robotConfigurationService = robotConfigurationService;
|
||||||
this.taskStatusService = taskStatusService;
|
this.taskStatusService = taskStatusService;
|
||||||
|
this.robotHistoryService = robotHistoryService;
|
||||||
this.mapper = mapper;
|
this.mapper = mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,5 +77,92 @@ namespace PARR.API.Controllers.V1.Statistics
|
|||||||
|
|
||||||
return Ok(new Response<List<StatRobotTaskResponse>>(response, true));
|
return Ok(new Response<List<StatRobotTaskResponse>>(response, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Статистика по заданиям для роботов за период
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="robot"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet(ApiRoutes.StatRobotTask.GetPeriodStatistics)]
|
||||||
|
public async Task<IActionResult> GetPeriodStatistics([FromRoute] RobotsEnum robot)
|
||||||
|
{
|
||||||
|
int minusHour = 24;
|
||||||
|
var queryDate = DateTimeOffset.UtcNow.AddHours(-minusHour);
|
||||||
|
//queryDate = queryDate.Date + new TimeSpan(queryDate.Hour, 0, 0);
|
||||||
|
//queryDate=queryDate.ToOffset(TimeSpan.Zero);
|
||||||
|
|
||||||
|
|
||||||
|
var query = robotHistoryService.Get()
|
||||||
|
.Include(t => t.RobotConfiguration)
|
||||||
|
.Where(t => t.RobotConfiguration!.RobotCode == (int)robot && t.DateCreated >= queryDate)
|
||||||
|
.GroupBy(t => new { Date = t.DateCreated.Date, Hour = t.DateCreated.Hour })
|
||||||
|
.Select(t => new
|
||||||
|
{
|
||||||
|
Date = t.Key,
|
||||||
|
Creating = t.Count(c => c.TaskStatusCode == (int)TaskStatusEnum.Creating),
|
||||||
|
Updating = t.Count(c => c.TaskStatusCode == (int)TaskStatusEnum.Updating),
|
||||||
|
Ok = t.Count(c => c.TaskStatusCode == (int)TaskStatusEnum.Ok),
|
||||||
|
Errors = t.Count(c => c.HistoryLevel == (int)RobotHistoryLevelEnum.Error),
|
||||||
|
});
|
||||||
|
|
||||||
|
var statistics = await query.ToListAsync();
|
||||||
|
|
||||||
|
if (!statistics.Any())
|
||||||
|
return NoContent();
|
||||||
|
|
||||||
|
var periodList = GetPeriodList(queryDate, DateTimeOffset.UtcNow);
|
||||||
|
|
||||||
|
//var response = statistics.Select(t => new StatRobotTaskPeriodResponse
|
||||||
|
//{
|
||||||
|
// Date = new DateTimeOffset(t.Date.Date.Year, t.Date.Date.Month, t.Date.Date.Day, t.Date.Hour, 0, 0, new TimeSpan(0)),
|
||||||
|
// Creating = t.Creating,
|
||||||
|
// Errors = t.Errors,
|
||||||
|
// Ok = t.Ok,
|
||||||
|
// Updating = t.Updating
|
||||||
|
//}).OrderBy(t => t.Date).ToList();
|
||||||
|
|
||||||
|
periodList.ForEach(item =>
|
||||||
|
{
|
||||||
|
var statItem = statistics.FirstOrDefault(t => t.Date.Date == item.Date.Date && t.Date.Hour == item.Date.Hour);
|
||||||
|
if (statItem != null)
|
||||||
|
{
|
||||||
|
item.Errors = statItem.Errors;
|
||||||
|
item.Creating = statItem.Creating;
|
||||||
|
item.Updating = statItem.Updating;
|
||||||
|
item.Ok = statItem.Ok;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return Ok(new Response<List<StatRobotTaskPeriodResponse>>(periodList, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<StatRobotTaskPeriodResponse> GetPeriodList(DateTimeOffset start, DateTimeOffset end)
|
||||||
|
{
|
||||||
|
var periodList = new List<StatRobotTaskPeriodResponse>();
|
||||||
|
|
||||||
|
if (end < start)
|
||||||
|
return periodList;
|
||||||
|
|
||||||
|
var periodDate = start;
|
||||||
|
|
||||||
|
while (periodDate <= end)
|
||||||
|
{
|
||||||
|
periodList.Add(new StatRobotTaskPeriodResponse
|
||||||
|
{
|
||||||
|
Creating = 0,
|
||||||
|
Errors = 0,
|
||||||
|
Ok = 0,
|
||||||
|
Updating = 0,
|
||||||
|
Date = new DateTimeOffset(periodDate.Date.Year, periodDate.Date.Month, periodDate.Date.Day, periodDate.Hour, 0, 0, new TimeSpan(0)),
|
||||||
|
});
|
||||||
|
|
||||||
|
periodDate = periodDate.AddHours(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return periodList;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user