MdpSyncWindowResolver.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Globalization;
  2. using System.Text.RegularExpressions;
  3. namespace Admin.NET.Plugin.AiDOP.DataPlatform.Executors;
  4. /// <summary>
  5. /// P-015:解析调度同步窗口(FULL / INCR / ROLLING),供抽数 SQL 使用。
  6. /// </summary>
  7. public static class MdpSyncWindowResolver
  8. {
  9. private static readonly Regex DurationRegex = new(
  10. @"^\s*(?<n>\d+)\s*(?<u>[dDhHmM])\s*$",
  11. RegexOptions.Compiled);
  12. /// <summary>
  13. /// 将窗口类型落到上下文:FULL→全量;INCR→水位;ROLLING→下界时间。
  14. /// 显式 FullRefresh 时强制 FULL。
  15. /// </summary>
  16. public static void Apply(MdpPullContext ctx)
  17. {
  18. if (ctx.FullRefresh)
  19. {
  20. ctx.SyncWindowType = "FULL";
  21. ctx.WindowFrom = null;
  22. return;
  23. }
  24. var type = (ctx.SyncWindowType ?? "INCR").Trim().ToUpperInvariant();
  25. if (type is "ROLLING_WINDOW") type = "ROLLING";
  26. switch (type)
  27. {
  28. case "FULL":
  29. ctx.SyncWindowType = "FULL";
  30. ctx.FullRefresh = true;
  31. ctx.WindowFrom = null;
  32. break;
  33. case "ROLLING":
  34. ctx.SyncWindowType = "ROLLING";
  35. ctx.WindowFrom = ResolveWindowFrom(ctx.SyncWindowValue, DateTime.Now)
  36. ?? DateTime.Now.AddDays(-7);
  37. break;
  38. default:
  39. // INCR / 其它:走 last_cursor
  40. ctx.SyncWindowType = "INCR";
  41. ctx.WindowFrom = null;
  42. break;
  43. }
  44. }
  45. /// <summary>
  46. /// 解析窗口参数:支持 <c>7d</c>/<c>24h</c>/<c>30m</c> 或绝对时间 <c>yyyy-MM-dd[ HH:mm:ss]</c>。
  47. /// </summary>
  48. public static DateTime? ResolveWindowFrom(string? value, DateTime now)
  49. {
  50. if (string.IsNullOrWhiteSpace(value)) return null;
  51. var raw = value.Trim();
  52. var m = DurationRegex.Match(raw);
  53. if (m.Success)
  54. {
  55. var n = int.Parse(m.Groups["n"].Value, CultureInfo.InvariantCulture);
  56. return m.Groups["u"].Value.ToUpperInvariant() switch
  57. {
  58. "D" => now.AddDays(-n),
  59. "H" => now.AddHours(-n),
  60. "M" => now.AddMinutes(-n),
  61. _ => null
  62. };
  63. }
  64. if (DateTime.TryParse(raw, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var abs)
  65. || DateTime.TryParse(raw, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out abs))
  66. return abs;
  67. return null;
  68. }
  69. }