Splunk Search
Highlighted

How to call a function every time I run a search?

Path Finder

I have written a small app on the django framework. One of its dashboards has a search bar that should accept either all numbers or a mix of numbers and letters. Based on the user's input (which is the "searchquery" token) it should then assign the "search_by" token to its appropriate value to be used by a search manager.

Here is the JS logic I am using.

    var test = function() {
             var userInput = tokens.get("searchquery");
            bool = isNaN(userInput)
            console.log(userInput + ' is not a number:' + bool);
            if (bool) {
                    tokens.set('search_by', 'email');
            }
            else {
                    tokens.set('search_by', 'id');
            }
    };

I can then call this function when the search bar is clicked but this is not ideal as the user can just press enter when in the search bar instead of clicking the search icon. This will run the search but return no results because the 'search_by' token has not been correctly set.

How can i call this function anytime I run or update a search?

Thanks!

Also:
Any information/links on the Splunk Web Framework's JavaScript would be appreciated. I have of course read the available docs and done some light goggling. There must be some listener already present to update panels when the search is run, etc. If I could just attach my function to this lister that would be great.

0 Karma
Highlighted

Re: How to call a function every time I run a search?

Contributor

You can add a listener to the search like this:

var user_search = splunkjs.mvc.Components.getInstance("user_search");
user_search.on("search:done", function(){
    test_function();
});

You can also do this when a search is started, cancelled, when there are results etc. Look at the events section on this page for other methods.
http://docs.splunk.com/Documentation/WebFramework

View solution in original post

Highlighted

Re: How to call a function every time I run a search?

Path Finder

Thank you for taking the time to answer this rather old question, hopefully it will help others in the community.

0 Karma