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