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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...