-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipelinehistory.go
More file actions
170 lines (135 loc) · 2.99 KB
/
Copy pathpipelinehistory.go
File metadata and controls
170 lines (135 loc) · 2.99 KB
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
package main
import (
"io"
"sort"
"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([]*HistoryEntry, 0, size+1),
size: size,
}
}
type PipelineHistory struct {
entries []*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()
var entry *HistoryEntry
for _, value := range p.entries {
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 = append(p.entries, 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()
}
return entry
}
func (p *PipelineHistory) Get(activityID string) (result HistoryEntry, ok bool) {
p.Lock()
defer p.Unlock()
for _, value := range p.entries {
if value.ActivityID == activityID {
return *value, true
}
}
return
}
func (p *PipelineHistory) Groups() []string {
p.Lock()
defer p.Unlock()
names := make(map[string]bool)
for _, entry := range p.entries {
names[entry.WorkerGroup] = true
}
result := make([]string, 0, len(names))
for key := range names {
result = append(result, key)
}
sort.Strings(result)
return result
}
func (p *PipelineHistory) Entries(workerGroups []string) []HistoryEntry {
p.Lock()
defer p.Unlock()
count := len(p.entries)
groups := make(map[string]bool, len(workerGroups))
for _, key := range workerGroups {
groups[key] = true
}
filterGroups := len(workerGroups) > 0
result := make([]HistoryEntry, 0, count)
for _, entry := range p.entries {
if !filterGroups || groups[entry.WorkerGroup] {
result = append(result, *entry)
}
}
return result
}
func (p *PipelineHistory) cleanup() {
count := len(p.entries)
filtered := make([]*HistoryEntry, 0, count)
free := p.size
for i := range p.entries {
value := p.entries[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 = result
}