feat(api): статистика по заданиям для роботов и роботам.
This commit is contained in:
@@ -22,6 +22,11 @@ namespace PARR.API.Contracts.V1
|
||||
|
||||
public const string Base = Root + "/" + Version;
|
||||
|
||||
/// <summary>
|
||||
/// Статистика
|
||||
/// </summary>
|
||||
public const string BaseStat = Base + "/statistics";
|
||||
|
||||
public static class ApiStatus
|
||||
{
|
||||
public const string Version = Base + "/version";
|
||||
@@ -160,6 +165,20 @@ namespace PARR.API.Contracts.V1
|
||||
|
||||
#endregion
|
||||
|
||||
#region Статистика
|
||||
|
||||
public static class StatRobotTask
|
||||
{
|
||||
public const string Get = BaseStat + "/robot-tasks/";
|
||||
}
|
||||
|
||||
public static class StatRobotStatus
|
||||
{
|
||||
public const string Get = BaseStat + "/robot-statuses/";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace PARR.API.Contracts.V1.Responses.Statistics
|
||||
{
|
||||
public class StatRobotStatusResponse
|
||||
{
|
||||
public RobotResponse? Robot { get; set; }
|
||||
public List<StatRobotStatusItemResponse> Statistics { get; set; } = new List<StatRobotStatusItemResponse>();
|
||||
}
|
||||
|
||||
public class StatRobotStatusItemResponse
|
||||
{
|
||||
public RobotStatusResponse? Status { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace PARR.API.Contracts.V1.Responses.Statistics
|
||||
{
|
||||
public class StatRobotTaskResponse
|
||||
{
|
||||
public RobotResponse? Robot { get; set; }
|
||||
public List<StatRobotTasksResponse> Statistics { get; set; } = new List<StatRobotTasksResponse>();
|
||||
}
|
||||
|
||||
public class StatRobotTasksResponse
|
||||
{
|
||||
public TaskStatusResponse? Status { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
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.Services.Interfaces;
|
||||
|
||||
namespace PARR.API.Controllers.V1.Statistics
|
||||
{
|
||||
/// <summary>
|
||||
/// Статистика выполняения заданий роботами, по полю RobotConfigurations.RobotStatusCode
|
||||
/// </summary>
|
||||
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
||||
public class StatRobotStatusController : BaseApiController
|
||||
{
|
||||
private readonly IRobotService robotService;
|
||||
private readonly IRobotConfigurationService robotConfigurationService;
|
||||
private readonly IRobotStatusService robotStatusService;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public StatRobotStatusController(
|
||||
IRobotService robotService,
|
||||
IRobotConfigurationService robotConfigurationService,
|
||||
IRobotStatusService robotStatusService,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
this.robotService = robotService;
|
||||
this.robotConfigurationService = robotConfigurationService;
|
||||
this.robotStatusService = robotStatusService;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Статистика работы роботов
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet(ApiRoutes.StatRobotStatus.Get)]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
var confStat = await robotConfigurationService.Get()
|
||||
.GroupBy(t => new { t.RobotCode, t.RobotStatusCode })
|
||||
.Select(t => new { RobotCode = t.Key.RobotCode, RobotStatusCode = t.Key.RobotStatusCode, Count = t.Count() })
|
||||
.ToListAsync();
|
||||
|
||||
var robots = await robotService.Get().ToListAsync();
|
||||
var robotStatuses = await robotStatusService.Get().ToListAsync();
|
||||
|
||||
var response = new List<StatRobotStatusResponse>();
|
||||
|
||||
foreach (var robot in robots)
|
||||
{
|
||||
var item = new StatRobotStatusResponse { Robot = mapper.Map<RobotResponse>(robot) };
|
||||
robotStatuses.ForEach(stat =>
|
||||
{
|
||||
item.Statistics.Add(new StatRobotStatusItemResponse
|
||||
{
|
||||
Status = mapper.Map<RobotStatusResponse>(stat),
|
||||
Count = confStat.FirstOrDefault(t => t.RobotStatusCode == stat.Code && t.RobotCode == robot.Code)?.Count ?? 0
|
||||
});
|
||||
});
|
||||
item.Statistics = item.Statistics.OrderByDescending(t => t.Status?.Code).ToList();
|
||||
response.Add(item);
|
||||
}
|
||||
|
||||
response = response.OrderBy(t => t.Robot?.Code).ToList();
|
||||
|
||||
return Ok(new Response<List<StatRobotStatusResponse>>(response, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
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.Services.Interfaces;
|
||||
|
||||
namespace PARR.API.Controllers.V1.Statistics
|
||||
{
|
||||
/// <summary>
|
||||
/// Статистика по заданиям для робота, по полю RobotConfigurations.TaskStatusCode
|
||||
/// </summary>
|
||||
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
||||
public class StatRobotTaskController : BaseApiController
|
||||
{
|
||||
private readonly IRobotService robotService;
|
||||
private readonly IRobotConfigurationService robotConfigurationService;
|
||||
private readonly ITaskStatusService taskStatusService;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public StatRobotTaskController(
|
||||
IRobotService robotService,
|
||||
IRobotConfigurationService robotConfigurationService,
|
||||
ITaskStatusService taskStatusService,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
this.robotService = robotService;
|
||||
this.robotConfigurationService = robotConfigurationService;
|
||||
this.taskStatusService = taskStatusService;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Статистика по заданиям для роботов
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet(ApiRoutes.StatRobotTask.Get)]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
var confStat = await robotConfigurationService.Get()
|
||||
.GroupBy(t => new { t.RobotCode, t.TaskStatusCode })
|
||||
.Select(t => new { RobotCode = t.Key.RobotCode, TaskStatusCode = t.Key.TaskStatusCode, Count = t.Count() })
|
||||
.ToListAsync();
|
||||
|
||||
var robots = await robotService.Get().ToListAsync();
|
||||
var statuses = await taskStatusService.Get().ToListAsync();
|
||||
|
||||
var response = new List<StatRobotTaskResponse>();
|
||||
|
||||
foreach (var robot in robots)
|
||||
{
|
||||
var item = new StatRobotTaskResponse { Robot = mapper.Map<RobotResponse>(robot) };
|
||||
statuses.ForEach(stat =>
|
||||
{
|
||||
item.Statistics.Add(new StatRobotTasksResponse
|
||||
{
|
||||
Status = mapper.Map<TaskStatusResponse>(stat),
|
||||
Count = confStat.FirstOrDefault(t => t.TaskStatusCode == stat.Code && t.RobotCode == robot.Code)?.Count ?? 0
|
||||
});
|
||||
});
|
||||
item.Statistics = item.Statistics.OrderBy(t => t.Status?.Code).ToList();
|
||||
response.Add(item);
|
||||
}
|
||||
|
||||
response = response.OrderBy(t => t.Robot?.Code).ToList();
|
||||
|
||||
return Ok(new Response<List<StatRobotTaskResponse>>(response, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user