Skip to content

Commit 771b971

Browse files
committed
Added 'args' parameter for Saved Search History function
1 parent f05a40f commit 771b971

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

splunk/src/main/java/com/splunk/SavedSearch.java

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ public Job dispatch() throws InterruptedException {
6868
* Runs the saved search using dispatch arguments.
6969
*
7070
* @param args Dispatch arguments: <ul>
71-
* <li>"dispatch.now": A time string that is used to dispatch the search as
71+
* <li>"dispatch.now": A time string that is used to dispatch the search as
7272
* though the specified time were the current time.</li>
73-
* <li>"dispatch.*": Overwrites the value of the search field specified in
73+
* <li>"dispatch.*": Overwrites the value of the search field specified in
7474
* "*".</li>
75-
* <li>"trigger_actions": A Boolean that indicates whether to trigger alert
75+
* <li>"trigger_actions": A Boolean that indicates whether to trigger alert
7676
* actions.</li>
77-
* <li>"force_dispatch": A Boolean that indicates whether to start a new
77+
* <li>"force_dispatch": A Boolean that indicates whether to start a new
7878
* search if another instance of this search is already running.</li></ul>
7979
* @return The search job.
8080
* @throws InterruptedException The InterruptedException instance
@@ -93,7 +93,7 @@ public Job dispatch(Map args) throws InterruptedException {
9393

9494
return job;
9595
}
96-
96+
9797
/**
9898
* Runs the saved search using dispatch arguments.
9999
*
@@ -114,6 +114,28 @@ public Job dispatch(SavedSearchDispatchArgs args) throws InterruptedException {
114114
*/
115115
public Job[] history() {
116116
ResponseMessage response = service.get(actionPath("history"));
117+
AtomFeed feed;
118+
return parseHistoryResponse(response);
119+
}
120+
121+
/**
122+
* Returns an array of search jobs based on passed search arguments
123+
*
124+
* @param args
125+
* @return An array of search jobs
126+
*/
127+
public Job[] history(Map<String, Object> args) {
128+
ResponseMessage response = service.get(actionPath("history"), args);
129+
return parseHistoryResponse(response);
130+
}
131+
132+
/**
133+
* Parses response message from history action path
134+
*
135+
* @param response
136+
* @return result An array of Job
137+
*/
138+
private Job[] parseHistoryResponse(final ResponseMessage response) {
117139
AtomFeed feed;
118140
try {
119141
feed = AtomFeed.parseStream(response.getContent());
@@ -170,7 +192,7 @@ public String getActionEmailCc() {
170192
* <p>
171193
* Generally, this command is a template search pipeline that is realized
172194
* with values from the saved search. To reference saved search field
173-
* values, wrap them in "$". For example, use "$name$" to reference the
195+
* values, wrap them in "$". For example, use "$name$" to reference the
174196
* saved search name, or use "$search$" to reference the search query.
175197
*
176198
* @return The search command (or pipeline).
@@ -630,7 +652,7 @@ public String getActionSummaryIndexName() {
630652
* <p>
631653
* Generally, this command is a template search pipeline that is realized
632654
* with values from the saved search. To reference saved search field
633-
* values, wrap them in "$". For example, use "$name$" to reference the
655+
* values, wrap them in "$". For example, use "$name$" to reference the
634656
* saved search name, or use "$search$" to reference the search query.
635657
*
636658
* @return The search command (or pipeline).
@@ -906,7 +928,7 @@ public int getDispatchMaxCount() {
906928
public int getDispatchMaxTime() {
907929
return getInteger("dispatch.max_time");
908930
}
909-
931+
910932
/**
911933
* Returns how frequently Splunk runs the MapReduce reduce phase
912934
* on accumulated map values.
@@ -928,7 +950,7 @@ public int getDispatchReduceFrequency() {
928950
public boolean getDispatchRtBackfill() {
929951
return getDispatchRealTimeBackfill();
930952
}
931-
953+
932954
/**
933955
* Indicates whether to back fill the real-time window for this search.
934956
* This attribute only applies to real-time searches.
@@ -1235,7 +1257,7 @@ public void setActionEmailCc(String cc) {
12351257
* <p>
12361258
* Generally, this command is a template search pipeline that is realized
12371259
* with values from the saved search. To reference saved search field
1238-
* values, wrap them in "$". For example, use "$name$" to reference the
1260+
* values, wrap them in "$". For example, use "$name$" to reference the
12391261
* saved search name, or use "$search$" to reference the search query.
12401262
*
12411263
* @param command The search command (or pipeline).
@@ -1547,7 +1569,7 @@ public void setActionPopulateLookupTtl(String ttl) {
15471569
* <p>
15481570
* Generally, this command is a template search pipeline that is realized
15491571
* with values from the saved search. To reference saved search field
1550-
* values, wrap them in "$". For example, use "$name$" to reference the
1572+
* values, wrap them in "$". For example, use "$name$" to reference the
15511573
* saved search name, or use "$search$" to reference the search query.
15521574
*
15531575
* @param command The search command (or pipeline).
@@ -1614,7 +1636,7 @@ public void setActionRssTtl(String ttl) {
16141636
* <p>
16151637
* Generally, this command is a template search pipeline that is realized
16161638
* with values from the saved search. To reference saved search field
1617-
* values, wrap them in "$". For example, use "$name$" to reference the
1639+
* values, wrap them in "$". For example, use "$name$" to reference the
16181640
* saved search name, or use "$search$" to reference the search query.
16191641
*
16201642
* @param command The search command (or pipeline).
@@ -1702,7 +1724,7 @@ public void setActionSummaryIndexName(String name) {
17021724
* <p>
17031725
* Generally, this command is a template search pipeline that is realized
17041726
* with values from the saved search. To reference saved search field
1705-
* values, wrap them in "$". For example, use "$name$" to reference the
1727+
* values, wrap them in "$". For example, use "$name$" to reference the
17061728
* saved search name, or use "$search$" to reference the search query.
17071729
*
17081730
* @param command The search command (or pipeline).
@@ -1954,7 +1976,7 @@ public void setDisabled(boolean disabled) {
19541976
public void setDispatchBuckets(String buckets) {
19551977
setDispatchBuckets(Integer.parseInt(buckets));
19561978
}
1957-
1979+
19581980
/**
19591981
* Sets the maximum number of timeline buckets.
19601982
*
@@ -2161,7 +2183,7 @@ public void setRequestUiDispatchView(String view) {
21612183
public void setRestartOnSearchpeerAdd(boolean restart) {
21622184
setRestartOnSearchPeerAdd(restart);
21632185
}
2164-
2186+
21652187
/**
21662188
* Sets whether a real-time search managed by the scheduler is
21672189
* restarted when a search peer becomes available for this saved search.

splunk/src/test/java/com/splunk/SavedSearchTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.junit.Before;
2222
import org.junit.Test;
2323

24+
import java.util.HashMap;
25+
2426
public class SavedSearchTest extends SDKTestCase {
2527
SavedSearchCollection savedSearches;
2628
String savedSearchName;
@@ -429,4 +431,34 @@ public boolean predicate() {
429431
Assert.fail(e.toString());
430432
}
431433
}
434+
435+
@Test
436+
public void testHistoryWithArgs(){
437+
savedSearch.refresh();
438+
Assert.assertEquals(0, savedSearch.history().length);
439+
try {
440+
Job job;
441+
for(int i = 0 ; i < 31 ; i++){
442+
job = savedSearch.dispatch();
443+
while(!job.isReady()){
444+
sleep(2);
445+
}
446+
}
447+
//history without any argument, it will return max 30 jobs only.
448+
Assert.assertEquals(30, savedSearch.history().length);
449+
450+
//history with argument 'count' set to '0' i.e it returns the whole history
451+
HashMap<String, Object> args = new HashMap<String, Object>();
452+
args.put("count", 0);
453+
Assert.assertEquals(31, savedSearch.history(args).length);
454+
455+
//history with argument 'count' set to '10' i.e. it will return only 10 jobs from history
456+
args.put("count", 10);
457+
args.put("sort_dir", "desc");
458+
Assert.assertEquals(10, savedSearch.history(args).length);
459+
460+
} catch (InterruptedException e) {
461+
Assert.fail(e.toString());
462+
}
463+
}
432464
}

0 commit comments

Comments
 (0)