Dashboards & Visualizations

Splunk Query using startSearch() is not running in a loop with token

nawneel
Communicator

Hi
I have a splunk query(with javascript) which i would like to run multiple times using javascript loop. Please find below code

`dupcount1=splunkjs.mvc.Components.get("search8");
dupcount1.data("results").on("data", function(results) {

if (DupData == true) {
    var dupcount = results._data['rows'][0][0];
    var i;
    var batchsize = 10
    for (i = 0; i <= dupcount / batchsize; i++) {

        if (i == 0) {
            defaultTokenModel.set("head1", batchsize);

            search4.startSearch();

        } else {
            defaultTokenModel.set("head2", batchsize * (i + 1));
            defaultTokenModel.set("tail", batchsize * i);

            search9.startSearch();

        }
    }

    return true;
}

});`

based on value of i i am setting token and start search. when (i==0) the search runs fine and everything completes graciously. Issue i am facing is when (i !=0) than only for last loop job is triggered. what i would like is that , if there are 9 loops, 9 searches should be triggered. i am not able to understand why only last search is starting in else loop.

Thanks in advance.

0 Karma

DalJeanis
SplunkTrust
SplunkTrust

Probably something wrong with the instantiation / initialization of search9, as opposed to search4 which is working.

Since you didn't show us that portion of the code, we aren't going to be able to tell you exactly what you did wrong, unless search9 is a typo. If search9 is a typo, then you should also check the line with search8.

0 Karma

nawneel
Communicator

HI @DalJeanis

Thanks for replying. i will clear out the air here.

  • I am using a for loop and where variable i decides the batch size
  • inside loop there is if condition, if satified one condition it starts search4, till now everythings work great.
  • In else Condition, i am triggering start search9 where the value of variable i is being used to set a token used in search
  • so for e.g
  1. if value of i =1 , i am setting token value token1=400 and token2= 200 and triggering search using startSearch().
  2. if value of i =2 , i am setting token value token1=600 and token2=400 and riggering search using startSearch(). and so on till loop is valid.
  • now ideally in else loop, if there are 3 values of i there should be 3 instances of token and search JOB, instead i always and only get last instance of search9.

please let me know if you need more explainations.

Thanks

0 Karma
Get Updates on the Splunk Community!

The Splunk Success Framework: Your Guide to Successful Splunk Implementations

Splunk Lantern is a customer success center that provides advice from Splunk experts on valuable data ...

Splunk Training for All: Meet Aspiring Cybersecurity Analyst, Marc Alicea

Splunk Education believes in the value of training and certification in today’s rapidly-changing data-driven ...

Investigate Security and Threat Detection with VirusTotal and Splunk Integration

As security threats and their complexities surge, security analysts deal with increased challenges and ...