-
Notifications
You must be signed in to change notification settings - Fork 6
/
SlavesHolder.java
206 lines (186 loc) · 4.82 KB
/
SlavesHolder.java
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package org.jenkinsci.plugins.jqsmonitoring.slaves;
import hudson.Extension;
import hudson.model.Api;
import hudson.model.Executor;
import hudson.model.Hudson;
import hudson.model.Slave;
import hudson.slaves.ComputerListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import org.jenkinsci.plugins.jqsmonitoring.jqscore.Constants;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
/**
* Holds all information about the slaves in the current jenkins instance.
*
* @author yboev
*
*/
@ExportedBean
public final class SlavesHolder extends ComputerListener {
/**
* List of all jenkins slaves.
*/
private ArrayList<Slave> slaves;
/**
* Singleton instance.
*/
private static SlavesHolder instance;
/**
* Constructor method. Called only from getInstance().
*/
@SuppressWarnings("deprecation")
private SlavesHolder() {
this.slaves = new ArrayList<Slave>(Hudson.getInstance().getSlaves());
sort();
// Adding ATExtension did no rigister my class, that is why this method
// is called.
this.register();
}
/**
* Return the instance for this class(Singleton).
*
* @return the instance.
*/
public static SlavesHolder getInstance() {
if (instance == null) {
instance = new SlavesHolder();
}
return instance;
}
/**
* REST API.
*
* @return the api.
*/
public Api getApi() {
return new Api(this);
}
@SuppressWarnings("deprecation")
@Override
public void onConfigurationChange() {
this.slaves = new ArrayList<Slave>(Hudson.getInstance().getSlaves());
sort();
}
/**
* List with all slaves.
*
* @return the list.
*/
@Exported
public ArrayList<Slave> getSlaves() {
return this.slaves;
}
/**
* The number of slaves.
*
* @return the number as int.
*/
@Exported
public int getNumberOfSlaves() {
if (this.slaves == null) {
return 0;
}
return this.slaves.size();
}
/**
* Returns the url for a certain slave.
*
* @param slave
* the slave.
* @return the url of the given slave.
*/
public String getUrl(Slave slave) {
try {
return slave.getComputer().getTimeline().getLastBuild().getUrl();
} catch (NullPointerException e) {
return "NoLastBuild";
}
}
/**
* Return the last build for a given slave.
*
* @param slave
* the slave.
* @return the last build of the given slave as String.
*/
public String getLastBuild(Slave slave) {
try {
return slave.getComputer().getTimeline().getLastBuild()
.getFullDisplayName();
} catch (NullPointerException e) {
return null;
}
}
/**
* Return number of online slaves.
*
* @return number of online slaves as int.
*/
@Exported
public int getNumberOfOnlineSlaves() {
int count = 0;
for (Slave s : slaves) {
if (s.getComputer().isOnline()) {
count++;
}
}
return count;
}
/**
* Return number of offline slaves.
*
* @return number of offline slaves as int.
*/
@Exported
public int getNumberOfOfflineSlaves() {
int count = 0;
for (Slave s : slaves) {
if (s.getComputer().isOffline()) {
count++;
}
}
return count;
}
/**
* Return the color assotiated with this slave . red - offline orange - all
* executors busy green - free executor
*
* @param s
* the slave.
* @return the color as String.
*/
public String getColor(Slave s) {
if (s.toComputer().isOffline()) {
return Constants.SLAVE_OFFLINE_COLOR;
}
if (this.areAllExecutorsBusy(s)) {
return Constants.SLAVE_NOEXECUTORS_COLOR;
}
return Constants.SLAVE_OK_COLOR;
}
/**
* Sort method for the slaves list.
*/
private void sort() {
final Comparator<Slave> c = Comparator.comparing(Slave::getNodeName);
this.slaves.sort(c);
}
/**
* Checks if all executors of a slave are busy.
*
* @param s
* the slave, which executors are being checked.
* @return true - all busy, false otherwise.
*/
private boolean areAllExecutorsBusy(Slave s) {
int count = 0;
for (Executor executor : s.getComputer().getExecutors()) {
if (!executor.isBusy()) {
count++;
}
}
return (count == 0);
}
}