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!

Why You Can't Miss .conf25: Unleashing the Power of Agentic AI with Splunk & Cisco

The Defining Technology Movement of Our Lifetime The advent of agentic AI is arguably the defining technology ...

Deep Dive into Federated Analytics: Unlocking the Full Power of Your Security Data

In today’s complex digital landscape, security teams face increasing pressure to protect sprawling data across ...

Your summer travels continue with new course releases

Summer in the Northern hemisphere is in full swing, and is often a time to travel and explore. If your summer ...