1515from elasticsearch_dsl import Document
1616
1717from .config import config
18- from .elastic import get_search_base , build_dsl_filter
18+ from .elastic import get_field_type , get_search_base , build_dsl_filter
1919from .logger import logger
2020from .timeutil import quantize_time_range , convert_kibana_time
2121
@@ -50,6 +50,8 @@ def create_default_params() -> Dict[str, Any]:
5050 "track_connection" : None ,
5151 "use_centroid" : False ,
5252 "user" : None ,
53+ "bucket_min" :0 ,
54+ "bucket_max" :1
5355 }
5456
5557
@@ -289,6 +291,8 @@ def extract_parameters(headers: Dict[Any, Any], query_params: Dict[Any, Any]) ->
289291 params ["geopoint_field" ] = query_params .get ("geopoint_field" , params ["geopoint_field" ])
290292 params ["timestamp_field" ] = query_params .get ("timestamp_field" , params ["timestamp_field" ])
291293 params .update (get_time_bounds (now , from_time , to_time ))
294+ params ["bucket_min" ] = float (query_params .get ("bucket_min" ,0 ))
295+ params ["bucket_max" ] = float (query_params .get ("bucket_max" ,1 ))
292296 params ["debug" ] = (query_params .get ("debug" , False ) == 'true' )
293297
294298 if params ["geopoint_field" ] is None :
@@ -305,8 +309,9 @@ def generate_global_params(headers, params, idx):
305309 category_field = params ["category_field" ]
306310 category_type = params ["category_type" ]
307311 category_histogram = params ["category_histogram" ]
312+ current_zoom = params ["mapZoom" ]
308313 span_range = params ["span_range" ]
309-
314+ resolution = params [ "resolution" ]
310315 histogram_range = 0
311316 histogram_interval = None
312317 histogram_cnt = None
@@ -316,7 +321,7 @@ def generate_global_params(headers, params, idx):
316321
317322 # Create base search
318323 base_s = get_search_base (config .elastic_hosts , headers , params , idx )
319-
324+ base_s = base_s [ 0 : 0 ]
320325 # west, south, east, north
321326 global_bounds = [- 180 , - 90 , 180 , 90 ]
322327 global_doc_cnt = 0
@@ -337,8 +342,10 @@ def generate_global_params(headers, params, idx):
337342 if category_type == "number" :
338343 bounds_s .aggs .metric ("field_stats" , "stats" , field = category_field )
339344
345+ field_type = get_field_type (config .elastic_hosts , headers , params ,geopoint_field , idx )
340346 # Execute and process search
341- if len (list (bounds_s .aggs )) > 0 :
347+ if len (list (bounds_s .aggs )) > 0 and field_type != "geo_shape" :
348+ logger .info (bounds_s .to_dict ())
342349 bounds_resp = bounds_s .execute ()
343350 assert len (bounds_resp .hits ) == 0
344351
@@ -403,6 +410,45 @@ def generate_global_params(headers, params, idx):
403410 )
404411 else :
405412 histogram_range = 0
413+ elif field_type == "geo_shape" :
414+ zoom = 0
415+ if resolution == "coarse" :
416+ zoom = 5
417+ elif resolution == "fine" :
418+ zoom = 6
419+ elif resolution == "finest" :
420+ zoom = 7
421+ geotile_precision = current_zoom + zoom
422+ max_value_s = copy .copy (base_s )
423+ bucket = max_value_s .aggs .bucket ("comp" , "geotile_grid" , field = geopoint_field ,precision = 0 ,size = 1 )
424+ resp = max_value_s .execute ()
425+ global_doc_cnt = resp .aggregations .comp .buckets [0 ].doc_count
426+ if global_doc_cnt > 100000 :
427+ histogram_cnt = 200
428+ else :
429+ histogram_cnt = 500
430+
431+ if category_field :
432+ max_value_s = copy .copy (base_s )
433+ bucket = max_value_s .aggs .bucket ("comp" , "geotile_grid" , field = geopoint_field ,precision = geotile_precision ,size = 1 )
434+ bucket .metric ("sum" ,"sum" ,field = category_field ,missing = 0 )
435+ resp = max_value_s .execute ()
436+ estimated_points_per_tile = resp .aggregations .comp .buckets [0 ].sum ['value' ]
437+ histogram_range = estimated_points_per_tile
438+ else :
439+ max_value_s = copy .copy (base_s )
440+ max_value_s .aggs .bucket ("comp" , "geotile_grid" , field = geopoint_field ,precision = geotile_precision ,size = 1 )
441+ resp = max_value_s .execute ()
442+ estimated_points_per_tile = resp .aggregations .comp .buckets [0 ].doc_count
443+ histogram_range = estimated_points_per_tile
444+ if histogram_range > 0 :
445+ # round to the nearest larger power of 10
446+ histogram_range = math .pow (
447+ 10 , math .ceil (math .log10 (histogram_range ))
448+ )
449+ histogram_interval = histogram_range / histogram_cnt
450+ field_min = 0
451+ field_max = estimated_points_per_tile
406452 else :
407453 logger .debug ("Skipping global query" )
408454
0 commit comments