|
| 1 | +using System.Security.Cryptography; |
| 2 | +using System.Text; |
| 3 | +using PythonScriptRunner.EntityFrameworkCore.Entities; |
| 4 | + |
| 5 | +namespace PythonScriptRunner.EntityFrameworkCore.Catalog; |
| 6 | + |
| 7 | +/// <summary>A step row with the stage version it references.</summary> |
| 8 | +internal sealed record StepRow(PipelineStepEntity Step, string StageKey, int StageVersion); |
| 9 | + |
| 10 | +/// <summary>Maps catalog rows to definitions and DTOs, and back.</summary> |
| 11 | +internal static class CatalogMapping |
| 12 | +{ |
| 13 | + public static StageDefinition ToStage(StageEntity stage, StageVersionEntity version) => new() |
| 14 | + { |
| 15 | + Key = stage.Key, |
| 16 | + Version = version.Version, |
| 17 | + Name = stage.Name, |
| 18 | + Description = stage.Description, |
| 19 | + Script = ScriptSource.Inline(version.Script), |
| 20 | + PythonVersion = version.PythonVersion, |
| 21 | + Requirements = version.Requirements, |
| 22 | + LockedRequirements = version.LockedRequirements, |
| 23 | + Inputs = StoredJson.Fields(version.InputFields), |
| 24 | + Outputs = StoredJson.Fields(version.OutputFields), |
| 25 | + RequiredSecrets = version.RequiredSecrets, |
| 26 | + Timeout = TimeSpan.FromSeconds(version.TimeoutSeconds), |
| 27 | + MemoryLimitMb = version.MemoryLimitMb, |
| 28 | + Retry = new RetryPolicy(version.RetryMaxAttempts, TimeSpan.FromSeconds(version.RetryInitialDelaySeconds), (double)version.RetryBackoffMultiplier), |
| 29 | + Idempotent = version.Idempotent, |
| 30 | + }; |
| 31 | + |
| 32 | + public static StageDraftData ToStageData(StageVersionEntity version) => new() |
| 33 | + { |
| 34 | + Script = version.Script, |
| 35 | + PythonVersion = version.PythonVersion, |
| 36 | + Requirements = version.Requirements, |
| 37 | + Inputs = StoredJson.Fields(version.InputFields), |
| 38 | + Outputs = StoredJson.Fields(version.OutputFields), |
| 39 | + RequiredSecrets = version.RequiredSecrets, |
| 40 | + Timeout = TimeSpan.FromSeconds(version.TimeoutSeconds), |
| 41 | + MemoryLimitMb = version.MemoryLimitMb, |
| 42 | + Retry = new RetryPolicy(version.RetryMaxAttempts, TimeSpan.FromSeconds(version.RetryInitialDelaySeconds), (double)version.RetryBackoffMultiplier), |
| 43 | + Idempotent = version.Idempotent, |
| 44 | + }; |
| 45 | + |
| 46 | + /// <summary>Copies editable content into a version row.</summary> |
| 47 | + public static void Apply(StageVersionEntity version, StageDraftData data) |
| 48 | + { |
| 49 | + version.Script = data.Script; |
| 50 | + version.ScriptSha256 = Sha256(data.Script); |
| 51 | + version.PythonVersion = data.PythonVersion; |
| 52 | + version.Requirements = string.IsNullOrWhiteSpace(data.Requirements) ? null : data.Requirements; |
| 53 | + version.InputFields = StoredJson.Fields(data.Inputs); |
| 54 | + version.OutputFields = StoredJson.Fields(data.Outputs); |
| 55 | + version.RequiredSecrets = [.. data.RequiredSecrets]; |
| 56 | + version.TimeoutSeconds = (int)Math.Ceiling(data.Timeout.TotalSeconds); |
| 57 | + version.MemoryLimitMb = data.MemoryLimitMb; |
| 58 | + version.RetryMaxAttempts = data.Retry.MaxAttempts; |
| 59 | + version.RetryInitialDelaySeconds = (int)data.Retry.InitialDelay.TotalSeconds; |
| 60 | + version.RetryBackoffMultiplier = (decimal)data.Retry.BackoffMultiplier; |
| 61 | + version.Idempotent = data.Idempotent; |
| 62 | + } |
| 63 | + |
| 64 | + public static StageDraftData ToStageData(StageDefinition stage, string defaultPythonVersion) => new() |
| 65 | + { |
| 66 | + Script = stage.Script switch |
| 67 | + { |
| 68 | + InlineScriptSource inline => inline.Content, |
| 69 | + FileScriptSource file => File.ReadAllText(file.Path), |
| 70 | + _ => throw new InvalidOperationException("Unknown script source."), |
| 71 | + }, |
| 72 | + PythonVersion = stage.PythonVersion ?? defaultPythonVersion, |
| 73 | + Requirements = stage.Requirements, |
| 74 | + Inputs = stage.Inputs, |
| 75 | + Outputs = stage.Outputs, |
| 76 | + RequiredSecrets = stage.RequiredSecrets, |
| 77 | + Timeout = stage.Timeout ?? TimeSpan.FromMinutes(5), |
| 78 | + MemoryLimitMb = stage.MemoryLimitMb, |
| 79 | + Retry = stage.Retry, |
| 80 | + Idempotent = stage.Idempotent, |
| 81 | + }; |
| 82 | + |
| 83 | + public static StageDraft ToStageDraft(StageEntity stage, StageVersionEntity version) => |
| 84 | + new(stage.Key, version.Version, ToStageData(version), version.RowVersion, Authorship(version)); |
| 85 | + |
| 86 | + public static StageVersionDetail ToStageVersionDetail(StageEntity stage, StageVersionEntity version) => new() |
| 87 | + { |
| 88 | + Key = stage.Key, |
| 89 | + Version = version.Version, |
| 90 | + Status = version.Status, |
| 91 | + Data = ToStageData(version), |
| 92 | + ScriptSha256 = version.ScriptSha256, |
| 93 | + LockedRequirements = version.LockedRequirements, |
| 94 | + EnvironmentHash = version.EnvironmentHash, |
| 95 | + PublishedAt = version.PublishedAt, |
| 96 | + PublishedBy = version.PublishedBy, |
| 97 | + ArchivedAt = version.ArchivedAt, |
| 98 | + ArchivedBy = version.ArchivedBy, |
| 99 | + RowVersion = version.RowVersion, |
| 100 | + Authorship = Authorship(version), |
| 101 | + }; |
| 102 | + |
| 103 | + public static PipelineDefinition ToPipeline(PipelineEntity pipeline, PipelineVersionEntity version, IEnumerable<StepRow> steps) => new() |
| 104 | + { |
| 105 | + Key = pipeline.Key, |
| 106 | + Version = version.Version, |
| 107 | + Name = pipeline.Name, |
| 108 | + Description = pipeline.Description, |
| 109 | + Inputs = StoredJson.Fields(version.InputFields), |
| 110 | + Steps = [.. steps.OrderBy(s => s.Step.Order).Select(s => new PipelineStepDefinition(s.Step.Key, s.StageKey, s.StageVersion) |
| 111 | + { |
| 112 | + DisplayName = s.Step.DisplayName, |
| 113 | + Bindings = StoredJson.Bindings(s.Step.Bindings), |
| 114 | + })], |
| 115 | + Outputs = StoredJson.Outputs(version.Outputs), |
| 116 | + }; |
| 117 | + |
| 118 | + public static PipelineDraftData ToPipelineData(PipelineDefinition pipeline, string? layout) => new() |
| 119 | + { |
| 120 | + Inputs = pipeline.Inputs, |
| 121 | + Steps = pipeline.Steps, |
| 122 | + Outputs = pipeline.Outputs, |
| 123 | + Layout = StoredJson.Node(layout), |
| 124 | + }; |
| 125 | + |
| 126 | + public static PipelineVersionDetail ToPipelineVersionDetail(PipelineEntity pipeline, PipelineVersionEntity version, IEnumerable<StepRow> steps) => new() |
| 127 | + { |
| 128 | + Key = pipeline.Key, |
| 129 | + Version = version.Version, |
| 130 | + Status = version.Status, |
| 131 | + Data = ToPipelineData(ToPipeline(pipeline, version, steps), version.Layout), |
| 132 | + PublishedAt = version.PublishedAt, |
| 133 | + PublishedBy = version.PublishedBy, |
| 134 | + ArchivedAt = version.ArchivedAt, |
| 135 | + ArchivedBy = version.ArchivedBy, |
| 136 | + RowVersion = version.RowVersion, |
| 137 | + Authorship = Authorship(version), |
| 138 | + }; |
| 139 | + |
| 140 | + public static Authorship Authorship(StageEntity row) => new(row.CreatedAt, row.CreatedBy, row.UpdatedAt, row.UpdatedBy); |
| 141 | + |
| 142 | + public static Authorship Authorship(StageVersionEntity row) => new(row.CreatedAt, row.CreatedBy, row.UpdatedAt, row.UpdatedBy); |
| 143 | + |
| 144 | + public static Authorship Authorship(PipelineEntity row) => new(row.CreatedAt, row.CreatedBy, row.UpdatedAt, row.UpdatedBy); |
| 145 | + |
| 146 | + public static Authorship Authorship(PipelineVersionEntity row) => new(row.CreatedAt, row.CreatedBy, row.UpdatedAt, row.UpdatedBy); |
| 147 | + |
| 148 | + public static string Sha256(string text) => Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(text))); |
| 149 | +} |
0 commit comments