-
Notifications
You must be signed in to change notification settings - Fork 0
/
qaBlink.go
179 lines (157 loc) · 4.35 KB
/
qaBlink.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
173
174
175
176
177
178
179
package main
import (
"github.com/g3force/go-blink1"
"github.com/g3force/qaBlink/config"
"github.com/g3force/qaBlink/watcher"
"log"
"os"
"sort"
"strings"
"time"
)
var CONFIG_LOCATIONS = []string{"config.json", os.Getenv("HOME") + "/.qaBlink.json"}
type QaBlinkSlot struct {
Id string
Jobs []watcher.QaBlinkJob
}
type QaBlink struct {
Slots []QaBlinkSlot
blink1Devices []*blink1.Device
Config *config.QaBlinkConfig
}
func NewQaBlink(config *config.QaBlinkConfig) *QaBlink {
qaBlink := new(QaBlink)
qaBlink.Config = config
for _, slot := range config.Slots {
var qaSlot QaBlinkSlot
qaSlot.Id = slot.Id
for _, refId := range slot.RefId {
var jenkinsJob = watcher.NewJenkinsJob(config.Jenkins, refId)
if jenkinsJob != nil {
qaSlot.Jobs = append(qaSlot.Jobs, jenkinsJob)
} else {
var sonarJob = watcher.NewSonarJob(config.Sonar, refId)
if sonarJob != nil {
qaSlot.Jobs = append(qaSlot.Jobs, sonarJob)
} else {
var sonarJob = watcher.NewSonar51Job(config.Sonar, refId)
if sonarJob != nil {
qaSlot.Jobs = append(qaSlot.Jobs, sonarJob)
} else {
log.Printf("Could not find a job for refId %v in slot %v", refId, slot)
}
}
}
}
qaBlink.Slots = append(qaBlink.Slots, qaSlot)
}
return qaBlink
}
func (qaBlink *QaBlink) UpdateStatus() {
log.Printf("Updating %d slots\n", len(qaBlink.Slots))
for _, slot := range qaBlink.Slots {
for jobId, job := range slot.Jobs {
job.Update()
log.Printf("%40s(job:%d): %8v [pending: %5v,score: %3v]", job.Id(), jobId, job.State(), job.State().Pending, job.State().Score)
}
}
}
func toState(state watcher.QaBlinkState) blink1.State {
if state.Pending {
return blink1.State{Red: 0, Green: 0, Blue: 255}
}
switch state.StatusCode {
case watcher.STABLE:
return blink1.State{Red: 0, Green: 255, Blue: 0}
case watcher.UNSTABLE:
return blink1.State{Red: 255, Green: 255, Blue: 0}
case watcher.FAILED:
return blink1.State{Red: 255, Green: 0, Blue: 0}
case watcher.UNKNOWN:
return blink1.State{Red: 0, Green: 0, Blue: 0}
case watcher.DISABLED:
return blink1.State{Red: 255, Green: 0, Blue: 255}
}
return blink1.State{}
}
func (qaBlink *QaBlink) UpdateBlink() {
perSlotDuration := time.Duration(qaBlink.Config.PerSlotDuration) * time.Millisecond
for _, slot := range qaBlink.Slots {
slotId := 0
for _, device := range qaBlink.blink1Devices {
for ledId := 0; ledId < 2; ledId++ {
var state blink1.State
if slotId < len(slot.Jobs) {
job := slot.Jobs[slotId]
state = toState(job.State())
} else {
state = blink1.State{}
}
state.FadeTime = time.Duration(qaBlink.Config.FadeTime) * time.Millisecond
switch ledId {
case 0:
state.LED = blink1.LED1
case 1:
state.LED = blink1.LED2
default:
continue
}
device.SetState(state)
slotId++
}
}
time.Sleep(perSlotDuration)
}
}
func (qaBlink *QaBlink) UpdateDevices() {
newDevices := 0
for i := 0; ; i++ {
device, err := blink1.OpenNextDevice()
if device == nil {
break
}
if err != nil {
log.Print(err)
break
}
device.SetState(blink1.State{Red: 255, Blue: 255})
newDevices++
}
qaBlink.blink1Devices = blink1.OpenDevices()
// sort devices, so that they are always used in the same order
sort.Slice(qaBlink.blink1Devices, func(i, j int) bool {
s1 := qaBlink.blink1Devices[i].Device.Bus + qaBlink.blink1Devices[i].Device.Device
s2 := qaBlink.blink1Devices[j].Device.Bus + qaBlink.blink1Devices[j].Device.Device
return strings.Compare(s1, s2) > 0
})
if newDevices > 0 {
log.Printf("Found %d new blink1 devices, %d now.\n", newDevices, len(qaBlink.blink1Devices))
}
}
func repeat(f func(), duration time.Duration) {
for {
time.Sleep(duration)
f()
}
}
func main() {
chosenConfig := ""
for _, configLocation := range CONFIG_LOCATIONS {
if _, err := os.Stat(configLocation); !os.IsNotExist(err) {
chosenConfig = configLocation
break
}
}
blinkConfig := config.NewQaBlinkConfig(chosenConfig)
qaBlink := NewQaBlink(blinkConfig)
statusUpdateInterval := time.Duration(qaBlink.Config.UpdateInterval) * time.Millisecond
deviceUpdateInterval := statusUpdateInterval
go qaBlink.UpdateDevices()
qaBlink.UpdateStatus()
go repeat(qaBlink.UpdateStatus, statusUpdateInterval)
go repeat(qaBlink.UpdateBlink, 0)
go repeat(qaBlink.UpdateDevices, deviceUpdateInterval)
for {
time.Sleep(time.Hour)
}
}