Splunk Search

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

bkeif
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
1 Solution

lquinn
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

lquinn
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

bkeif
Path Finder

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

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Observability Simplified: Combining User Experience, Application Performance & ...

Tech Talk Observability Simplified: Combining User Experience, Application Performance & Network ...

Event Series May & June: From Network Visibility to Service Intelligence

Unifying the Network: Moving from Alert Noise to Service Intelligence with Splunk ITSI In today’s hybrid ...