|
|
|
@ -1,13 +1,28 @@ |
|
|
|
|
function get_object_fields(obj) { |
|
|
|
|
// Wrap this all up in a 'kbn' object so I don't have a billion globals
|
|
|
|
|
(function() { |
|
|
|
|
|
|
|
|
|
// Save a reference to this
|
|
|
|
|
var self = this; |
|
|
|
|
|
|
|
|
|
// Save a reference to the old versionof this
|
|
|
|
|
var wasKbn = self.kbn; |
|
|
|
|
|
|
|
|
|
// Create a safe refernce to the kbn object, for use below
|
|
|
|
|
var kbn = function(obj) { return new wrapper(obj); }; |
|
|
|
|
|
|
|
|
|
// Create a global object for accessing these functions
|
|
|
|
|
self.kbn = kbn; |
|
|
|
|
|
|
|
|
|
kbn.get_object_fields = function(obj) { |
|
|
|
|
var field_array = []; |
|
|
|
|
obj = flatten_json(obj._source) |
|
|
|
|
obj = kbn.flatten_json(obj._source) |
|
|
|
|
for (field in obj) { |
|
|
|
|
field_array.push(field); |
|
|
|
|
} |
|
|
|
|
return field_array.sort(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function get_all_fields(data) { |
|
|
|
|
kbn.get_all_fields = function(data) { |
|
|
|
|
var fields = []; |
|
|
|
|
_.each(data,function(hit) { |
|
|
|
|
fields = _.uniq(fields.concat(_.keys(hit))) |
|
|
|
@ -15,18 +30,18 @@ function get_all_fields(data) { |
|
|
|
|
// Remove stupid angular key
|
|
|
|
|
fields = _.without(fields,'$$hashKey') |
|
|
|
|
return fields; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function has_field(obj,field) { |
|
|
|
|
kbn.has_field = function(obj,field) { |
|
|
|
|
var obj_fields = get_object_fields(obj); |
|
|
|
|
if (_.inArray(obj_fields,field) < 0) { |
|
|
|
|
return false; |
|
|
|
|
} else { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function get_related_fields(docs,field) { |
|
|
|
|
kbn.get_related_fields = function(docs,field) { |
|
|
|
|
var field_array = [] |
|
|
|
|
_.each(docs, function(doc) { |
|
|
|
|
var keys = _.keys(doc) |
|
|
|
@ -35,9 +50,9 @@ function get_related_fields(docs,field) { |
|
|
|
|
}) |
|
|
|
|
var counts = _.countBy(_.without(field_array,field),function(field){return field;}); |
|
|
|
|
return counts; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function recurse_field_dots(object,field) { |
|
|
|
|
kbn.recurse_field_dots = function(object,field) { |
|
|
|
|
var value = null; |
|
|
|
|
if (typeof object[field] != 'undefined') |
|
|
|
|
value = object[field]; |
|
|
|
@ -48,12 +63,12 @@ function recurse_field_dots(object,field) { |
|
|
|
|
object[nested[1]],nested[2]); |
|
|
|
|
|
|
|
|
|
return value; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Probably useless now, leaving for cases where you might not want
|
|
|
|
|
// a flat dot notated data structure
|
|
|
|
|
function get_field_value(object,field,opt) { |
|
|
|
|
var value = recurse_field_dots(object['_source'],field); |
|
|
|
|
// Probably useless now, leaving for cases where you might not want
|
|
|
|
|
// a flat dot notated data structure
|
|
|
|
|
kbn.get_field_value = function(object,field,opt) { |
|
|
|
|
var value = kbn.recurse_field_dots(object['_source'],field); |
|
|
|
|
|
|
|
|
|
if(value === null) |
|
|
|
|
return '' |
|
|
|
@ -79,26 +94,17 @@ function get_field_value(object,field,opt) { |
|
|
|
|
return JSON.stringify(value,null,4) |
|
|
|
|
|
|
|
|
|
return (value != null) ? value.toString() : ''; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function top_field_values(docs,field,count) { |
|
|
|
|
kbn.top_field_values = function(docs,field,count) { |
|
|
|
|
var counts = _.countBy(_.pluck(docs,field),function(field){ |
|
|
|
|
return _.isUndefined(field) ? '' : field; |
|
|
|
|
}); |
|
|
|
|
return _.pairs(counts).sort(function(a, b) { |
|
|
|
|
return a[1] - b[1] |
|
|
|
|
}).reverse().slice(0,count) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function add_to_query(original,field,value,negate) { |
|
|
|
|
var not = negate ? "-" : ""; |
|
|
|
|
if(value !== '') |
|
|
|
|
var query = field + ":" + "\"" + addslashes(value.toString()) + "\""; |
|
|
|
|
else |
|
|
|
|
var query = "_missing_:" + field; |
|
|
|
|
var glue = original != "" ? " AND " : ""; |
|
|
|
|
return original + glue + not + query; |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* Calculate a graph interval |
|
|
|
|
* |
|
|
|
@ -108,19 +114,15 @@ function add_to_query(original,field,value,negate) { |
|
|
|
|
* user_interval:: User specified histogram interval |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
function calculate_interval(from,to,size,user_interval) { |
|
|
|
|
kbn.calculate_interval = function(from,to,size,user_interval) { |
|
|
|
|
if(_.isObject(from)) |
|
|
|
|
from = from.valueOf(); |
|
|
|
|
if(_.isObject(to)) |
|
|
|
|
to = to.valueOf(); |
|
|
|
|
return user_interval == 0 ? round_interval((to - from)/size) : user_interval; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function get_bar_count(from,to,interval) { |
|
|
|
|
return (to - from)/interval; |
|
|
|
|
} |
|
|
|
|
return user_interval == 0 ? kbn.round_interval((to - from)/size) : user_interval; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function round_interval (interval) { |
|
|
|
|
kbn.round_interval = function(interval) { |
|
|
|
|
switch (true) { |
|
|
|
|
// 0.5s
|
|
|
|
|
case (interval <= 500): return 100; // 0.1s
|
|
|
|
@ -156,9 +158,9 @@ function round_interval (interval) { |
|
|
|
|
case (interval < 3628800000): return 2592000000; // 30d
|
|
|
|
|
default: return 31536000000; // 1y
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function secondsToHms(seconds){ |
|
|
|
|
kbn.secondsToHms = function(seconds){ |
|
|
|
|
var numyears = Math.floor(seconds / 31536000); |
|
|
|
|
if(numyears){ |
|
|
|
|
return numyears + 'y'; |
|
|
|
@ -180,71 +182,22 @@ function secondsToHms(seconds){ |
|
|
|
|
return numseconds + 's'; |
|
|
|
|
} |
|
|
|
|
return 'less then a second'; //'just now' //or other string you like;
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function to_percent(number,outof) { |
|
|
|
|
kbn.to_percent = function(number,outof) { |
|
|
|
|
return Math.round((number/outof)*10000)/100 + "%"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function addslashes(str) { |
|
|
|
|
kbn.addslashes = function(str) { |
|
|
|
|
str = str.replace(/\\/g, '\\\\'); |
|
|
|
|
str = str.replace(/\'/g, '\\\''); |
|
|
|
|
str = str.replace(/\"/g, '\\"'); |
|
|
|
|
str = str.replace(/\0/g, '\\0'); |
|
|
|
|
return str; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create an ISO8601 compliant timestamp for ES
|
|
|
|
|
//function ISODateString(unixtime) {
|
|
|
|
|
//var d = new Date(parseInt(unixtime));
|
|
|
|
|
function ISODateString(d) { |
|
|
|
|
if(is_int(d)) { |
|
|
|
|
d = new Date(parseInt(d)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function pad(n) { |
|
|
|
|
return n < 10 ? '0' + n : n |
|
|
|
|
} |
|
|
|
|
return d.getFullYear() + '-' + |
|
|
|
|
pad(d.getMonth() + 1) + '-' + |
|
|
|
|
pad(d.getDate()) + 'T' + |
|
|
|
|
pad(d.getHours()) + ':' + |
|
|
|
|
pad(d.getMinutes()) + ':' + |
|
|
|
|
pad(d.getSeconds()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function pickDateString(d) { |
|
|
|
|
return dateFormat(d,'yyyy-mm-dd HH:MM:ss') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function prettyDateString(d) { |
|
|
|
|
d = new Date(parseInt(d)); |
|
|
|
|
d = utc_date_obj(d); |
|
|
|
|
return dateFormat(d,window.time_format); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function utc_date_obj(d) { |
|
|
|
|
return new Date( |
|
|
|
|
d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(),
|
|
|
|
|
d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), |
|
|
|
|
d.getUTCMilliseconds()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function local_date_obj(d) { |
|
|
|
|
return new Date(Date.UTC( |
|
|
|
|
d.getFullYear(), d.getMonth(), d.getDate(),
|
|
|
|
|
d.getHours(), d.getMinutes(), d.getSeconds())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function is_int(value) { |
|
|
|
|
if ((parseFloat(value) == parseInt(value)) && !isNaN(value)) { |
|
|
|
|
return true; |
|
|
|
|
} else { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function interval_to_seconds(string) { |
|
|
|
|
// histogram & trends
|
|
|
|
|
kbn.interval_to_seconds = function(string) { |
|
|
|
|
var matches = string.match(/(\d+)([Mwdhmsy])/); |
|
|
|
|
switch (matches[2]) { |
|
|
|
|
case 'y': return matches[1]*31536000;; |
|
|
|
@ -255,13 +208,15 @@ function interval_to_seconds(string) { |
|
|
|
|
case 'm': return matches[1]*60;; |
|
|
|
|
case 's': return matches[1]; |
|
|
|
|
}
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function time_ago(string) { |
|
|
|
|
return new Date(new Date().getTime() - (interval_to_seconds(string)*1000)) |
|
|
|
|
} |
|
|
|
|
// This should go away, moment.js can do this
|
|
|
|
|
kbn.time_ago = function(string) { |
|
|
|
|
return new Date(new Date().getTime() - (kbn.interval_to_seconds(string)*1000)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function flatten_json(object,root,array) { |
|
|
|
|
// LOL. hahahahaha. DIE.
|
|
|
|
|
kbn.flatten_json = function(object,root,array) { |
|
|
|
|
if (typeof array === 'undefined') |
|
|
|
|
var array = {}; |
|
|
|
|
if (typeof root === 'undefined') |
|
|
|
@ -287,16 +242,16 @@ function flatten_json(object,root,array) { |
|
|
|
|
array[rootname] = typeof obj === 'undefined' ? null : obj; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
flatten_json(obj,rootname,array) |
|
|
|
|
kbn.flatten_json(obj,rootname,array) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
array[rootname] = typeof obj === 'undefined' ? null : obj; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return sortObj(array); |
|
|
|
|
} |
|
|
|
|
return kbn.sortObj(array); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function xmlEnt(value) { |
|
|
|
|
kbn.xmlEnt = function(value) { |
|
|
|
|
if(_.isString(value)) { |
|
|
|
|
var stg1 = value.replace(/</g, '<') |
|
|
|
|
.replace(/>/g, '>') |
|
|
|
@ -307,13 +262,13 @@ function xmlEnt(value) { |
|
|
|
|
.replace(/ /g, ' ') |
|
|
|
|
.replace(/<del>/g, '<del>') |
|
|
|
|
.replace(/<\/del>/g, '</del>'); |
|
|
|
|
return stg1 |
|
|
|
|
return stg1; |
|
|
|
|
} else { |
|
|
|
|
return value |
|
|
|
|
return value; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function sortObj(arr) { |
|
|
|
|
kbn.sortObj = function(arr) { |
|
|
|
|
// Setup Arrays
|
|
|
|
|
var sortedKeys = new Array(); |
|
|
|
|
var sortedObj = {}; |
|
|
|
@ -329,84 +284,12 @@ function sortObj(arr) { |
|
|
|
|
sortedObj[sortedKeys[i]] = arr[sortedKeys[i]]; |
|
|
|
|
} |
|
|
|
|
return sortedObj; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// WTF. Has to be a better way to do this. Hi Tyler.
|
|
|
|
|
function int_to_tz(offset) { |
|
|
|
|
var hour = offset / 1000 / 3600 |
|
|
|
|
var str = "" |
|
|
|
|
if (hour == 0) { |
|
|
|
|
str = "+0000" |
|
|
|
|
} |
|
|
|
|
if (hour < 0) { |
|
|
|
|
if (hour > -10) |
|
|
|
|
str = "-0" + (hour * -100) |
|
|
|
|
else |
|
|
|
|
str = "-" + (hour * -100) |
|
|
|
|
} |
|
|
|
|
if (hour > 0) { |
|
|
|
|
if (hour < 10) |
|
|
|
|
str = "+0" + (hour * 100) |
|
|
|
|
else |
|
|
|
|
str = "+" + (hour * 100) |
|
|
|
|
} |
|
|
|
|
str = str.substring(0,3) + ":" + str.substring(3); |
|
|
|
|
return str |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Sets #hash, thus refreshing results
|
|
|
|
|
function setHash(json) { |
|
|
|
|
window.location.hash = encodeURIComponent(Base64.encode(JSON.stringify(json))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Add commas to numbers
|
|
|
|
|
function addCommas(nStr) { |
|
|
|
|
nStr += ''; |
|
|
|
|
var x = nStr.split('.'); |
|
|
|
|
var x1 = x[0]; |
|
|
|
|
var x2 = x.length > 1 ? '.' + x[1] : ''; |
|
|
|
|
var rgx = /(\d+)(\d{3})/; |
|
|
|
|
while (rgx.test(x1)) { |
|
|
|
|
x1 = x1.replace(rgx, '$1' + ',' + '$2'); |
|
|
|
|
} |
|
|
|
|
return x1 + x2; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Split up log spaceless strings
|
|
|
|
|
// Str = string to split
|
|
|
|
|
// num = number of letters between <wbr> tags
|
|
|
|
|
function wbr(str, num) { |
|
|
|
|
str = htmlEntities(str); |
|
|
|
|
return str.replace( |
|
|
|
|
RegExp("(@?\\w{" + num + "}|[:;,])([\\w\"'])([\\w@]*)", "g"), |
|
|
|
|
function (all, text, char, trailer) { |
|
|
|
|
if (/@KIBANA_\w+_(START|END)@/.test(all)) { |
|
|
|
|
return text + char + trailer; |
|
|
|
|
} else { |
|
|
|
|
return text + "<del>​</del>" + char + trailer; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function htmlEntities(str) { |
|
|
|
|
return String(str).replace( |
|
|
|
|
/&/g, '&').replace( |
|
|
|
|
/</g, '<').replace( |
|
|
|
|
/>/g, '>').replace( |
|
|
|
|
/"/g, '"'); |
|
|
|
|
} |
|
|
|
|
}).call(this); |
|
|
|
|
|
|
|
|
|
function bucket_round(start,num,dir) { |
|
|
|
|
var resto = start%num; |
|
|
|
|
if (resto <= (num/2) || dir === 'down') { |
|
|
|
|
// Down
|
|
|
|
|
return start-resto; |
|
|
|
|
} else { |
|
|
|
|
// Up
|
|
|
|
|
return start+num-resto; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
/* |
|
|
|
|
UNDERSCORE.js Mixins |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
_.mixin({ |
|
|
|
|
move: function (array, fromIndex, toIndex) { |
|
|
|
|