Dashboards & Visualizations

How do I close a Search Manager request with javascript?

llacoste
Path Finder

Hi all,

I am trying to start a search with javascript after a user clicks on an "ok" button in a pop-up.

So far, so good it is working and triggering the request.

However, I am unable to do it several times as it says "Error: Already have instance with id: mysearch"

I am not familiar at all with javascript and Splunk Search Manager functions... I tried to use:

delete mysearch;

...thinking it would delete the variable that holds the search manager instance and remove the object... but it doesn't seem to work.

Here is my code:

 require([
    "jquery",
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc",
    "splunkjs/mvc/simplexml/ready!"
  ], function(
      $,
      SearchManager
  ) {


      $("#btn-submit").on("click", function (){

     var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
     var totok = defaultTokenModel.get("totok");
          var mysearch = new SearchManager(totok,{
          id: "mysearch",
          autostart: "false",
          search: "|makeresults eval test="+totok+"|eval toto='true'"
      });
          var ok = confirm("Confirm you want to start the search!");
          if ( ok==true ){
              mysearch.startSearch(totok);
              mysearch.finalize();
              console.log("just closed it");
          } else {
              alert('You cancelled this action!');
          }
      });
 });

Basically it opens a pop up whenever I click on a button and finds the token value I set in the dashboard when clicking on a row.

Not sure if that can help but I'm also giving the dashboard code:

<form script="test.js">
  <label>Dashboard with Javascript on Button</label>
  <fieldset submitButton="false">
    <input type="text" token="field1">
      <label>"$totok$"</label>
    </input>
  </fieldset>
  <row>
    <panel>
      <html>
         <div>
           <input id="btn-submit" type="button" value="Click"/>
         </div>
       </html>
    </panel>
  </row>
  <row>
    <panel>
      <table>
        <search>
          <query>|makeresults | eval test="tokenvalue"</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
        </search>
        <option name="drilldown">cell</option>
        <drilldown>
          <set token="totok">$click.value2$</set>
        </drilldown>
      </table>
    </panel>
  </row>
</form>

Thank you for your help!

0 Karma
1 Solution

jeffland
SplunkTrust
SplunkTrust

You only need one search manager, not multiple. A search manager can run more than one search for you. You have to update its search string and restart the search. See this answer, I think it is what you're looking for.

I haven't checked this if it works, but what you want is basically:

require([
    "jquery",
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc",
    "splunkjs/mvc/simplexml/ready!"
    ], function($, SearchManager) {
    // declare token model an search manager once
    var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
    var mysearch = new SearchManager({
        id: "mysearch",
        autostart: "false",
        search: "|makeresults"
    });
    var results = mysearch.data("results");
    // listen to button click and do stuff there
    $("#btn-submit").on("click", function (){
        var totok = defaultTokenModel.get("totok");
        var ok = confirm("Confirm you want to start the search!");
        if (ok == true) {
            mysearch.settings.set("search", "| makeresults | eval test=\""+totok+"\" | eval toto=\"true\""
            mysearch.startSearch();
            console.log("just started it");
        } else {
            alert('You cancelled this action!');
        }
    });
    // Do stuff with the search results as soon as they are back
    results.on("data", function() {
        console.log("results are back:");
        console.log(results.data.rows);
    });
});

I hope this helps you get started.

View solution in original post

0 Karma

jeffland
SplunkTrust
SplunkTrust

You only need one search manager, not multiple. A search manager can run more than one search for you. You have to update its search string and restart the search. See this answer, I think it is what you're looking for.

I haven't checked this if it works, but what you want is basically:

require([
    "jquery",
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc",
    "splunkjs/mvc/simplexml/ready!"
    ], function($, SearchManager) {
    // declare token model an search manager once
    var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
    var mysearch = new SearchManager({
        id: "mysearch",
        autostart: "false",
        search: "|makeresults"
    });
    var results = mysearch.data("results");
    // listen to button click and do stuff there
    $("#btn-submit").on("click", function (){
        var totok = defaultTokenModel.get("totok");
        var ok = confirm("Confirm you want to start the search!");
        if (ok == true) {
            mysearch.settings.set("search", "| makeresults | eval test=\""+totok+"\" | eval toto=\"true\""
            mysearch.startSearch();
            console.log("just started it");
        } else {
            alert('You cancelled this action!');
        }
    });
    // Do stuff with the search results as soon as they are back
    results.on("data", function() {
        console.log("results are back:");
        console.log(results.data.rows);
    });
});

I hope this helps you get started.

0 Karma

llacoste
Path Finder

Thanks, your answer and the link provided helped me fixing the code.

Also I was facing another problem... I had to make it work on internet explorer 11, when I am normally working on Chrome or Firefox. Needless to say it's never easy ah ah 🙂

Thank you.

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@llacoste,

Have tried to declare searchManager once and use it on ok button? Please check below javascript code?

require([
    "jquery",
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc",
    "splunkjs/mvc/simplexml/ready!"
], function (
    $,
    SearchManager, mvc
) {
        console.log("hiwwweeee");
        var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
        var totok = defaultTokenModel.get("totok");
        var mysearch = new SearchManager(totok, {
            id: "mysearch",
            autostart: "false",
            search: "|makeresults eval test=" + totok + "|eval toto='true'"
        });

        $("#btn-submit").on("click", function () {
            var ok = confirm("Confirm you want to start the search!");
            if (ok == true) {
                mysearch.startSearch(totok);
                mysearch.finalize();
                console.log("just closed it");
            } else {
                alert('You cancelled this action!');
            }
        });
    });

I'm not sure what are you trying to achieve, bcoz your XML doesn't have any token totok. So if possible give information about the same also.

Thansks

0 Karma

mstjohn_splunk
Splunk Employee
Splunk Employee

hi @llacoste

Did the answer below solve your problem? If so, please resolve this post by approving it! If your problem is still not solved, keep us updated so that someone else can help ya. Thanks for posting!

0 Karma

llacoste
Path Finder

Hi,

Thanks for taking the time to look into it.

I actually do have a token totok if you look at the XML, it's the
<set token="totok">$click.value2$</set>

Whenever a user click on the drilldown, I have my variable totok populated.

My code is working, I manage to do what I want, however it seems I can only lunch it once. I mean, I get to click on the "OK" button once and it starts the search... it's fine... but then after whenever I try to select another value and try again the button it says "Error: Already have instance with id: mysearch".

Same happening with your code, It doesn't seem to change that 😞

However if I Control + R and reload the page, it works again and I get to start again the search with the OK button.

That's why I was wondering if there is a way to delete the instance/object "mysearch" that stays active it seems even after the request has been executed.

Any idea?

Thank you

0 Karma
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...