Dashboards & Visualizations

Splunk 6 Web Framework: setting the initial value of a TimeRangeView after page load

helge
Builder

I am passing URL parameters to a view built with Django and the web framework in Splunk 6. Among those paramters are the earliest and latest time, which I need to apply to the TimeRangeView on the page. That works, too, but only if the timerange does not have a preset. If if does, the value my code sets is overwritten a few hundred milliseconds later by the preset value.

This is my code:

  1. content block:

    {% timerange id="timerange1" managerid="search1" preset="Today" earliest_time="$earlyval$"|token_safe latest_time="$lateval$"|token_safe %}

  2. js block:

    var deps =
    [
    "splunkjs/ready!",
    "splunkjs/mvc/timerangeview"
    ];
    require(deps, function(mvc)
    {
    // Get the parameters
    var paramEarliest = getParameterByName("earliest");
    var paramLatest = getParameterByName("latest");

    // Set the timerange
    if (paramEarliest != "" && paramLatest != "")
    {
    var timerange = splunkjs.mvc.Components.getInstance("timerange1");
    timerange.settings.set("value", {"earliest_time" : paramEarliest, "latest_time" : paramLatest});
    }
    });

How can wait until the preset has been applied before I set my custom values?
Alternatively: how can I disable the preset in my script?

Tags (2)
0 Karma
1 Solution

dfoster_splunk
Splunk Employee
Splunk Employee

I've coded up a sample that conditionally either sets a preset or sets a specific value:
https://gist.github.com/davidfstr/eb7fdf952d4648b4957c

In general you should be using timerange.settings to alter the properties of a TimeRangeView (or any component) after it is initialized. Any underscore-prefixed field or method like timerange._state is private and may change without warning in future framework versions.

View solution in original post

dfoster_splunk
Splunk Employee
Splunk Employee

I've coded up a sample that conditionally either sets a preset or sets a specific value:
https://gist.github.com/davidfstr/eb7fdf952d4648b4957c

In general you should be using timerange.settings to alter the properties of a TimeRangeView (or any component) after it is initialized. Any underscore-prefixed field or method like timerange._state is private and may change without warning in future framework versions.

dfoster_splunk
Splunk Employee
Splunk Employee

You're right about view.settings not being documented. It should be. I'll get on that.

0 Karma

dfoster_splunk
Splunk Employee
Splunk Employee

Would it be correct to say that a preset configured in Django cannot be overwritten in JavaScript?

It should be reconfigurable. The original code was actually hitting an unrelated bug that prevented changes to {earliest_time, latest_time} during initialization time if no "value" property was set. This bug should be fixed in the next Splunk release.

0 Karma

helge
Builder

I replaced timerange._state with timerange.settings in the question.

0 Karma

helge
Builder

Thanks - "settings.set" is certainly better; I would have used it if it were documented!
Your script shows a nice workaround to having a preset and a custom timerange. Would it be correct to say that a preset configured in Django cannot be overwritten in JavaScript?

0 Karma
Get Updates on the Splunk Community!

Preparing your Splunk Environment for OpenSSL3

The Splunk platform will transition to OpenSSL version 3 in a future release. Actions are required to prepare ...

Unleash Unified Security and Observability with Splunk Cloud Platform

     Now Available on Microsoft AzureThursday, March 27, 2025  |  11AM PST / 2PM EST | Register NowStep boldly ...

Splunk AppDynamics with Cisco Secure Application

Web applications unfortunately present a target rich environment for security vulnerabilities and attacks. ...