MireaBackend/Endpoint/Common/Services/CronUpdateSkipService.cs

78 lines
3.0 KiB
C#
Raw Normal View History

2025-02-02 03:30:52 +03:00
using Cronos;
using Mirea.Api.Endpoint.Configuration.Model.GeneralSettings;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Mirea.Api.Endpoint.Common.Services;
public static class CronUpdateSkipService
{
public static ScheduleSettings.CronUpdateSkip Get(this Dto.Common.CronUpdateSkip date)
{
if (date.Date.HasValue)
return new ScheduleSettings.CronUpdateSkip(date.Date.Value);
if (date is { Start: not null, End: not null })
return new ScheduleSettings.CronUpdateSkip(date.Start.Value, date.End.Value);
throw new ArgumentException("It is impossible to create a structure because it has incorrect values.");
}
2025-02-02 20:28:04 +03:00
public static List<ScheduleSettings.CronUpdateSkip> FilterDateEntry(this List<ScheduleSettings.CronUpdateSkip> data, DateOnly? currentDate = null)
2025-02-02 03:30:52 +03:00
{
currentDate ??= DateOnly.FromDateTime(DateTime.Now);
return data.OrderBy(x => x.End ?? x.Date)
.Where(x => x.Date == currentDate || (x.Start <= currentDate && x.End >= currentDate))
.ToList();
}
2025-02-02 20:28:04 +03:00
public static List<ScheduleSettings.CronUpdateSkip> FilterDateEntry(this List<ScheduleSettings.CronUpdateSkip> data, DateTime? currentDate = null) =>
data.FilterDateEntry(DateOnly.FromDateTime(currentDate ?? DateTime.Now));
public static List<ScheduleSettings.CronUpdateSkip> Filter(this List<ScheduleSettings.CronUpdateSkip> data, DateOnly? currentDate = null)
{
currentDate ??= DateOnly.FromDateTime(DateTime.Now);
return data.Where(x => x.Date >= currentDate || x.End >= currentDate)
.OrderBy(x => x.End ?? x.Date)
.ToList();
}
2025-02-06 16:27:20 +03:00
2025-02-02 03:30:52 +03:00
public static List<DateTimeOffset> GetNextTask(this List<ScheduleSettings.CronUpdateSkip> data,
CronExpression expression, int depth = 1, DateOnly? currentDate = null)
{
if (depth <= 0)
return [];
2025-02-11 16:35:56 +03:00
DateTimeOffset nextRunTime = (currentDate?.ToDateTime(TimeOnly.MinValue) ?? DateTime.Now).ToUniversalTime();
2025-02-02 03:30:52 +03:00
List<DateTimeOffset> result = [];
do
{
2025-02-11 16:35:56 +03:00
var lastSkippedEntry = data.FilterDateEntry(nextRunTime.DateTime).LastOrDefault();
2025-02-02 03:30:52 +03:00
2025-02-11 16:35:56 +03:00
if (lastSkippedEntry is { Start: not null, End: not null })
nextRunTime = lastSkippedEntry.End.Value.ToDateTime(TimeOnly.MinValue).AddDays(1);
else if (lastSkippedEntry.Date.HasValue)
nextRunTime = lastSkippedEntry.Date.Value.ToDateTime(TimeOnly.MinValue).AddDays(1);
2025-02-02 03:30:52 +03:00
2025-02-11 16:35:56 +03:00
var nextOccurrence = expression.GetNextOccurrence(nextRunTime.AddMinutes(-1), TimeZoneInfo.Local);
2025-02-02 03:30:52 +03:00
2025-02-11 16:35:56 +03:00
if (!nextOccurrence.HasValue)
2025-02-02 03:30:52 +03:00
return result;
2025-02-11 16:35:56 +03:00
if (data.FilterDateEntry(nextOccurrence.Value.DateTime).Count != 0)
{
nextRunTime = nextOccurrence.Value.AddDays(1);
2025-02-02 03:30:52 +03:00
continue;
2025-02-11 16:35:56 +03:00
}
2025-02-02 03:30:52 +03:00
2025-02-11 16:35:56 +03:00
result.Add(nextOccurrence.Value.ToLocalTime());
nextRunTime = nextOccurrence.Value.AddMinutes(1);
2025-02-02 03:30:52 +03:00
} while (result.Count < depth);
return result;
}
}