Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a generic iterator over build results #269

Merged
merged 14 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"doc": "doc"
},
"dependencies": {
"echarts": "^5.4.0",
"echarts": "^5.4.1",
"remark-cli": "^11.0.0",
"remark-lint": "^9.1.1",
"remark-preset-lint-recommended": "^6.1.2"
Expand Down
20 changes: 5 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>analysis-pom</artifactId>
<version>5.37.0</version>
<version>6.2.0</version>
<relativePath />
</parent>

Expand All @@ -18,14 +18,13 @@
<url>https://github.com/jenkinsci/echarts-api-plugin</url>

<properties>
<revision>5.4.0-2</revision>
<revision>5.4.1-1</revision>
<changelist>-SNAPSHOT</changelist>

<module.name>${project.groupId}.echarts</module.name>

<echarts-build-trends.version>3.3.0</echarts-build-trends.version>
<eclipse-collections.version>9.2.0</eclipse-collections.version>

<echarts-build-trends.version>4.1.0</echarts-build-trends.version>
<plugin-util-api.version>3.0.0-rc693.c098b_871ea_49</plugin-util-api.version>
</properties>

<licenses>
Expand All @@ -48,6 +47,7 @@
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>plugin-util-api</artifactId>
<version>${plugin-util-api.version}</version>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
Expand Down Expand Up @@ -101,16 +101,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-api</artifactId>
<version>${eclipse-collections.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId>
<version>${eclipse-collections.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jackson2-api</artifactId>
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/io/jenkins/plugins/echarts/ActionSelector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.jenkins.plugins.echarts;

import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;

import hudson.model.Run;

import io.jenkins.plugins.util.BuildAction;

/**
* Selects a specific action from the all actions that are attached to the given build. The action is selected by a
* generic predicate that works on the expected concrete action type. If the baseline build does not contain the action
* then previous builds will be inspected until the action is found.
*
* @param <T>
* the type of the action to select
*/
public class ActionSelector<T extends BuildAction<?>> implements Function<Run<?, ?>, Optional<T>> {
private final Class<T> actionType;
private final Predicate<? super T> predicate;

/**
* Creates a new instance of {@link ActionSelector}. This selector will select the first action of the given type
* that matches.
*
* @param actionType
* the type of the action to select
*/
public ActionSelector(final Class<T> actionType) {
this(actionType, action -> true);
}

/**
* Creates a new instance of {@link ActionSelector}.
*
* @param actionType
* the type of the action to select
* @param predicate
* the predicate that selects the action (if there are multiple actions of the same type)
*/
public ActionSelector(final Class<T> actionType, final Predicate<? super T> predicate) {
this.actionType = actionType;
this.predicate = predicate;
}

@Override
public Optional<T> apply(final Run<?, ?> baseline) {
for (Run<?, ?> run = baseline; run != null; run = run.getPreviousBuild()) {
Optional<T> action = run.getActions(actionType)
.stream()
.filter(predicate)
.findAny();
if (action.isPresent()) {
return action;
}
}

return Optional.empty();
}

/**
* Searches in the build history for the first action that matches the specified predicate.
*
* @param baseline
* the baseline build to start the search from
*
* @return the action, if found
*/
public final Optional<T> findFirst(final Run<?, ?> baseline) {
return apply(baseline);
}
}
29 changes: 2 additions & 27 deletions src/main/java/io/jenkins/plugins/echarts/BuildActionIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

/**
* Iterates over a collection of builds that contain results of a given generic type. These results are available via a
* given sub type of {@link BuildAction} that has to be attached to each build of the selected job. A new iterator
* given subtype of {@link BuildAction} that has to be attached to each build of the selected job. A new iterator
* starts from a baseline build where it selects the attached action of the given type. Then it moves back in the build
* history until no more builds are available.
*
Expand Down Expand Up @@ -65,7 +65,7 @@ public boolean hasNext() {

@Override
public BuildResult<T> next() {
if (!latestAction.isPresent()) {
if (latestAction.isEmpty()) {
throw new NoSuchElementException(
"There is no action available anymore. Use hasNext() before calling next().");
}
Expand All @@ -78,29 +78,4 @@ public BuildResult<T> next() {
Build build = new Build(run.getNumber(), run.getDisplayName(), buildTimeInSeconds);
return new BuildResult<>(build, buildAction);
}

private static class ActionSelector<T extends BuildAction<?>> implements Function<Run<?, ?>, Optional<T>> {
private final Class<T> actionType;
private final Predicate<? super T> predicate;

ActionSelector(final Class<T> actionType, final Predicate<? super T> predicate) {
this.actionType = actionType;
this.predicate = predicate;
}

@Override
public Optional<T> apply(final Run<?, ?> baseline) {
for (Run<?, ?> run = baseline; run != null; run = run.getPreviousBuild()) {
Optional<T> action = run.getActions(actionType)
.stream()
.filter(predicate)
.findAny();
if (action.isPresent()) {
return action;
}
}

return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package io.jenkins.plugins.echarts;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;

import edu.hm.hafner.echarts.Build;
import edu.hm.hafner.echarts.BuildResult;

import hudson.model.Run;

import io.jenkins.plugins.util.BuildAction;

/**
* Iterates over a collection of builds that contain results of a given generic type. These results are available via a
* given subtype of {@link BuildAction} that has to be attached to each build of the selected job. A new iterator starts
* from a baseline build where it selects the attached action of the given type. Then it moves back in the build history
* until no more builds are available.
*
* @param <A>
* the type of the action
* @param <R>
* the type of the result
*
* @author Ullrich Hafner
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public class GenericBuildActionIterator<A extends BuildAction<?>, R> implements Iterator<BuildResult<R>> {
private final ActionSelector<A> actionSelector;
private Optional<A> latestAction;
private final Function<A, R> function;

GenericBuildActionIterator(final Class<A> actionType, final Optional<A> latestAction,
final Predicate<A> predicate, final Function<A, R> function) {
this.latestAction = latestAction;
this.function = function;
actionSelector = new ActionSelector<>(actionType, predicate);
}

@Override
public boolean hasNext() {
return latestAction.isPresent();
}

@Override
public BuildResult<R> next() {
if (latestAction.isEmpty()) {
throw new NoSuchElementException(
"There is no action available anymore. Use hasNext() before calling next().");
}

A buildAction = latestAction.get();
Run<?, ?> run = buildAction.getOwner();
latestAction = actionSelector.apply(run.getPreviousBuild());

int buildTimeInSeconds = (int) (run.getTimeInMillis() / 1000);
Build build = new Build(run.getNumber(), run.getDisplayName(), buildTimeInSeconds);

return new BuildResult<>(build, function.apply(buildAction));
}

/**
* An iterable that provides an iterator for specific values of build that should be rendered in a trend chart. The
* build values must be stored in a concrete subclass of {@link BuildAction}. The property that contains the build value
* is selected by a generic predicate.
*
* @param <A>
* the type of the action
* @param <R>
* the type of the result
*/
public static class BuildActionIterable<A extends BuildAction<?>, R> implements Iterable<BuildResult<R>> {
private final Class<A> actionType;
private final Optional<A> latestAction;
private final Predicate<A> filter;
private final Function<A, R> function;

/**
* Creates a new instance of {@link BuildActionIterable}.
*
* @param actionType
* the type of the action to select
* @param latestAction
* the latest action that will be used as starting point for the sequence of results
* @param filter
* filter that selects the action (if there are multiple actions of the same type)
* @param function
* the supplier that extracts the specific results from the action
*/
public BuildActionIterable(final Class<A> actionType, final Optional<A> latestAction,
final Predicate<A> filter, final Function<A, R> function) {
this.actionType = actionType;
this.latestAction = latestAction;
this.filter = filter;
this.function = function;
}

@Override
public Iterator<BuildResult<R>> iterator() {
return new GenericBuildActionIterator<>(actionType, latestAction, filter, function);
}
}
}
Loading