-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathCSVSeries.java
More file actions
401 lines (347 loc) · 13.1 KB
/
Copy pathCSVSeries.java
File metadata and controls
401 lines (347 loc) · 13.1 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
* The copyrights to the contents of this file are licensed under the MIT License
* (http://www.opensource.org/licenses/mit-license.php)
*/
package hudson.plugins.plot;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Descriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.ObjectUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest2;
/**
* Represents a plot data series configuration from an CSV file.
*
* @author Allen Reese
*/
public class CSVSeries extends Series {
private static final transient Logger LOGGER = Logger.getLogger(CSVSeries.class.getName());
// Debugging hack, so I don't have to change FINE/INFO...
private static final transient Level DEFAULT_LOG_LEVEL = Level.FINEST;
private static final transient Pattern PAT_SEMICOLON_ENCLOSURE = Pattern.compile("\"(.*?)\"");
private static final transient Pattern PAT_COMMA = Pattern.compile(",");
public enum InclusionFlag {
OFF,
INCLUDE_BY_STRING,
EXCLUDE_BY_STRING,
INCLUDE_BY_COLUMN,
EXCLUDE_BY_COLUMN
}
/**
* Set for excluding values by column name
*/
private Set<String> strExclusionSet;
/**
* Set for excluding values by column #
*/
private Set<Integer> colExclusionSet;
/**
* Flag controlling how values are excluded.
*/
private InclusionFlag inclusionFlag = InclusionFlag.OFF;
/**
* Comma separated list of columns to exclude.
*/
private String exclusionValues;
/**
* Comma separated list of columns to exclude.
*/
private List<String> exclusionValuesList;
/**
* Url to use as a base for mapping points.
*/
private String url;
/**
* Show table of the single values in charts.
*/
private boolean displayTableFlag;
@DataBoundConstructor
public CSVSeries(String file, String url, String inclusionFlag, String exclusionValues, boolean displayTableFlag) {
super(file, "", "csv");
this.url = url;
this.displayTableFlag = displayTableFlag;
this.exclusionValues = exclusionValues;
if (this.exclusionValues == null) {
this.inclusionFlag = InclusionFlag.OFF;
} else {
this.inclusionFlag = InclusionFlag.valueOf(inclusionFlag);
this.exclusionValuesList = new ArrayList<>();
/**
* first: try to handle the regex. The values are enclosed by ""
* If there are no values found, use plain splitting by comma
*/
Matcher m = PAT_SEMICOLON_ENCLOSURE.matcher(this.exclusionValues);
int results = 0;
while (m.find()) {
this.exclusionValuesList.add(m.group().replaceAll("\"", ""));
results++;
}
if (results == 0) {
this.exclusionValuesList = Arrays.asList(PAT_COMMA.split(this.exclusionValues));
}
}
loadExclusionSet();
}
public String getInclusionFlag() {
return ObjectUtils.toString(inclusionFlag);
}
public String getExclusionValues() {
return exclusionValues;
}
public List<String> getExclusionValuesList() {
return exclusionValuesList;
}
public String getUrl() {
return url;
}
public boolean getDisplayTableFlag() {
return displayTableFlag;
}
/**
* Load the series from a properties file.
*/
@Override
public List<PlotPoint> loadSeries(FilePath workspaceRootDir, int buildNumber, PrintStream logger) {
List<PlotPoint> plotPoints = null;
FilePath[] seriesFiles;
try {
seriesFiles = workspaceRootDir.list(getFile());
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Exception trying to retrieve series files", e);
return null;
}
if (ArrayUtils.isEmpty(seriesFiles)) {
LOGGER.info("No plot data file found: " + workspaceRootDir.getName() + " " + getFile());
return null;
}
for (FilePath seriesFile : seriesFiles) {
List<PlotPoint> seriesList = loadSeriesFile(seriesFile, buildNumber);
if (seriesList != null) {
if (plotPoints != null) {
plotPoints.addAll(seriesList);
} else {
plotPoints = seriesList;
}
}
}
return plotPoints;
}
private List<PlotPoint> loadSeriesFile(FilePath seriesFile, int buildNumber) {
CSVReader reader = null;
InputStream in = null;
InputStreamReader inputReader = null;
try {
List<PlotPoint> ret = new ArrayList<>();
try {
if (LOGGER.isLoggable(DEFAULT_LOG_LEVEL)) {
LOGGER.log(DEFAULT_LOG_LEVEL, "Loading plot series data from: " + getFile());
}
in = seriesFile.read();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Exception reading plot series data from " + seriesFile, e);
return null;
}
if (LOGGER.isLoggable(DEFAULT_LOG_LEVEL)) {
LOGGER.log(DEFAULT_LOG_LEVEL, "Loaded CSV Plot file: " + getFile());
}
// load existing plot file
inputReader = new InputStreamReader(in, Charset.defaultCharset());
reader = new CSVReader(inputReader);
String[] nextLine;
// save the header line to use it for the plot labels.
String[] headerLine = reader.readNext();
// read each line of the CSV file and add to rawPlotData
int lineNum = 0;
while ((nextLine = reader.readNext()) != null) {
// skip empty lines
if (nextLine.length == 1 && nextLine[0].isEmpty()) {
continue;
}
for (int index = 0; index < nextLine.length; index++) {
String yvalue;
String label = null;
yvalue = nextLine[index].trim();
// empty value, caused by e.g. trailing comma in CSV
if (yvalue.trim().isEmpty()) {
continue;
}
if (index < headerLine.length) {
label = headerLine[index].trim();
}
if (label == null || label.length() <= 0) {
// if there isn't a label, use the index as the label
label = "" + index;
}
// LOGGER.finest("Loaded point: " + point);
// create a new point with the yvalue from the csv file and
// url from the URL_index in the properties file.
if (!excludePoint(label, index)) {
PlotPoint point = new PlotPoint(yvalue, getUrl(url, label, index, buildNumber), label);
if (LOGGER.isLoggable(DEFAULT_LOG_LEVEL)) {
LOGGER.log(DEFAULT_LOG_LEVEL, "CSV Point: [" + index + ":" + lineNum + "]" + point);
}
ret.add(point);
} else {
if (LOGGER.isLoggable(DEFAULT_LOG_LEVEL)) {
LOGGER.log(DEFAULT_LOG_LEVEL, "excluded CSV Column: " + index + " : " + label);
}
}
}
lineNum++;
}
return ret;
} catch (CsvValidationException | IOException ioe) {
LOGGER.log(Level.SEVERE, "Exception loading series", ioe);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to close series reader", e);
}
}
IOUtils.closeQuietly(inputReader);
IOUtils.closeQuietly(in);
}
return null;
}
/**
* This function checks the exclusion/inclusion filters from the properties
* file and returns true if a point should be excluded.
*
* @return true if the point should be excluded based on label or column
*/
private boolean excludePoint(final String label, int index) {
if (inclusionFlag == null || inclusionFlag == InclusionFlag.OFF) {
return false;
}
boolean retVal =
switch (inclusionFlag) {
case INCLUDE_BY_STRING -> !checkExclusionSet(label); // if the set contains it, don't exclude it.
case EXCLUDE_BY_STRING -> checkExclusionSet(label); // if the set doesn't contain it, exclude it.
case INCLUDE_BY_COLUMN ->
!(colExclusionSet.contains(index)); // if the set contains it, don't exclude it.
case EXCLUDE_BY_COLUMN ->
colExclusionSet.contains(index); // if the set doesn't contain it, don't exclude it.
default -> false;
};
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest(((retVal) ? "excluded" : "included") + " CSV Column: " + index + " : " + label);
}
return retVal;
}
/**
* Checks if the current label / header is known in the strExclusionSet (plain text or regex).
*
* @param label
* @return if the label is in the set
*/
private boolean checkExclusionSet(String label) {
if (strExclusionSet.contains(label)) {
return true;
} else {
for (String s : strExclusionSet) {
if (checkPatternIsValid(s) && label.matches(s)) {
return true;
}
}
}
return false;
}
private boolean checkPatternIsValid(String pattern) {
try {
Pattern.compile(pattern);
} catch (java.util.regex.PatternSyntaxException e) {
return false;
}
return true;
}
/**
* This function loads the set of columns that should be included or
* excluded.
*/
private void loadExclusionSet() {
if (inclusionFlag == InclusionFlag.OFF) {
return;
}
if (exclusionValues == null) {
inclusionFlag = InclusionFlag.OFF;
return;
}
switch (inclusionFlag) {
case INCLUDE_BY_STRING:
case EXCLUDE_BY_STRING:
strExclusionSet = new HashSet<>();
break;
case INCLUDE_BY_COLUMN:
case EXCLUDE_BY_COLUMN:
colExclusionSet = new HashSet<>();
break;
default:
LOGGER.log(Level.SEVERE, "Failed to initialize columns exclusions set.");
}
for (String str : exclusionValuesList) {
if (str == null || str.length() <= 0) {
continue;
}
switch (inclusionFlag) {
case INCLUDE_BY_STRING:
case EXCLUDE_BY_STRING:
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest(inclusionFlag + " CSV Column: " + str);
}
strExclusionSet.add(str);
break;
case INCLUDE_BY_COLUMN:
case EXCLUDE_BY_COLUMN:
try {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest(inclusionFlag + " CSV Column: " + str);
}
colExclusionSet.add(Integer.valueOf(str));
} catch (NumberFormatException nfe) {
LOGGER.log(Level.SEVERE, "Exception converting to integer", nfe);
}
break;
default:
LOGGER.log(Level.SEVERE, "Failed to identify columns exclusions.");
}
}
}
@Override
public Descriptor<Series> getDescriptor() {
return new DescriptorImpl();
}
@Extension
public static class DescriptorImpl extends Descriptor<Series> {
@NonNull
public String getDisplayName() {
return Messages.Plot_CsvSeries();
}
@Override
public Series newInstance(StaplerRequest2 req, @NonNull JSONObject formData) throws FormException {
return SeriesFactory.createSeries(formData, req);
}
}
}