Skip to content
This repository has been archived by the owner on Nov 6, 2018. It is now read-only.

Commit

Permalink
Updated to later parent plugin and fixed findbugs bugs ...
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdavies committed Mar 14, 2017
1 parent 746d06c commit 681b9d5
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 120 deletions.
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.609.1</version>
<version>2.3</version>
<relativePath></relativePath>
</parent>
<groupId>org.jenkins-ci.plugins.fabric8</groupId>
<artifactId>fabric8-jenkins-workflow-steps</artifactId>
<version>1.0</version>
<version>1.1</version>
<packaging>hpi</packaging>

<name>Fabric8 Workflow Steps</name>
Expand All @@ -24,9 +24,9 @@


<properties>
<git.version>2.4.0</git.version>
<jackson2.version>2.6.2</jackson2.version>
<workflow.api.version>1.8</workflow.api.version>
<git.version>2.5.2</git.version>
<jackson2.version>2.8.5</jackson2.version>
<workflow.api.version>2.4</workflow.api.version>
</properties>

<organization>
Expand Down Expand Up @@ -83,7 +83,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4</version>
<version>4.4.6</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand All @@ -98,7 +98,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static BuildDTO createBuildDTO(WorkflowJob job, WorkflowRun build) {
return null;
}
Run.Summary summary = build.getBuildStatusSummary();
String summaryMessage = (summary != null) ? summary.message : null;
String summaryMessage = summary.message;
String result = Runs.getResultText(build);
Map<String,Object> parameters = Runs.getBuildParameters(build);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jenkinsci.plugins.fabric8.support.FlowNodes;
import org.jenkinsci.plugins.fabric8.support.Jobs;
import org.jenkinsci.plugins.fabric8.support.Runs;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;

Expand All @@ -34,17 +35,15 @@
import java.util.List;
import java.util.Map;

import static org.jenkinsci.plugins.fabric8.support.FlowNodes.getSortedStageNodes;

