-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathMatrixPlotPublisher.java
More file actions
257 lines (231 loc) · 8.19 KB
/
MatrixPlotPublisher.java
File metadata and controls
257 lines (231 loc) · 8.19 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package hudson.plugins.plot;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixRun;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import hudson.util.FormValidation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.commons.lang.ObjectUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest2;
/**
* @author lucinka
*/
public class MatrixPlotPublisher extends AbstractPlotPublisher {
private transient Map<MatrixConfiguration, List<Plot>> plotsOfConfigurations = new HashMap<>();
private transient Map<String, List<Plot>> groupMap = new HashMap<>();
/**
* Configured plots.
*/
private List<Plot> plots = new ArrayList<>();
public String urlGroupToOriginalGroup(String urlGroup, MatrixConfiguration c) {
if (urlGroup == null || "nogroup".equals(urlGroup)) {
return "Plots";
}
if (groupMap.containsKey(urlGroup)) {
List<Plot> plotList = new ArrayList<>();
for (Plot plot : groupMap.get(urlGroup)) {
if (ObjectUtils.equals(plot.getProject(), c)) {
plotList.add(plot);
}
}
if (!plotList.isEmpty()) {
return plotList.get(0).group;
}
}
return "";
}
/**
* Returns all group names as the original user specified strings.
*/
public List<String> getOriginalGroups(MatrixConfiguration configuration) {
List<String> originalGroups = new ArrayList<>();
for (String urlGroup : groupMap.keySet()) {
originalGroups.add(urlGroupToOriginalGroup(urlGroup, configuration));
}
Collections.sort(originalGroups);
return originalGroups;
}
/**
* Reset Configuration and set plots settings for matrixConfiguration
*
* @param plots the new list of plots
*/
public void setPlots(List<Plot> plots) {
this.plots = plots;
groupMap = new HashMap<>();
plotsOfConfigurations = new HashMap<>();
}
/**
* Adds the new plot to the plot data structures managed by this object.
*
* @param plot the new plot
*/
public void addPlot(Plot plot) {
String urlGroup = originalGroupToUrlEncodedGroup(plot.getGroup());
if (groupMap.containsKey(urlGroup)) {
List<Plot> list = groupMap.get(urlGroup);
list.add(plot);
} else {
List<Plot> list = new ArrayList<>();
list.add(plot);
groupMap.put(urlGroup, list);
}
if (plotsOfConfigurations.get((MatrixConfiguration) plot.getProject()) == null) {
List<Plot> list = new ArrayList<>();
list.add(plot);
plotsOfConfigurations.put((MatrixConfiguration) plot.getProject(), list);
} else {
plotsOfConfigurations.get((MatrixConfiguration) plot.getProject()).add(plot);
}
}
/**
* Returns the entire list of plots managed by this object.
*/
public List<Plot> getPlots(MatrixConfiguration configuration) {
List<Plot> p = plotsOfConfigurations.get(configuration);
return (p != null) ? p : new ArrayList<>();
}
public List<Plot> getPlots() {
return plots;
}
/**
* Returns the list of plots with the given group name. The given group must
* be the URL friendly form of the group name.
*/
public List<Plot> getPlots(String urlGroup, MatrixConfiguration configuration) {
List<Plot> groupPlots = new ArrayList<>();
List<Plot> p = groupMap.get(urlGroup);
if (p != null) {
for (Plot plot : p) {
if (ObjectUtils.equals(plot.getProject(), configuration)) {
groupPlots.add(plot);
}
}
}
return groupPlots;
}
/**
* Called by Jenkins.
*/
@Override
public Action getProjectAction(AbstractProject<?, ?> project) {
if (project instanceof MatrixConfiguration) {
return new MatrixPlotAction((MatrixConfiguration) project, this);
}
return null;
}
@Override
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
if (!plotsOfConfigurations.containsKey((MatrixConfiguration) build.getProject())) {
for (Plot p : plots) {
Plot plot = new Plot(
p.title,
p.yaxis,
p.group,
p.numBuilds,
p.csvFileName,
p.style,
p.useDescr,
p.getKeepRecords(),
p.getExclZero(),
p.isLogarithmic(),
p.yaxisMinimum,
p.yaxisMaximum,
p.description,
p.chartWidth,
p.chartHeight,
p.skipZeroValues,
p.useDecimalFormat);
plot.series = p.series;
plot.setProject(build.getProject());
addPlot(plot);
}
}
return true;
}
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws IOException, InterruptedException {
if (!(build instanceof MatrixRun)) {
return true;
}
listener.getLogger().println("Recording plot data");
// add the build to each plot
for (Plot plot : plotsOfConfigurations.get(((MatrixRun) build).getProject())) {
plot.addBuild(build, listener.getLogger());
}
// misconfigured plots will not fail a build so always return true
return true;
}
/**
* Setup the groupMap upon deserialization.
*/
private Object readResolve() {
setPlots(plots);
return this;
}
@Extension
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
/**
* Called by Jenkins.
*/
@Override
public BuildStepDescriptor<Publisher> getDescriptor() {
return DESCRIPTOR;
}
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
public DescriptorImpl() {
super(MatrixPlotPublisher.class);
}
@NonNull
public String getDisplayName() {
return Messages.Plot_Publisher_DisplayName();
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return MatrixProject.class.isAssignableFrom(jobType);
}
/**
* Called when the user saves the project configuration.
*/
@Override
public Publisher newInstance(StaplerRequest2 req, JSONObject formData) throws FormException {
MatrixPlotPublisher publisher = new MatrixPlotPublisher();
List<Plot> plots = new ArrayList<>();
for (Object data : SeriesFactory.getArray(formData.get("plots"))) {
plots.add(bindPlot((JSONObject) data, req));
}
publisher.setPlots(plots);
return publisher;
}
private static Plot bindPlot(JSONObject data, StaplerRequest2 req) {
Plot p = req.bindJSON(Plot.class, data);
p.series = SeriesFactory.createSeriesList(data.get("series"), req);
return p;
}
/**
* Checks if the series file is valid.
*/
public FormValidation doCheckSeriesFile(@AncestorInPath AbstractProject project, @QueryParameter String value)
throws IOException {
return FilePath.validateFileMask(project.getSomeWorkspace(), value);
}
}
}