Dashboards & Visualizations

Is there a way that we can set a search result to a token in js ?

Jhanzkie
Path Finder

Hello , sorry for this another noob question.

Is there a way that we can set a search result to a token in js ?

For example: 
<row>
<panel depends="$panel_show$">
<single>
<search>
<query>|makeresults|eval result=1016</query>
<done>
<set token="mytoken">$result.result$</set>
</done>
</search>
<option name="rangeColors">["0x006d9c","0xf8be34","0xf1813f","0xdc4e41"]</option>
<option name="rangeValues">[100,200,300]</option>
<option name="underLabel">cool number</option>
<option name="useColors">1</option>
</single>
</panel>
</row>

"<done>
<set token="mytoken">$result.result$</set>
</done>"

is there a way to transfer this to js ? 

thank you



Labels (2)
0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

@Jhanzkie 

You can try this.

 

<dashboard script="a.js">
  <label>Set Token Example</label>
  <row>
    <panel>
      <single>
        <search id="singleViewSearch">
          <query>|makeresults|eval result=1016</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
        </search>
        <option name="drilldown">none</option>
      </single>
    </panel>
  </row>
  <row>
    <panel>
      <html>
        $mytoken$
      </html>
    </panel>
  </row>
</dashboard>

 

 

a.js

 

require([
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/simplexml/ready!'
], function($, mvc) {
    var singleViewSearch = mvc.Components.getInstance("singleViewSearch"); 
    var defaultTokenModel = mvc.Components.get('default');
    var submittedTokenModel = mvc.Components.get("submitted");
    console.log(singleViewSearch)
    singleViewSearch.on("search:done", function() {

        var myResults = singleViewSearch.data("results"); // get the data from that search
        console.log("Search done", myResults)
        myResults.on("data", function() {
            resultArray = myResults.data().rows;
            console.log(resultArray, resultArray[0][1])
            defaultTokenModel.set("mytoken", resultArray[0][1]);
            submittedTokenModel.set(defaultTokenModel.toJSON());
        })
    })
});

 

 

Just try and change it as per your requirement.

Thanks

KV

 

View solution in original post

kamlesh_vaghela
SplunkTrust
SplunkTrust

@Jhanzkie 

You can try this.

 

<dashboard script="a.js">
  <label>Set Token Example</label>
  <row>
    <panel>
      <single>
        <search id="singleViewSearch">
          <query>|makeresults|eval result=1016</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
        </search>
        <option name="drilldown">none</option>
      </single>
    </panel>
  </row>
  <row>
    <panel>
      <html>
        $mytoken$
      </html>
    </panel>
  </row>
</dashboard>

 

 

a.js

 

require([
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/simplexml/ready!'
], function($, mvc) {
    var singleViewSearch = mvc.Components.getInstance("singleViewSearch"); 
    var defaultTokenModel = mvc.Components.get('default');
    var submittedTokenModel = mvc.Components.get("submitted");
    console.log(singleViewSearch)
    singleViewSearch.on("search:done", function() {

        var myResults = singleViewSearch.data("results"); // get the data from that search
        console.log("Search done", myResults)
        myResults.on("data", function() {
            resultArray = myResults.data().rows;
            console.log(resultArray, resultArray[0][1])
            defaultTokenModel.set("mytoken", resultArray[0][1]);
            submittedTokenModel.set(defaultTokenModel.toJSON());
        })
    })
});

 

 

Just try and change it as per your requirement.

Thanks

KV

 

Jhanzkie
Path Finder

Thanks it worked 🙂

Tags (1)
0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...