feat(api): статистика за период о выполнении заданий роботами
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NodaTime.Extensions;
|
||||
using PARR.API.Contracts.V1;
|
||||
using PARR.API.Contracts.V1.Responses;
|
||||
using PARR.API.Contracts.V1.Responses.Base;
|
||||
using PARR.API.Contracts.V1.Responses.Statistics;
|
||||
using PARR.API.Controllers.V1.Base;
|
||||
using PARR.Constants;
|
||||
using PARR.DAL.Contracts;
|
||||
using PARR.DAL.Services.Interfaces;
|
||||
|
||||
namespace PARR.API.Controllers.V1.Statistics
|
||||
@@ -21,18 +23,21 @@ namespace PARR.API.Controllers.V1.Statistics
|
||||
private readonly IRobotService robotService;
|
||||
private readonly IRobotConfigurationService robotConfigurationService;
|
||||
private readonly ITaskStatusService taskStatusService;
|
||||
private readonly IRobotHistoryService robotHistoryService;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public StatRobotTaskController(
|
||||
IRobotService robotService,
|
||||
IRobotConfigurationService robotConfigurationService,
|
||||
ITaskStatusService taskStatusService,
|
||||
IRobotHistoryService robotHistoryService,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
this.robotService = robotService;
|
||||
this.robotConfigurationService = robotConfigurationService;
|
||||
this.taskStatusService = taskStatusService;
|
||||
this.robotHistoryService = robotHistoryService;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@@ -72,5 +77,92 @@ namespace PARR.API.Controllers.V1.Statistics
|
||||
|
||||
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