feat(schedules): switched how periods were parsed to allow for "2 weeks"
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
D. Moonfire 2023-01-18 14:27:12 -06:00
parent 2892ec3445
commit 189273692c
5 changed files with 38 additions and 46 deletions

View file

@ -14,6 +14,8 @@
<PackageReference Include="MfGames.Gallium" Version="0.4.0" /> <PackageReference Include="MfGames.Gallium" Version="0.4.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Serilog" Version="2.11.0" /> <PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="TimeSpanParserUtil" Version="1.2.0" />
<PackageReference Include="YamlDotNet" Version="12.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -6,6 +6,8 @@ using MfGames.Nitride.Generators;
using NodaTime; using NodaTime;
using TimeSpanParserUtil;
using Zio; using Zio;
namespace MfGames.Nitride.Temporal.Schedules; namespace MfGames.Nitride.Temporal.Schedules;
@ -48,10 +50,26 @@ public partial class NumericalPathSchedule : ISchedule
public Regex? PathRegex { get; set; } public Regex? PathRegex { get; set; }
/// <summary> /// <summary>
/// Gets or sets the period between each matching entity. More precisely, /// Gets or sets the period between each matching entity. The period is
/// the schedule will be TimeSpan * (CaptureGroup + CaptureOffset). /// amount of time between each item based on the number. So the first will
/// be on the ScheduleDate, the second SchedulePeriod later, the third
/// SchedulePeriod after the second, etc. More precisely, the date for
/// any item TimeSpan * ((int)CaptureGroup + (int)CaptureOffset).
/// </summary> /// </summary>
public SchedulePeriod SchedulePeriod { get; set; } public string? SchedulePeriod { get; set; }
/// <summary>
/// Gets or sets the schedule period as a TimeSpan object. This is converted
/// from SchedulePeriod using https://github.com/pengowray/TimeSpanParser.
/// If the value is null, then this will be instant (TimeSpan.Zero).
/// </summary>
public virtual TimeSpan SchedulePeriodTimeSpan
{
get => this.SchedulePeriod == null
? TimeSpan.Zero
: TimeSpanParser.Parse(this.SchedulePeriod);
set => this.SchedulePeriod = value.ToString();
}
/// <summary> /// <summary>
/// Gets or sets when the first item is scheduled. /// Gets or sets when the first item is scheduled.
@ -81,20 +99,10 @@ public partial class NumericalPathSchedule : ISchedule
+ this.CaptureOffset; + this.CaptureOffset;
// Figure out the time from the start. // Figure out the time from the start.
TimeSpan span = this.SchedulePeriod switch
{
SchedulePeriod.Instant => TimeSpan.Zero,
SchedulePeriod.Day => TimeSpan.FromDays(1),
SchedulePeriod.Week => TimeSpan.FromDays(7),
_ => throw new InvalidOperationException(
"Cannot parse schedule period from "
+ this.SchedulePeriod
+ "."),
};
DateTime start = this.ScheduleStart DateTime start = this.ScheduleStart
?? throw new NullReferenceException( ?? throw new NullReferenceException(
"Cannot use a schedule without a start date."); "Cannot use a schedule without a start date.");
DateTime when = start + span * number; DateTime when = start + this.SchedulePeriodTimeSpan * number;
Instant instant = timekeeper.CreateInstant(when); Instant instant = timekeeper.CreateInstant(when);
// If the time hasn't past, then we don't apply it. // If the time hasn't past, then we don't apply it.

View file

@ -85,7 +85,8 @@ var schedules = new List<ISchedule>
{ {
PathRegex = "chapter-(\d+), PathRegex = "chapter-(\d+),
ScheduleStart = DateTime.Parse("2023-01-01"), ScheduleStart = DateTime.Parse("2023-01-01"),
SchedulePeriod = SchedulePeriod.Week, SchedulePeriod = "1 week",
// Alternatively, SchedulePeriodTimeSpan = TimeSpan.FromDays(7),
}, },
} }
@ -106,11 +107,11 @@ chapter will be set to 2023-01-08, and the third at 2023-01-15.
- `DateTime ScheduleStart` - `DateTime ScheduleStart`
- The date that the schedule starts. - The date that the schedule starts.
- No default. - No default.
- `SchedulePeriod SchedulePeriod` - `string SchedulePeriod`
- Values: - Parsed using https://github.com/pengowray/TimeSpanParser
- Instant (meaning everything at once, default) - Supports any `TimeSpan` value, also "2 weeks" and Humanizer formatted values
- Week - `TimeSpan SchedulePeriodTimeSpan`
- Day - Parsed from `SchedulePeriod`
- `int CaptureGroup` - `int CaptureGroup`
- The numerical index of the capture group. - The numerical index of the capture group.
- Defaults to `1` because the first match in Regex is 1. - Defaults to `1` because the first match in Regex is 1.

View file

@ -1,19 +0,0 @@
namespace MfGames.Nitride.Temporal.Schedules;
public enum SchedulePeriod
{
/// <summary>
/// Indicates that all the matching schedule periods are instant (zero time).
/// </summary>
Instant,
/// <summary>
/// Indicates that the entities will be scheduled a day apart.
/// </summary>
Day,
/// <summary>
/// Indicates that the entities will be scheduled seven days apart.
/// </summary>
Week,
}

View file

@ -211,7 +211,7 @@ public class NumericalPathScheduleTests : TemporalSchedulesTestBase
{ {
public TestSchedule() public TestSchedule()
{ {
this.SchedulePeriod = SchedulePeriod.Week; this.SchedulePeriod = "1 week";
} }
public string? Access { get; set; } public string? Access { get; set; }