Получение списка задач для Хоста по Ip

This commit is contained in:
Mikhail Kuznetsov
2023-06-20 16:54:38 +10:00
parent 37390fa1c8
commit 8747fea111
10 changed files with 274 additions and 29 deletions

View File

@@ -4,5 +4,9 @@
{ {
public string? Ip { get; set; } public string? Ip { get; set; }
public DateTimeOffset? Date { get; set; } public DateTimeOffset? Date { get; set; }
///// <summary>
///// Возвращает jobs которые ещё не выполнены
///// </summary>
//public bool? IsPlanned { get; set; }
} }
} }

View File

@@ -7,7 +7,9 @@ using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.API.Extensions; using PARR.API.Extensions;
using PARR.API.MappingProfiles;
using PARR.API.Services.Interfaces; using PARR.API.Services.Interfaces;
using PARR.DAL.Contracts;
using PARR.DAL.DomainModels; using PARR.DAL.DomainModels;
using PARR.DAL.Models; using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
@@ -23,12 +25,14 @@ namespace PARR.API.Controllers.V1
//private readonly ISchedulerService schedulerService; //private readonly ISchedulerService schedulerService;
private readonly IClientService clientService; private readonly IClientService clientService;
private readonly IHostService hostService; private readonly IHostService hostService;
private readonly IJobJournalService jobJournalService;
public JobController( public JobController(
IMapper mapper, IMapper mapper,
IJobService jobService, IJobService jobService,
IClientService clientService, IClientService clientService,
IHostService hostService IHostService hostService,
IJobJournalService jobJournalService
) )
{ {
this.mapper = mapper; this.mapper = mapper;
@@ -36,6 +40,7 @@ namespace PARR.API.Controllers.V1
//this.schedulerService = schedulerService; //this.schedulerService = schedulerService;
this.clientService = clientService; this.clientService = clientService;
this.hostService = hostService; this.hostService = hostService;
this.jobJournalService = jobJournalService;
} }
@@ -67,7 +72,6 @@ namespace PARR.API.Controllers.V1
return Ok(paginationResponse); return Ok(paginationResponse);
} }
[HttpGet(ApiRoutes.Jobs.GetByIp)] [HttpGet(ApiRoutes.Jobs.GetByIp)]
public async Task<IActionResult> GetByClientIP([FromQuery] JobGetByIpQuery requestQuery) public async Task<IActionResult> GetByClientIP([FromQuery] JobGetByIpQuery requestQuery)
{ {
@@ -84,31 +88,229 @@ namespace PARR.API.Controllers.V1
{ {
var jobsHost = await jobService.Get() var jobsHost = await jobService.Get()
.Include(a => a.Application).ThenInclude(ah => ah.ApplicationsInHosts)/*.ThenInclude(h => h.Host)*/ .Include(a => a.Application).ThenInclude(ah => ah.ApplicationsInHosts)/*.ThenInclude(h => h.Host)*/
.Include(jm=>jm.JobMode) .Include(jm => jm.JobMode)
.AsSplitQuery() .AsSplitQuery()
.Where(j => j.Application != null && j.Application.ApplicationsInHosts.Any(ah => ah.HostId == host.Id)) .Where(j => j.Application != null && j.Application.ApplicationsInHosts.Any(ah => ah.HostId == host.Id) && j.IsEnabled)
.ToListAsync(); .ToListAsync();
if (jobsHost.Any())
hostJobsDict.Add(host, jobsHost);
}
//hostJobsDict.Values.Distinct();
var jobsJoined = new List<Job>(); if (jobsHost.Any())
foreach(var job in hostJobsDict.Values)
{
job.ForEach(item =>
{ {
jobsJoined.Add(item); hostJobsDict.Add(host, jobsHost);
}
}
var jobsList = new List<Job>();
var response = new Dictionary<string, string>();
foreach (var hostAndJobs in hostJobsDict)
{
hostAndJobs.Value.ForEach(async item =>
{
if (!jobsList.Contains(item))
{
jobsList.Add(item);
var plannedJobs = await GetListJobsAsync(item, hostAndJobs.Key, date);
//todo:!!! эти списки склеивать в один, отсортировать по дате и перегнать в дикт
//var baseTime = new DateTimeOffset(date.Year, date.Year, 01, 0, 0, 0, new TimeSpan(0));
////если более суток то идём в JobJournal и получаем дату последнегно запуска. Если нет то приравниваем к StartAt
//var freq = item.Frequency;
//if (true)
//{
//}
//var curTimeCreation = baseTime;
//while(curTimeCreation <= curTimeCreation.EndOfDay())
//{
// curTimeCreation=curTimeCreation.DateTime.AddMinutes(freq);
//}
//kaskjdhaskjkjasklhd
}
}); });
} }
if (!jobsJoined.Any()) // var jobListForResponse = new
return NoContent();
var jobResponse = mapper.Map<List<JobGetAllResponse>>(jobsJoined.Distinct());
return Ok(new Response<List<JobGetAllResponse>>(jobResponse, true, new List<ErrorModel>(), $"{ip},{date}"));
//var dic = new Dictionary<string, string>();
//dic.Add("sss1", "222");
//dic.Add("sss2", "333");
return Ok();
} }
private async Task<List<Job>> GetListJobsAsync(Job job, DAL.Models.Host host, DateTimeOffset queryDate)
{
var jobs = new List<Job>();
// freaq > суток или нет?
//-----------
// меньше суток
//фрек=10 мин
// старт эт = 17:00 и != сегодня
//цикл 17:00 -10мин пока не начнется вчерашний день
//цикл 17:00 +10мин пока не начентся завтрашний день
// елси страт эт == сегодня
//цикл 17:00 +10мин пока не начентся завтрашний день
//-----------
// ------freaq больше суток--------
// журнал берем последнее событие (че там со статусами? не надо пока), к нему пребавляем фрек, и смотрим == сегодня или нет, если сегодня то добавляем
// --------------
var allDayMinutes = 1440;
if (job.Frequency >= allDayMinutes)
{
//больше или = суток
var lastJournalItem = await jobJournalService.GetLastAsync(job.Id, host.Id, queryDate);
if (lastJournalItem == null)
{
// если в истории нет записей, то запускаем сегодня в стартЭт
// берем квери дэйт и ем задаем время из Джобс СтартЭт
var startDateTimeNext = new DateTimeOffset(queryDate.Year, queryDate.Month, queryDate.Day, job.StartAt.Hour, job.StartAt.Minute, job.StartAt.Second, new TimeSpan(0));
//todo:
var jobToAdded = new Job { StartAt = startDateTimeNext };
jobs.Add(jobToAdded);
}
else
{
var startDateNext = lastJournalItem.DateOper.DateTime.AddMinutes(job.Frequency);
if (startDateNext.Date == queryDate.DateTime.Date)
{
// если берем прошлую последнюю операцию из журнала, и к ней прибавить фрекунси, то время следующего старта сдвинется на время выполнения прошлого задания
// чтобы это избежать, берем квери дэйт и ем задаем время из Джобс СтартЭт
var startDateTimeNext = new DateTimeOffset(queryDate.Year, queryDate.Month, queryDate.Day, job.StartAt.Hour, job.StartAt.Minute, job.StartAt.Second, new TimeSpan(0));
//todo:
var jobToAdded = new Job { StartAt = startDateTimeNext };
jobs.Add(jobToAdded);
}
}
}
else
{
//меньше суток
var startAtMinutes = (job.StartAt - job.StartAt.StartOfDay()).TotalMinutes;
// если дата старта != queryDate
if (job.StartAt.DateTime.Date != queryDate.DateTime.Date)
{
// отнимаем пока не наступили прошлые стуки
var min = startAtMinutes;
while (min > 0)
{
//TODO
var jobToAdded = new Job { StartAt = queryDate.StartOfDay().AddMinutes(min) };
jobs.Add(jobToAdded);
min = min - job.Frequency;
}
// прибавляем пока не наступят след сутки
while (min <= allDayMinutes)
{
//TODO
var jobToAdded = new Job { StartAt = queryDate.StartOfDay().AddMinutes(min) };
jobs.Add(jobToAdded);
min = min + job.Frequency;
}
}
else
{
// = queryDate
// прибавляем пока не наступят след сутки
var min = startAtMinutes;
while (min <= allDayMinutes)
{
//TODO
var jobToAdded = new Job { StartAt = queryDate.StartOfDay().AddMinutes(min) };
jobs.Add(jobToAdded);
min = min + job.Frequency;
}
}
}
return jobs;
}
//[HttpGet(ApiRoutes.Jobs.GetByIp)]
//public async Task<IActionResult> GetByClientIP([FromQuery] JobGetByIpQuery requestQuery)
//{
// var ip = requestQuery.Ip ?? clientService.GetClientIp()?.ToString();
// if (string.IsNullOrEmpty(ip))
// return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Client IP address is null." } }));
// var date = requestQuery.Date ?? DateTimeOffset.Now;
// // С одним IP может быть несколько информационных систем, соответственного а таблице храниться несколько записей хостов с одинаковым IP
// var hosts = await hostService.Get().Where(h => h.IP == ip).ToListAsync();
// var hostJobsDict = new Dictionary<DAL.Models.Host, List<Job>>();
// foreach (var host in hosts)
// {
// var jobsHostQuery = jobService.Get()
// .Include(a => a.Application).ThenInclude(ah => ah.ApplicationsInHosts)/*.ThenInclude(h => h.Host)*/
// .Include(jm => jm.JobMode)
// .AsSplitQuery()
// .Where(j => j.Application != null && j.Application.ApplicationsInHosts.Any(ah => ah.HostId == host.Id));
// if (requestQuery.IsPlanned == true)
// {
// // ототбражем только включенные
// jobsHostQuery = jobsHostQuery.Where(t => t.IsEnabled == true);
// }
// var jobsHost = await jobsHostQuery.ToListAsync();
// if (requestQuery.IsPlanned == true)
// {
// // смотрим в истории, если задание уже было запущено, то его не отображаем
// foreach (var jobItem in jobsHost)
// {
// var journalItem = await jobJournalService.GetLastAsync(jobItem.Id, host.Id, date);
// if (JobStatusEnum.started) { }
// }
// }
// if (jobsHost.Any())
// {
// //Проверяем Job вк
// hostJobsDict.Add(host, jobsHost);
// }
// }
// //hostJobsDict.Values.Distinct();
// var jobsJoined = new List<Job>();
// foreach (var job in hostJobsDict.Values)
// {
// job.ForEach(item =>
// {
// jobsJoined.Add(item);
// });
// }
// if (!jobsJoined.Any())
// return NoContent();
// var jobResponse = mapper.Map<List<JobGetAllResponse>>(jobsJoined.Distinct());
// return Ok(new Response<List<JobGetAllResponse>>(jobResponse, true, new List<ErrorModel>(), $"{ip},{date}"));
//}
} }
} }

