Skip to content

Commit cdcb1d7

Browse files
committed
more sorting options now supported
by default, length optional alphabeitcal and date Achieved by -t switch
1 parent 1439fbe commit cdcb1d7

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed

Diff for: src/main/java/hudson/plugins/nested_view/NestedViewsSearch.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,14 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException,
9898
}
9999
}
100100
putToHistory(query, hits.size(), new Date());
101+
switch (this.query.getSort()){
102+
case 2: Collections.sort(hits, new NestedViewsSearchResult.DateComparator()); break;
103+
case 3: Collections.sort(hits, new NestedViewsSearchResult.NameComparator()); break;
104+
default: Collections.sort(hits, new NestedViewsSearchResult.LenghtComparator()); break;
105+
}
106+
} else {
107+
Collections.sort(hits, new NestedViewsSearchResult.LenghtComparator());
101108
}
102-
Collections.sort(hits);
103109
//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
104110
RequestDispatcher v = req.getView(this, "search-results.jelly");
105111
v.forward(req, rsp);
@@ -151,6 +157,10 @@ public List<HelpItem> getSearchHelp() throws IOException {
151157
r.add(new HelpItem("n", "search only in nested views (default is in all -jw (-jvn))"));
152158
r.add(new HelpItem("w", "search only in views and nested views (default is in all -jw (-jvn))"));
153159
r.add(new HelpItem("!", "invert result"));
160+
r.add(new HelpItem("t", "sort results; have digital parameter:"));
161+
r.add(new HelpItem("1", "default - by lenght of items"));
162+
r.add(new HelpItem("2", "by date - requires B and/or L"));
163+
r.add(new HelpItem("3", "alphabetically"));
154164
r.add(new HelpItem("Xn", "for NEXTn searches Nested View search will be turned off. n is optional number 1-9"));
155165
r.add(new HelpItem("eg \"-Rjo: dacapo sp[ei]c\"", "will find all Jobs which Matches .*dacapo.* or .*sp[ei]c.* "));
156166
r.add(new HelpItem(" Project/build details in search: ", ""));

Diff for: src/main/java/hudson/plugins/nested_view/search/BuildDetails.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package hudson.plugins.nested_view.search;
22

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

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

65-
public Date getDateTime() {
66-
return dateTime;
67+
public long getDateTime() {
68+
return dateTime.getTime();
6769
}
6870
}

Diff for: src/main/java/hudson/plugins/nested_view/search/NestedViewsSearchResult.java

+37-7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import hudson.search.SearchIndex;
66
import hudson.search.SearchItem;
77

8+
import java.io.Serializable;
89
import java.util.Collection;
10+
import java.util.Comparator;
911
import java.util.Optional;
1012

11-
public class NestedViewsSearchResult implements SearchItem, Comparable, ExtendedSearch {
13+
public class NestedViewsSearchResult implements SearchItem, ExtendedSearch {
1214
private final String searchName;
1315
private final String searchUrl;
1416
private final ProjectWrapper project;
@@ -54,12 +56,6 @@ public String toPlainOldHref() {
5456
return "<a href=\"" + searchUrl + "\">" + searchName + "</a>";
5557
}
5658

57-
@Override
58-
@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")
59-
public int compareTo(Object o) {
60-
return this.toString().length() - o.toString().length();
61-
}
62-
6359
@Override
6460
public ProjectWrapper getProject() {
6561
return project;
@@ -68,4 +64,38 @@ public ProjectWrapper getProject() {
6864
public void createDetails() {
6965
project.createDetails();
7066
}
67+
68+
public static class LenghtComparator implements Comparator<NestedViewsSearchResult>, Serializable {
69+
70+
@Override
71+
public int compare(NestedViewsSearchResult a, NestedViewsSearchResult b) {
72+
return a.toString().length() - b.toString().length();
73+
}
74+
}
75+
76+
public static class NameComparator implements Comparator<NestedViewsSearchResult>, Serializable {
77+
78+
@Override
79+
public int compare(NestedViewsSearchResult a, NestedViewsSearchResult b) {
80+
return a.toString().compareTo(b.toString());
81+
}
82+
}
83+
84+
public static class DateComparator implements Comparator<NestedViewsSearchResult>, Serializable {
85+
86+
@Override
87+
public int compare(NestedViewsSearchResult a, NestedViewsSearchResult b) {
88+
if (a.getDate() == b.getDate()) {
89+
return 0;
90+
} else if (a.getDate() > b.getDate()) {
91+
return -1;
92+
} else {
93+
return 1;
94+
}
95+
}
96+
}
97+
98+
private long getDate() {
99+
return project.getDateTime();
100+
}
71101
}

Diff for: src/main/java/hudson/plugins/nested_view/search/ProjectWrapper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ public List<LinkableCandidate> createDetailsImpl() {
176176
}
177177

178178
private void setDateTime(BuildDetails build) {
179-
dateTime = new Date(Math.max(dateTime.getTime(), build.getDateTime().getTime()));
179+
dateTime = new Date(Math.max(dateTime.getTime(), build.getDateTime()));
180180
}
181181

182-
public Date getDateTime() {
183-
return dateTime;
182+
public long getDateTime() {
183+
return dateTime.getTime();
184184
}
185185

186186
public boolean isMultiline() {

Diff for: src/main/java/hudson/plugins/nested_view/search/Query.java

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Query {
1919
private int stats = -1;
2020
private int builds = -1;
2121
private int last = -1;
22+
private int sort = 1;
2223

2324
public String getOriginal() {
2425
return original;
@@ -36,6 +37,10 @@ public String getPart() {
3637
return part;
3738
}
3839

40+
public int getSort() {
41+
return sort;
42+
}
43+
3944
private String where = "vnj"; //v,n,j
4045
private String how = "c"; //c,s,e,r,R,q,Q
4146
private String bool = ""; //a,o,""
@@ -111,6 +116,9 @@ public Query(boolean search, String ooriginal) {
111116
} else {
112117
last = -1;
113118
}
119+
if (query.contains("t")) {
120+
sort = getNumber(query, "t", 1);
121+
}
114122
if (query.contains("j") || query.contains("v") || query.contains("n") || query.contains("w")) {
115123
where = "";
116124
}

0 commit comments

Comments
 (0)