module.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. labjs = labjs.script("common/lib/panels/jquery.jvectormap.min.js");
  2. angular.module('kibana.map', [])
  3. .directive('map', function() {
  4. return {
  5. restrict: 'A',
  6. link: function(scope, elem, attrs) {
  7. // Specify defaults for ALL directives
  8. var _d = {
  9. queries : ["*"],
  10. map : "world",
  11. interval: secondsToHms(calculate_interval(scope.from,scope.to,40,0)/1000),
  12. colors : ["#BF3030","#1D7373","#86B32D","#A98A21","#411F73"],
  13. show : ['bars'],
  14. size : 100,
  15. exclude : []
  16. }
  17. // Set ready flag and fill parameters (REQUIRED IN EVERY PANEL)
  18. scope.$watch(function () {
  19. return (attrs.params && scope.index) ? true : false;
  20. }, function (ready) {
  21. scope.ready = ready;
  22. if(ready) {
  23. scope.params = JSON.parse(attrs.params);
  24. _.each(_d, function(v, k) {
  25. scope.params[k] = _.isUndefined(scope.params[k])
  26. ? _d[k] : scope.params[k];
  27. });
  28. }
  29. });
  30. // Also get the data if time frame changes.
  31. // (REQUIRED IN EVERY PANEL)
  32. scope.$watch(function() {
  33. return angular.toJson([scope.from, scope.to, scope.ready])
  34. }, function(){
  35. if(scope.ready)
  36. get_data(scope,elem,attrs);
  37. });
  38. // Re-rending panel if data changes
  39. scope.$watch('data', function() {
  40. if(scope.ready)
  41. render_panel(scope,elem,attrs);
  42. });
  43. // Or if the window is resized
  44. angular.element(window).bind('resize', function(){
  45. render_panel(scope,elem,attrs);
  46. });
  47. // Function for getting data
  48. function get_data(scope,elem,attrs) {
  49. var params = scope.params;
  50. var ejs = scope.ejs;
  51. var request = ejs.Request().indices(scope.index);
  52. // Build the question part of the query
  53. var query = ejs.FilteredQuery(
  54. ejs.QueryStringQuery(params.query || '*'),
  55. ejs.RangeFilter(config.timefield)
  56. .from(scope.from)
  57. .to(scope.to)
  58. .cache(false)
  59. );
  60. // Then the insert into facet and make the request
  61. var results = request
  62. .facet(ejs.TermsFacet('map')
  63. .field(params.field)
  64. .size(params['size'])
  65. .exclude(params.exclude)
  66. .facetFilter(ejs.QueryFilter(query))
  67. )
  68. .doSearch();
  69. // Populate scope when we have results
  70. results.then(function(results) {
  71. scope.hits = results.hits.total;
  72. scope.data = {};
  73. _.each(results.facets.map.terms, function(v) {
  74. scope.data[v.term.toUpperCase()] = v.count;
  75. });
  76. });
  77. }
  78. // Function for rendering panel
  79. function render_panel(scope,elem,attrs) {
  80. // Parse our params object
  81. var params = scope.params;
  82. elem.text('');
  83. $('.jvectormap-label,.jvectormap-zoomin,.jvectormap-zoomout').remove();
  84. elem.append("<div class='jvectormap-label'>Loading Map</div>");
  85. loadmap = $LAB.script("common/lib/panels/map."+params.map+".js")
  86. // Populate element. Note that jvectormap appends, does not replace.
  87. loadmap.wait(function(){
  88. elem.vectorMap({
  89. map: params.map,
  90. regionStyle: {initial: {fill: '#ddd'}},
  91. zoomOnScroll: false,
  92. backgroundColor: '#fff',
  93. series: {
  94. regions: [{
  95. values: scope.data,
  96. scale: ['#C8EEFF', '#0071A4'],
  97. normalizeFunction: 'polynomial'
  98. }]
  99. }
  100. });
  101. })
  102. //elem.show();
  103. }
  104. }
  105. };
  106. });