Skip to content

Commit

Permalink
Added field type recognition and auto-conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jrzmurray committed Sep 12, 2019
1 parent ff3e985 commit 2a36155
Showing 1 changed file with 80 additions and 14 deletions.
94 changes: 80 additions & 14 deletions src/splunk-wdc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var res = []; // Response Handler
var max_record_limit = 10000; // Max record splunk would return in one go. (max is 50K)
var myConnector = new tableau.makeConnector(); // Create the connector object
var fields = {}; // Response field types dictionary

// returns array of SPL Query string and Auth string
_params = lzw_decode(b64DecodeUnicode(window.location.href.split("?")[1].split("query=")[1])).split("[-][-][-]");
Expand Down Expand Up @@ -200,12 +201,71 @@
log("added colum definition");
Object.keys(res[0]).forEach(function (key) {
log('Key : ' + key + ', Value : ' + res[0][key]);
cols.push({
id: key,
alias: key,
dataType: tableau.dataTypeEnum.string
});
});

if ( res[0][key].match(/^-?\d+$/) ) {

// Integer column definition (int)
cols.push({
id: key,
alias: key,
dataType: tableau.dataTypeEnum.int
});
fields[key] = "int";
//log("Column " + key + " added as an integer");

} else if ( res[0][key].match(/^\d*\.\d+$/)) {

// Float column definition (float)
cols.push({
id: key,
alias: key,
dataType: tableau.dataTypeEnum.float
});
fields[key] = "float";
//log("Column " + key + " added as a float");

} else if ( res[0][key].match(/^(t|f|true|false)$/i) ) {
// Boolean column definition (bool)
cols.push({
id: key,
alias: key,
dataType: tableau.dataTypeEnum.bool
});
fields[key] = "bool";
//log("Column " + key + " added as a boolean");

} else if ( res[0][key].match(/^\d{4}[\/-]\d{2}[\/-]\d{2}$/)
|| res[0][key].match(/^\d{2}[\/-]\d{2}[\/-]\d{4}$/)
|| res[0][key].match(/^\d{1,2}[\/ -][A-Z][a-z]+(\s\d{2,4})?$/)
|| res[0][key].match(/^[A-Z][a-z]+\s+\d{1,2}(\s\d{2,4})?$/) ) {
// Date column definition
cols.push({
id: key,
alias: key,
dataType: tableau.dataTypeEnum.date
});
fields[key] = "date";
//log("Column " + key + " added as a date");

} else if ( ! isNaN(Date.parse(res[0][key])) ) {
// Datetime column definition
cols.push({
id: key,
alias: key,
dataType: tableau.dataTypeEnum.datetime
});
fields[key] = "datetime";
//log("Column " + key + " added as a datetime");

} else {
// String column definition
cols.push({
id: key,
alias: key,
dataType: tableau.dataTypeEnum.string
});
}
}); // End foreach key


log('tableInfo');
Expand Down Expand Up @@ -242,13 +302,6 @@
if (err) console.log("Error: " + err);
}
); // Async Ends







}
});
}
Expand All @@ -262,7 +315,20 @@
var tableData = [];
log("Total records: ");
log(res.length);
for (var i = 0; i < res.length; ++i) {
d = new Date();
date_offset = 0;
// Correct the displayed time to be local time
// Comment the following line to get the true time in UTC
date_offset = d.getTimezoneOffset();
for (var i=0; i<res.length; ++i) {
if (i == 0) log(res[i]); // Convert datetime field data
for (var field in fields) {
if ( fields[field] == "datetime" ) {
if ( ! isNaN(Date.parse(res[i][field]))) {
res[i][field] = new Date(Date.parse(res[i][field]) - date_offset*60000);
}
}
}
if (i == 0) log(res[i]);
tableData.push(res[i]);
}
Expand Down

0 comments on commit 2a36155

Please sign in to comment.