Skip to content

Commit

Permalink
Sort reds by repair time
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Oliveira <[email protected]>
  • Loading branch information
Gabriel Oliveira committed Feb 3, 2017
1 parent f993215 commit 519dbac
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
27 changes: 20 additions & 7 deletions src/main/java/com/evo/jenkinsplugins/redtime/RedInterval.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@
import hudson.model.Job;
import hudson.model.Run;

import javax.annotation.Nonnull;
import java.util.Comparator;
import java.util.GregorianCalendar;

/**
* Created by gabriel on 16/01/17.
*/
public class RedInterval {
public static final Comparator<RedInterval> ORDER_BY_REPAIR_DATE = new Comparator<RedInterval>() {
public int compare(@Nonnull RedInterval lRed, @Nonnull RedInterval rRed) {
long lt = lRed.getRepairTime();
long rt = rRed.getRepairTime();
if (lt > rt) return -1;
if (lt < rt) return 1;
return 0;
}
};
private Job job;
private Run failure;
private Run repair;
Expand Down Expand Up @@ -50,17 +61,19 @@ public void setCalendar(GregorianCalendar calendar) {
}

public long getDuration() {
long failedAt = failure.getStartTimeInMillis() + failure.getDuration();
long repairedAt;
return getRepairTime() - getFailureTime();
}

public long getFailureTime() {
return failure.getStartTimeInMillis() + failure.getDuration();
}

public long getRepairTime() {
if (isFixed()) {
repairedAt = repair.getStartTimeInMillis() + repair.getDuration();
} else {
// Use current timestamp
repairedAt = getCalendar().getTimeInMillis();
return repair.getStartTimeInMillis() + repair.getDuration();
}

return repairedAt - failedAt;
return getCalendar().getTimeInMillis();
}

public String getDurationString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

public class RedtimeReportPortlet extends DashboardPortlet {

Expand All @@ -24,44 +25,58 @@ public RedtimeReportPortlet(String name) {

public List<RedInterval> getReds() {
List<Job> jobs = getDashboardJobs();
List<RedInterval> reds = new ArrayList<>();
PriorityQueue<RedInterval> queue = new PriorityQueue<>(jobs.size(),
RedInterval.ORDER_BY_REPAIR_DATE);

for (Job job : jobs) {
Run lastUnsuccessfulBuild = job.getLastUnsuccessfulBuild();
int i = 0;

while (lastUnsuccessfulBuild != null && (maxReds == 0) || i < maxReds) {
Run repair = lastUnsuccessfulBuild.getNextBuild();
Run initialFailure;
Run previousSuccessfulBuild = lastUnsuccessfulBuild.getPreviousSuccessfulBuild();
if (previousSuccessfulBuild == null) {
// never succeeded, get the first build
initialFailure = lastUnsuccessfulBuild;
while (initialFailure.getPreviousBuild() != null) {
initialFailure = initialFailure.getPreviousBuild();
}
} else {
initialFailure = previousSuccessfulBuild.getNextBuild();
}
if (lastUnsuccessfulBuild != null) {
queue.add(getLastRed(job, lastUnsuccessfulBuild));
}
}

reds.add(new RedInterval(job, repair, initialFailure));
List<RedInterval> reds = new ArrayList<>();
while (queue.peek() != null) {
RedInterval red = queue.poll();
reds.add(red);
if (maxReds > 0 && reds.size() == maxReds) {
break;
}

lastUnsuccessfulBuild = getPreviousUnsuccessfulBuild(initialFailure);
i++;
Run lastUnsuccessfulBuild = getPreviousUnsuccessfulBuild(red.getFailure());
if (lastUnsuccessfulBuild != null) {
queue.add(getLastRed(red.getJob(), lastUnsuccessfulBuild));
}
}

return reds;
}

private RedInterval getLastRed(Job job, Run lastUnsuccessfulBuild) {
Run repair = lastUnsuccessfulBuild.getNextBuild();
Run initialFailure;
Run previousSuccessfulBuild = lastUnsuccessfulBuild.getPreviousSuccessfulBuild();
if (previousSuccessfulBuild == null) {
// never succeeded, get the first build
initialFailure = lastUnsuccessfulBuild;
while (initialFailure.getPreviousBuild() != null) {
initialFailure = initialFailure.getPreviousBuild();
}
} else {
initialFailure = previousSuccessfulBuild.getNextBuild();
}

return new RedInterval(job, repair, initialFailure);
}

/**
* for unit test
*/
protected List<Job> getDashboardJobs() {
return getDashboard().getJobs();
}

protected Run getPreviousUnsuccessfulBuild(Run build) {
private Run getPreviousUnsuccessfulBuild(Run build) {
Run r = build.getPreviousBuild();
while (r != null && r.getResult() == Result.SUCCESS)
r = r.getPreviousBuild();
Expand Down

0 comments on commit 519dbac

Please sign in to comment.