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!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

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 ...