-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathmodule.js
More file actions
executable file
·308 lines (275 loc) · 11.2 KB
/
Copy pathmodule.js
File metadata and controls
executable file
·308 lines (275 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
## Map
### Parameters
* map :: 'world', 'world-antarctica', 'us','france_dept' or 'europe'
* colors :: an array of colors to use for the regions of the map. If this is a 2
element array, jquerymap will generate shades between these colors
* size :: How big to make the facet. Higher = more countries
* exclude :: Exlude the array of counties
* spyable :: Show the 'eye' icon that reveals the last Solr query
* index_limit :: This does nothing yet. Eventually will limit the query to the first
N indices
*/
define([
'angular',
'app',
'underscore',
'jquery',
'./lib/map.world.codes',
'./lib/jquery.jvectormap.min'
],
function (angular, app, _, $, worldmap) {
'use strict';
var module = angular.module('kibana.panels.map', []);
app.useModule(module);
module.controller('map', function($scope, $rootScope, querySrv, dashboard, filterSrv) {
$scope.panelMeta = {
editorTabs : [
{title:'Queries', src:'app/partials/querySelect.html'}
],
modals : [
{
description: "Inspect",
icon: "icon-info-sign",
partial: "app/partials/inspector.html",
show: $scope.panel.spyable
}
],
status : "Stable",
description : "Displays a map of shaded regions using a field containing a 2 letter country code or US state code. Regions with more hits are shaded darker. It uses Solr faceting, so it is important that you set field values to the appropriate 2-letter codes at index time. Recent additions provide the ability to compute mean/max/min/sum of a numeric field by country or state."
};
// Set and populate defaults
var _d = {
queries : {
mode : 'all',
ids : [],
query : '*:*',
custom : ''
},
mode : 'count', // mode to tell which number will be used to plot the chart.
field : '', // field to be used for rendering the map.
stats_field : '',
decimal_points : 0, // The number of digits after the decimal point
map : "world",
useNames : false,
colors : ['#A0E2E2', '#265656'],
size : 100,
exclude : [],
spyable : true,
index_limit : 0,
show_queries:true,
};
_.defaults($scope.panel,_d);
$scope.init = function() {
// $scope.testMultivalued();
$scope.$on('refresh',function(){$scope.get_data();});
$scope.get_data();
};
$scope.testMultivalued = function() {
if($scope.panel.field && $scope.fields.typeList[$scope.panel.field].schema.indexOf("M") > -1) {
$scope.panel.error = "Can't proceed with Multivalued field";
return;
}
if($scope.panel.stats_field && $scope.fields.typeList[$scope.panel.stats_field].schema.indexOf("M") > -1) {
$scope.panel.error = "Can't proceed with Multivalued field";
return;
}
};
$scope.set_refresh = function (state) {
$scope.refresh = state;
// if 'count' mode is selected, set decimal_points to zero automatically.
if ($scope.panel.mode === 'count') {
$scope.panel.decimal_points = 0;
}
};
$scope.close_edit = function() {
if ($scope.refresh) {
// $scope.testMultivalued();
$scope.get_data();
}
$scope.refresh = false;
};
$scope.get_data = function() {
// Make sure we have everything for the request to complete
if(dashboard.indices.length === 0) {
return;
}
$scope.panelMeta.loading = true;
delete $scope.panel.error;
// Solr
$scope.sjs.client.server(dashboard.current.solr.server + dashboard.current.solr.core_name);
var request;
request = $scope.sjs.Request().indices(dashboard.indices);
$scope.panel.queries.ids = querySrv.idsByMode($scope.panel.queries);
// This could probably be changed to a BoolFilter
var boolQuery = $scope.ejs.BoolQuery();
_.each($scope.panel.queries.ids,function(id) {
boolQuery = boolQuery.should(querySrv.getEjsObj(id));
});
// Then the insert into facet and make the request
request = request
.facet($scope.ejs.TermsFacet('map')
.field($scope.panel.field)
.size($scope.panel.size)
.exclude($scope.panel.exclude)
.facetFilter($scope.ejs.QueryFilter(
$scope.ejs.FilteredQuery(
boolQuery,
filterSrv.getBoolFilter(filterSrv.ids)
)))).size(0);
$scope.populate_modal(request);
// Build Solr query
var fq = '';
if (filterSrv.getSolrFq()) {
fq = '&' + filterSrv.getSolrFq();
}
var wt_json = '&wt=json';
var rows_limit = '&rows=0'; // for map module, we don't display results from row, but we use facets.
var facet = '';
if ($scope.panel.mode === 'count') {
facet = '&facet=true&facet.field=' + $scope.panel.field + '&facet.limit=' + $scope.panel.size;
} else {
// if mode != 'count' then we need to use stats query
facet = '&stats=true&stats.facet=' + $scope.panel.field + '&stats.field=' + $scope.panel.stats_field;
}
// Set the panel's query
$scope.panel.queries.query = querySrv.getORquery() + wt_json + fq + rows_limit + facet;
// Set the additional custom query
if ($scope.panel.queries.custom != null) {
request = request.setQuery($scope.panel.queries.query + $scope.panel.queries.custom);
} else {
request = request.setQuery($scope.panel.queries.query);
}
var results = request.doSearch();
// Populate scope when we have results
results.then(function(results) {
$scope.panelMeta.loading = false;
// Check for error and abort if found
if(!(_.isUndefined(results.error))) {
$scope.panel.error = $scope.parse_error(results.error.msg);
return;
}
$scope.data = {}; // empty the data for new results
var terms = [];
if (results.response.numFound) {
$scope.hits = results.response.numFound;
} else {
// Undefined numFound or zero, clear the map.
$scope.$emit('render');
return false;
}
if ($scope.panel.mode === 'count') {
terms = results.facet_counts.facet_fields[$scope.panel.field];
} else { // stats mode
_.each(results.stats.stats_fields[$scope.panel.stats_field].facets[$scope.panel.field], function(stats_obj,facet_field) {
terms.push(facet_field, stats_obj[$scope.panel.mode]);
});
}
if ($scope.hits > 0) {
for (var i=0; i < terms.length; i += 2) {
// Skip states with zero count to make them greyed out in the map.
if (terms[i+1] > 0) {
// if $scope.data[terms] is undefined, assign the value to it
// otherwise, we will add the value. This case can happen when
// the data contains both uppercase and lowercase state letters with
// duplicate states (e.g. CA and ca). By adding the value, the map will
// show correct counts for states with mixed-case letters.
if(($scope.panel.map === 'world' || $scope.panel.map === 'world-antarctica') && $scope.panel.useNames) {
if(worldmap.countryCodes[terms[i]]) {
if (!$scope.data[worldmap.countryCodes[terms[i]]]) {
$scope.data[worldmap.countryCodes[terms[i]]] = terms[i+1];
} else {
$scope.data[worldmap.countryCodes[terms[i]]] += terms[i+1];
}
}
}
else {
if (!$scope.data[terms[i].toUpperCase()]) {
$scope.data[terms[i].toUpperCase()] = terms[i+1];
} else {
$scope.data[terms[i].toUpperCase()] += terms[i+1];
}
}
}
}
}
$scope.$emit('render');
});
};
// I really don't like this function, too much dom manip. Break out into directive?
$scope.populate_modal = function(request) {
$scope.inspector = angular.toJson(JSON.parse(request.toString()),true);
};
$scope.build_search = function(field,value) {
// Set querystring to both uppercase and lowercase state values with double-quote around the value
// to prevent query error from state=OR (Oregon).
// When using Country Name option, the country name is supposed to be in capitalized format. But we
// will also add queries for searching both uppercase and lowercase (e.g. Thailand OR THAILAND OR thailand).
if (!$scope.panel.useNames) {
filterSrv.set({type:'querystring',mandate:'must',query:field+':"'+value.toUpperCase()+
'" OR '+field+':"'+value.toLowerCase()+'"'});
} else {
filterSrv.set({type:'querystring',mandate:'must',query:field+':"'+value.toUpperCase()+
'" OR '+field+':"'+value.toLowerCase()+'" OR '+field+':"'+value+'"'});
}
dashboard.refresh();
};
});
module.directive('map', function() {
return {
restrict: 'A',
link: function(scope, elem) {
elem.html('<center><img src="img/load_big.gif"></center>');
// Receive render events
scope.$on('render',function(){
render_panel();
});
function render_panel() {
elem.text('');
$('.jvectormap-zoomin,.jvectormap-zoomout,.jvectormap-label').remove();
require(['./panels/map/lib/map.'+scope.panel.map], function () {
elem.vectorMap({
map: scope.panel.map,
regionStyle: {initial: {fill: '#8c8c8c'}},
zoomOnScroll: false,
backgroundColor: null,
series: {
regions: [{
values: scope.data,
scale: scope.panel.colors,
normalizeFunction: 'polynomial'
}]
},
onRegionLabelShow: function(event, label, code){
elem.children('.map-legend').show();
var count = _.isUndefined(scope.data[code]) ? 0 : scope.data[code];
// if (scope.panel.mode === 'count') {
// count = count.toFixed(0);
// } else {
// count = count.toFixed(scope.panel.decimal_points);
// }
elem.children('.map-legend').text(label.text() + ": " + count.toFixed(scope.panel.decimal_points));
},
onRegionOut: function() {
$('.map-legend').hide();
},
onRegionClick: function(event, code) {
var count = _.isUndefined(scope.data[code]) ? 0 : scope.data[code];
if (count !== 0) {
if (!scope.panel.useNames) {
scope.build_search(scope.panel.field, code);
} else {
var countryNames = _.invert(worldmap.countryCodes);
scope.build_search(scope.panel.field, countryNames[code]);
}
}
}
});
elem.prepend('<span class="map-legend"></span>');
$('.map-legend').hide();
});
}
}
};
});
});