Commit f2e1859f by Rashid Khan

Linted shared and moved to js dir

parent 5a3028f7
...@@ -25,11 +25,11 @@ var labjs = $LAB ...@@ -25,11 +25,11 @@ var labjs = $LAB
.script("common/lib/elastic.min.js") .script("common/lib/elastic.min.js")
.script("common/lib/elastic-angular-client.js").wait() .script("common/lib/elastic-angular-client.js").wait()
.script("common/lib/moment.js") .script("common/lib/moment.js")
.script("common/lib/shared.js")
.script("common/lib/filesaver.js") .script("common/lib/filesaver.js")
.script("common/lib/bootstrap.min.js") .script("common/lib/bootstrap.min.js")
.script('common/lib/datepicker.js') .script('common/lib/datepicker.js')
.script('common/lib/timepicker.js').wait() .script('common/lib/timepicker.js').wait()
.script("js/shared.js")
.script("js/services.js") .script("js/services.js")
.script("js/controllers.js") .script("js/controllers.js")
.script("js/filters.js") .script("js/filters.js")
......
// Wrap this all up in a 'kbn' object so I don't have a billion globals // Wrap this all up in a 'kbn' object so I don't have a billion globals
(function() { (function() {
'use strict';
// Save a reference to this // Save a reference to this
var self = this; var self = this;
...@@ -8,102 +9,109 @@ ...@@ -8,102 +9,109 @@
var wasKbn = self.kbn; var wasKbn = self.kbn;
// Create a safe refernce to the kbn object, for use below // Create a safe refernce to the kbn object, for use below
var kbn = function(obj) { return new wrapper(obj); }; var kbn = this;
// Create a global object for accessing these functions // Create a global object for accessing these functions
self.kbn = kbn; self.kbn = kbn;
kbn.get_object_fields = function(obj) { kbn.get_object_fields = function(obj) {
var field_array = []; var field_array = [];
obj = kbn.flatten_json(obj._source) obj = kbn.flatten_json(obj._source);
for (field in obj) { for (var field in obj) {
field_array.push(field); field_array.push(field);
} }
return field_array.sort(); return field_array.sort();
} };
kbn.get_all_fields = function(data) { kbn.get_all_fields = function(data) {
var fields = []; var fields = [];
_.each(data,function(hit) { _.each(data,function(hit) {
fields = _.uniq(fields.concat(_.keys(hit))) fields = _.uniq(fields.concat(_.keys(hit)));
}) });
// Remove stupid angular key // Remove stupid angular key
fields = _.without(fields,'$$hashKey') fields = _.without(fields,'$$hashKey');
return fields; return fields;
} };
kbn.has_field = function(obj,field) { kbn.has_field = function(obj,field) {
var obj_fields = get_object_fields(obj); var obj_fields = kbn.get_object_fields(obj);
if (_.inArray(obj_fields,field) < 0) { if (_.inArray(obj_fields,field) < 0) {
return false; return false;
} else { } else {
return true; return true;
} }
} };
kbn.get_related_fields = function(docs,field) { kbn.get_related_fields = function(docs,field) {
var field_array = [] var field_array = [];
_.each(docs, function(doc) { _.each(docs, function(doc) {
var keys = _.keys(doc) var keys = _.keys(doc);
if(_.contains(keys,field)) if(_.contains(keys,field)) {
field_array = field_array.concat(keys) field_array = field_array.concat(keys);
}) }
});
var counts = _.countBy(_.without(field_array,field),function(field){return field;}); var counts = _.countBy(_.without(field_array,field),function(field){return field;});
return counts; return counts;
} };
kbn.recurse_field_dots = function(object,field) { kbn.recurse_field_dots = function(object,field) {
var value = null; var value = null;
if (typeof object[field] != 'undefined') var nested;
if (typeof object[field] !== 'undefined') {
value = object[field]; value = object[field];
else if (nested = field.match(/(.*?)\.(.*)/)) }
if(typeof object[nested[1]] != 'undefined') else if (nested = field.match(/(.*?)\.(.*)/)) {
value = (typeof object[nested[1]][nested[2]] != 'undefined') ? if(typeof object[nested[1]] !== 'undefined') {
object[nested[1]][nested[2]] : recurse_field_dots( value = (typeof object[nested[1]][nested[2]] !== 'undefined') ?
object[nested[1]][nested[2]] : kbn.recurse_field_dots(
object[nested[1]],nested[2]); object[nested[1]],nested[2]);
}
}
return value; return value;
} };
// Probably useless now, leaving for cases where you might not want // Probably useless now, leaving for cases where you might not want
// a flat dot notated data structure // a flat dot notated data structure
kbn.get_field_value = function(object,field,opt) { kbn.get_field_value = function(object,field,opt) {
var value = kbn.recurse_field_dots(object['_source'],field); var value = kbn.recurse_field_dots(object._source,field);
if(value === null) if(value === null) {
return '' return '';
if(_.isArray(value))
if (opt == 'raw') {
return value;
} }
else { if(_.isArray(value)) {
if (opt === 'raw') {
return value;
} else {
var complex = false; var complex = false;
_.each(value, function(el, index) { _.each(value, function(el, index) {
if (typeof(el) == 'object') { if (typeof(el) === 'object') {
complex = true; complex = true;
} }
}) });
if (complex) { if (complex) {
return JSON.stringify(value, null, 4); return JSON.stringify(value, null, 4);
} }
return value.toString(); return value.toString();
} }
if(typeof value === 'object' && value != null) }
if(typeof value === 'object' && value !== null) {
// Leaving this out for now // Leaving this out for now
//return opt == 'raw' ? value : JSON.stringify(value,null,4) //return opt == 'raw' ? value : JSON.stringify(value,null,4)
return JSON.stringify(value,null,4) return JSON.stringify(value,null,4);
return (value != null) ? value.toString() : '';
} }
return (value !== null) ? value.toString() : '';
};
kbn.top_field_values = function(docs,field,count) { kbn.top_field_values = function(docs,field,count) {
var counts = _.countBy(_.pluck(docs,field),function(field){ var counts = _.countBy(_.pluck(docs,field),function(field){
return _.isUndefined(field) ? '' : field; return _.isUndefined(field) ? '' : field;
}); });
return _.pairs(counts).sort(function(a, b) { return _.pairs(counts).sort(function(a, b) {
return a[1] - b[1] return a[1] - b[1];
}).reverse().slice(0,count) }).reverse().slice(0,count);
} };
/** /**
* Calculate a graph interval * Calculate a graph interval
...@@ -115,50 +123,69 @@ ...@@ -115,50 +123,69 @@
* *
*/ */
kbn.calculate_interval = function(from,to,size,user_interval) { kbn.calculate_interval = function(from,to,size,user_interval) {
if(_.isObject(from)) if(_.isObject(from)) {
from = from.valueOf(); from = from.valueOf();
if(_.isObject(to)) }
if(_.isObject(to)) {
to = to.valueOf(); to = to.valueOf();
return user_interval == 0 ? kbn.round_interval((to - from)/size) : user_interval;
} }
return user_interval === 0 ? kbn.round_interval((to - from)/size) : user_interval;
};
kbn.round_interval = function(interval) { kbn.round_interval = function(interval) {
switch (true) { switch (true) {
// 0.5s // 0.5s
case (interval <= 500): return 100; // 0.1s case (interval <= 500):
return 100; // 0.1s
// 5s // 5s
case (interval <= 5000): return 1000; // 1s case (interval <= 5000):
return 1000; // 1s
// 7.5s // 7.5s
case (interval <= 7500): return 5000; // 5s case (interval <= 7500):
return 5000; // 5s
// 15s // 15s
case (interval <= 15000): return 10000; // 10s case (interval <= 15000):
return 10000; // 10s
// 45s // 45s
case (interval <= 45000): return 30000; // 30s case (interval <= 45000):
return 30000; // 30s
// 3m // 3m
case (interval <= 180000): return 60000; // 1m case (interval <= 180000):
return 60000; // 1m
// 9m // 9m
case (interval <= 450000): return 300000; // 5m case (interval <= 450000):
return 300000; // 5m
// 20m // 20m
case (interval <= 1200000): return 600000; // 10m case (interval <= 1200000):
return 600000; // 10m
// 45m // 45m
case (interval <= 2700000): return 1800000; // 30m case (interval <= 2700000):
return 1800000; // 30m
// 2h // 2h
case (interval <= 7200000): return 3600000; // 1h case (interval <= 7200000):
return 3600000; // 1h
// 6h // 6h
case (interval <= 21600000): return 10800000; // 3h case (interval <= 21600000):
return 10800000; // 3h
// 24h // 24h
case (interval <= 86400000): return 43200000; // 12h case (interval <= 86400000):
return 43200000; // 12h
// 48h // 48h
case (interval <= 172800000): return 86400000; // 24h case (interval <= 172800000):
return 86400000; // 24h
// 1w // 1w
case (interval <= 604800000): return 86400000; // 24h case (interval <= 604800000):
return 86400000; // 24h
// 3w // 3w
case (interval <= 1814400000): return 604800000; // 1w case (interval <= 1814400000):
return 604800000; // 1w
// 2y // 2y
case (interval < 3628800000): return 2592000000; // 30d case (interval < 3628800000):
default: return 31536000000; // 1y return 2592000000; // 30d
} default:
return 31536000000; // 1y
} }
};
kbn.secondsToHms = function(seconds){ kbn.secondsToHms = function(seconds){
var numyears = Math.floor(seconds / 31536000); var numyears = Math.floor(seconds / 31536000);
...@@ -182,11 +209,11 @@ ...@@ -182,11 +209,11 @@
return numseconds + 's'; return numseconds + 's';
} }
return 'less then a second'; //'just now' //or other string you like; return 'less then a second'; //'just now' //or other string you like;
} };
kbn.to_percent = function(number,outof) { kbn.to_percent = function(number,outof) {
return Math.round((number/outof)*10000)/100 + "%"; return Math.round((number/outof)*10000)/100 + "%";
} };
kbn.addslashes = function(str) { kbn.addslashes = function(str) {
str = str.replace(/\\/g, '\\\\'); str = str.replace(/\\/g, '\\\\');
...@@ -194,37 +221,46 @@ ...@@ -194,37 +221,46 @@
str = str.replace(/\"/g, '\\"'); str = str.replace(/\"/g, '\\"');
str = str.replace(/\0/g, '\\0'); str = str.replace(/\0/g, '\\0');
return str; return str;
} };
// histogram & trends // histogram & trends
kbn.interval_to_seconds = function(string) { kbn.interval_to_seconds = function(string) {
var matches = string.match(/(\d+)([Mwdhmsy])/); var matches = string.match(/(\d+)([Mwdhmsy])/);
switch (matches[2]) { switch (matches[2]) {
case 'y': return matches[1]*31536000;; case 'y':
case 'M': return matches[1]*2592000;; return matches[1]*31536000;
case 'w': return matches[1]*604800;; case 'M':
case 'd': return matches[1]*86400;; return matches[1]*2592000;
case 'h': return matches[1]*3600;; case 'w':
case 'm': return matches[1]*60;; return matches[1]*604800;
case 's': return matches[1]; case 'd':
} return matches[1]*86400;
} case 'h':
return matches[1]*3600;
case 'm':
return matches[1]*60;
case 's':
return matches[1];
}
};
// This should go away, moment.js can do this // This should go away, moment.js can do this
kbn.time_ago = function(string) { kbn.time_ago = function(string) {
return new Date(new Date().getTime() - (kbn.interval_to_seconds(string)*1000)) return new Date(new Date().getTime() - (kbn.interval_to_seconds(string)*1000));
} };
// LOL. hahahahaha. DIE. // LOL. hahahahaha. DIE.
kbn.flatten_json = function(object,root,array) { kbn.flatten_json = function(object,root,array) {
if (typeof array === 'undefined') if (typeof array === 'undefined') {
var array = {}; array = {};
if (typeof root === 'undefined') }
var root = ''; if (typeof root === 'undefined') {
root = '';
}
for(var index in object) { for(var index in object) {
var obj = object[index] var obj = object[index];
var rootname = root.length == 0 ? index : root + '.' + index; var rootname = root.length === 0 ? index : root + '.' + index;
if(typeof obj == 'object' ) { if(typeof obj === 'object' ) {
if(_.isArray(obj)) { if(_.isArray(obj)) {
if(obj.length > 0 && typeof obj[0] === 'object') { if(obj.length > 0 && typeof obj[0] === 'object') {
var strval = ''; var strval = '';
...@@ -242,14 +278,14 @@ ...@@ -242,14 +278,14 @@
array[rootname] = typeof obj === 'undefined' ? null : obj; array[rootname] = typeof obj === 'undefined' ? null : obj;
} }
} else { } else {
kbn.flatten_json(obj,rootname,array) kbn.flatten_json(obj,rootname,array);
} }
} else { } else {
array[rootname] = typeof obj === 'undefined' ? null : obj; array[rootname] = typeof obj === 'undefined' ? null : obj;
} }
} }
return kbn.sortObj(array); return kbn.sortObj(array);
} };
kbn.xmlEnt = function(value) { kbn.xmlEnt = function(value) {
if(_.isString(value)) { if(_.isString(value)) {
...@@ -266,33 +302,34 @@ ...@@ -266,33 +302,34 @@
} else { } else {
return value; return value;
} }
} };
kbn.sortObj = function(arr) { kbn.sortObj = function(arr) {
// Setup Arrays // Setup Arrays
var sortedKeys = new Array(); var sortedKeys = [];
var sortedObj = {}; var sortedObj = {};
var i;
// Separate keys and sort them // Separate keys and sort them
for (var i in arr) { for (i in arr) {
sortedKeys.push(i); sortedKeys.push(i);
} }
sortedKeys.sort(); sortedKeys.sort();
// Reconstruct sorted obj based on keys // Reconstruct sorted obj based on keys
for (var i in sortedKeys) { for (i in sortedKeys) {
sortedObj[sortedKeys[i]] = arr[sortedKeys[i]]; sortedObj[sortedKeys[i]] = arr[sortedKeys[i]];
} }
return sortedObj; return sortedObj;
} };
}).call(this); }).call(this);
/* /*
UNDERSCORE.js Mixins UNDERSCORE.js Mixins
*/ */
_.mixin({ _.mixin({
move: function (array, fromIndex, toIndex) { move: function (array, fromIndex, toIndex) {
'use strict';
array.splice(toIndex, 0, array.splice(fromIndex, 1)[0] ); array.splice(toIndex, 0, array.splice(fromIndex, 1)[0] );
return array; return array;
} }
...@@ -300,6 +337,8 @@ _.mixin({ ...@@ -300,6 +337,8 @@ _.mixin({
_.mixin({ _.mixin({
remove: function (array, index) { remove: function (array, index) {
'use strict';
array.splice(index, 1); array.splice(index, 1);
return array; return array;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment