Skip to content

Commit

Permalink
[FIXED JENKINS-15358] Not using redundant table_*.csv file anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Nielsen committed Oct 29, 2013
1 parent 7731e71 commit 3899ab7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 88 deletions.
83 changes: 0 additions & 83 deletions src/main/java/hudson/plugins/plot/Plot.java
Expand Up @@ -471,14 +471,6 @@ public void addBuild(Build build, PrintStream logger) {

// save the updated plot data to disk
savePlotData();

// currently only support for csv type
if (getSeries() != null) {
Series series = getSeries()[0];
if ("csv".equals(series.getFileType())) {
saveTableData(build, series.getFile());
}
}
}

/**
Expand Down Expand Up @@ -749,79 +741,4 @@ private void savePlotData() {
}
}
}

private void saveTableData(AbstractBuild<?,?> build, String fileName) {
ArrayList rawTableData = new ArrayList();

File tableFile = new File(project.getRootDir(), "table_"+getCsvFileName());
File rawCSVFile = new File(build.getWorkspace() + File.separator + fileName);
CSVReader existingTableReader = null;
CSVReader newTupleReader = null;
CSVWriter newTableWriter = null;
try {
newTupleReader = new CSVReader(new FileReader(rawCSVFile));

// new header including build #
String [] header = newTupleReader.readNext();
String [] headerIncBuild = new String [header.length+1];
headerIncBuild[0] = Messages.Plot_build() + " #";
System.arraycopy(header, 0, headerIncBuild, 1, header.length);

// add a new tuple
String [] tuple = newTupleReader.readNext();
String [] tupleIncBuild = new String [tuple.length+1];
tupleIncBuild[0] = ""+ build.getNumber();
System.arraycopy(tuple, 0, tupleIncBuild, 1, tuple.length);
rawTableData.add (tupleIncBuild);

// load existing data
if (tableFile.exists()) {
existingTableReader = new CSVReader(new FileReader(tableFile));
// skip header
existingTableReader.readNext();

int numBuilds;
try {
numBuilds = Integer.parseInt(getNumBuilds());
} catch (NumberFormatException nfe) {
numBuilds = Integer.MAX_VALUE;
}

String [] nextLine;
int count = 0;
while ((nextLine = existingTableReader.readNext()) != null && count++ < numBuilds-1) {
rawTableData.add(nextLine);
}
}

// write to CSV file
newTableWriter = new CSVWriter(new FileWriter(tableFile));
newTableWriter.writeNext(headerIncBuild);
newTableWriter.writeAll(rawTableData);
} catch (IOException ioe) {
//ignore
} finally {
if (existingTableReader != null) {
try {
existingTableReader.close();
} catch (IOException ignore) {
//ignore
}
}
if (newTupleReader != null) {
try {
newTupleReader.close();
} catch (IOException ignore) {
//ignore
}
}
if (newTableWriter != null) {
try {
newTableWriter.close();
} catch (IOException ignore) {
//ignore
}
}
}
}
}
63 changes: 58 additions & 5 deletions src/main/java/hudson/plugins/plot/PlotReport.java
Expand Up @@ -18,6 +18,7 @@
import org.kohsuke.stapler.StaplerResponse;

import au.com.bytecode.opencsv.CSVReader;
import org.apache.commons.lang.StringUtils;

/**
* Represents a plot report for a single group of plots.
Expand Down Expand Up @@ -96,19 +97,71 @@ public boolean getDisplayTableFlag(int i) {

// called from PlotReport/index.jelly
public ArrayList getTable(int i) {
ArrayList tableData = new ArrayList<String[]>();
ArrayList<ArrayList<String>> tableData = new ArrayList<ArrayList<String>>();

Plot plot = getPlot(""+i);

// load existing csv file
File tableFile = new File(project.getRootDir(), "table_"+plot.getCsvFileName());
if (!tableFile.exists()) {
File plotFile = new File(project.getRootDir(), plot.getCsvFileName());
if (!plotFile.exists()) {
return tableData;
}
CSVReader reader = null;
try {
reader = new CSVReader(new FileReader(tableFile));
tableData = (ArrayList)reader.readAll();
reader = new CSVReader(new FileReader(plotFile));
// throw away 2 header lines
reader.readNext(); reader.readNext();
// array containing header titles
ArrayList<String> header = new ArrayList<String>();
header.add(Messages.Plot_Build() + " #");
tableData.add(header);
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
String buildNumber = nextLine[2];
if (project.getBuildByNumber(Integer.parseInt(buildNumber)) == null) {
continue;
}
String seriesLabel = nextLine[1];
// index of the column where the value should be located
int index = header.lastIndexOf(seriesLabel);
if (index <= 0) {
// add header label
index = header.size();
header.add(seriesLabel);
}
ArrayList<String> tableRow = null;
for (int j = 1; j < tableData.size(); j++) {
ArrayList<String> r = tableData.get(j);
if (StringUtils.equals(r.get(0), buildNumber)) {
// found table row corresponding to the build number
tableRow = r;
break;
}
}
// table row corresponding to the build number not found
if (tableRow == null) {
// create table row with build number at first column
tableRow = new ArrayList<String>();
tableRow.add(buildNumber);
tableData.add(tableRow);
}
// set value at index column
String value = nextLine[0];
if (index < tableRow.size()) {
tableRow.set(index, value);
} else {
for (int j = tableRow.size(); j < index; j++) {
tableRow.add(StringUtils.EMPTY);
}
tableRow.add(value);
}
}
int lastColumn = tableData.get(0).size();
for (ArrayList<String> tableRow : tableData) {
for (int j = tableRow.size(); j < lastColumn; j++) {
tableRow.add(StringUtils.EMPTY);
}
}
} catch (IOException ioe) {
//ignore
} finally {
Expand Down

0 comments on commit 3899ab7

Please sign in to comment.