Skip to content

Commit

Permalink
[JENKINS-14504] sloccount churn graph
Browse files Browse the repository at this point in the history
- Trend graph with values computed as delta between builds N and N-1 implemented.
- SLOCCount plugin now displays two graphs: absolute and delta.
- Indentation fixed.
  • Loading branch information
mixalturek committed Jan 4, 2014
1 parent 6371a72 commit 1c05513
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 17 deletions.
22 changes: 11 additions & 11 deletions src/main/java/hudson/plugins/sloccount/ReportSummary.java
Expand Up @@ -17,7 +17,7 @@ private ReportSummary(){
}

public static String createReportSummary(List<SloccountLanguageStatistics> current,
List<SloccountLanguageStatistics> previous){
List<SloccountLanguageStatistics> previous){
StringBuilder builder = new StringBuilder();

if(current != null){
Expand Down Expand Up @@ -55,15 +55,15 @@ public static String createReportSummary(List<SloccountLanguageStatistics> curre
}

public static String createReportSummaryDetails(List<SloccountLanguageStatistics> current,
List<SloccountLanguageStatistics> previous){
List<SloccountLanguageStatistics> previous){

StringBuilder builder = new StringBuilder();

if(current != null){

for(SloccountLanguageStatistics language : current){

SloccountLanguageStatistics previousLanguage = null;
SloccountLanguageStatistics previousLanguage = null;

if(previous != null) {
previousLanguage = getLanguage(previous, language.getName());
Expand All @@ -77,7 +77,7 @@ public static String createReportSummaryDetails(List<SloccountLanguageStatistics
}

private static void appendLanguageDetails(SloccountLanguageStatistics current,
SloccountLanguageStatistics previous, StringBuilder builder){
SloccountLanguageStatistics previous, StringBuilder builder){

String strLines = Messages.Sloccount_ReportSummary_Lines();
String strFiles = Messages.Sloccount_ReportSummary_Files();
Expand Down Expand Up @@ -136,7 +136,7 @@ private static int getLineCount(List<SloccountLanguageStatistics> statistics)
lineCount += it.getLineCount();
}

return lineCount;
return lineCount;
}

private static int getFileCount(List<SloccountLanguageStatistics> statistics)
Expand All @@ -147,22 +147,22 @@ private static int getFileCount(List<SloccountLanguageStatistics> statistics)
fileCount += it.getFileCount();
}

return fileCount;
return fileCount;
}

private static int getLanguageCount(List<SloccountLanguageStatistics> statistics)
{
return statistics.size();
}

private static SloccountLanguageStatistics getLanguage(List<SloccountLanguageStatistics> statistics, String name)
static SloccountLanguageStatistics getLanguage(List<SloccountLanguageStatistics> statistics, String name)
{
for(SloccountLanguageStatistics it : statistics) {
for(SloccountLanguageStatistics it : statistics) {
if(it.getName().equals(name)) {
return it;
return it;
}
}
return new SloccountLanguageStatistics(name, 0, 0);
return new SloccountLanguageStatistics(name, 0, 0);
}
}
78 changes: 75 additions & 3 deletions src/main/java/hudson/plugins/sloccount/SloccountChartBuilder.java
@@ -1,14 +1,13 @@
package hudson.plugins.sloccount;

import hudson.plugins.sloccount.model.Language;
import hudson.plugins.sloccount.model.SloccountLanguageStatistics;
import hudson.plugins.sloccount.model.SloccountReport;
import hudson.util.ChartUtil.NumberOnlyBuildLabel;
import hudson.util.DataSetBuilder;
import hudson.util.ShiftedCategoryAxis;

import java.awt.Color;
import java.io.Serializable;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
Expand All @@ -33,7 +32,9 @@ public static JFreeChart buildChart(SloccountBuildAction action){

String strLines = Messages.Sloccount_ReportSummary_Lines();

JFreeChart chart = ChartFactory.createStackedAreaChart(null, null, strLines, buildDataset(action), PlotOrientation.VERTICAL, true, false, true);
JFreeChart chart = ChartFactory.createStackedAreaChart(null, null,
strLines, buildDataset(action), PlotOrientation.VERTICAL,
true, false, true);

chart.setBackgroundPaint(Color.white);

Expand Down Expand Up @@ -82,4 +83,75 @@ private static CategoryDataset buildDataset(SloccountBuildAction lastAction){

return builder.build();
}

public static JFreeChart buildChartDelta(SloccountBuildAction action){

String strLinesDelta = Messages.Sloccount_ReportSummary_Lines()
+ " " + Messages.Sloccount_Trend_Delta();

JFreeChart chart = ChartFactory.createStackedAreaChart(null, null,
strLinesDelta, buildDatasetDelta(action), PlotOrientation.VERTICAL,
true, false, true);

chart.setBackgroundPaint(Color.white);

CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setOutlinePaint(null);
plot.setForegroundAlpha(0.8f);
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.black);

CategoryAxis domainAxis = new ShiftedCategoryAxis(null);
plot.setDomainAxis(domainAxis);
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
domainAxis.setLowerMargin(0.0);
domainAxis.setUpperMargin(0.0);
domainAxis.setCategoryMargin(0.0);

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

// crop extra space around the graph
plot.setInsets(new RectangleInsets(0, 0, 0, 5.0));

SloccountAreaRenderer renderer = new SloccountAreaRenderer(action.getUrlName());
plot.setRenderer(renderer);

return chart;
}

private static CategoryDataset buildDatasetDelta(SloccountBuildAction lastAction){
DataSetBuilder<String, NumberOnlyBuildLabel> builder = new DataSetBuilder<String, NumberOnlyBuildLabel>();

SloccountBuildAction action = lastAction;

while(action != null){
SloccountBuildAction previousAction = action.getPreviousAction();
SloccountResult result = action.getResult();
List<SloccountLanguageStatistics> previousStatistics = null;

if(result != null){
NumberOnlyBuildLabel buildLabel = new NumberOnlyBuildLabel(action.getBuild());

if(previousAction != null && previousAction.getResult() != null){
previousStatistics = previousAction.getResult().getStatistics();
} else {
// This will produce zero delta for the first build
previousStatistics = result.getStatistics();
}

for(SloccountLanguageStatistics current : result.getStatistics()){
SloccountLanguageStatistics previous = ReportSummary.getLanguage(previousStatistics, current.getName());

builder.add(current.getLineCount() - previous.getLineCount(),
current.getName(), buildLabel);
}
}

action = previousAction;
};

return builder.build();
}
}
51 changes: 49 additions & 2 deletions src/main/java/hudson/plugins/sloccount/SloccountProjectAction.java
Expand Up @@ -54,8 +54,8 @@ public void doIndex(final StaplerRequest request, final StaplerResponse response
if (build != null) {
response.sendRedirect2(String.format("../%d/%s", build.getNumber(), SloccountBuildAction.URL_NAME));
}else{
// Click to the link in menu on the job page before the first build
response.sendRedirect2("..");
// Click to the link in menu on the job page before the first build
response.sendRedirect2("..");
}
}

Expand Down Expand Up @@ -154,4 +154,51 @@ public void doTrend(final StaplerRequest request, final StaplerResponse response
CHART_WIDTH,
CHART_HEIGHT);
}

/**
* Display the trend delta map. Delegates to the the associated
* {@link ResultAction}.
*
* @param request
* Stapler request
* @param response
* Stapler response
* @throws IOException
* in case of an error
*/
public void doTrendDeltaMap(final StaplerRequest request, final StaplerResponse response) throws IOException {
AbstractBuild<?,?> lastBuild = this.getLastFinishedBuild();
SloccountBuildAction lastAction = lastBuild.getAction(SloccountBuildAction.class);

ChartUtil.generateClickableMap(
request,
response,
SloccountChartBuilder.buildChartDelta(lastAction),
CHART_WIDTH,
CHART_HEIGHT);
}

/**
* Display the trend delta graph. Delegates to the the associated
* {@link ResultAction}.
*
* @param request
* Stapler request
* @param response
* Stapler response
* @throws IOException
* in case of an error in
* {@link ResultAction#doGraph(StaplerRequest, StaplerResponse, int)}
*/
public void doTrendDelta(final StaplerRequest request, final StaplerResponse response) throws IOException {
AbstractBuild<?,?> lastBuild = this.getLastFinishedBuild();
SloccountBuildAction lastAction = lastBuild.getAction(SloccountBuildAction.class);

ChartUtil.generateGraph(
request,
response,
SloccountChartBuilder.buildChartDelta(lastAction),
CHART_WIDTH,
CHART_HEIGHT);
}
}
Expand Up @@ -8,4 +8,6 @@ Sloccount.ReportSummary.Lines=lines of code
Sloccount.ReportSummary.Files=files
Sloccount.ReportSummary.and=and
Sloccount.ReportSummary.in=in
Sloccount.ReportSummary.Languages=languages
Sloccount.ReportSummary.Languages=languages

Sloccount.Trend.Delta=(delta)
Expand Up @@ -8,6 +8,9 @@
<div>
<img src="${from.urlName}/trend" lazymap="${from.urlName}/trendMap"/>
</div>
<div>
<img src="${from.urlName}/trendDelta" lazymap="${from.urlName}/trendDeltaMap"/>
</div>
</div>
</j:if>
</j:jelly>

0 comments on commit 1c05513

Please sign in to comment.