Skip to content

Instantly share code, notes, and snippets.

@hobbes3
Last active August 29, 2015 14:01
Show Gist options
  • Save hobbes3/aa775a06480d159627d1 to your computer and use it in GitHub Desktop.
Save hobbes3/aa775a06480d159627d1 to your computer and use it in GitHub Desktop.
format time datepicker oneshot
require([
'jquery',
'underscore',
'splunkjs/mvc',
// The ready! requirement makes sure the code below runs after Splunk initialize all the objects on the dashboards
// Without it the Javascript may try to act on a Splunk object that doesn't exist yet (like trying to hide a panel before the panel is created
'splunkjs/mvc/simplexml/ready!'
],
function(
$,
_,
mvc
) {
// Submits the unsubmitted tokens and update the URL
function submit_and_update_url() {
submitted_tokens.set(unsubmitted_tokens.toJSON());
mvc.Components.get('url').saveOnlyWithPrefix('form\\.', unsubmitted_tokens.toJSON(), {
replaceState: false
});
}
// Converts a Javascript Date object to a human-readable format
function convert_date_to_string(date) {
return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
}
var service = mvc.createService();
var unsubmitted_tokens = mvc.Components.get('default');
var submitted_tokens = mvc.Components.get('submitted');
// Hide the first 4 fields
// These are placeholders for the following tokens:
// search_earliest, search_latest, from_date_string, to_date_string
$("#field1, #field2, #field3, #field4").hide();
// Convert the 3rd field (Date) as a jQuery datepicker
var datepicker_input = $("#field5 input");
// Format the datepicker in the form of 1987-03-13
datepicker_input.datepicker({
dateFormat: 'yy-mm-dd'
})
// Set the date to today
.datepicker("setDate", new Date());
// Setting the date doesn't actually kick off the date change listener (the function below)
// So actually set the token with the value of the input
unsubmitted_tokens.set('form.date', datepicker_input.val());
// When the date token changes...
submitted_tokens.on('change:date', function() {
var date = submitted_tokens.get("date");
var convert_search = '| stats count | eval date="' + date + '" | convert timeformat="%F" mktime(date) as epoch | table epoch';
// Run a oneshot search to convert the date string to epoch *to the user's timezone preference*
service.oneshotSearch(convert_search, {id: "date_convert"}, function(err, results) {
var day = 86400;
var epoch = parseInt(results.rows[0][0]);
var search_earliest = epoch;
var search_latest = epoch + day;
var from_date_string = convert_date_to_string(new Date(search_earliest*1000));
var to_date_string = convert_date_to_string(new Date(search_latest*1000));
// Set the tokens to be submitted
unsubmitted_tokens.set('form.search_earliest', search_earliest);
unsubmitted_tokens.set('form.search_latest', search_latest);
unsubmitted_tokens.set('form.from_date_string', from_date_string);
unsubmitted_tokens.set('form.to_date_string', to_date_string);
submit_and_update_url();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment