-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender.go
170 lines (154 loc) · 3.79 KB
/
render.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
package main
/*
Package tjob - Output Rendering
Copyright (c) 2014 Ohmu Ltd.
Licensed under the Apache License, Version 2.0 (see LICENSE)
*/
import (
"code.google.com/p/go.crypto/ssh/terminal"
"fmt"
"github.com/ohmu/tjob/config"
"github.com/ohmu/tjob/jenkins"
"github.com/ohmu/tjob/pipeline"
"os"
"sort"
"strings"
"sync"
"time"
)
var globalProgramStart time.Time
func init() {
globalProgramStart = time.Now()
}
func filterByStartedSince(display *displayOptions, jobStatus *jenkins.JobStatus) bool {
if display.SinceDuration == 0 {
return true
} else if jobStatus == nil || jobStatus.Timestamp == 0 {
return false
}
minDate := globalProgramStart.Add(-display.SinceDuration)
return time.Unix(int64(jobStatus.Timestamp)/1000, 0).After(minDate)
}
type resultFilterer struct {
pipeline.Node
flags *filterFlags
display *displayOptions
Input chan *JobStatus
Output chan *JobStatus
}
// filterResults input is sorted in ascending BuildNumber order
func (node *resultFilterer) Run() error {
defer close(node.Output)
var prev *JobStatus
for cur := range node.Input {
var sendVal *JobStatus
switch {
case node.flags.FilterCommit != "" &&
(cur.Status == nil || !strings.HasPrefix(cur.Status.CommitID(), node.flags.FilterCommit)):
case node.display.SinceDuration != 0 &&
!filterByStartedSince(node.display, cur.Status):
case node.flags.OnlyAllFailed && (cur.Status == nil ||
cur.Status.IsFailed()):
sendVal = cur
case node.flags.OnlyAllFailed:
case (node.flags.OnlyFailing && prev == nil):
case (node.flags.OnlyFailing && prev.Status != nil &&
!prev.Status.IsFailed()):
case (node.flags.OnlyFailing && (cur.Runner != prev.Runner ||
cur.JobName != prev.JobName)):
sendVal = prev
case node.flags.OnlyFailing:
default:
sendVal = cur
}
if sendVal != nil {
select {
case <-node.AbortChannel():
return nil
case node.Output <- sendVal:
}
}
prev = cur
}
if node.flags.OnlyFailing && prev != nil && prev.Status.IsFailed() {
select {
case <-node.AbortChannel():
return nil
case node.Output <- prev:
}
}
return nil
}
type jobStatusQuery struct {
pipeline.Node
conf *config.Config
display *displayOptions
Input chan *config.Job
Output chan *JobStatus
}
func (node *jobStatusQuery) Run() error {
defer close(node.Output)
limiter := make(chan struct{}, 10) // max concurrent ops
wgJenk := sync.WaitGroup{}
i := 0
reportProgress := (terminal.IsTerminal(int(os.Stdout.Fd())) &&
!node.display.NoSorting)
for job := range node.Input {
jenk, err := getJenkins(node.conf, job.Runner)
if err != nil {
return node.AbortWithError(err)
}
// report query progress
if reportProgress {
locStr := fmt.Sprintf("%s %s %s", job.Runner,
job.JobName, job.BuildNumber)
if len(locStr) > 40 {
locStr = locStr[:40]
}
fmt.Printf("%6d %-60s\r", i, locStr)
}
wgJenk.Add(1)
limiter <- struct{}{} // don't want too many goroutines
go func(job *config.Job) {
defer wgJenk.Done()
status, err := jenk.QueryJobStatus(job.JobName,
job.BuildNumber, node.display.ShowTestDetails)
select {
case <-node.AbortChannel():
return
case node.Output <- &JobStatus{job, status, err}:
}
<-limiter
}(job)
i++
}
if reportProgress {
fmt.Printf("%6s %-60s\r", "", "") // clean up output
}
// wait for tasks to complete before closing output channel
wgJenk.Wait()
return nil
}
type jobStatusSorter struct {
pipeline.Node
Input chan *JobStatus
Output chan *JobStatus
}
func (node *jobStatusSorter) Run() error {
defer close(node.Output)
var results []*JobStatus
i := 0
for job := range node.Input {
results = append(results, job)
i++
}
sort.Sort(jobStatusByBuildNumber(results))
for _, res := range results {
select {
case <-node.AbortChannel():
return nil
case node.Output <- res:
}
}
return nil
}