View File

@@ -0,0 +1,15 @@
namespace PARR.API.Extensions
{
public static class GeneralExtensions
{
public static DateTimeOffset EndOfDay(this DateTimeOffset date)
{
return new DateTimeOffset(date.Year, date.Month, date.Day, 23, 59, 59, 999, new TimeSpan(0));
}
public static DateTimeOffset StartOfDay(this DateTimeOffset date)
{
return new DateTimeOffset(date.Year, date.Month, date.Day, 0, 0, 0, 0, new TimeSpan(0));
}
}
}

View File

@@ -31,16 +31,16 @@ namespace PARR.DAL.Context
modelBuilder.Entity<JobMode>(f => modelBuilder.Entity<JobMode>(f =>
{ {
f.HasData( f.HasData(
new() { Id = new Guid("FBA4202C-5BAD-49C4-B076-D1CA6E1FB158"), DateCreated = dateCreated, DateModified = null, Name = "auto" }, new() { Id = new Guid("FBA4202C-5BAD-49C4-B076-D1CA6E1FB158"), DateCreated = dateCreated, DateModified = null, Name = JobModeEnum.auto.ToString() },
new() { Id = new Guid("7516B952-510D-4AAA-971C-A14B184FC1D5"), DateCreated = dateCreated, DateModified = null, Name = "manual" } new() { Id = new Guid("7516B952-510D-4AAA-971C-A14B184FC1D5"), DateCreated = dateCreated, DateModified = null, Name = JobModeEnum.manual.ToString() }
); );
}); });
modelBuilder.Entity<JobStatus>(f => modelBuilder.Entity<JobStatus>(f =>
{ {
f.HasData( f.HasData(
new() { Id = new Guid("EDEF6DC3-ADAD-4C77-8AD1-63BB9052355A"), DateCreated = dateCreated, DateModified = null, Name = "started", Description = "Задание запущено" }, new() { Id = new Guid("EDEF6DC3-ADAD-4C77-8AD1-63BB9052355A"), DateCreated = dateCreated, DateModified = null, Name = JobStatusEnum.started.ToString(), Description = "Задание запущено" },
new() { Id = new Guid("7B945EB6-D885-4570-AFF5-8866F1F75FF2"), DateCreated = dateCreated, DateModified = null, Name = "finished", Description = "Задание выполнено" } new() { Id = new Guid("7B945EB6-D885-4570-AFF5-8866F1F75FF2"), DateCreated = dateCreated, DateModified = null, Name = JobStatusEnum.finished.ToString(), Description = "Задание выполнено" }
); );
}); });

