11package io .jenkins .plugins .agent_build_history ;
22
33import edu .umd .cs .findbugs .annotations .NonNull ;
4- import hudson .Functions ;
5- import hudson .model .BallColor ;
64import hudson .model .Cause ;
5+ import hudson .model .Job ;
6+ import hudson .model .Result ;
77import hudson .model .Run ;
8- import jenkins .console .ConsoleUrlProvider ;
9- import jenkins .util .ProgressiveRendering ;
10- import net .sf .json .JSON ;
11- import net .sf .json .JSONArray ;
12- import net .sf .json .JSONObject ;
13- import org .jenkinsci .plugins .workflow .job .WorkflowRun ;
14- import org .kohsuke .accmod .Restricted ;
15- import org .kohsuke .accmod .restrictions .NoExternalUse ;
16-
178import java .text .SimpleDateFormat ;
189import java .util .ArrayList ;
1910import java .util .Date ;
2011import java .util .List ;
12+ import org .jenkinsci .plugins .workflow .job .WorkflowRun ;
13+ import org .kohsuke .accmod .Restricted ;
14+ import org .kohsuke .accmod .restrictions .NoExternalUse ;
15+ import org .kohsuke .stapler .export .Exported ;
16+ import org .kohsuke .stapler .export .ExportedBean ;
2117
2218@ Restricted (NoExternalUse .class )
23- public class RunListTable extends ProgressiveRendering {
19+ @ ExportedBean (defaultVisibility = 2 )
20+ public class RunListTable {
2421
2522 String computerName ;
2623 private static final double MAX_LIKELY_RUNS = 20 ;
2724 private Iterable <AgentExecution > runs ;
28- private final List <JSONObject > results = new ArrayList <>();
25+ private final List <RunResult > results = new ArrayList <>();
2926
3027 public RunListTable (String computerName ) {
3128 this .computerName = computerName ;
@@ -35,93 +32,97 @@ public void setRuns(Iterable<AgentExecution> runs) {
3532 this .runs = runs ;
3633 }
3734
35+ @ Exported (name = "runs" )
36+ public List <RunResult > getResults () {
37+ return results ;
38+ }
39+
3840 @ NonNull
39- private JSONObject calculate (Run <?, ?> run , AgentExecution execution ) {
40- JSONObject element = new JSONObject ();
41- BallColor iconColor = run .getIconColor ();
42- element .put ("iconColorOrdinal" , iconColor .ordinal ());
43- element .put ("iconColorDescription" , iconColor .getDescription ());
44- element .put ("url" , run .getUrl ());
45- element .put ("number" , run .getNumber ());
46- element .put ("consoleUrl" , ConsoleUrlProvider .getRedirectUrl (run ));
47- element .put ("iconName" , run .getIconColor ().getIconName ());
48- element .put ("parentUrl" , run .getParent ().getUrl ());
49- element .put ("parentFullDisplayName" , Functions .breakableString (Functions .escape (run .getParent ().getFullDisplayName ())));
50- element .put ("displayName" , run .getDisplayName ());
51- element .put ("duration" , run .getDuration ());
52- element .put ("durationString" , run .getDurationString ());
53- element .put ("timestampString" , run .getTimestampString ());
54- element .put ("timestampString2" , run .getTimestampString2 ());
55- element .put ("startTimeInMillis" , run .getStartTimeInMillis ());
56- element .put ("startTimeReadable" , formatStartTime (run .getStartTimeInMillis ()));
57- Run .Summary buildStatusSummary = run .getBuildStatusSummary ();
58- element .put ("buildStatusSummaryWorse" , buildStatusSummary .isWorse );
59- element .put ("buildStatusSummaryMessage" , buildStatusSummary .message );
41+ private RunResult calculate (Run <?, ?> run , AgentExecution execution ) {
42+ RunResult result = new RunResult (run .getParent (), run );
6043 List <Cause > causeList = run .getCauses ();
6144 if (!causeList .isEmpty ()) {
62- element . put ( "shortDescription" , causeList .get (causeList .size () - 1 ).getShortDescription ());
45+ result . setCause ( causeList .get (causeList .size () - 1 ).getShortDescription ());
6346 } else {
64- element . put ( "shortDescription" , "UnknownCause" );
47+ result . setCause ( "UnknownCause" );
6548 }
6649
67- JSONArray flowNodes = new JSONArray ();
6850 if (run instanceof WorkflowRun ) {
6951 for (AgentExecution .FlowNodeExecution nodeExec : execution .getFlowNodes ()) {
7052 if (nodeExec .getNodeName ().equals (computerName )) {
71- flowNodes . add ( calculateFlowNode ( nodeExec ) );
53+ result . addExecution ( nodeExec );
7254 }
7355 }
7456 }
75- element .put ("flowNodes" , flowNodes );
76- return element ;
57+ return result ;
7758 }
7859
79- private JSONObject calculateFlowNode (AgentExecution .FlowNodeExecution node ) {
80- JSONObject element = new JSONObject ();
81- element .put ("duration" , node .getDuration ());
82- element .put ("durationString" , node .getDurationString ());
83- element .put ("startTime" , node .getStartTimeString ());
84- element .put ("startTimeString" , node .getStartTimeSince ());
85- element .put ("flowNodeId" , node .getNodeId ());
86- element .put ("flowNodeStatusWorse" , node .getFlowNodeStatus ().isWorse ());
87- element .put ("flowNodeStatusMessage" , node .getFlowNodeStatus ().getMessage ());
88- return element ;
89- }
60+ @ ExportedBean (defaultVisibility = 2 )
61+ public static class RunResult {
62+ private final Job <?, ?> job ;
63+ private final Run <?, ?> run ;
64+ private String cause ;
65+ private final List <AgentExecution .FlowNodeExecution > executions = new ArrayList <>();
9066
91- private String formatStartTime (long startTimeInMillis ) {
92- SimpleDateFormat formatter = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss" );
93- return formatter .format (new Date (startTimeInMillis ));
67+ public RunResult (Job <?, ?> job , Run <?, ?> run ) {
68+ this .job = job ;
69+ this .run = run ;
70+ }
71+
72+ public void addExecution (AgentExecution .FlowNodeExecution execution ) {
73+ this .executions .add (execution );
74+ }
75+
76+ @ Exported
77+ public List <AgentExecution .FlowNodeExecution > getExecutions () {
78+ return executions ;
79+ }
80+
81+ public void setCause (String cause ) {
82+ this .cause = cause ;
83+ }
84+
85+ @ Exported
86+ public String getCause () {
87+ return cause ;
88+ }
89+
90+ @ Exported
91+ public Job <?, ?> getJob () {
92+ return job ;
93+ }
94+
95+ @ Exported
96+ public String getFullName () {
97+ return job .getFullName ();
98+ }
99+
100+ @ Exported (name = "build" )
101+ public Run <?, ?> getRun () {
102+ return run ;
103+ }
104+
105+ @ Exported
106+ public Result getResult () {
107+ return run .getResult ();
108+ }
109+
110+ @ Exported
111+ public int getNumber () {
112+ return run .getNumber ();
113+ }
114+
115+ public String getStartTime () {
116+ SimpleDateFormat formatter = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss" );
117+ return formatter .format (new Date (run .getStartTimeInMillis ()));
118+ }
94119 }
95120
96- @ Override
97- protected void compute () throws Exception {
98- double decay = 1 ;
99- Functions functions = new Functions ();
121+ void compute () {
100122 for (AgentExecution execution : runs ) {
101- if (canceled ()) {
102- return ;
103- }
104123 Run <?, ?> run = execution .getRun ();
105- JSONObject element = calculate (run , execution );
106- String runId = functions .generateId ();
107- if (run instanceof WorkflowRun ) {
108- element .put ("runId" , runId );
109- } else {
110- element .put ("runId" , "" );
111- }
112- synchronized (this ) {
113- results .add (element );
114- }
115- decay *= 1 - 1 / MAX_LIKELY_RUNS ;
116- progress (1 - decay );
124+ RunResult element = calculate (run , execution );
125+ results .add (element );
117126 }
118127 }
119-
120- @ NonNull
121- @ Override
122- protected synchronized JSON data () {
123- JSONArray d = JSONArray .fromObject (results );
124- results .clear ();
125- return d ;
126- }
127128}
0 commit comments