-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelinehistory.go
172 lines (136 loc) · 3.15 KB
/
pipelinehistory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"io"
"sync"
"time"
"github.com/djherbis/stream"
"github.com/reeveci/reeve-lib/schema"
"github.com/reeveci/reeve-lib/streams"
)
func NewPipelineHistory(size int) *PipelineHistory {
return &PipelineHistory{
Entries: make(map[string][]*HistoryEntry),
size: size,
}
}
type PipelineHistory struct {
Entries map[string][]*HistoryEntry
size int
sync.Mutex
}
type HistoryEntry struct {
schema.PipelineStatus
StartTime, EndTime *time.Time
Logs schema.LogReaderProvider
}
func (p *PipelineHistory) Put(status schema.PipelineStatus) *HistoryEntry {
p.Lock()
defer p.Unlock()
group := p.Entries[status.WorkerGroup]
var entry *HistoryEntry
for _, value := range group {
if value.ActivityID == status.ActivityID {
entry = value
entry.PipelineStatus = status
break
}
}
if entry == nil {
now := time.Now()
entry = &HistoryEntry{
PipelineStatus: status,
StartTime: &now,
Logs: nil,
}
p.Entries[status.WorkerGroup] = append(group, entry)
}
entry.PipelineStatus.Logs = nil
if entry.Logs != nil || !status.Logs.Available() {
status.Logs.Close()
} else {
reader, err := status.Logs.Reader()
if err != nil {
status.Logs.Close()
} else {
stream := stream.NewMemStream()
entry.Logs = streams.NewStreamProvider(stream)
go func() {
io.Copy(stream, reader)
entry.Logs.Close()
reader.Close()
status.Logs.Close()
}()
}
}
if status.Finished() {
now := time.Now()
entry.EndTime = &now
p.cleanup(status.WorkerGroup)
}
return entry
}
func (p *PipelineHistory) Get(workerGroup, activityID string) (result HistoryEntry, ok bool) {
p.Lock()
defer p.Unlock()
group := p.Entries[workerGroup]
for _, value := range group {
if value.ActivityID == activityID {
return *value, true
}
}
return
}
func (p *PipelineHistory) Groups() []string {
p.Lock()
defer p.Unlock()
result := make([]string, 0, len(p.Entries))
for key := range p.Entries {
result = append(result, key)
}
return result
}
func (p *PipelineHistory) Summary(workerGroup string) WorkerGroupResponse {
p.Lock()
defer p.Unlock()
group := p.Entries[workerGroup]
count := len(group)
result := WorkerGroupResponse{
Pipelines: make([]PipelineSummaryResponse, count),
}
for i, entry := range group {
result.Pipelines[count-1-i] = PipelineSummaryResponse{
ID: entry.ActivityID,
Name: entry.Pipeline.Name,
Headline: entry.Pipeline.Headline,
StartTime: entry.StartTime,
EndTime: entry.EndTime,
Status: entry.Status,
Result: entry.Result,
}
}
return result
}
func (p *PipelineHistory) cleanup(workerGroup string) {
group := p.Entries[workerGroup]
count := len(group)
filtered := make([]*HistoryEntry, 0, count)
free := p.size
for i := range group {
value := group[count-1-i]
if !value.Finished() {
filtered = append(filtered, value)
} else if free > 0 {
free -= 1
filtered = append(filtered, value)
}
}
remaining := len(filtered)
if remaining == count {
return
}
result := make([]*HistoryEntry, remaining)
for i, value := range filtered {
result[remaining-1-i] = value
}
p.Entries[workerGroup] = result
}