Search in sources :

Example 1 with BlueRunContainer

use of io.jenkins.blueocean.rest.model.BlueRunContainer in project blueocean-plugin by jenkinsci.

the class MultiBranchPipelineImpl method getRuns.

@Override
public BlueRunContainer getRuns() {
    return new BlueRunContainer() {

        @Override
        public Link getLink() {
            return MultiBranchPipelineImpl.this.getLink().rel("runs");
        }

        @Override
        public BlueRun get(String name) {
            return null;
        }

        @Override
        public Iterator<BlueRun> iterator() {
            throw new ServiceException.NotImplementedException("Not implemented");
        }

        /**
             * Fetches maximum up to  MAX_MBP_RUNS_ROWS rows from each branch and does pagination on that.
             *
             * JVM property MAX_MBP_RUNS_ROWS can be used to tune this value to optimize performance for given setup
             */
        @Override
        public Iterator<BlueRun> iterator(int start, int limit) {
            List<BlueRun> c = new ArrayList<>();
            List<BluePipeline> branches;
            // Check for branch filter
            StaplerRequest req = Stapler.getCurrentRequest();
            String branchFilter = null;
            if (req != null) {
                branchFilter = req.getParameter("branch");
            }
            if (!StringUtils.isEmpty(branchFilter)) {
                BluePipeline pipeline = getBranches().get(branchFilter);
                if (pipeline != null) {
                    branches = Collections.singletonList(pipeline);
                } else {
                    branches = Collections.emptyList();
                }
            } else {
                branches = Lists.newArrayList(getBranches().list());
                sortBranchesByLatestRun(branches);
            }
            for (final BluePipeline b : branches) {
                Iterator<BlueRun> it = b.getRuns().iterator(0, MAX_MBP_RUNS_ROWS);
                int count = 0;
                Iterators.skip(it, start);
                while (it.hasNext() && count++ < limit) {
                    c.add(it.next());
                }
            }
            Collections.sort(c, new Comparator<BlueRun>() {

                @Override
                public int compare(BlueRun o1, BlueRun o2) {
                    return o2.getStartTime().compareTo(o1.getStartTime());
                }
            });
            return c.iterator();
        }

        private boolean retry(boolean[] retries) {
            //if at least one of the branch needs retry we will retry it
            for (boolean r : retries) {
                if (r) {
                    return true;
                }
            }
            return false;
        }

        private int computeLimit(boolean[] retries, int limit) {
            //if at least one of the branch needs retry we will retry it
            int count = 0;
            for (boolean r : retries) {
                if (r) {
                    count++;
                }
            }
            if (count == 0) {
                return 0;
            }
            return limit / count > 0 ? limit / count : 1;
        }

        private int collectRuns(List<BluePipeline> branches, List<BlueRun> runs, boolean[] retries, int remainingCount, int[] startIndexes, int[] limits) {
            int count = 0;
            for (int i = 0; i < branches.size(); i++) {
                BluePipeline b = branches.get(i);
                if (!retries[i]) {
                    continue;
                }
                Iterator<BlueRun> it = b.getRuns().iterator(startIndexes[i], limits[i]);
                int lcount = 0;
                while (it.hasNext() && count < remainingCount) {
                    lcount++;
                    count++;
                    runs.add(it.next());
                }
                if (lcount < limits[i]) {
                    //if its less than l
                    //iterator already exhausted so lets not retry next time
                    retries[i] = false;
                } else {
                    //set the new start index for next time
                    startIndexes[i] = startIndexes[i] + lcount;
                }
            }
            return count;
        }

        @Override
        public BlueQueueItem create(StaplerRequest request) {
            throw new ServiceException.NotImplementedException("This action is not supported");
        }
    };
}
Also used : ArrayList(java.util.ArrayList) StaplerRequest(org.kohsuke.stapler.StaplerRequest) BlueRun(io.jenkins.blueocean.rest.model.BlueRun) BlueRunContainer(io.jenkins.blueocean.rest.model.BlueRunContainer) BluePipeline(io.jenkins.blueocean.rest.model.BluePipeline) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BluePipeline (io.jenkins.blueocean.rest.model.BluePipeline)1 BlueRun (io.jenkins.blueocean.rest.model.BlueRun)1 BlueRunContainer (io.jenkins.blueocean.rest.model.BlueRunContainer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 StaplerRequest (org.kohsuke.stapler.StaplerRequest)1