Dashboards & Visualizations

How to import the token value in the JavaScript in Simple XML?

kedjjang
Explorer

alt text
Create a wish come test.js code to get the value of $ time_ok1.earliest $.

0 Karma

jeffland
SplunkTrust
SplunkTrust

In test.js, you need to require the right components, and then you can simply grab the tokens from the token model. Something like this:

 require([
     'splunkjs/mvc/simplexml/ready!',
     "splunkjs/mvc"
 ],
 function(
     mvc
     ) {
         // Get the default model
         var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
         // Get some token from there
         var token_kang = defaultTokenModel.get("kang");
         ...

See here for the docs. Be aware though that the object "token_kang" at the moment contains two fields, and to get to the earliest in js, you need to do token_kang.earliest.

0 Karma

joao_amorim
Communicator

Why the answers are not being posted here

0 Karma

jeffland
SplunkTrust
SplunkTrust

Your comment might not be published immediately because it is in moderation.
Two things: have you surrounded your js in a splunkjs/ready!-require statement? That should ensure everything is loaded before your code runs. Also, does your input have a default setting?

0 Karma

joao_amorim
Communicator

Ok yes it has a default setting, ah i didn't has the splunkjs/ready! in the require statement.
Now it looks like it's working. But now i want to add behavior to the time input like on change

0 Karma

jeffland
SplunkTrust
SplunkTrust

You can do that by listening to the token model change:

var tokens = mvc.Components.get("default");
tokens.on("change:token_name", function(model, value, options) {
    // do stuff
});
0 Karma

joao_amorim
Communicator

Ok ty i will try that in the other tokens i usually do this:
JS:
var year = $("#input1");
year.on("change", function(newValue) {
refreshHOT();
});

XML:

This works for the other tokens but since time inputs are not dropdowns, that's why this doesn't work for them.
Or it's more reliable to change them all to your example of:

tokens.on("change:token_name", function(model, value, options) {
// do stuff
});

0 Karma

jeffland
SplunkTrust
SplunkTrust

If that was a question, no, I doubt one way is more reliable than the other. See which one works for you, and go with it... or use both.

0 Karma

joao_amorim
Communicator

I changed them to you suggestion and they work, but again the time input token doesn't work.
But probably to time input i need to do:
tokens.on("change:time_range.earliest", ...)
Not only:
tokens.on("change:time_range", ...)

0 Karma

jeffland
SplunkTrust
SplunkTrust

Yes, that's probably it.

0 Karma

joao_amorim
Communicator

Thanks unfortunately the behavior after using your method was not the expected but i give a try to the method explained by kedjjang and it works perfect.
Just if you want to know:
XML:

<input id="time_range" type="time" token="time_range" searchWhenChanged="true">

JS:

var time_range = mvc.Components.getInstance("time_range");
time_range.on("change", function(newValue) {
    // do stuff
});
0 Karma

jeffland
SplunkTrust
SplunkTrust

Thanks for posting a working solution. Please enter xml code by leavin one blank line and indenting all lines of code by four spaces, otherwise the board will mess your code up.

0 Karma

joao_amorim
Communicator

The input tag makes code disappear xP

0 Karma

joao_amorim
Communicator

When i open the dashboard the only tokens that don't work are these related to time inputs because the others work fine. As I have the tokens set are working but just after i change the time input to other date. The first time the dashboard loads, the get of the earliest and latest gives undefined

0 Karma

kedjjang
Explorer

It appears to be undefined.

splunkjs.mvc.Components.getInstance ("default");

Will that be called justice in simplexml default?

0 Karma

jeffland
SplunkTrust
SplunkTrust

What exactly is undefined?

0 Karma

kedjjang
Explorer
require(["splunkjs/ready!"], function(mvc) {
var deps = [
        "jquery",
        "splunkjs/ready!",      
        "splunkjs/mvc/searchmanager",
    ];
    require(deps, function(mvc) {               

    var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
    var token_def = defaultTokenModel.get("kang");

    alert(token_def);  << undefined
    console.log(token_def);
});
});
0 Karma

jeffland
SplunkTrust
SplunkTrust

Oh, my bad. I mistook your id and token name. It has to be

var token_def = defaultTokenModel.get("time_ok1");

Sorry!

0 Karma

kedjjang
Explorer

You're welcome...

"time_ok1" Even if I value the results.
I think that haneunge "default" settings on the source code ... I do not know anything.

0 Karma

jeffland
SplunkTrust
SplunkTrust

I'm sorry, I don't quite get what you're saying - is something still not working?

0 Karma

joao_amorim
Communicator

Hi this var token_def = defaultTokenModel.get("time_ok1");
Doesn't work for me. Any idea?

0 Karma
Get Updates on the Splunk Community!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...