|
| 1 | +package durabletasks |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/labstack/echo/v4" |
| 8 | + |
| 9 | + "github.com/hatchet-dev/hatchet/api/v1/server/oas/gen" |
| 10 | + "github.com/hatchet-dev/hatchet/pkg/repository" |
| 11 | + "github.com/hatchet-dev/hatchet/pkg/repository/sqlcv1" |
| 12 | +) |
| 13 | + |
| 14 | +func (t *DurableTasksService) V1DurableTaskEventLogList(ctx echo.Context, request gen.V1DurableTaskEventLogListRequestObject) (gen.V1DurableTaskEventLogListResponseObject, error) { |
| 15 | + task := ctx.Get("durable-task").(*sqlcv1.V1TasksOlap) |
| 16 | + |
| 17 | + limit := int64(1000) |
| 18 | + offset := int64(0) |
| 19 | + |
| 20 | + if request.Params.Limit != nil { |
| 21 | + limit = *request.Params.Limit |
| 22 | + } |
| 23 | + |
| 24 | + if request.Params.Offset != nil { |
| 25 | + offset = *request.Params.Offset |
| 26 | + } |
| 27 | + |
| 28 | + entries, err := t.config.V1.DurableEvents().ListDurableEventLog( |
| 29 | + ctx.Request().Context(), |
| 30 | + task.TenantID, |
| 31 | + task.InsertedAt, |
| 32 | + task.ID, |
| 33 | + limit, |
| 34 | + offset, |
| 35 | + ) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + return gen.V1DurableTaskEventLogList200JSONResponse(toDurableEventLogEntries(entries)), nil |
| 41 | +} |
| 42 | + |
| 43 | +func toDurableEventLogEntries(entries []*sqlcv1.ListDurableEventLogForTaskRow) []gen.V1DurableEventLogEntry { |
| 44 | + result := make([]gen.V1DurableEventLogEntry, 0, len(entries)) |
| 45 | + |
| 46 | + for _, e := range entries { |
| 47 | + var insertedAt time.Time |
| 48 | + if e.InsertedAt.Valid { |
| 49 | + insertedAt = e.InsertedAt.Time |
| 50 | + } |
| 51 | + |
| 52 | + entry := gen.V1DurableEventLogEntry{ |
| 53 | + NodeId: e.NodeID, |
| 54 | + BranchId: e.BranchID, |
| 55 | + Kind: gen.V1DurableEventLogKind(e.Kind), |
| 56 | + IsSatisfied: e.IsSatisfied, |
| 57 | + InsertedAt: insertedAt, |
| 58 | + TaskExternalId: e.DurableTaskExternalID, |
| 59 | + TaskDisplayName: e.DurableTaskDisplayName, |
| 60 | + } |
| 61 | + |
| 62 | + if e.UserMessage.Valid { |
| 63 | + entry.UserMessage = &e.UserMessage.String |
| 64 | + } |
| 65 | + |
| 66 | + if e.SatisfiedAt.Valid { |
| 67 | + entry.SatisfiedAt = &e.SatisfiedAt.Time |
| 68 | + } |
| 69 | + |
| 70 | + if len(e.WaitData) > 0 { |
| 71 | + entry.WaitData = toGenWaitData(e.WaitData) |
| 72 | + } |
| 73 | + |
| 74 | + result = append(result, entry) |
| 75 | + } |
| 76 | + |
| 77 | + return result |
| 78 | +} |
| 79 | + |
| 80 | +func toGenWaitData(raw []byte) *gen.V1WaitData { |
| 81 | + var wd repository.WaitData |
| 82 | + if err := json.Unmarshal(raw, &wd); err != nil { |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + var items []gen.V1WaitItem |
| 87 | + |
| 88 | + for _, c := range wd.Conditions { |
| 89 | + kind := gen.V1DurableWaitConditionKind(c.Kind) |
| 90 | + items = append(items, gen.V1WaitItem{ |
| 91 | + Kind: &kind, |
| 92 | + SleepDurationMs: c.SleepDurationMs, |
| 93 | + EventKey: c.EventKey, |
| 94 | + WorkflowName: c.WorkflowName, |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + for _, g := range wd.OrGroups { |
| 99 | + if len(g.Conditions) == 1 { |
| 100 | + // normalize legacy single-condition OR groups |
| 101 | + kind := gen.V1DurableWaitConditionKind(g.Conditions[0].Kind) |
| 102 | + items = append(items, gen.V1WaitItem{ |
| 103 | + Kind: &kind, |
| 104 | + SleepDurationMs: g.Conditions[0].SleepDurationMs, |
| 105 | + EventKey: g.Conditions[0].EventKey, |
| 106 | + WorkflowName: g.Conditions[0].WorkflowName, |
| 107 | + }) |
| 108 | + continue |
| 109 | + } |
| 110 | + genConds := make([]gen.V1DurableWaitCondition, 0, len(g.Conditions)) |
| 111 | + for _, c := range g.Conditions { |
| 112 | + kind := gen.V1DurableWaitConditionKind(c.Kind) |
| 113 | + genConds = append(genConds, gen.V1DurableWaitCondition{ |
| 114 | + Kind: kind, |
| 115 | + SleepDurationMs: c.SleepDurationMs, |
| 116 | + EventKey: c.EventKey, |
| 117 | + WorkflowName: c.WorkflowName, |
| 118 | + }) |
| 119 | + } |
| 120 | + items = append(items, gen.V1WaitItem{Or: &genConds}) |
| 121 | + } |
| 122 | + |
| 123 | + if len(items) == 0 { |
| 124 | + return nil |
| 125 | + } |
| 126 | + |
| 127 | + result := gen.V1WaitData(items) |
| 128 | + return &result |
| 129 | +} |
0 commit comments