Skip to content

Commit

Permalink
more sorting options now supported
Browse files Browse the repository at this point in the history
by default, length
optional alphabeitcal and  date

Achieved by -t switch
  • Loading branch information
judovana committed Mar 15, 2023
1 parent 1439fbe commit cdcb1d7
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/main/java/hudson/plugins/nested_view/NestedViewsSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,14 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException,
}
}
putToHistory(query, hits.size(), new Date());
switch (this.query.getSort()){
case 2: Collections.sort(hits, new NestedViewsSearchResult.DateComparator()); break;
case 3: Collections.sort(hits, new NestedViewsSearchResult.NameComparator()); break;
default: Collections.sort(hits, new NestedViewsSearchResult.LenghtComparator()); break;
}
} else {
Collections.sort(hits, new NestedViewsSearchResult.LenghtComparator());
}
Collections.sort(hits);
//todo, add paging &start=&count= .. defaulting to 0 and somwhere on 1000. Probably add next/prev links to jelly. Include `showing x/form` in jelly
RequestDispatcher v = req.getView(this, "search-results.jelly");
v.forward(req, rsp);
Expand Down Expand Up @@ -151,6 +157,10 @@ public List<HelpItem> getSearchHelp() throws IOException {
r.add(new HelpItem("n", "search only in nested views (default is in all -jw (-jvn))"));
r.add(new HelpItem("w", "search only in views and nested views (default is in all -jw (-jvn))"));
r.add(new HelpItem("!", "invert result"));
r.add(new HelpItem("t", "sort results; have digital parameter:"));
r.add(new HelpItem("1", "default - by lenght of items"));
r.add(new HelpItem("2", "by date - requires B and/or L"));
r.add(new HelpItem("3", "alphabetically"));
r.add(new HelpItem("Xn", "for NEXTn searches Nested View search will be turned off. n is optional number 1-9"));
r.add(new HelpItem("eg \"-Rjo: dacapo sp[ei]c\"", "will find all Jobs which Matches .*dacapo.* or .*sp[ei]c.* "));
r.add(new HelpItem(" Project/build details in search: ", ""));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.plugins.nested_view.search;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.Result;
import hudson.model.Run;
import jenkins.model.Jenkins;
Expand All @@ -19,6 +20,7 @@ public BuildDetails(String prefix, Run run) {
this(prefix, run.getId(), run.getDisplayName(), run.getResult(), run.getTimestampString(), run.getTime());
}

@SuppressFBWarnings(value = {"EI_EXPOSE_REP2"}, justification = "date is not cared")
public BuildDetails(String prefix, String id, String displayName, Result result, String timeStampString, Date dateTime) {
this.prefix = prefix;
this.id = id;
Expand Down Expand Up @@ -62,7 +64,7 @@ public static String getJenkinsUrl() {
return Jenkins.get().getRootUrl().replaceAll("[\\/]+$", "");
}

public Date getDateTime() {
return dateTime;
public long getDateTime() {
return dateTime.getTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import hudson.search.SearchIndex;
import hudson.search.SearchItem;

import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Optional;

public class NestedViewsSearchResult implements SearchItem, Comparable, ExtendedSearch {
public class NestedViewsSearchResult implements SearchItem, ExtendedSearch {
private final String searchName;
private final String searchUrl;
private final ProjectWrapper project;
Expand Down Expand Up @@ -54,12 +56,6 @@ public String toPlainOldHref() {
return "<a href=\"" + searchUrl + "\">" + searchName + "</a>";
}

@Override
@SuppressFBWarnings(value = {"EQ_COMPARETO_USE_OBJECT_EQUALS"}, justification = "intentional. We check the types when filling the allCached, and the classes have not much in common")
public int compareTo(Object o) {
return this.toString().length() - o.toString().length();
}

@Override
public ProjectWrapper getProject() {
return project;
Expand All @@ -68,4 +64,38 @@ public ProjectWrapper getProject() {
public void createDetails() {
project.createDetails();
}

public static class LenghtComparator implements Comparator<NestedViewsSearchResult>, Serializable {

@Override
public int compare(NestedViewsSearchResult a, NestedViewsSearchResult b) {
return a.toString().length() - b.toString().length();
}
}

public static class NameComparator implements Comparator<NestedViewsSearchResult>, Serializable {

@Override
public int compare(NestedViewsSearchResult a, NestedViewsSearchResult b) {
return a.toString().compareTo(b.toString());
}
}

public static class DateComparator implements Comparator<NestedViewsSearchResult>, Serializable {

@Override
public int compare(NestedViewsSearchResult a, NestedViewsSearchResult b) {
if (a.getDate() == b.getDate()) {
return 0;
} else if (a.getDate() > b.getDate()) {
return -1;
} else {
return 1;
}
}
}

private long getDate() {
return project.getDateTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ public List<LinkableCandidate> createDetailsImpl() {
}

private void setDateTime(BuildDetails build) {
dateTime = new Date(Math.max(dateTime.getTime(), build.getDateTime().getTime()));
dateTime = new Date(Math.max(dateTime.getTime(), build.getDateTime()));
}

public Date getDateTime() {
return dateTime;
public long getDateTime() {
return dateTime.getTime();
}

public boolean isMultiline() {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/hudson/plugins/nested_view/search/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Query {
private int stats = -1;
private int builds = -1;
private int last = -1;
private int sort = 1;

public String getOriginal() {
return original;
Expand All @@ -36,6 +37,10 @@ public String getPart() {
return part;
}

public int getSort() {
return sort;
}

private String where = "vnj"; //v,n,j
private String how = "c"; //c,s,e,r,R,q,Q
private String bool = ""; //a,o,""
Expand Down Expand Up @@ -111,6 +116,9 @@ public Query(boolean search, String ooriginal) {
} else {
last = -1;
}
if (query.contains("t")) {
sort = getNumber(query, "t", 1);
}
if (query.contains("j") || query.contains("v") || query.contains("n") || query.contains("w")) {
where = "";
}
Expand Down

0 comments on commit cdcb1d7

Please sign in to comment.