| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System.Globalization;
- using System.Text.RegularExpressions;
- namespace Admin.NET.Plugin.AiDOP.DataPlatform.Executors;
- /// <summary>
- /// P-015:解析调度同步窗口(FULL / INCR / ROLLING),供抽数 SQL 使用。
- /// </summary>
- public static class MdpSyncWindowResolver
- {
- private static readonly Regex DurationRegex = new(
- @"^\s*(?<n>\d+)\s*(?<u>[dDhHmM])\s*$",
- RegexOptions.Compiled);
- /// <summary>
- /// 将窗口类型落到上下文:FULL→全量;INCR→水位;ROLLING→下界时间。
- /// 显式 FullRefresh 时强制 FULL。
- /// </summary>
- public static void Apply(MdpPullContext ctx)
- {
- if (ctx.FullRefresh)
- {
- ctx.SyncWindowType = "FULL";
- ctx.WindowFrom = null;
- return;
- }
- var type = (ctx.SyncWindowType ?? "INCR").Trim().ToUpperInvariant();
- if (type is "ROLLING_WINDOW") type = "ROLLING";
- switch (type)
- {
- case "FULL":
- ctx.SyncWindowType = "FULL";
- ctx.FullRefresh = true;
- ctx.WindowFrom = null;
- break;
- case "ROLLING":
- ctx.SyncWindowType = "ROLLING";
- ctx.WindowFrom = ResolveWindowFrom(ctx.SyncWindowValue, DateTime.Now)
- ?? DateTime.Now.AddDays(-7);
- break;
- default:
- // INCR / 其它:走 last_cursor
- ctx.SyncWindowType = "INCR";
- ctx.WindowFrom = null;
- break;
- }
- }
- /// <summary>
- /// 解析窗口参数:支持 <c>7d</c>/<c>24h</c>/<c>30m</c> 或绝对时间 <c>yyyy-MM-dd[ HH:mm:ss]</c>。
- /// </summary>
- public static DateTime? ResolveWindowFrom(string? value, DateTime now)
- {
- if (string.IsNullOrWhiteSpace(value)) return null;
- var raw = value.Trim();
- var m = DurationRegex.Match(raw);
- if (m.Success)
- {
- var n = int.Parse(m.Groups["n"].Value, CultureInfo.InvariantCulture);
- return m.Groups["u"].Value.ToUpperInvariant() switch
- {
- "D" => now.AddDays(-n),
- "H" => now.AddHours(-n),
- "M" => now.AddMinutes(-n),
- _ => null
- };
- }
- if (DateTime.TryParse(raw, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var abs)
- || DateTime.TryParse(raw, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out abs))
- return abs;
- return null;
- }
- }
|