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!

Splunk Observability Cloud’s AI Assistant in Action Series: Analyzing and ...

This is the second post in our Splunk Observability Cloud’s AI Assistant in Action series, in which we look at ...

Elevate Your Organization with Splunk’s Next Platform Evolution

 Thursday, July 10, 2025  |  11AM PDT / 2PM EDT Whether you're managing complex deployments or looking to ...

Splunk Answers Content Calendar, June Edition

Get ready for this week’s post dedicated to Splunk Dashboards! We're celebrating the power of community by ...