Hi All,
For a particular filter which is used across my dashboard , I have run a search in javascript and set a token and use it .
This search has to be run post login.
1. WHere can run such search in javascript
2. Need to set token in that javascript function and has to be used across dashboard ( like a global variable).
Please give me inputs its kind of urgent requirement.
Regards,
Prathi
Hi @Prathikpisplunk,
If you already have the mvc
components loaded:
require([
"splunkjs/mvc",
"splunkjs/mvc/simplexml/ready!"
], function(mvc) {
...
});
You can use tokens directly:
// Access the "default" token model
var tokens = mvc.Components.get("default");
// Retrieve the value of a token $mytoken$
var tokenValue = tokens.get("mytoken");
// Change the value of a token $mytoken$
tokens.set("mytoken", "this is the new value");
Another example from here:
new DropdownView({
id: "selIndex",
value: mvc.tokenSafe("$indexName$"),
el: $("#selIndex")
}).render();
new TextInputView({
id: "txtKeywords",
value: mvc.tokenSafe("$keywords$"),
el: $("#txtKeywords")
}).render();
new SearchManager({
id: "mySearch",
search: mvc.tokenSafe("index=$indexName$ $keywords$")
});
Hi Xander,
Thanks for the reply. I have created one js file and running a search in that and setting the token.
Now I am planning to include this js file in all the xml files which are in my dashboard. There are about 20 xml files. So it is not a right thing to include in all 20. Also the search in js wil run 20 times based on user clicking each panel. so I wanted to know if there is any post login javascript function where I can just run the javascript function only once and set the token which can later be used globally?
-----------------Below is the javascript funtion where I am trying to see if user has admin rights. based on which in xml I need to show him the drop down options--------------
function(mvc, SearchManager) {
var userSearch = new SearchManager({
"id": "userSearch",
"preview": "false",
"cache": "false",
"search": "|rest splunk_server=local /services/authentication/current-context |table roles |mvexpand roles"
}, {tokens: false});
userSearch.startSearch();
var roleSearchResults = userSearch.data('results');
roleSearchResults.on("data", function() {
var userRoles = roleSearchResults.data().rows;
var tokens = mvc.Components.get("default");
tokens.set('RoleCheckQuery', "");
for(var i=0; i<userRoles.length; i++){
console.log(userRoles[i][0]);
var userRole = userRoles[i][0];
if (userRole === "admin" ) {
tokens.set('RoleCheckQuery', "");
break;
}
else {
tokens.set('RoleCheckQuery', '| join cpyKey[| rest /services/authentication/current-context|search (NOT username=\"splunk-system-user\") | fields + roles | mvexpand roles| makemv roles delim="-" | eval cpyKey=mvindex(roles, 3)|fields cpyKey]');
}
}
var tokenValue = tokens.get("RoleCheckQuery");
});
One thing you could do is output the token to lookup and then load it from the lookup in the other dashboards?