1package models
2
3import (
4 "fmt"
5 "regexp"
6 "slices"
7
8 "tangled.org/core/api/tangled"
9
10 "github.com/bluesky-social/indigo/atproto/syntax"
11)
12
13var (
14 re = regexp.MustCompile(`[^a-zA-Z0-9_.-]`)
15)
16
17type PipelineId struct {
18 Knot string
19 Rkey string
20}
21
22func (p *PipelineId) AtUri() syntax.ATURI {
23 return syntax.ATURI(fmt.Sprintf("at://did:web:%s/%s/%s", p.Knot, tangled.PipelineNSID, p.Rkey))
24}
25
26type WorkflowId struct {
27 PipelineId
28 Name string
29}
30
31func (wid WorkflowId) String() string {
32 return fmt.Sprintf("%s-%s-%s", normalize(wid.Knot), wid.Rkey, normalize(wid.Name))
33}
34
35func normalize(name string) string {
36 normalized := re.ReplaceAllString(name, "-")
37 return normalized
38}
39
40type StatusKind string
41
42var (
43 StatusKindPending StatusKind = "pending"
44 StatusKindRunning StatusKind = "running"
45 StatusKindFailed StatusKind = "failed"
46 StatusKindTimeout StatusKind = "timeout"
47 StatusKindCancelled StatusKind = "cancelled"
48 StatusKindSuccess StatusKind = "success"
49
50 StartStates [2]StatusKind = [2]StatusKind{
51 StatusKindPending,
52 StatusKindRunning,
53 }
54 FinishStates [4]StatusKind = [4]StatusKind{
55 StatusKindCancelled,
56 StatusKindFailed,
57 StatusKindSuccess,
58 StatusKindTimeout,
59 }
60)
61
62func (s StatusKind) String() string {
63 return string(s)
64}
65
66func (s StatusKind) IsStart() bool {
67 return slices.Contains(StartStates[:], s)
68}
69
70func (s StatusKind) IsFinish() bool {
71 return slices.Contains(FinishStates[:], s)
72}
73
74type LogKind string
75
76var (
77 // step log data
78 LogKindData LogKind = "data"
79 // indicates start/end of a step
80 LogKindControl LogKind = "control"
81)
82
83type LogLine struct {
84 Kind LogKind `json:"kind"`
85 Content string `json:"content"`
86
87 // fields if kind is "data"
88 Stream string `json:"stream,omitempty"`
89
90 // fields if kind is "control"
91 StepId int `json:"step_id,omitempty"`
92 StepKind StepKind `json:"step_kind,omitempty"`
93 StepCommand string `json:"step_command,omitempty"`
94}
95
96func NewDataLogLine(content, stream string) LogLine {
97 return LogLine{
98 Kind: LogKindData,
99 Content: content,
100 Stream: stream,
101 }
102}
103
104func NewControlLogLine(idx int, step Step) LogLine {
105 return LogLine{
106 Kind: LogKindControl,
107 Content: step.Name(),
108 StepId: idx,
109 StepKind: step.Kind(),
110 StepCommand: step.Command(),
111 }
112}