Dashboards & Visualizations

How to dynamically remove components (eg. SingleView) from a dashboard created with javascript.

cchitten
Path Finder

I have written a django template for a dashboard which dynamically adds singleview elements to the dashboard based on the number of events that are returned. I have also added a timerange picker at the top. However when I change the time the searches for each individual singleview element updates but I can't seem to delete the singleview elements themselves to then recreate news ones for the new data. There will be a different number of singleview elements depending on the time range picked.

How do I delete all of the singleview elements in the dashboard when the timerange changes?

I have tried:

for (index = 0, len = Object.keys(splunkjs.mvc.Components.attributes).length; index < len; ++index) {
          var id = Object.keys(splunkjs.mvc.Components.attributes)[index];
          if ( /^network_view/.test(id) ){
                    splunkjs.mvc.Components.getInstance(id).dispose();
          }
}

This essentially finds all the components that exist and dispose of the ones with a name like "network_view1" or "network_view_2" etc.
However I get a problem where some of the new singleviews created are 'undefined' in the components list and are therefore not deleted by this. Then the new ones aren't created because it find duplicates with the same instance name.

Is there another way to do this or am I missing something in my method?

1 Solution

cchitten
Path Finder

I solved this.

Instead of using:

splunkjs.mvc.Components.getInstance(id).dispose();

I use:

splunkjs.mvc.Components.getInstance(id).remove();

However this also removed the html section that held the SingleView element.
So therefore what I needed to do was also add a line which re-adds the html section.
My new code looks like this:

for (index = 0, len = Object.keys(splunkjs.mvc.Components.attributes).length; index < len; ++index) {
                                        var id = Object.keys(splunkjs.mvc.Components.attributes)[index];
                                        if ( /^network_view/.test(id) ){
                                                splunkjs.mvc.Components.getInstance(id).remove();
                                        }
                                }
                                document.getElementById("network_panel").innerHTML+= '<div id="network_status" class="splunk-view splunk-single">'

Where "network_panel" is the name of my original html section which contains the SingleViews and "network_status" is the name of the SingleViews section.

View solution in original post

cchitten
Path Finder

I solved this.

Instead of using:

splunkjs.mvc.Components.getInstance(id).dispose();

I use:

splunkjs.mvc.Components.getInstance(id).remove();

However this also removed the html section that held the SingleView element.
So therefore what I needed to do was also add a line which re-adds the html section.
My new code looks like this:

for (index = 0, len = Object.keys(splunkjs.mvc.Components.attributes).length; index < len; ++index) {
                                        var id = Object.keys(splunkjs.mvc.Components.attributes)[index];
                                        if ( /^network_view/.test(id) ){
                                                splunkjs.mvc.Components.getInstance(id).remove();
                                        }
                                }
                                document.getElementById("network_panel").innerHTML+= '<div id="network_status" class="splunk-view splunk-single">'

Where "network_panel" is the name of my original html section which contains the SingleViews and "network_status" is the name of the SingleViews section.

Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...