feat(schedules): switched how periods were parsed to allow for "2 weeks"
This commit is contained in:
parent
2892ec3445
commit
189273692c
5 changed files with 38 additions and 46 deletions
|
@ -10,15 +10,17 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac" Version="6.4.0"/>
|
||||
<PackageReference Include="MfGames.Gallium" Version="0.4.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0"/>
|
||||
<PackageReference Include="Serilog" Version="2.11.0"/>
|
||||
<PackageReference Include="Autofac" Version="6.4.0" />
|
||||
<PackageReference Include="MfGames.Gallium" Version="0.4.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
|
||||
<PackageReference Include="Serilog" Version="2.11.0" />
|
||||
<PackageReference Include="TimeSpanParserUtil" Version="1.2.0" />
|
||||
<PackageReference Include="YamlDotNet" Version="12.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MfGames.Nitride.Temporal\MfGames.Nitride.Temporal.csproj"/>
|
||||
<ProjectReference Include="..\MfGames.Nitride\MfGames.Nitride.csproj"/>
|
||||
<ProjectReference Include="..\MfGames.Nitride.Temporal\MfGames.Nitride.Temporal.csproj" />
|
||||
<ProjectReference Include="..\MfGames.Nitride\MfGames.Nitride.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Include the source generator -->
|
||||
|
|
|
@ -6,6 +6,8 @@ using MfGames.Nitride.Generators;
|
|||
|
||||
using NodaTime;
|
||||
|
||||
using TimeSpanParserUtil;
|
||||
|
||||
using Zio;
|
||||
|
||||
namespace MfGames.Nitride.Temporal.Schedules;
|
||||
|
@ -48,10 +50,26 @@ public partial class NumericalPathSchedule : ISchedule
|
|||
public Regex? PathRegex { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the period between each matching entity. More precisely,
|
||||
/// the schedule will be TimeSpan * (CaptureGroup + CaptureOffset).
|
||||
/// Gets or sets the period between each matching entity. The period is
|
||||
/// 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>
|
||||
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>
|
||||
/// Gets or sets when the first item is scheduled.
|
||||
|
@ -81,20 +99,10 @@ public partial class NumericalPathSchedule : ISchedule
|
|||
+ this.CaptureOffset;
|
||||
|
||||
// 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
|
||||
?? throw new NullReferenceException(
|
||||
"Cannot use a schedule without a start date.");
|
||||
DateTime when = start + span * number;
|
||||
DateTime when = start + this.SchedulePeriodTimeSpan * number;
|
||||
Instant instant = timekeeper.CreateInstant(when);
|
||||
|
||||
// If the time hasn't past, then we don't apply it.
|
||||
|
|
|
@ -85,7 +85,8 @@ var schedules = new List<ISchedule>
|
|||
{
|
||||
PathRegex = "chapter-(\d+),
|
||||
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`
|
||||
- The date that the schedule starts.
|
||||
- No default.
|
||||
- `SchedulePeriod SchedulePeriod`
|
||||
- Values:
|
||||
- Instant (meaning everything at once, default)
|
||||
- Week
|
||||
- Day
|
||||
- `string SchedulePeriod`
|
||||
- Parsed using https://github.com/pengowray/TimeSpanParser
|
||||
- Supports any `TimeSpan` value, also "2 weeks" and Humanizer formatted values
|
||||
- `TimeSpan SchedulePeriodTimeSpan`
|
||||
- Parsed from `SchedulePeriod`
|
||||
- `int CaptureGroup`
|
||||
- The numerical index of the capture group.
|
||||
- Defaults to `1` because the first match in Regex is 1.
|
||||
|
|
|
@ -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,
|
||||
}
|
|
@ -211,7 +211,7 @@ public class NumericalPathScheduleTests : TemporalSchedulesTestBase
|
|||
{
|
||||
public TestSchedule()
|
||||
{
|
||||
this.SchedulePeriod = SchedulePeriod.Week;
|
||||
this.SchedulePeriod = "1 week";
|
||||
}
|
||||
|
||||
public string? Access { get; set; }
|
||||
|
|
Reference in a new issue