Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #13 from olivergondza/fix-matrix
[FIXED JENKINS-24666] No data on the plot when using matrix project
  • Loading branch information
ericbn committed Sep 13, 2014
2 parents 0fc3da7 + 3e79b7c commit 6d69034
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 16 deletions.
15 changes: 9 additions & 6 deletions src/main/java/hudson/plugins/plot/MatrixPlotPublisher.java
Expand Up @@ -3,8 +3,8 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixRun;
import hudson.matrix.MatrixProject;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
Expand All @@ -22,6 +22,7 @@
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;
Expand All @@ -37,7 +38,10 @@ public class MatrixPlotPublisher extends AbstractPlotPublisher {

transient private Map<String, List<Plot>> groupMap = new HashMap<String, List<Plot>>();

private List<Plot> plots = new ArrayList<Plot>(); //plots setting for matrix project
/**
* Configured plots.
*/
private List<Plot> plots = new ArrayList<Plot>();

public String urlGroupToOriginalGroup(String urlGroup, MatrixConfiguration c) {
if (urlGroup == null || "nogroup".equals(urlGroup)) {
Expand Down Expand Up @@ -148,7 +152,7 @@ public Action getProjectAction(AbstractProject<?, ?> project) {
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);
Plot plot = new Plot(p.title, p.yaxis, p.group, p.numBuilds, p.csvFileName, p.style, p.useDescr, p.getKeepRecords());
plot.series = p.series;
plot.setProject((MatrixConfiguration) build.getProject());
addPlot(plot);
Expand All @@ -160,14 +164,13 @@ public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws IOException, InterruptedException {
// Should always be a Build due to isApplicable below
if (!(build instanceof MatrixBuild)) {
if (!(build instanceof MatrixRun)) {
return true;
}
listener.getLogger().println("Recording plot data");

// add the build to each plot
for (Plot plot : plotsOfConfigurations.get((MatrixConfiguration) build.getProject())) {
for (Plot plot : plotsOfConfigurations.get(((MatrixRun) build).getProject())) {
plot.addBuild(build, listener.getLogger());
}
// misconfigured plots will not fail a build so always return true
Expand Down
45 changes: 39 additions & 6 deletions src/main/java/hudson/plugins/plot/Plot.java
Expand Up @@ -29,8 +29,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jfree.chart.ChartFactory;
Expand All @@ -50,6 +48,9 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;

/**
* Represents the configuration for a single plot. A plot can
* have one or more data series (lines). Each data series
Expand Down Expand Up @@ -177,14 +178,17 @@ public class Plot implements Comparable<Plot> {
/** Whether or not to use build descriptions as X-axis labels. Optional. */
public boolean useDescr;

/** keep records for builds that was deleted */
private boolean keepRecords;

/**
* Creates a new plot with the given paramenters. If numBuilds
* is the empty string, then all builds will be included. Must
* not be zero.
*/
@DataBoundConstructor
public Plot(String title, String yaxis,
String group, String numBuilds, String csvFileName, String style, boolean useDescr)
String group, String numBuilds, String csvFileName, String style, boolean useDescr, boolean keepRecords)
{
this.title = title;
this.yaxis = yaxis;
Expand All @@ -197,11 +201,24 @@ public Plot(String title, String yaxis,
this.csvFileName = csvFileName;
this.style = style;
this.useDescr = useDescr;
this.keepRecords = keepRecords;
}

/**
* @deprecated Kept for backward compatibility.
*/
@Deprecated
public Plot(String title, String yaxis, String group, String numBuilds, String csvFileName, String style, boolean useDescr) {
this(title, yaxis, group, numBuilds, csvFileName, style, useDescr, false);
}

// needed for serialization
public Plot() {}

public boolean getKeepRecords() {
return keepRecords;
}

public int compareTo(Plot o) {
return title.compareTo(o.getTitle());
}
Expand Down Expand Up @@ -542,8 +559,7 @@ public String numDateString() {
int buildNum;
try {
buildNum = Integer.valueOf(record[2]);
if (project.getBuildByNumber(buildNum) == null
|| buildNum > getRightBuildNum()) {
if (!reportBuild(buildNum) || buildNum > getRightBuildNum()) {
continue; // skip this record
}
} catch (NumberFormatException nfe) {
Expand Down Expand Up @@ -736,7 +752,7 @@ private void savePlotData() {
writer.writeNext(header2);
// write each entry of rawPlotData to a new line in the CSV file
for (String[] entry : rawPlotData) {
if (project.getBuildByNumber(Integer.parseInt(entry[2])) != null) {
if (reportBuild(Integer.parseInt(entry[2]))) {
writer.writeNext(entry);
}
}
Expand All @@ -752,4 +768,21 @@ private void savePlotData() {
}
}
}

/**
* @return true if the build should be part of the graph.
*/
/*package*/ boolean reportBuild(int buildNumber) {
int numBuilds;
try {

numBuilds = Integer.parseInt(this.numBuilds);
} catch (NumberFormatException ex) {
// Report all builds
numBuilds = Integer.MAX_VALUE;
}

if (buildNumber < project.getNextBuildNumber() - numBuilds) return false;
return keepRecords || project.getBuildByNumber(buildNumber) != null;
}
}
8 changes: 4 additions & 4 deletions src/main/java/hudson/plugins/plot/PlotReport.java
Expand Up @@ -9,18 +9,18 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

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

/**
* Represents a plot report for a single group of plots.
Expand Down Expand Up @@ -120,7 +120,7 @@ public List<List<String>> getTable(int i) {
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
String buildNumber = nextLine[2];
if (project.getBuildByNumber(Integer.parseInt(buildNumber)) == null) {
if (!plot.reportBuild(Integer.parseInt(buildNumber))) {
continue;
}
String seriesLabel = nextLine[1];
Expand Down
Expand Up @@ -42,6 +42,10 @@
<f:checkbox name="useDescr" checked="${plot.useDescr}" />
</f:entry>

<f:entry title="${%Keep records for deleted builds}" help="/plugin/plot/help-keepRecords.html">
<f:checkbox name="keepRecords" checked="${plot.keepRecords}" />
</f:entry>

<f:entry title="" description="${%A new data series definition}">
<f:repeatable var="series" items="${plot.series}" minimum="1">
<table width="100%" bgcolor="#EEEEEE">
Expand Down
5 changes: 5 additions & 0 deletions src/main/webapp/help-keepRecords.html
@@ -0,0 +1,5 @@
<div>
When unchecked (default), data from deleted builds are not part of the graph.
<p/>
When checked, show all builds up to 'Number of builds to include'.
</div>

0 comments on commit 6d69034

Please sign in to comment.