Dashboards & Visualizations

How to pass searchmanager results to a JavaScript module in a Django dashboard?

david_rose
Communicator

I am trying to pass fields from searchmanager results to variables in the JS block in a django dashboard.

Here is my code:

{% searchmanager id="donut"
        search='search goes here'
        autostart=True
        cache=True
 %}

{% endblock managers %}

    {% block js %}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
    <script src="{{STATIC_URL}}{{app_name}}/radialProgress.js"></script>
    <script>

        start();


        function start() {

            var rp1 = radialProgress(document.getElementById('div1'))
                    .label($value1$)
                    .diameter(200)
                    .value($value2$)
                    .capacity($value3$)
                    .render();
        }

    </script>
    {% endblock js %}

How can I feed the result values from the donut search manager to the value# in the rp1 variable in the JS block?

0 Karma

badarsebard
Communicator

This one's a little tricky and took some time for me to figure out, but essentially you need to use the splunkjs MVC to retrieve the information. You will also have to perform most of the code inside the require function in order to utilize the splunk JS tools.

{% searchmanager id="django_searchmanager_id" search="<searchstring>" %}
<script>
    var deps=[
        "splunkjs/ready!",
        "splunkjs/mvc/searchmanager"
    ];
    require(deps, function(mvc) {
        // Retrieve search object
        var searchObject = splunkjs.mvc.Components.getInstance("django_searchmanager_id");
        // Create an object for the data from the search
        var searchObjectResults = searchObject.data("results"); //this yields and array with your fields and values
        // create the default token, and set it to a field value 
        var tokens = mvc.Components.getInstance("default");
        // x and y are indeces in your array for the data you're looking for
        var fieldName = searchObjectResults.data().rows[x][y]; 
        tokens.set("newTokenName",fieldName);
</script>

If you just need the field values as variables in the JS block then you only need to set the variable using the "var.data().rows[x][y]" command. However, if you'd like to use those values in other searches or parts of the HTML of the page, set it to the token like I did and you can use it pretty much anywhere.

Let me know if you have any questions, I'm also typically on the IRC if you want to talk directly.

0 Karma
Get Updates on the Splunk Community!

Dashboards: Hiding charts while search is being executed and other uses for tokens

There are a couple of features of SimpleXML / Classic dashboards that can be used to enhance the user ...

Splunk Observability Cloud's AI Assistant in Action Series: Explaining Metrics and ...

This is the fourth post in the Splunk Observability Cloud’s AI Assistant in Action series that digs into how ...

Brains, Bytes, and Boston: Learn from the Best at .conf25

When you think of Boston, you might picture colonial charm, world-class universities, or even the crack of a ...