provisioning-sync-evidence.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {
  2. providerProvisioningResultSchema,
  3. providerProvisioningTriggerSchema,
  4. type ProviderProvisioningResult,
  5. type ProviderProvisioningTrigger,
  6. } from "@zcode/shared";
  7. export interface ProvisioningSyncEvent {
  8. kind: "completed" | "warning";
  9. environmentKey: string;
  10. trigger: ProviderProvisioningTrigger;
  11. status: ProviderProvisioningResult["status"];
  12. durationMs: number;
  13. }
  14. /** 只认 Main 完整落盘的结构化结果,保留重复事件,不转抄自由文本错误。 */
  15. export function parseProvisioningSyncEvents(text: string): ProvisioningSyncEvent[] {
  16. const events: ProvisioningSyncEvent[] = [];
  17. for (const line of text.split("\n").slice(0, -1)) {
  18. const match = line.match(
  19. /^\[[^\]]+\] \[(\w+)\] \[pid:\d+\] \[main\] \[provider-provisioning\] Environment sync (completed|did not apply) (.+)$/u,
  20. );
  21. if (!match) continue;
  22. const context: Record<string, unknown> = JSON.parse(match[3]!);
  23. if (
  24. !context ||
  25. typeof context !== "object" ||
  26. typeof context.environmentKey !== "string" ||
  27. !context.environmentKey ||
  28. typeof context.durationMs !== "number" ||
  29. !Number.isFinite(context.durationMs) ||
  30. context.durationMs < 0
  31. ) {
  32. throw new Error("Provisioning Main 日志缺少合法上下文");
  33. }
  34. const status = providerProvisioningResultSchema.shape.status.parse(context.status);
  35. const trigger = providerProvisioningTriggerSchema.parse(context.trigger);
  36. const kind = match[2] === "completed" ? "completed" : "warning";
  37. const applied = status === "applied" || status === "already-applied";
  38. if ((kind === "completed") !== applied || match[1] !== (applied ? "info" : "warn")) {
  39. throw new Error("Provisioning Main 日志级别、事件与状态不一致");
  40. }
  41. events.push({
  42. kind,
  43. environmentKey: context.environmentKey,
  44. trigger,
  45. status,
  46. durationMs: context.durationMs,
  47. });
  48. }
  49. return events;
  50. }