|
1 | 1 | from dataclasses import asdict, dataclass
|
2 | 2 | from functools import lru_cache
|
3 | 3 | from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
|
4 |
| -from datetime import datetime, timezone |
5 | 4 | import copy
|
6 | 5 | import math
|
7 | 6 | import time
|
@@ -1175,19 +1174,25 @@ def generate_tile(idx, x, y, z, headers, params, tile_width_px=256, tile_height_
|
1175 | 1174 | spread = 1
|
1176 | 1175 | geotile_precision = current_zoom+zoom
|
1177 | 1176 | searches = []
|
1178 |
| - if category_field: |
1179 |
| - max_value_s = copy.copy(base_s) |
1180 |
| - bucket = max_value_s.aggs.bucket("comp", "geotile_grid", field=geopoint_field,precision=geotile_precision,size=1) |
1181 |
| - bucket.metric("sum","sum",field=category_field,missing=0) |
1182 |
| - resp = max_value_s.execute() |
1183 |
| - estimated_points_per_tile = resp.aggregations.comp.buckets[0].sum['value'] |
| 1177 | + |
| 1178 | + if params.get("generated_params", {}).get('complete',False): |
| 1179 | + estimated_points_per_tile = params["generated_params"]['global_doc_cnt'] |
1184 | 1180 | span = [0,estimated_points_per_tile]
|
| 1181 | + logger.info("USING GENERATED PARAMS") |
1185 | 1182 | else:
|
1186 |
| - max_value_s = copy.copy(base_s) |
1187 |
| - max_value_s.aggs.bucket("comp", "geotile_grid", field=geopoint_field,precision=geotile_precision,size=1) |
1188 |
| - resp = max_value_s.execute() |
1189 |
| - estimated_points_per_tile = resp.aggregations.comp.buckets[0].doc_count |
1190 |
| - span = [0,estimated_points_per_tile] |
| 1183 | + if category_field: |
| 1184 | + max_value_s = copy.copy(base_s) |
| 1185 | + bucket = max_value_s.aggs.bucket("comp", "geotile_grid", field=geopoint_field,precision=geotile_precision,size=1) |
| 1186 | + bucket.metric("sum","sum",field=category_field,missing=0) |
| 1187 | + resp = max_value_s.execute() |
| 1188 | + estimated_points_per_tile = resp.aggregations.comp.buckets[0].sum['value'] |
| 1189 | + span = [0,estimated_points_per_tile] |
| 1190 | + else: |
| 1191 | + max_value_s = copy.copy(base_s) |
| 1192 | + max_value_s.aggs.bucket("comp", "geotile_grid", field=geopoint_field,precision=geotile_precision,size=1) |
| 1193 | + resp = max_value_s.execute() |
| 1194 | + estimated_points_per_tile = resp.aggregations.comp.buckets[0].doc_count |
| 1195 | + span = [0,estimated_points_per_tile] |
1191 | 1196 | logger.info("EST Points: %s %s",estimated_points_per_tile,category_field)
|
1192 | 1197 |
|
1193 | 1198 | searches = []
|
@@ -1384,41 +1389,23 @@ def create_time_interval_searches(base_s,subtile_bb_dict,start_time,stop_time,ti
|
1384 | 1389 | stime = start_time
|
1385 | 1390 | searches = []
|
1386 | 1391 | if interval == "auto":
|
1387 |
| - subtile_s = copy.copy(base_s) |
1388 |
| - subtile_s = subtile_s[0:0] |
1389 |
| - subtile_s = subtile_s.filter("geo_bounding_box", **{geopoint_field: subtile_bb_dict}) |
1390 |
| - subtile_s.aggs.bucket("by_time", "auto_date_histogram", field="lastupdated",buckets=546) |
1391 |
| - resp = subtile_s.execute() |
1392 |
| - interval = resp.aggregations.by_time.interval |
1393 |
| - #create a search for each bucket using the bucket time plus the interval |
1394 |
| - logger.info("Doing multiple queries based on interval %s",interval) |
1395 |
| - |
1396 |
| - for bucket in resp.aggregations.by_time: |
1397 |
| - subtile_s = copy.copy(base_s) |
1398 |
| - bucket_start_time = datetime.strptime(bucket.key_as_string,"%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc) |
1399 |
| - bucket_stop_time = bucket_start_time+ parse_duration_interval(interval) |
1400 |
| - |
1401 |
| - if timestamp_field: |
1402 |
| - time_range = {timestamp_field: {}} |
1403 |
| - if bucket_start_time is not None: |
1404 |
| - time_range[timestamp_field]["gte"] = bucket_start_time |
1405 |
| - if stop_time is not None: |
1406 |
| - time_range[timestamp_field]["lte"] = bucket_stop_time |
1407 |
| - |
1408 |
| - if time_range and time_range[timestamp_field]: |
1409 |
| - subtile_s = subtile_s.filter("range", **time_range) |
1410 |
| - bucket = subtile_s.aggs.bucket("comp", "geotile_grid", field=geopoint_field,precision=geotile_precision,size=composite_agg_size,bounds=subtile_bb_dict) |
1411 |
| - if category_field: |
1412 |
| - bucket.metric("sum","sum",field=category_field,missing=0) |
1413 |
| - searches.append(subtile_s) |
1414 |
| - return searches |
1415 |
| - |
| 1392 | + delta = stop_time - start_time |
| 1393 | + minutes = delta.total_seconds() /60 |
| 1394 | + step = 1 #step through all the minutes in an hour to find a fit |
| 1395 | + while minutes/step > 546: |
| 1396 | + step = step +1 |
| 1397 | + interval = str(step)+"m" |
| 1398 | + |
| 1399 | + logger.info("Actual time bucket %s",interval) |
1416 | 1400 | while stime < stop_time:
|
1417 | 1401 | subtile_s = copy.copy(base_s)
|
1418 | 1402 | subtile_s = subtile_s.filter("geo_bounding_box", **{geopoint_field: subtile_bb_dict})
|
1419 | 1403 | subtile_s = subtile_s[0:0]
|
1420 | 1404 | bucket_start_time = stime
|
1421 |
| - bucket_stop_time = bucket_start_time+ parse_duration_interval(interval) |
| 1405 | + bucket_duration = parse_duration_interval(interval) |
| 1406 | + #logger.info(bucket_duration) |
| 1407 | + bucket_stop_time = bucket_start_time+ bucket_duration |
| 1408 | + bucket_stop_time = min(bucket_stop_time, stop_time) |
1422 | 1409 | time_range = {timestamp_field: {}}
|
1423 | 1410 | time_range[timestamp_field]["gte"] = bucket_start_time
|
1424 | 1411 | time_range[timestamp_field]["lte"] = bucket_stop_time
|
|
0 commit comments