Skip to content

Added 'args' parameter for Saved Search History function #188

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

Merged
merged 1 commit into from
Jul 12, 2022
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
52 changes: 37 additions & 15 deletions splunk/src/main/java/com/splunk/SavedSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public Job dispatch() throws InterruptedException {
* Runs the saved search using dispatch arguments.
*
* @param args Dispatch arguments: <ul>
* <li>"dispatch.now": A time string that is used to dispatch the search as
* <li>"dispatch.now": A time string that is used to dispatch the search as
* though the specified time were the current time.</li>
* <li>"dispatch.*": Overwrites the value of the search field specified in
* <li>"dispatch.*": Overwrites the value of the search field specified in
* "*".</li>
* <li>"trigger_actions": A Boolean that indicates whether to trigger alert
* <li>"trigger_actions": A Boolean that indicates whether to trigger alert
* actions.</li>
* <li>"force_dispatch": A Boolean that indicates whether to start a new
* <li>"force_dispatch": A Boolean that indicates whether to start a new
* search if another instance of this search is already running.</li></ul>
* @return The search job.
* @throws InterruptedException The InterruptedException instance
Expand All @@ -93,7 +93,7 @@ public Job dispatch(Map args) throws InterruptedException {

return job;
}

/**
* Runs the saved search using dispatch arguments.
*
Expand All @@ -114,6 +114,28 @@ public Job dispatch(SavedSearchDispatchArgs args) throws InterruptedException {
*/
public Job[] history() {
ResponseMessage response = service.get(actionPath("history"));
AtomFeed feed;
return parseHistoryResponse(response);
}

/**
* Returns an array of search jobs based on passed search arguments
*
* @param args
* @return An array of search jobs
*/
public Job[] history(Map<String, Object> args) {
ResponseMessage response = service.get(actionPath("history"), args);
return parseHistoryResponse(response);
}

/**
* Parses response message from history action path
*
* @param response
* @return result An array of Job
*/
private Job[] parseHistoryResponse(final ResponseMessage response) {
AtomFeed feed;
try {
feed = AtomFeed.parseStream(response.getContent());
Expand Down Expand Up @@ -170,7 +192,7 @@ public String getActionEmailCc() {
* <p>
* Generally, this command is a template search pipeline that is realized
* with values from the saved search. To reference saved search field
* values, wrap them in "$". For example, use "$name$" to reference the
* values, wrap them in "$". For example, use "$name$" to reference the
* saved search name, or use "$search$" to reference the search query.
*
* @return The search command (or pipeline).
Expand Down Expand Up @@ -630,7 +652,7 @@ public String getActionSummaryIndexName() {
* <p>
* Generally, this command is a template search pipeline that is realized
* with values from the saved search. To reference saved search field
* values, wrap them in "$". For example, use "$name$" to reference the
* values, wrap them in "$". For example, use "$name$" to reference the
* saved search name, or use "$search$" to reference the search query.
*
* @return The search command (or pipeline).
Expand Down Expand Up @@ -906,7 +928,7 @@ public int getDispatchMaxCount() {
public int getDispatchMaxTime() {
return getInteger("dispatch.max_time");
}

/**
* Returns how frequently Splunk runs the MapReduce reduce phase
* on accumulated map values.
Expand All @@ -928,7 +950,7 @@ public int getDispatchReduceFrequency() {
public boolean getDispatchRtBackfill() {
return getDispatchRealTimeBackfill();
}

/**
* Indicates whether to back fill the real-time window for this search.
* This attribute only applies to real-time searches.
Expand Down Expand Up @@ -1235,7 +1257,7 @@ public void setActionEmailCc(String cc) {
* <p>
* Generally, this command is a template search pipeline that is realized
* with values from the saved search. To reference saved search field
* values, wrap them in "$". For example, use "$name$" to reference the
* values, wrap them in "$". For example, use "$name$" to reference the
* saved search name, or use "$search$" to reference the search query.
*
* @param command The search command (or pipeline).
Expand Down Expand Up @@ -1547,7 +1569,7 @@ public void setActionPopulateLookupTtl(String ttl) {
* <p>
* Generally, this command is a template search pipeline that is realized
* with values from the saved search. To reference saved search field
* values, wrap them in "$". For example, use "$name$" to reference the
* values, wrap them in "$". For example, use "$name$" to reference the
* saved search name, or use "$search$" to reference the search query.
*
* @param command The search command (or pipeline).
Expand Down Expand Up @@ -1614,7 +1636,7 @@ public void setActionRssTtl(String ttl) {
* <p>
* Generally, this command is a template search pipeline that is realized
* with values from the saved search. To reference saved search field
* values, wrap them in "$". For example, use "$name$" to reference the
* values, wrap them in "$". For example, use "$name$" to reference the
* saved search name, or use "$search$" to reference the search query.
*
* @param command The search command (or pipeline).
Expand Down Expand Up @@ -1702,7 +1724,7 @@ public void setActionSummaryIndexName(String name) {
* <p>
* Generally, this command is a template search pipeline that is realized
* with values from the saved search. To reference saved search field
* values, wrap them in "$". For example, use "$name$" to reference the
* values, wrap them in "$". For example, use "$name$" to reference the
* saved search name, or use "$search$" to reference the search query.
*
* @param command The search command (or pipeline).
Expand Down Expand Up @@ -1954,7 +1976,7 @@ public void setDisabled(boolean disabled) {
public void setDispatchBuckets(String buckets) {
setDispatchBuckets(Integer.parseInt(buckets));
}

/**
* Sets the maximum number of timeline buckets.
*
Expand Down Expand Up @@ -2161,7 +2183,7 @@ public void setRequestUiDispatchView(String view) {
public void setRestartOnSearchpeerAdd(boolean restart) {
setRestartOnSearchPeerAdd(restart);
}

/**
* Sets whether a real-time search managed by the scheduler is
* restarted when a search peer becomes available for this saved search.
Expand Down
32 changes: 32 additions & 0 deletions splunk/src/test/java/com/splunk/SavedSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;

public class SavedSearchTest extends SDKTestCase {
SavedSearchCollection savedSearches;
String savedSearchName;
Expand Down Expand Up @@ -429,4 +431,34 @@ public boolean predicate() {
Assert.fail(e.toString());
}
}

@Test
public void testHistoryWithArgs(){
savedSearch.refresh();
Assert.assertEquals(0, savedSearch.history().length);
try {
Job job;
for(int i = 0 ; i < 31 ; i++){
job = savedSearch.dispatch();
while(!job.isReady()){
sleep(2);
}
}
//history without any argument, it will return max 30 jobs only.
Assert.assertEquals(30, savedSearch.history().length);

//history with argument 'count' set to '0' i.e it returns the whole history
HashMap<String, Object> args = new HashMap<String, Object>();
args.put("count", 0);
Assert.assertEquals(31, savedSearch.history(args).length);

//history with argument 'count' set to '10' i.e. it will return only 10 jobs from history
args.put("count", 10);
args.put("sort_dir", "desc");
Assert.assertEquals(10, savedSearch.history(args).length);

} catch (InterruptedException e) {
Assert.fail(e.toString());
}
}
}