View File

@@ -0,0 +1,8 @@
namespace PARR.DAL.Contracts
{
public enum JobModeEnum
{
auto,
manual
}
}

View File

@@ -0,0 +1,8 @@
namespace PARR.DAL.Contracts
{
public enum JobStatusEnum
{
started,
finished
}
}

View File

@@ -11,7 +11,7 @@ namespace PARR.DAL.Models
public Guid Id { get; set; } public Guid Id { get; set; }
public DateTimeOffset DateCreated { get; set; } public DateTimeOffset DateCreated { get; set; }
public DateTimeOffset? DateModified { get; set; } public DateTimeOffset? DateModified { get; set; }
public string? DateOper { get; set; } public DateTimeOffset DateOper { get; set; }
public string? Other { get; set; } public string? Other { get; set; }
//Возможно нужно перенести в Job //Возможно нужно перенести в Job
public int? TimeOut { get; set; } public int? TimeOut { get; set; }

View File

@@ -20,6 +20,7 @@ namespace PARR.DAL
services.AddTransient<IApplicationTypeService, ApplicationTypeService>(); services.AddTransient<IApplicationTypeService, ApplicationTypeService>();
services.AddTransient<IApplicationInHostService, ApplicationInHostService>(); services.AddTransient<IApplicationInHostService, ApplicationInHostService>();
services.AddTransient<IJobService, JobService>(); services.AddTransient<IJobService, JobService>();
services.AddTransient<IJobJournalService, JobJournalService>();
//services.AddTransient<ISchedulerService, SchedulerService>(); //services.AddTransient<ISchedulerService, SchedulerService>();
//services.AddTransient<IAreasInLayerService, AreasInLayerService>(); //services.AddTransient<IAreasInLayerService, AreasInLayerService>();

View File

@@ -20,5 +20,15 @@ namespace PARR.DAL.Services.Implementations
protected override DbSet<JobJournal> EntitySet => dataContext.JobJournals; protected override DbSet<JobJournal> EntitySet => dataContext.JobJournals;
protected override DataContext EntitiContext => dataContext; protected override DataContext EntitiContext => dataContext;
public async Task<JobJournal?> GetLastAsync(Guid jobId, Guid hostId, DateTimeOffset date)
{
var opers = await EntitySet
.Where(j => j.JobId == jobId && j.HostId == hostId && j.DateOper.DateTime.Date == date.DateTime.Date)
.OrderBy(t => t.DateOper)
.LastOrDefaultAsync();
return opers;
}
} }
} }

View File

@@ -1,12 +1,9 @@
using System; using PARR.DAL.Models;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PARR.DAL.Services.Interfaces namespace PARR.DAL.Services.Interfaces
{ {
internal interface IJobJournalService public interface IJobJournalService
{ {
Task<JobJournal?> GetLastAsync(Guid jobId, Guid hostId, DateTimeOffset date);
} }
} }