Commit b3052d0c by Rashid Khan

Modularized panels

parent 59503c5e
......@@ -8,4 +8,8 @@
#upload {
display: inline-block;
}
[ng\:cloak], [ng-cloak], .ng-cloak {
display: none !important;
}
\ No newline at end of file
/*! LAB.js (LABjs :: Loading And Blocking JavaScript)
v2.0.3 (c) Kyle Simpson
MIT License
*/
(function(o){var K=o.$LAB,y="UseLocalXHR",z="AlwaysPreserveOrder",u="AllowDuplicates",A="CacheBust",B="BasePath",C=/^[^?#]*\//.exec(location.href)[0],D=/^\w+\:\/\/\/?[^\/]+/.exec(C)[0],i=document.head||document.getElementsByTagName("head"),L=(o.opera&&Object.prototype.toString.call(o.opera)=="[object Opera]")||("MozAppearance"in document.documentElement.style),q=document.createElement("script"),E=typeof q.preload=="boolean",r=E||(q.readyState&&q.readyState=="uninitialized"),F=!r&&q.async===true,M=!r&&!F&&!L;function G(a){return Object.prototype.toString.call(a)=="[object Function]"}function H(a){return Object.prototype.toString.call(a)=="[object Array]"}function N(a,c){var b=/^\w+\:\/\//;if(/^\/\/\/?/.test(a)){a=location.protocol+a}else if(!b.test(a)&&a.charAt(0)!="/"){a=(c||"")+a}return b.test(a)?a:((a.charAt(0)=="/"?D:C)+a)}function s(a,c){for(var b in a){if(a.hasOwnProperty(b)){c[b]=a[b]}}return c}function O(a){var c=false;for(var b=0;b<a.scripts.length;b++){if(a.scripts[b].ready&&a.scripts[b].exec_trigger){c=true;a.scripts[b].exec_trigger();a.scripts[b].exec_trigger=null}}return c}function t(a,c,b,d){a.onload=a.onreadystatechange=function(){if((a.readyState&&a.readyState!="complete"&&a.readyState!="loaded")||c[b])return;a.onload=a.onreadystatechange=null;d()}}function I(a){a.ready=a.finished=true;for(var c=0;c<a.finished_listeners.length;c++){a.finished_listeners[c]()}a.ready_listeners=[];a.finished_listeners=[]}function P(d,f,e,g,h){setTimeout(function(){var a,c=f.real_src,b;if("item"in i){if(!i[0]){setTimeout(arguments.callee,25);return}i=i[0]}a=document.createElement("script");if(f.type)a.type=f.type;if(f.charset)a.charset=f.charset;if(h){if(r){e.elem=a;if(E){a.preload=true;a.onpreload=g}else{a.onreadystatechange=function(){if(a.readyState=="loaded")g()}}a.src=c}else if(h&&c.indexOf(D)==0&&d[y]){b=new XMLHttpRequest();b.onreadystatechange=function(){if(b.readyState==4){b.onreadystatechange=function(){};e.text=b.responseText+"\n//@ sourceURL="+c;g()}};b.open("GET",c);b.send()}else{a.type="text/cache-script";t(a,e,"ready",function(){i.removeChild(a);g()});a.src=c;i.insertBefore(a,i.firstChild)}}else if(F){a.async=false;t(a,e,"finished",g);a.src=c;i.insertBefore(a,i.firstChild)}else{t(a,e,"finished",g);a.src=c;i.insertBefore(a,i.firstChild)}},0)}function J(){var l={},Q=r||M,n=[],p={},m;l[y]=true;l[z]=false;l[u]=false;l[A]=false;l[B]="";function R(a,c,b){var d;function f(){if(d!=null){d=null;I(b)}}if(p[c.src].finished)return;if(!a[u])p[c.src].finished=true;d=b.elem||document.createElement("script");if(c.type)d.type=c.type;if(c.charset)d.charset=c.charset;t(d,b,"finished",f);if(b.elem){b.elem=null}else if(b.text){d.onload=d.onreadystatechange=null;d.text=b.text}else{d.src=c.real_src}i.insertBefore(d,i.firstChild);if(b.text){f()}}function S(c,b,d,f){var e,g,h=function(){b.ready_cb(b,function(){R(c,b,e)})},j=function(){b.finished_cb(b,d)};b.src=N(b.src,c[B]);b.real_src=b.src+(c[A]?((/\?.*$/.test(b.src)?"&_":"?_")+~~(Math.random()*1E9)+"="):"");if(!p[b.src])p[b.src]={items:[],finished:false};g=p[b.src].items;if(c[u]||g.length==0){e=g[g.length]={ready:false,finished:false,ready_listeners:[h],finished_listeners:[j]};P(c,b,e,((f)?function(){e.ready=true;for(var a=0;a<e.ready_listeners.length;a++){e.ready_listeners[a]()}e.ready_listeners=[]}:function(){I(e)}),f)}else{e=g[0];if(e.finished){j()}else{e.finished_listeners.push(j)}}}function v(){var e,g=s(l,{}),h=[],j=0,w=false,k;function T(a,c){a.ready=true;a.exec_trigger=c;x()}function U(a,c){a.ready=a.finished=true;a.exec_trigger=null;for(var b=0;b<c.scripts.length;b++){if(!c.scripts[b].finished)return}c.finished=true;x()}function x(){while(j<h.length){if(G(h[j])){try{h[j++]()}catch(err){}continue}else if(!h[j].finished){if(O(h[j]))continue;break}j++}if(j==h.length){w=false;k=false}}function V(){if(!k||!k.scripts){h.push(k={scripts:[],finished:true})}}e={script:function(){for(var f=0;f<arguments.length;f++){(function(a,c){var b;if(!H(a)){c=[a]}for(var d=0;d<c.length;d++){V();a=c[d];if(G(a))a=a();if(!a)continue;if(H(a)){b=[].slice.call(a);b.unshift(d,1);[].splice.apply(c,b);d--;continue}if(typeof a=="string")a={src:a};a=s(a,{ready:false,ready_cb:T,finished:false,finished_cb:U});k.finished=false;k.scripts.push(a);S(g,a,k,(Q&&w));w=true;if(g[z])e.wait()}})(arguments[f],arguments[f])}return e},wait:function(){if(arguments.length>0){for(var a=0;a<arguments.length;a++){h.push(arguments[a])}k=h[h.length-1]}else k=false;x();return e}};return{script:e.script,wait:e.wait,setOptions:function(a){s(a,g);return e}}}m={setGlobalDefaults:function(a){s(a,l);return m},setOptions:function(){return v().setOptions.apply(null,arguments)},script:function(){return v().script.apply(null,arguments)},wait:function(){return v().wait.apply(null,arguments)},queueScript:function(){n[n.length]={type:"script",args:[].slice.call(arguments)};return m},queueWait:function(){n[n.length]={type:"wait",args:[].slice.call(arguments)};return m},runQueue:function(){var a=m,c=n.length,b=c,d;for(;--b>=0;){d=n.shift();a=a[d.type].apply(null,d.args)}return a},noConflict:function(){o.$LAB=K;return m},sandbox:function(){return J()}};return m}o.$LAB=J();(function(a,c,b){if(document.readyState==null&&document[a]){document.readyState="loading";document[a](c,b=function(){document.removeEventListener(c,b,false);document.readyState="complete"},false)}})("addEventListener","DOMContentLoaded")})(this);
\ No newline at end of file
......@@ -8,6 +8,9 @@ refresh: Milliseconds between auto refresh.
timeformat: Format for time in histograms (might go away)
timefield: Field to use for ISO8601 timestamps (might go away)
indexpattern: Timestamping pattern for time based indices,
modules: Panel modules to load. In the future these will be inferred
from your initial dashboard, though if you share dashboards you
will probably need to list them all here
NOTE: No timezone support yet, everything is in UTC at the moment.
......@@ -17,8 +20,6 @@ shared.json contains an example sharable dashboard. Note the subtle differences
between dashboard.js and shared.json. Once is a javascript object, the other is
json.
PLEASE SEE js/
*/
var config = new Settings(
{
......@@ -29,8 +30,7 @@ var config = new Settings(
timefield: '@timestamp',
//indexpattern: '"logstash-"yyyy.mm.dd',
indexpattern: '"shakespeare"',
modules: ['histogram','piequery','pieterms','stackedquery','map'],
modules: ['histogram','pieterms','piequery','stackedquery','map'],
defaultfields: ['line_text'],
perpage: 50,
......@@ -42,4 +42,4 @@ var config = new Settings(
indexdefault: 'logstash-*',
primaryfield: '_all'
}
);
\ No newline at end of file
);
......@@ -17,44 +17,17 @@
<link rel="stylesheet" href="/common/css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="/common/css/elasticjs.css">
<link rel="stylesheet" href="/common/css/datepicker.css">
<link rel="stylesheet" href="/common/css/jquery-jvectormap.css">
<!-- project dependency libs -->
<script src="common/lib/jquery-1.8.0.min.js"></script>
<script src="common/lib/modernizr-2.6.1.min.js"></script>
<script src="/common/lib/LAB.min.js"></script>
<script src="common/lib/underscore.min.js"></script>
<script src="common/lib/angular.min.js"></script>
<script src="common/lib/elastic.min.js"></script>
<script src="common/lib/elastic-angular-client.min.js"></script>
<script src="common/lib/dateformat.js"></script>
<script src="common/lib/jquery.flot.js"></script>
<script src="common/lib/jquery.flot.time.js"></script>
<script src="common/lib/jquery.flot.stack.js"></script>
<script src="common/lib/jquery.flot.pie.js"></script>
<script src="common/lib/jquery.jvectormap.min.js"></script>
<script src="common/lib/jquery-jvectormap-world-mill-en.js"></script>
<script src="common/lib/date.js"></script>
<script src="common/lib/datepicker.js"></script>
<script src="common/lib/settings.js"></script>
<script src="config.js"></script>
<script src="common/lib/shared.js"></script>
<script src="dashboards.js"></script>
<!-- project specific files -->
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controllers.js"></script>
<script src="js/filters.js"></script>
<script src="js/directives.js"></script>
<script src="js/panels.js"></script>
</head>
<body ng-controller="DashCtrl">
<body ng-controller="DashCtrl" ng-cloak>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
......
......@@ -12,13 +12,41 @@ var modules = [
'kibana.panels',
]
var scripts = []
var labjs = $LAB
.script("common/lib/jquery-1.8.0.min.js").wait()
.script("common/lib/modernizr-2.6.1.min.js")
.script("common/lib/underscore.min.js")
.script("common/lib/angular.min.js")
.script("common/lib/elastic.min.js")
.script("common/lib/elastic-angular-client.min.js")
.script("common/lib/dateformat.js")
.script("common/lib/date.js")
.script("common/lib/datepicker.js")
.script("common/lib/shared.js")
.script("js/services.js")
.script("js/controllers.js")
.script("js/filters.js")
.script("js/directives.js")
.script("js/panels.js")
.script("dashboards.js");
_.each(config.modules, function(v) {
labjs = labjs.script('js/panels/'+v+'/module.js').wait()
modules.push('kibana.'+v)
})
/* Application level module which depends on filters, controllers, and services */
angular.module('kibana', modules).config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/dashboard', {
templateUrl: 'partials/dashboard.html'
})
.otherwise({
redirectTo: '/dashboard'
});
}]);
labjs.wait(function(){
angular.module('kibana', modules).config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/dashboard', {
templateUrl: 'partials/dashboard.html'
})
.otherwise({
redirectTo: '/dashboard'
});
}]);
});
......@@ -12,7 +12,7 @@ angular.module('kibana.directives', [])
return (attrs.panel && scope.index) ? true : false;
}, function (ready) {
if (ready) {
$compile("<div "+attrs.panel+" params={{panel}} style='height:{{row.height}}'></div>")(scope).replaceAll(element);
element.html($compile("<div "+attrs.panel+" params={{panel}} style='height:{{row.height}}'></div>")(scope))
}
});
}
......@@ -23,7 +23,6 @@ angular.module('kibana.directives', [])
return {
restrict: 'A',
link: function(scope, elem, attrs) {
console.log(elem);
function file_selected(evt) {
var files = evt.target.files; // FileList object
......
labjs = labjs.script("common/lib/jquery.flot.js")
.script("common/lib/jquery.flot.time.js")
angular.module('kibana.histogram', [])
.directive('histogram', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
// Specify defaults for ALL directives
var _d = {
query : "*",
interval: secondsToHms(calculate_interval(scope.from,scope.to,40,0)/1000),
color : "#27508C",
show : ['bars']
}
// Set ready flag and fill parameters (REQUIRED IN EVERY PANEL)
scope.$watch(function () {
return (attrs.params && scope.index) ? true : false;
}, function (ready) {
scope.ready = ready;
if(ready) {
scope.params = JSON.parse(attrs.params);
_.each(_d, function(v, k) {
scope.params[k] = _.isUndefined(scope.params[k])
? _d[k] : scope.params[k];
});
}
});
// Also get the data if time frame changes.
// (REQUIRED IN EVERY PANEL)
scope.$watch(function() {
return angular.toJson([scope.from, scope.to, scope.ready])
}, function(){
if(scope.ready)
if (_.isUndefined(attrs.params.interval))
scope.params.interval = secondsToHms(
calculate_interval(scope.from,scope.to,50,0)/1000),
get_data(scope,elem,attrs);
});
// Re-rending the panel if it is resized,
scope.$watch('data', function() {
if(scope.ready)
render_panel(scope,elem,attrs);
});
// Or if the model changes
angular.element(window).bind('resize', function(){
render_panel(scope,elem,attrs);
});
// Function for getting data
function get_data(scope,elem,attrs) {
var params = scope.params;
var ejs = scope.ejs;
var request = ejs.Request().indices(scope.index);
// Build the question part of the query
var query = ejs.FilteredQuery(
ejs.QueryStringQuery(params.query || '*'),
ejs.RangeFilter(config.timefield)
.from(scope.from)
.to(scope.to)
.cache(false)
);
// Then the insert into facet and make the request
var results = request
.facet(ejs.DateHistogramFacet('histogram')
.field(config.timefield)
.interval(params.interval)
.facetFilter(ejs.QueryFilter(query))
)
.doSearch();
// Populate scope when we have results
results.then(function(results) {
scope.hits = results.hits.total;
scope.data = results.facets.histogram.entries;
});
}
// Function for rendering panel
function render_panel(scope,elem,attrs) {
// Parse our params object
var params = scope.params;
// Determine format
var show = _.isUndefined(params.show) ? {
bars: true, lines: false, points: false
} : {
lines: _.indexOf(params.show,'lines') < 0 ? false : true,
bars: _.indexOf(params.show,'bars') < 0 ? false : true,
points: _.indexOf(params.show,'points') < 0 ? false : true,
}
// Push null values at beginning and end of timeframe
scope.graph = [
[scope.from.getTime(), null],[scope.to.getTime(), null]];
// Create FLOT value array
_.each(scope.data, function(v, k) {
scope.graph.push([v['time'],v['count']])
});
// Set barwidth based on specified interval
var barwidth = interval_to_seconds(params.interval)*1000
// Populate element
$.plot(elem, [{
label: _.isUndefined(params.label) ? params.query: params.label,
data: scope.graph
}], {
legend: {
position: "nw",
labelFormatter: function(label, series) {
return '<span class="legend">' + label + ' / ' + params.interval
+ '</span>';
}
},
series: {
lines: { show: show.lines, fill: false },
bars: { show: show.bars, fill: 1, barWidth: barwidth/1.8 },
points: { show: show.points },
color: params.color,
shadowSize: 1
},
yaxis: { min: 0, color: "#000" },
xaxis: {
mode: "time",
timeformat: "%H:%M:%S<br>%m-%d",
label: "Datetime",
color: "#000",
},
grid: {
backgroundColor: '#fff',
borderWidth: 0,
borderColor: '#eee',
color: "#eee",
hoverable: true,
}
});
//elem.show();
}
}
};
})
\ No newline at end of file
labjs = labjs.script("common/lib/jquery.jvectormap.min.js")
.script("common/lib/jquery-jvectormap-world-mill-en.js")
angular.module('kibana.map', [])
.directive('map', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
// Specify defaults for ALL directives
var _d = {
queries : ["*"],
interval: secondsToHms(calculate_interval(scope.from,scope.to,40,0)/1000),
colors : ["#BF3030","#1D7373","#86B32D","#A98A21","#411F73"],
show : ['bars'],
size : 100,
exclude : []
}
// Set ready flag and fill parameters (REQUIRED IN EVERY PANEL)
scope.$watch(function () {
return (attrs.params && scope.index) ? true : false;
}, function (ready) {
scope.ready = ready;
if(ready) {
scope.params = JSON.parse(attrs.params);
_.each(_d, function(v, k) {
scope.params[k] = _.isUndefined(scope.params[k])
? _d[k] : scope.params[k];
});
}
});
// Also get the data if time frame changes.
// (REQUIRED IN EVERY PANEL)
scope.$watch(function() {
return angular.toJson([scope.from, scope.to, scope.ready])
}, function(){
if(scope.ready)
get_data(scope,elem,attrs);
});
// Re-rending panel if data changes
scope.$watch('data', function() {
if(scope.ready)
render_panel(scope,elem,attrs);
});
// Or if the window is resized
angular.element(window).bind('resize', function(){
render_panel(scope,elem,attrs);
});
// Function for getting data
function get_data(scope,elem,attrs) {
var params = scope.params;
var ejs = scope.ejs;
var request = ejs.Request().indices(scope.index);
// Build the question part of the query
var query = ejs.FilteredQuery(
ejs.QueryStringQuery(params.query || '*'),
ejs.RangeFilter(config.timefield)
.from(scope.from)
.to(scope.to)
.cache(false)
);
// Then the insert into facet and make the request
var results = request
.facet(ejs.TermsFacet('worldmap')
.field(params.field)
.size(params['size'])
.exclude(params.exclude)
.facetFilter(ejs.QueryFilter(query))
)
.doSearch();
// Populate scope when we have results
results.then(function(results) {
scope.hits = results.hits.total;
scope.data = {};
_.each(results.facets.worldmap.terms, function(v) {
scope.data[v.term.toUpperCase()] = v.count;
});
});
}
// Function for rendering panel
function render_panel(scope,elem,attrs) {
// Parse our params object
var params = scope.params;
// Populate element
$('.jvectormap-label,.jvectormap-zoomin,.jvectormap-zoomout').remove();
elem.text('');
elem.vectorMap({
map: 'world_mill_en',
regionStyle: {initial: {fill: '#ddd'}},
zoomOnScroll: false,
backgroundColor: '#fff',
series: {
regions: [{
values: scope.data,
scale: ['#C8EEFF', '#0071A4'],
normalizeFunction: 'polynomial'
}]
}
});
//elem.show();
}
}
};
});
\ No newline at end of file
labjs = labjs.script("common/lib/jquery.flot.js")
.script("common/lib/jquery.flot.pie.js")
angular.module('kibana.piequery', [])
.directive('piequery', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
// Specify defaults for ALL directives
var _d = {
queries : ["*"],
donut : false,
tilt : false,
legend : true,
}
// Set ready flag and fill parameters (REQUIRED IN EVERY PANEL)
scope.$watch(function () {
return (attrs.params && scope.index) ? true : false;
}, function (ready) {
scope.ready = ready;
if(ready) {
scope.params = JSON.parse(attrs.params);
_.each(_d, function(v, k) {
scope.params[k] = _.isUndefined(scope.params[k])
? _d[k] : scope.params[k];
});
}
});
// Also get the data if time frame changes.
// (REQUIRED IN EVERY PANEL)
scope.$watch(function() {
return angular.toJson([scope.from, scope.to, scope.ready])
}, function(){
if(scope.ready)
get_data(scope,elem,attrs);
});
// Re-rending the panel if it is resized,
scope.$watch('data', function() {
if(scope.ready)
render_panel(scope,elem,attrs);
});
// Or if the model changes
angular.element(window).bind('resize', function(){
render_panel(scope,elem,attrs);
});
// Function for getting data
function get_data(scope,elem,attrs) {
var params = scope.params;
var ejs = scope.ejs;
var request = ejs.Request().indices(scope.index);
var queries = [];
// Build the question part of the query
_.each(params.queries, function(v) {
queries.push(ejs.FilteredQuery(
ejs.QueryStringQuery(v || '*'),
ejs.RangeFilter(config.timefield)
.from(scope.from)
.to(scope.to)
.cache(false))
)
});
_.each(queries, function(v) {
request = request.facet(ejs.QueryFacet(_.indexOf(queries,v))
.query(v)
.facetFilter(ejs.QueryFilter(v))
)
})
// Then the insert into facet and make the request
var results = request.doSearch();
// Populate scope when we have results
results.then(function(results) {
scope.hits = results.hits.total;
scope.data = results.facets;
});
}
// Function for rendering panel
function render_panel(scope,elem,attrs) {
// Parse our params object
var params = scope.params;
// Create graph array
scope.graph = [];
_.each(scope.data, function(v, k) {
var point = {
label : params.queries[k],
data : v['count']
}
if(!_.isUndefined(params.colors))
point.color = params.colors[k%params.colors.length];
scope.graph.push(point)
});
// Populate element
$.plot(elem, scope.graph, {
series: {
pie: {
innerRadius: params.donut ? 0.4 : 0,
tilt: params.tilt ? 0.45 : 1,
radius: 1,
show: true,
combine: {
color: '#999',
label: 'The Rest'
},
label: {
show: true,
radius: 2/3,
formatter: function(label, series){
return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
label+'<br/>'+Math.round(series.percent)+'%</div>';
},
threshold: 0.1
}
}
},
//grid: { hoverable: true, clickable: true },
legend: { show: params.legend }
});
//elem.show();
}
}
};
})
\ No newline at end of file
labjs = labjs.script("common/lib/jquery.flot.js")
.script("common/lib/jquery.flot.pie.js")
angular.module('kibana.pieterms', [])
.directive('pieterms', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
// Specify defaults for ALL directives
var _d = {
size : 5,
query : "*",
exclude : [],
donut : false,
tilt : false,
legend : true,
}
// Set ready flag and fill parameters (REQUIRED IN EVERY PANEL)
scope.$watch(function () {
return (attrs.params && scope.index) ? true : false;
}, function (ready) {
scope.ready = ready;
if(ready) {
scope.params = JSON.parse(attrs.params);
_.each(_d, function(v, k) {
scope.params[k] = _.isUndefined(scope.params[k])
? _d[k] : scope.params[k];
});
}
});
// Also get the data if time frame changes.
// (REQUIRED IN EVERY PANEL)
scope.$watch(function() {
return angular.toJson([scope.from, scope.to, scope.ready])
}, function(){
if(scope.ready)
get_data(scope,elem,attrs);
});
// Re-rending the panel if it is resized,
scope.$watch('data', function() {
if(scope.ready)
render_panel(scope,elem,attrs);
});
// Or if the model changes
angular.element(window).bind('resize', function(){
render_panel(scope,elem,attrs);
});
// Function for getting data
function get_data(scope,elem,attrs) {
var params = scope.params;
var ejs = scope.ejs;
var request = ejs.Request().indices(scope.index);
// Build the question part of the query
var query = ejs.FilteredQuery(
ejs.QueryStringQuery(params.query || '*'),
ejs.RangeFilter(config.timefield)
.from(scope.from)
.to(scope.to)
.cache(false)
);
// Then the insert into facet and make the request
var results = request
.facet(ejs.TermsFacet('termpie')
.field(params.field)
.size(params['size'])
.exclude(params.exclude)
.facetFilter(ejs.QueryFilter(query))
)
.doSearch();
// Populate scope when we have results
results.then(function(results) {
scope.hits = results.hits.total;
scope.data = results.facets.termpie.terms;
});
}
// Function for rendering panel
function render_panel(scope,elem,attrs) {
// Parse our params object
var params = scope.params;
// Create graph array
scope.graph = [];
_.each(scope.data, function(v, k) {
if(!_.isUndefined(params.only) && _.indexOf(params.only,v['term']) < 0)
return
var point = {
label : v['term'],
data : v['count']
}
if(!_.isUndefined(params.colors))
point.color = params.colors[_.indexOf(params.only,v['term'])]
scope.graph.push(point)
});
var pie = {
series: {
pie: {
innerRadius: params.donut ? 0.4 : 0,
tilt: params.tilt ? 0.45 : 1,
radius: 1,
show: true,
combine: {
color: '#999',
label: 'The Rest'
},
label: {
show: true,
radius: 2/3,
formatter: function(label, series){
return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'+
label+'<br/>'+Math.round(series.percent)+'%</div>';
},
threshold: 0.1
}
}
},
//grid: { hoverable: true, clickable: true },
legend: { show: params.legend }
};
// Populate element
$.plot(elem, scope.graph, pie);
//elem.show();
}
}
};
})
\ No newline at end of file
labjs = labjs.script("common/lib/jquery.flot.js")
.script("common/lib/jquery.flot.time.js")
.script("common/lib/jquery.flot.stack.js")
angular.module('kibana.stackedquery', [])
.directive('stackedquery', function() {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
// Specify defaults for ALL directives
var _d = {
queries : ["*"],
interval: secondsToHms(calculate_interval(scope.from,scope.to,40,0)/1000),
colors : ["#BF3030","#1D7373","#86B32D","#A98A21","#411F73"],
show : ['bars']
}
// Set ready flag and fill parameters (REQUIRED IN EVERY PANEL)
scope.$watch(function () {
return (attrs.params && scope.index) ? true : false;
}, function (ready) {
scope.ready = ready;
if(ready) {
scope.params = JSON.parse(attrs.params);
_.each(_d, function(v, k) {
scope.params[k] = _.isUndefined(scope.params[k])
? _d[k] : scope.params[k];
});
}
});
// Also get the data if time frame changes.
// (REQUIRED IN EVERY PANEL)
scope.$watch(function() {
return angular.toJson([scope.from, scope.to, scope.ready])
}, function(){
if(scope.ready)
if (_.isUndefined(attrs.params.interval))
scope.params.interval = secondsToHms(
calculate_interval(scope.from,scope.to,50,0)/1000),
get_data(scope,elem,attrs);
});
// Re-rending the panel if it is resized,
scope.$watch('data', function() {
if(scope.ready)
render_panel(scope,elem,attrs);
});
// Or if the model changes
angular.element(window).bind('resize', function(){
render_panel(scope,elem,attrs);
});
// Function for getting data
function get_data(scope,elem,attrs) {
var params = scope.params;
var ejs = scope.ejs;
var request = ejs.Request().indices(scope.index);
// Build the question part of the query
var queries = [];
_.each(params.queries, function(v) {
queries.push(ejs.FilteredQuery(
ejs.QueryStringQuery(v || '*'),
ejs.RangeFilter(config.timefield)
.from(scope.from)
.to(scope.to)
.cache(false))
)
});
// Build the facet part
_.each(queries, function(v) {
request = request
.facet(ejs.DateHistogramFacet(_.indexOf(queries,v))
.field(config.timefield)
.interval(params.interval)
.facetFilter(ejs.QueryFilter(v))
)
})
// Then run it
var results = request.doSearch();
// Populate scope when we have results
results.then(function(results) {
scope.hits = results.hits.total;
scope.data = results.facets;
});
}
// Function for rendering panel
function render_panel(scope,elem,attrs) {
// Parse our params object
var params = scope.params;
// Determine format
var show = _.isUndefined(params.show) ? {
bars: true, lines: false, points: false, fill: false
} : {
lines: _.indexOf(params.show,'lines') < 0 ? false : true,
bars: _.indexOf(params.show,'bars') < 0 ? false : true,
points: _.indexOf(params.show,'points') < 0 ? false : true,
fill: _.indexOf(params.show,'fill') < 0 ? false : true
}
scope.graph = [];
// Push null values at beginning and end of timeframe
_.each(scope.data, function(v, k) {
var series = {};
var data = [[scope.from.getTime(), null]];
_.each(v.entries, function(v, k) {
data.push([v['time'],v['count']])
});
data.push([scope.to.getTime(), null])
series.data = {
label: params.queries[k],
data: data,
color: params.colors[k%params.colors.length]
};
scope.graph.push(series.data)
});
// Set barwidth based on specified interval
var barwidth = interval_to_seconds(params.interval)*1000
// Populate element
$.plot(elem, scope.graph, {
legend: {
position: "nw",
labelFormatter: function(label, series) {
return '<span class="legend">' + label + ' / ' + params.interval
+ '</span>';
}
},
series: {
stack: 0,
lines: { show: show.lines, fill: show.fill },
bars: { show: show.bars, fill: 1, barWidth: barwidth/1.8 },
points: { show: show.points },
color: params.color,
shadowSize: 1
},
yaxis: { min: 0, color: "#000" },
xaxis: {
mode: "time",
timeformat: "%H:%M:%S<br>%m-%d",
label: "Datetime",
color: "#000",
},
grid: {
backgroundColor: '#fff',
borderWidth: 0,
borderColor: '#eee',
color: "#eee",
hoverable: true,
}
});
//elem.show();
}
}
};
})
\ No newline at end of file
This diff is collapsed. Click to expand it.
#!/usr/bin/env bash
echo "Generating bulk indexable shakespeare lines with timestamp 3 hours in the past and 5 hours into the future"
node reader.js > indexme.json
echo "Performing bulk indexing into localhost:9200"
curl -XPUT localhost:9200/_bulk --data-binary @indexme.json;echo
echo
echo "If tons of JSON just scrolled above, I've probably successfully loaded over 100,000 lines of shakespeare into localhost:9200/shakespeare"
language: node_js
node_js:
- 0.8
\ No newline at end of file
test:
@NODE_ENV=test ./node_modules/.bin/mocha
test-w:
@NODE_ENV=test ./node_modules/.bin/mocha \
--growl \
--watch
.PHONY: test test-w
# Random Weighted Choice
[![Build Status](https://secure.travis-ci.org/parmentf/random-weighted-choice.png?branch=master)](http://travis-ci.org/parmentf/random-weighted-choice)
Node.js module to make a random choice among weighted elements of table.
## Installation
With [npm](http://npmjs.org) do:
$ npm install random-weighted-choice
## Examples
Although you can add several times the same id
var rwc = require('random-weighted-choice');
var table = [
{ weight: 1, id: "item1"} // Element 1
, { weight: 1, id: "item2"} // Element 2
, { weight: 4, id: "item3"} // Element with a 4 times likelihood
, { weight: 2, id: "item1"} // Element 1, weight added with 2 => 3
];
var choosenItem = rwc(table);
var choosenUnlikely = rwc(table, 100); // The last shall be first
var choosenDeterministically = rwc(table, 0);
It is better to not use the same twice, if you want a temperature other than
the default one (50).
var rwc = require('random-weighted-choice');
var table = [
{ weight: 1, id: "item1"} // Element 1
, { weight: 1, id: "item2"} // Element 2
, { weight: 4, id: "item3"} // Element with a 4 times likelihood
, { weight: 2, id: "item4"} // Element 4
, { weight: 2, id: "item5"}
];
var choosenItem = rwc(table);
var choosenUnlikely = rwc(table, 100); // The last shall be first
var choosenDeterministically = rwc(table, 0);
Without temperature (second parameter) or a 50 value, likelihoods are:
{ item1: 10%, item2: 10%, item3: 40%, item4: 20%, item5: 20% }
With a temperature value of 100:
{ item1: 30%, item2: 30%, item3: 0%, item4: 20%, item5: 20% }
With a temperature value of 0, modified weights are:
{ item1: 0, item2: 0, item3: 8, item4: 2, item5: 2 }
## Usage
### random-weighted-choice(Array table, Number temperature = 50)
Return the ``id`` of the chosen item from ``table``.
The ``table`` parameter should contain an Array. Each item of that Array must
bean object, with at least ``weight`` and ``id`` property.
Weight values are relative to each other. They are integers.
When the sum of the weight values is ``null``, ``null`` is returned (can't choose).
When the Array is empty, ``null`` is returned.
More explanations on how it works on [Everything2](http://everything2.com/title/Blackboard+temperature).
## Also
* https://github.com/Schoonology/weighted
\ No newline at end of file
/*jshint node:true, laxcomma:true */
"use strict";
var debug = require('debug')('rwc');
var RandomWeightedChoice = function (table, temperature, randomFunction, influence) {
influence = influence || 2; // Seems fine, difficult to tune
if (typeof(temperature)=="undefined") temperature = 50; // in [0,100], 50 is neutral
temperature = temperature | 50;
debug('temperature', temperature);
var T = (temperature - 50) / 50;
if (typeof(randomFunction)=="undefined") randomFunction = Math.random;
var nb = table.length;
if(!nb) return null; // No item given.
var total = 0;
table.forEach(function(element, index) {
total += element.weight;
});
var avg = total / nb;
debug('total', total);
debug('nb', nb);
debug('avg', avg);
// Compute amplified urgencies (depending on temperature)
var ur = {};
var urgencySum = 0;
table.forEach(function(element, index) {
var urgency = element.weight + T * influence * (avg - element.weight);
if (urgency < 0) urgency = 0;
urgencySum += urgency;
ur[element.id] = (ur[element.id] || 0 ) + urgency;
});
var cumulatedUrgencies = {};
var currentUrgency = 0;
Object.keys(ur).forEach(function(id, index) {
currentUrgency += ur[id];
cumulatedUrgencies[id] = currentUrgency;
});
if(urgencySum < 1) return null; // No weight given
// Choose
var choice = randomFunction() * urgencySum;
debug('ur', ur);
debug('cumulatedUrgencies', cumulatedUrgencies);
debug('urgencySum', urgencySum);
debug('choice', choice);
var ids = Object.keys(cumulatedUrgencies);
for(var i=0; i<ids.length; i++) {
var id = ids[i];
var urgency = cumulatedUrgencies[id];
if(choice <= urgency) {
debug('return', id);
return id;
}
}
};
module.exports = RandomWeightedChoice;
\ No newline at end of file
0.7.0 / 2012-05-04
==================
* Added .component to package.json
* Added debug.component.js build
0.6.0 / 2012-03-16
==================
* Added support for "-" prefix in DEBUG [Vinay Pulim]
* Added `.enabled` flag to the node version [TooTallNate]
0.5.0 / 2012-02-02
==================
* Added: humanize diffs. Closes #8
* Added `debug.disable()` to the CS variant
* Removed padding. Closes #10
* Fixed: persist client-side variant again. Closes #9
0.4.0 / 2012-02-01
==================
* Added browser variant support for older browsers [TooTallNate]
* Added `debug.enable('project:*')` to browser variant [TooTallNate]
* Added padding to diff (moved it to the right)
0.3.0 / 2012-01-26
==================
* Added millisecond diff when isatty, otherwise UTC string
0.2.0 / 2012-01-22
==================
* Added wildcard support
0.1.0 / 2011-12-02
==================
* Added: remove colors unless stderr isatty [TooTallNate]
0.0.1 / 2010-01-03
==================
* Initial release
debug.component.js: head.js debug.js tail.js
cat $^ > $@
# debug
tiny node.js debugging utility.
## Installation
```
$ npm install debug
```
## Example
This module is modelled after node core's debugging technique, allowing you to enable one or more topic-specific debugging functions, for example core does the following within many modules:
```js
var debug;
if (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) {
debug = function(x) {
var prefix = process.pid + ',' +
(process.env.NODE_WORKER_ID ? 'Worker' : 'Master');
console.error(prefix, x);
};
} else {
debug = function() { };
}
```
This concept is extremely simple but it works well. With `debug` you simply invoke the exported function to generate your debug function, passing it a name which will determine if a noop function is returned, or a decorated `console.error`, so all of the `console` format string goodies you're used to work fine. A unique color is selected per-function for visibility.
Example _app.js_:
```js
var debug = require('debug')('http')
, http = require('http')
, name = 'My App';
// fake app
debug('booting %s', name);
http.createServer(function(req, res){
debug(req.method + ' ' + req.url);
res.end('hello\n');
}).listen(3000, function(){
debug('listening');
});
// fake worker of some kind
require('./worker');
```
Example _worker.js_:
```js
var debug = require('debug')('worker');
setInterval(function(){
debug('doing some work');
}, 1000);
```
The __DEBUG__ environment variable is then used to enable these based on space or comma-delimited names. Here are some examples:
![debug http and worker](http://f.cl.ly/items/18471z1H402O24072r1J/Screenshot.png)
![debug worker](http://f.cl.ly/items/1X413v1a3M0d3C2c1E0i/Screenshot.png)
## Millisecond diff
When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the "+NNNms" will show you how much time was spent between calls.
![](http://f.cl.ly/items/2i3h1d3t121M2Z1A3Q0N/Screenshot.png)
When stdout is not a TTY, `Date#toUTCString()` is used, making it more useful for logging the debug information as shown below:
![](http://f.cl.ly/items/112H3i0e0o0P0a2Q2r11/Screenshot.png)
## Conventions
If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser".
## Wildcards
The "*" character may be used as a wildcard. Suppose for example your library has debuggers named "connect:bodyParser", "connect:compress", "connect:session", instead of listing all three with `DEBUG=connect:bodyParser,connect.compress,connect:session`, you may simply do `DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`.
You can also exclude specific debuggers by prefixing them with a "-" character. For example, `DEBUG=* -connect:*` would include all debuggers except those starting with "connect:".
## Browser support
Debug works in the browser as well, currently persisted by `localStorage`. For example if you have `worker:a` and `worker:b` as shown below, and wish to debug both type `debug.enable('worker:*')` in the console and refresh the page, this will remain until you disable with `debug.disable()`.
```js
a = debug('worker:a');
b = debug('worker:b');
setInterval(function(){
a('doing some work');
}, 1000);
setInterval(function(){
a('doing some work');
}, 1200);
```
## License
(The MIT License)
Copyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
;(function(){
/**
* Create a debugger with the given `name`.
*
* @param {String} name
* @return {Type}
* @api public
*/
function debug(name) {
if (!debug.enabled(name)) return function(){};
return function(fmt){
var curr = new Date;
var ms = curr - (debug[name] || curr);
debug[name] = curr;
fmt = name
+ ' '
+ fmt
+ ' +' + debug.humanize(ms);
// This hackery is required for IE8
// where `console.log` doesn't have 'apply'
window.console
&& console.log
&& Function.prototype.apply.call(console.log, console, arguments);
}
}
/**
* The currently active debug mode names.
*/
debug.names = [];
debug.skips = [];
/**
* Enables a debug mode by name. This can include modes
* separated by a colon and wildcards.
*
* @param {String} name
* @api public
*/
debug.enable = function(name) {
localStorage.debug = name;
var split = (name || '').split(/[\s,]+/)
, len = split.length;
for (var i = 0; i < len; i++) {
name = split[i].replace('*', '.*?');
if (name[0] === '-') {
debug.skips.push(new RegExp('^' + name.substr(1) + '$'));
}
else {
debug.names.push(new RegExp('^' + name + '$'));
}
}
};
/**
* Disable debug output.
*
* @api public
*/
debug.disable = function(){
debug.enable('');
};
/**
* Humanize the given `ms`.
*
* @param {Number} m
* @return {String}
* @api private
*/
debug.humanize = function(ms) {
var sec = 1000
, min = 60 * 1000
, hour = 60 * min;
if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
if (ms >= min) return (ms / min).toFixed(1) + 'm';
if (ms >= sec) return (ms / sec | 0) + 's';
return ms + 'ms';
};
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
debug.enabled = function(name) {
for (var i = 0, len = debug.skips.length; i < len; i++) {
if (debug.skips[i].test(name)) {
return false;
}
}
for (var i = 0, len = debug.names.length; i < len; i++) {
if (debug.names[i].test(name)) {
return true;
}
}
return false;
};
// persist
if (window.localStorage) debug.enable(localStorage.debug);
module.exports = debug;
})();
\ No newline at end of file
/**
* Create a debugger with the given `name`.
*
* @param {String} name
* @return {Type}
* @api public
*/
function debug(name) {
if (!debug.enabled(name)) return function(){};
return function(fmt){
var curr = new Date;
var ms = curr - (debug[name] || curr);
debug[name] = curr;
fmt = name
+ ' '
+ fmt
+ ' +' + debug.humanize(ms);
// This hackery is required for IE8
// where `console.log` doesn't have 'apply'
window.console
&& console.log
&& Function.prototype.apply.call(console.log, console, arguments);
}
}
/**
* The currently active debug mode names.
*/
debug.names = [];
debug.skips = [];
/**
* Enables a debug mode by name. This can include modes
* separated by a colon and wildcards.
*
* @param {String} name
* @api public
*/
debug.enable = function(name) {
localStorage.debug = name;
var split = (name || '').split(/[\s,]+/)
, len = split.length;
for (var i = 0; i < len; i++) {
name = split[i].replace('*', '.*?');
if (name[0] === '-') {
debug.skips.push(new RegExp('^' + name.substr(1) + '$'));
}
else {
debug.names.push(new RegExp('^' + name + '$'));
}
}
};
/**
* Disable debug output.
*
* @api public
*/
debug.disable = function(){
debug.enable('');
};
/**
* Humanize the given `ms`.
*
* @param {Number} m
* @return {String}
* @api private
*/
debug.humanize = function(ms) {
var sec = 1000
, min = 60 * 1000
, hour = 60 * min;
if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
if (ms >= min) return (ms / min).toFixed(1) + 'm';
if (ms >= sec) return (ms / sec | 0) + 's';
return ms + 'ms';
};
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
debug.enabled = function(name) {
for (var i = 0, len = debug.skips.length; i < len; i++) {
if (debug.skips[i].test(name)) {
return false;
}
}
for (var i = 0, len = debug.names.length; i < len; i++) {
if (debug.names[i].test(name)) {
return true;
}
}
return false;
};
// persist
if (window.localStorage) debug.enable(localStorage.debug);
\ No newline at end of file
var debug = require('../')('http')
, http = require('http')
, name = 'My App';
// fake app
debug('booting %s', name);
http.createServer(function(req, res){
debug(req.method + ' ' + req.url);
res.end('hello\n');
}).listen(3000, function(){
debug('listening');
});
// fake worker of some kind
require('./worker');
\ No newline at end of file
<html>
<head>
<title>debug()</title>
<script src="../debug.js"></script>
<script>
// type debug.enable('*') in
// the console and refresh :)
a = debug('worker:a');
b = debug('worker:b');
setInterval(function(){
a('doing some work');
}, 1000);
setInterval(function(){
a('doing some work');
}, 1200);
</script>
</head>
<body>
</body>
</html>
var debug = {
foo: require('../')('test:foo'),
bar: require('../')('test:bar'),
baz: require('../')('test:baz')
};
debug.foo('foo')
debug.bar('bar')
debug.baz('baz')
\ No newline at end of file
// DEBUG=* node example/worker
// DEBUG=worker:* node example/worker
// DEBUG=worker:a node example/worker
// DEBUG=worker:b node example/worker
var a = require('../')('worker:a')
, b = require('../')('worker:b');
function work() {
a('doing lots of uninteresting work');
setTimeout(work, Math.random() * 1000);
}
work();
function workb() {
b('doing some work');
setTimeout(workb, Math.random() * 2000);
}
workb();
\ No newline at end of file
module.exports = require('./lib/debug');
\ No newline at end of file
/**
* Module dependencies.
*/
var tty = require('tty');
/**
* Expose `debug()` as the module.
*/
module.exports = debug;
/**
* Enabled debuggers.
*/
var names = []
, skips = [];
(process.env.DEBUG || '')
.split(/[\s,]+/)
.forEach(function(name){
name = name.replace('*', '.*?');
if (name[0] === '-') {
skips.push(new RegExp('^' + name.substr(1) + '$'));
} else {
names.push(new RegExp('^' + name + '$'));
}
});
/**
* Colors.
*/
var colors = [6, 2, 3, 4, 5, 1];
/**
* Previous debug() call.
*/
var prev = {};
/**
* Previously assigned color.
*/
var prevColor = 0;
/**
* Is stdout a TTY? Colored output is disabled when `true`.
*/
var isatty = tty.isatty(2);
/**
* Select a color.
*
* @return {Number}
* @api private
*/
function color() {
return colors[prevColor++ % colors.length];
}
/**
* Humanize the given `ms`.
*
* @param {Number} m
* @return {String}
* @api private
*/
function humanize(ms) {
var sec = 1000
, min = 60 * 1000
, hour = 60 * min;
if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
if (ms >= min) return (ms / min).toFixed(1) + 'm';
if (ms >= sec) return (ms / sec | 0) + 's';
return ms + 'ms';
}
/**
* Create a debugger with the given `name`.
*
* @param {String} name
* @return {Type}
* @api public
*/
function debug(name) {
function disabled(){}
disabled.enabled = false;
var match = skips.some(function(re){
return re.test(name);
});
if (match) return disabled;
match = names.some(function(re){
return re.test(name);
});
if (!match) return disabled;
var c = color();
function colored(fmt) {
var curr = new Date;
var ms = curr - (prev[name] || curr);
prev[name] = curr;
fmt = ' \033[9' + c + 'm' + name + ' '
+ '\033[3' + c + 'm\033[90m'
+ fmt + '\033[3' + c + 'm'
+ ' +' + humanize(ms) + '\033[0m';
console.error.apply(this, arguments);
}
function plain(fmt) {
fmt = new Date().toUTCString()
+ ' ' + name + ' ' + fmt;
console.error.apply(this, arguments);
}
colored.enabled = plain.enabled = true;
return isatty
? colored
: plain;
}
{
"name": "debug",
"version": "0.7.0",
"description": "small debugging utility",
"keywords": [
"debug",
"log",
"debugger"
],
"author": {
"name": "TJ Holowaychuk",
"email": "tj@vision-media.ca"
},
"dependencies": {},
"devDependencies": {
"mocha": "*"
},
"main": "index",
"browserify": "debug.component.js",
"engines": {
"node": "*"
},
"component": {
"scripts": {
"debug": "debug.component.js"
}
},
"readme": "\n# debug\n\n tiny node.js debugging utility.\n\n## Installation\n\n```\n$ npm install debug\n```\n\n## Example\n\n This module is modelled after node core's debugging technique, allowing you to enable one or more topic-specific debugging functions, for example core does the following within many modules:\n\n```js\nvar debug;\nif (process.env.NODE_DEBUG && /cluster/.test(process.env.NODE_DEBUG)) {\n debug = function(x) {\n var prefix = process.pid + ',' +\n (process.env.NODE_WORKER_ID ? 'Worker' : 'Master');\n console.error(prefix, x);\n };\n} else {\n debug = function() { };\n}\n```\n\n This concept is extremely simple but it works well. With `debug` you simply invoke the exported function to generate your debug function, passing it a name which will determine if a noop function is returned, or a decorated `console.error`, so all of the `console` format string goodies you're used to work fine. A unique color is selected per-function for visibility.\n \nExample _app.js_:\n\n```js\nvar debug = require('debug')('http')\n , http = require('http')\n , name = 'My App';\n\n// fake app\n\ndebug('booting %s', name);\n\nhttp.createServer(function(req, res){\n debug(req.method + ' ' + req.url);\n res.end('hello\\n');\n}).listen(3000, function(){\n debug('listening');\n});\n\n// fake worker of some kind\n\nrequire('./worker');\n```\n\nExample _worker.js_:\n\n```js\nvar debug = require('debug')('worker');\n\nsetInterval(function(){\n debug('doing some work');\n}, 1000);\n```\n\n The __DEBUG__ environment variable is then used to enable these based on space or comma-delimited names. Here are some examples:\n\n ![debug http and worker](http://f.cl.ly/items/18471z1H402O24072r1J/Screenshot.png)\n\n ![debug worker](http://f.cl.ly/items/1X413v1a3M0d3C2c1E0i/Screenshot.png)\n\n## Millisecond diff\n\n When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the \"+NNNms\" will show you how much time was spent between calls.\n\n ![](http://f.cl.ly/items/2i3h1d3t121M2Z1A3Q0N/Screenshot.png)\n\n When stdout is not a TTY, `Date#toUTCString()` is used, making it more useful for logging the debug information as shown below:\n \n ![](http://f.cl.ly/items/112H3i0e0o0P0a2Q2r11/Screenshot.png)\n\n## Conventions\n\n If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use \":\" to separate features. For example \"bodyParser\" from Connect would then be \"connect:bodyParser\". \n\n## Wildcards\n\n The \"*\" character may be used as a wildcard. Suppose for example your library has debuggers named \"connect:bodyParser\", \"connect:compress\", \"connect:session\", instead of listing all three with `DEBUG=connect:bodyParser,connect.compress,connect:session`, you may simply do `DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`.\n\n You can also exclude specific debuggers by prefixing them with a \"-\" character. For example, `DEBUG=* -connect:*` would include all debuggers except those starting with \"connect:\".\n\n## Browser support\n\n Debug works in the browser as well, currently persisted by `localStorage`. For example if you have `worker:a` and `worker:b` as shown below, and wish to debug both type `debug.enable('worker:*')` in the console and refresh the page, this will remain until you disable with `debug.disable()`. \n\n```js\na = debug('worker:a');\nb = debug('worker:b');\n\nsetInterval(function(){\n a('doing some work');\n}, 1000);\n\nsetInterval(function(){\n a('doing some work');\n}, 1200);\n```\n\n## License \n\n(The MIT License)\n\nCopyright (c) 2011 TJ Holowaychuk &lt;tj@vision-media.ca&gt;\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
"readmeFilename": "Readme.md",
"_id": "debug@0.7.0",
"_from": "debug@0.7.x"
}
module.exports = debug;
})();
\ No newline at end of file
{
"name": "random-weighted-choice",
"version": "0.1.1",
"engines": {
"node": "*"
},
"author": {
"name": "François Parmentier"
},
"main": "lib/random-weighted-choice.js",
"readmeFilename": "README.md",
"description": "Node.js module to make a random choice among weighted elements of table.",
"dependencies": {
"debug": "0.7.x"
},
"devDependencies": {
"mocha": "1.7.x"
},
"scripts": {
"test": "make test"
},
"homepage": "http://github.com/parmentf/random-weighted-choice",
"repository": {
"type": "git",
"url": "https://github.com/parmentf/random-weighted-choice.git"
},
"keywords": [
"random",
"weighted"
],
"license": "BSD",
"readme": "# Random Weighted Choice\n\n[![Build Status](https://secure.travis-ci.org/parmentf/random-weighted-choice.png?branch=master)](http://travis-ci.org/parmentf/random-weighted-choice)\n\nNode.js module to make a random choice among weighted elements of table.\n\n## Installation\n\nWith [npm](http://npmjs.org) do:\n\n $ npm install random-weighted-choice\n\n\n## Examples\n\nAlthough you can add several times the same id\n\n var rwc = require('random-weighted-choice');\n var table = [\n { weight: 1, id: \"item1\"} // Element 1\n , { weight: 1, id: \"item2\"} // Element 2\n , { weight: 4, id: \"item3\"} // Element with a 4 times likelihood\n , { weight: 2, id: \"item1\"} // Element 1, weight added with 2 => 3\n ];\n var choosenItem = rwc(table);\n var choosenUnlikely = rwc(table, 100); // The last shall be first\n var choosenDeterministically = rwc(table, 0);\n\nIt is better to not use the same twice, if you want a temperature other than\nthe default one (50).\n\n var rwc = require('random-weighted-choice');\n var table = [\n { weight: 1, id: \"item1\"} // Element 1\n , { weight: 1, id: \"item2\"} // Element 2\n , { weight: 4, id: \"item3\"} // Element with a 4 times likelihood\n , { weight: 2, id: \"item4\"} // Element 4\n , { weight: 2, id: \"item5\"}\n ];\n var choosenItem = rwc(table);\n var choosenUnlikely = rwc(table, 100); // The last shall be first\n var choosenDeterministically = rwc(table, 0);\n\nWithout temperature (second parameter) or a 50 value, likelihoods are:\n\n { item1: 10%, item2: 10%, item3: 40%, item4: 20%, item5: 20% }\n\nWith a temperature value of 100:\n\n { item1: 30%, item2: 30%, item3: 0%, item4: 20%, item5: 20% }\n\nWith a temperature value of 0, modified weights are:\n\n { item1: 0, item2: 0, item3: 8, item4: 2, item5: 2 }\n\n## Usage\n\n### random-weighted-choice(Array table, Number temperature = 50)\n\nReturn the ``id`` of the chosen item from ``table``.\n\nThe ``table`` parameter should contain an Array. Each item of that Array must\nbean object, with at least ``weight`` and ``id`` property.\n\nWeight values are relative to each other. They are integers.\n\nWhen the sum of the weight values is ``null``, ``null`` is returned (can't choose).\n\nWhen the Array is empty, ``null`` is returned.\n\nMore explanations on how it works on [Everything2](http://everything2.com/title/Blackboard+temperature).\n\n## Also\n\n* https://github.com/Schoonology/weighted",
"_id": "random-weighted-choice@0.1.1",
"_from": "random-weighted-choice"
}
/*jshint node:true, laxcomma:true */
/*global describe:true, it:true */
"use strict";
var debug = require('debug')('rwc:test');
var assert = require('assert');
var rwc = require('../lib/random-weighted-choice');
var randomCounter = 0;
var randomValues = [0,0.19,0.5,0.7,0.9];
var randomMock = function(values, reset) {
if(typeof(values)=="undefined") values = randomValues;
if (typeof(reset)=="undefined") reset = false;
if (reset) randomCounter = 0;
return values[randomCounter++];
};
describe('Temperature 50', function () {
var table = [
{ weight: 1, id: "item1"} // Element 1
, { weight: 1, id: "item2"} // Element 2
, { weight: 4, id: "item3"} // Element with a 4 times likelihood
, { weight: 2, id: "item4"} // Element 4
, { weight: 2, id: "item5"}
];
it('should return "item1"', function (){
assert.equal('item1', rwc(table,null,randomMock));
});
it('should return "item2"', function (){
assert.equal('item2', rwc(table,null,randomMock));
});
it('should return "item3"', function (){
assert.equal('item3', rwc(table,null,randomMock));
});
it('should return "item4"', function (){
assert.equal('item4', rwc(table,null,randomMock));
});
it('should return "item5"', function (){
assert.equal('item5', rwc(table,null,randomMock));
});
});
describe('Empty table', function () {
it('should return null', function () {
assert.equal(null, rwc([]));
});
});
describe('One element', function () {
it('should return the element', function () {
assert.equal('a', rwc([{weight:1, id: 'a'}]));
});
});
describe('No weight', function () {
it('should return null', function () {
assert.equal(null, rwc([{weight:0, id: 'a'}]));
});
});
module.exports.random = randomMock;
\ No newline at end of file
require("./date.js")
var rwc = require('random-weighted-choice');
_ = require("./underscore.min.js")
fs = require('fs')
fs.readFile('shakespeare.json', 'utf8', function (err,data) {
i = 0;
if (err) {
return console.log(err);
}
var obj = JSON.parse(data);
_.each(obj, function (o) {
setTimeout(print_obj(o), 10000) //wait ten seconds before continuing
});
function getRandomInRange(from, to, fixed) {
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
}
function print_obj(o) {
var countries = get_countries();
var randomnumber=Math.floor(Math.random()*28800000)
var command = {index:{_index: "shakespeare", _type: "line", _id: i}};
o['@timestamp'] = new Date((new Date()).getTime() -9000000 + randomnumber);
o.geo = [getRandomInRange(-90, 90, 3),getRandomInRange(-180, 180, 3)]
o.country = rwc(countries);
console.log(JSON.stringify(command))
console.log(JSON.stringify(o));
i = i + 1;
}
function get_countries() {
return [{id:"CN",weight:1330044000},
{id:"IN",weight:1173108018},
{id:"US",weight:610232863},
{id:"ID",weight:242968342},
{id:"BR",weight:201103330},
{id:"PK",weight:184404791},
{id:"BD",weight:156118464},
{id:"NG",weight:154000000},
{id:"RU",weight:140702000},
{id:"JP",weight:127288000},
{id:"MX",weight:112468855},
{id:"PH",weight:99900177},
{id:"VN",weight:89571130},
{id:"ET",weight:88013491},
{id:"DE",weight:81802257},
{id:"EG",weight:80471869},
{id:"TR",weight:77804122},
{id:"IR",weight:76923300},
{id:"CD",weight:70916439},
{id:"TH",weight:67089500},
{id:"FR",weight:64768389},
{id:"GB",weight:62348447},
{id:"IT",weight:60340328},
{id:"MM",weight:53414374},
{id:"ZA",weight:49000000},
{id:"KR",weight:48422644},
{id:"ES",weight:46505963},
{id:"UA",weight:45415596},
{id:"CO",weight:44205293},
{id:"TZ",weight:41892895},
{id:"AR",weight:41343201},
{id:"KE",weight:40046566},
{id:"PL",weight:38500000},
{id:"SD",weight:35000000},
{id:"DZ",weight:34586184},
{id:"CA",weight:33679000},
{id:"UG",weight:33398682},
{id:"MA",weight:31627428},
{id:"PE",weight:29907003},
{id:"IQ",weight:29671605},
{id:"AF",weight:29121286},
{id:"NP",weight:28951852},
{id:"MY",weight:28274729},
{id:"UZ",weight:27865738},
{id:"VE",weight:27223228},
{id:"SA",weight:25731776},
{id:"GH",weight:24339838},
{id:"YE",weight:23495361},
{id:"KP",weight:22912177},
{id:"TW",weight:22894384},
{id:"SY",weight:22198110},
{id:"MZ",weight:22061451},
{id:"RO",weight:21959278},
{id:"AU",weight:21515754},
{id:"LK",weight:21513990},
{id:"MG",weight:21281844},
{id:"CI",weight:21058798},
{id:"CM",weight:19294149},
{id:"CL",weight:16746491},
{id:"NL",weight:16645000},
{id:"BF",weight:16241811},
{id:"NE",weight:15878271},
{id:"MW",weight:15447500},
{id:"KZ",weight:15340000},
{id:"EC",weight:14790608},
{id:"KH",weight:14453680},
{id:"ML",weight:13796354},
{id:"GT",weight:13550440},
{id:"ZM",weight:13460305},
{id:"AO",weight:13068161},
{id:"SN",weight:12323252},
{id:"ZW",weight:11651858},
{id:"CU",weight:11423000},
{id:"RW",weight:11055976},
{id:"GR",weight:11000000},
{id:"CS",weight:10829175},
{id:"PT",weight:10676000},
{id:"TN",weight:10589025},
{id:"TD",weight:10543464},
{id:"CZ",weight:10476000},
{id:"BE",weight:10403000},
{id:"GN",weight:10324025},
{id:"SO",weight:10112453},
{id:"BO",weight:9947418},
{id:"HU",weight:9930000},
{id:"BI",weight:9863117},
{id:"DO",weight:9823821},
{id:"BY",weight:9685000},
{id:"HT",weight:9648924},
{id:"BJ",weight:9056010},
{id:"SE",weight:9045000},
{id:"AZ",weight:8303512},
{id:"SS",weight:8260490},
{id:"AT",weight:8205000},
{id:"HN",weight:7989415},
{id:"CH",weight:7581000},
{id:"TJ",weight:7487489},
{id:"IL",weight:7353985},
{id:"RS",weight:7344847},
{id:"BG",weight:7148785},
{id:"HK",weight:6898686},
{id:"TG",weight:6587239},
{id:"LY",weight:6461454},
{id:"JO",weight:6407085},
{id:"PY",weight:6375830},
{id:"LA",weight:6368162},
{id:"PG",weight:6064515},
{id:"SV",weight:6052064},
{id:"NI",weight:5995928},
{id:"ER",weight:5792984},
{id:"KG",weight:5508626},
{id:"DK",weight:5484000},
{id:"SK",weight:5455000},
{id:"SL",weight:5245695},
{id:"FI",weight:5244000},
{id:"NO",weight:5009150},
{id:"AE",weight:4975593},
{id:"TM",weight:4940916},
{id:"CF",weight:4844927},
{id:"SG",weight:4701069},
{id:"GE",weight:4630000},
{id:"IE",weight:4622917},
{id:"BA",weight:4590000},
{id:"CR",weight:4516220},
{id:"HR",weight:4491000},
{id:"MD",weight:4324000},
{id:"NZ",weight:4252277},
{id:"LB",weight:4125247},
{id:"PR",weight:3916632},
{id:"PS",weight:3800000},
{id:"LR",weight:3685076},
{id:"LT",weight:3565000},
{id:"UY",weight:3477000},
{id:"PA",weight:3410676},
{id:"MR",weight:3205060},
{id:"MN",weight:3086918},
{id:"CG",weight:3039126},
{id:"AL",weight:2986952},
{id:"AM",weight:2968000},
{id:"OM",weight:2967717},
{id:"JM",weight:2847232},
{id:"KW",weight:2789132},
{id:"LV",weight:2217969},
{id:"NA",weight:2128471},
{id:"MK",weight:2061000},
{id:"BW",weight:2029307},
{id:"SI",weight:2007000},
{id:"LS",weight:1919552},
{id:"XK",weight:1800000},
{id:"GM",weight:1593256},
{id:"GW",weight:1565126},
{id:"GA",weight:1545255},
{id:"SZ",weight:1354051},
{id:"MU",weight:1294104},
{id:"EE",weight:1291170},
{id:"TT",weight:1228691},
{id:"TL",weight:1154625},
{id:"CY",weight:1102677},
{id:"GQ",weight:1014999},
{id:"FJ",weight:875983},
{id:"QA",weight:840926},
{id:"RE",weight:776948},
{id:"KM",weight:773407},
{id:"GY",weight:748486},
{id:"DJ",weight:740528},
{id:"BH",weight:738004},
{id:"BT",weight:699847},
{id:"ME",weight:666730},
{id:"SB",weight:559198},
{id:"CV",weight:508659},
{id:"LU",weight:497538},
{id:"SR",weight:492829},
{id:"MO",weight:449198},
{id:"GP",weight:443000},
{id:"MQ",weight:432900},
{id:"MT",weight:403000},
{id:"MV",weight:395650},
{id:"BN",weight:395027},
{id:"BZ",weight:314522},
{id:"IS",weight:308910},
{id:"BS",weight:301790},
{id:"BB",weight:285653},
{id:"EH",weight:273008},
{id:"PF",weight:270485},
{id:"VU",weight:221552},
{id:"NC",weight:216494},
{id:"GF",weight:195506},
{id:"WS",weight:192001},
{id:"ST",weight:175808},
{id:"LC",weight:160922},
{id:"GU",weight:159358},
{id:"YT",weight:159042},
{id:"CW",weight:141766},
{id:"AN",weight:136197},
{id:"TO",weight:122580},
{id:"VI",weight:108708},
{id:"GD",weight:107818},
{id:"FM",weight:107708},
{id:"VC",weight:104217},
{id:"KI",weight:92533},
{id:"JE",weight:90812},
{id:"SC",weight:88340},
{id:"AG",weight:86754},
{id:"AD",weight:84000},
{id:"IM",weight:75049},
{id:"DM",weight:72813},
{id:"AW",weight:71566},
{id:"MH",weight:65859},
{id:"BM",weight:65365},
{id:"GG",weight:65228},
{id:"AS",weight:57881},
{id:"GL",weight:56375},
{id:"MP",weight:53883},
{id:"KN",weight:49898},
{id:"FO",weight:48228},
{id:"KY",weight:44270},
{id:"SX",weight:37429},
{id:"MF",weight:35925},
{id:"LI",weight:35000},
{id:"MC",weight:32965},
{id:"SM",weight:31477},
{id:"GI",weight:27884},
{id:"AX",weight:26711},
{id:"VG",weight:21730},
{id:"CK",weight:21388},
{id:"TC",weight:20556},
{id:"PW",weight:19907},
{id:"BQ",weight:18012},
{id:"WF",weight:16025},
{id:"AI",weight:13254},
{id:"TV",weight:10472},
{id:"NR",weight:10065},
{id:"MS",weight:9341},
{id:"BL",weight:8450},
{id:"SH",weight:7460},
{id:"PM",weight:7012},
{id:"IO",weight:4000},
{id:"FK",weight:2638},
{id:"SJ",weight:2550},
{id:"NU",weight:2166},
{id:"NF",weight:1828},
{id:"CX",weight:1500},
{id:"TK",weight:1466},
{id:"VA",weight:921},
{id:"CC",weight:628},
{id:"TF",weight:140},
{id:"PN",weight:46},
{id:"GS",weight:30}];
}
});
This source diff could not be displayed because it is too large. You can view the blob instead.
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