/**
*/
public class JobCollector extends Collector {
private static final String DEFAULT_NAMESPACE = "default";

private String fullname = "builds";
private double[] buildHistogramBuckets = { 1000, 30000, 60000 };
//private double[] buildHistogramBuckets = { 1000, 30000, 60000 };
private String subsystem = "jenkins";
private String namespace;
private String namespace = "";

public JobCollector() {
namespace = System.getenv("KUBERNETES_NAMESPACE");
Expand Down Expand Up @@ -109,9 +108,14 @@ protected void appendJobMetrics(List<MetricFamilySamples> mfsList, Job job) {

if (build instanceof WorkflowRun) {
WorkflowRun workflowRun = (WorkflowRun) build;
List<FlowNode> stages = getSortedStageNodes(workflowRun.getExecution());
for (FlowNode stage : stages) {
observeStage(stageCollectorMap, job, build, stage);
FlowExecution flowExecution = workflowRun.getExecution();
if (flowExecution != null) {
List<FlowNode> stages = FlowNodes.getSortedStageNodes(flowExecution);
if (stages != null) {
for (FlowNode stage : stages) {
observeStage(stageCollectorMap, job, build, stage);
}
}
}
}
}
Expand All @@ -128,7 +132,7 @@ protected void appendJobMetrics(List<MetricFamilySamples> mfsList, Job job) {

mfsList.addAll(summary.collect());
Collection<Summary> stageCollectors = stageCollectorMap.values();
for (Summary stageCollector : stageCollectorMap.values()) {
for (Summary stageCollector : stageCollectors) {
mfsList.addAll(stageCollector.collect());
}
//mfsList.addAll(histogram.collect());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import hudson.Extension;
import hudson.model.Job;
import hudson.model.UnprotectedRootAction;
import io.prometheus.client.CollectorRegistry;
import org.jenkinsci.plugins.fabric8.dto.JobMetricDTO;
import org.jenkinsci.plugins.fabric8.prometheus.JobCollector;
import org.jenkinsci.plugins.fabric8.support.Callback;
import org.jenkinsci.plugins.fabric8.support.JSONHelper;
import org.jenkinsci.plugins.fabric8.support.Jobs;
Expand All @@ -33,8 +31,8 @@
*/
@Extension
public class MetricsAction implements UnprotectedRootAction {
private CollectorRegistry collectorRegistry;
private JobCollector jobCollector = new JobCollector();
//private CollectorRegistry collectorRegistry;
//private JobCollector jobCollector = new JobCollector();

@Override
public String getIconFileName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@
import org.jenkinsci.plugins.fabric8.support.LogHelper;
import org.jenkinsci.plugins.fabric8.support.hack.AnnotatedLargeText;
import org.jenkinsci.plugins.fabric8.support.hack.AnnotatedLargeTexts;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.framework.io.CharSpool;
import org.kohsuke.stapler.framework.io.LineEndNormalizingWriter;

import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -65,9 +61,10 @@ public Object doIndex() {
}

public Object doStages() {
Object result = null;
WorkflowRun run = getTarget();
final BuildDTO buildDTO = BuildDTO.createBuildDTO(run.getParent(), run);
if (run != null) {
final BuildDTO buildDTO = BuildDTO.createBuildDTO(run.getParent(), run);
Callback<FlowNode> callback = new Callback<FlowNode>() {

@Override
Expand All @@ -80,8 +77,9 @@ public void invoke(FlowNode node) {
};
FlowNodes.forEach(run.getExecution(), callback);
FlowNodes.sortInStageIdOrder(buildDTO.getStages());
result = JSONHelper.jsonResponse(buildDTO);
}
return JSONHelper.jsonResponse(buildDTO);
return result;
}

public Object doLog() throws IOException {
Expand Down Expand Up @@ -109,11 +107,14 @@ public Object doNodes() {
WorkflowRun run = getTarget();
List<NodeDTO> answer = new ArrayList<NodeDTO>();
if (run != null) {
List<FlowNode> nodes = FlowNodes.getSortedFlowNodes(run.getExecution());
for (FlowNode node : nodes) {
NodeDTO dto = NodeDTO.createNodeDTO(node);
if (dto != null) {
answer.add(dto);
FlowExecution flowExecution = run.getExecution();
if (flowExecution != null) {
List<FlowNode> nodes = FlowNodes.getSortedFlowNodes(flowExecution);
for (FlowNode node : nodes) {
NodeDTO dto = NodeDTO.createNodeDTO(node);
if (dto != null) {
answer.add(dto);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ public static HttpResponse jsonResponse(final Object payload) {
@Override
public void generateResponse(StaplerRequest request, StaplerResponse response, Object node) throws IOException, ServletException {
response.setContentType("application/json");
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
objectMapper.writeValue(buffer, payload);
response.getOutputStream().write(buffer.toByteArray());
if (payload != null) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
objectMapper.writeValue(buffer, payload);
response.getOutputStream().write(buffer.toByteArray());
}
}
};
}
Expand Down
87 changes: 45 additions & 42 deletions src/main/java/org/jenkinsci/plugins/fabric8/support/LogHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,54 +39,57 @@ public static HttpResponse jsonResponse(final AnnotatedLargeText text, boolean b
req.setAttribute("html", Boolean.valueOf(true));
}

if (text != null) {
long start = getLongParameter(req, "start", 0);
long first = getLongParameter(req, "first", -1);
int size = getIntParameter(req, "size", DEFAULT_SIZE);
boolean tail = getBoolParameter(req, "tail");

long start = getLongParameter(req, "start", 0);
long first = getLongParameter(req, "first", -1);
int size = getIntParameter(req, "size", DEFAULT_SIZE);
boolean tail = getBoolParameter(req, "tail");

if (text.length() < start) {
start = 0; // text rolled over
}
if (size < 0) {
size = DEFAULT_SIZE;
}
if (size > MAX_SIZE) {
size = MAX_SIZE;
}
boolean backwards = false;
long logLength = text.length();
if (tail) {
if (start == logLength) {
// lets return earlier data!
// if we've data to be read before 'first'
if (first > 0) {
backwards = true;
start = first - size;
if (first < size) {
// lets not return too much data as we're getting the first little chunk
size = (int) first;
if (text.length() < start) {
start = 0; // text rolled over
}
if (size < 0) {
size = DEFAULT_SIZE;
}
if (size > MAX_SIZE) {
size = MAX_SIZE;
}
boolean backwards = false;
long logLength = text.length();
if (tail) {
if (start == logLength) {
// lets return earlier data!
// if we've data to be read before 'first'
if (first > 0) {
backwards = true;
start = first - size;
if (first < size) {
// lets not return too much data as we're getting the first little chunk
size = (int) first;
}
}
}
}
// lets support jumping to the end of the file if its long
if (start == 0) {
long end = start + size;
if (end < logLength) {
start = logLength - size;
// lets support jumping to the end of the file if its long
if (start == 0) {
long end = start + size;
if (end < logLength) {
start = logLength - size;
}
}
}
if (start < 0) {
start = 0;
}
StringWriter buffer = new StringWriter();
long textSize = text.writeLogTo(start, size, new LineEndNormalizingWriter(buffer));
boolean completed = text.isComplete();
long returnedLength = textSize - start;
LogsDTO logsDTO = new LogsDTO(completed, start, returnedLength, backwards, logLength);
logsDTO.setLogText(buffer.toString());
return JSONHelper.jsonResponse(logsDTO);
}
if (start < 0) {
start = 0;
}
StringWriter buffer = new StringWriter();
long textSize = text.writeLogTo(start, size, new LineEndNormalizingWriter(buffer));
boolean completed = text.isComplete();
long returnedLength = textSize - start;
LogsDTO logsDTO = new LogsDTO(completed, start, returnedLength, backwards, logLength);
logsDTO.setLogText(buffer.toString());
return JSONHelper.jsonResponse(logsDTO);
return JSONHelper.jsonResponse(null);

}

public static int getIntParameter(StaplerRequest req, String name, int defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.jenkinsci.plugins.fabric8.support;

import hudson.model.TaskListener;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
Expand All @@ -29,6 +28,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;

Expand Down Expand Up @@ -73,19 +73,28 @@ public static HttpHost getHost(TaskListener listener, String name, String protoc
}

public static String parse(CloseableHttpResponse response) throws IOException {
StringBuilder builder = new StringBuilder();
InputStreamReader inputStreamReader=null;
BufferedReader bufferedReader = null;
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder builder = new StringBuilder();
inputStreamReader = new InputStreamReader(response.getEntity().getContent(), Charset.defaultCharset());
bufferedReader = new BufferedReader(inputStreamReader);
builder.append("status: "+ response.getStatusLine());
String line = "";
while ((line = rd.readLine()) != null) {
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
builder.append(line);
builder.append("/n");
}
return builder.toString();
} finally {
response.close();
if (inputStreamReader != null){
inputStreamReader.close();
}
if (bufferedReader != null){
bufferedReader.close();
}
}
return builder.toString();
}
}
Loading

0 comments on commit 681b9d5

Please sign in to comment.