|
4 | 4 | import logging |
5 | 5 | import os |
6 | 6 | from base64 import urlsafe_b64decode, urlsafe_b64encode |
| 7 | +from copy import deepcopy |
7 | 8 | from typing import Any, Dict, Iterable, List, Optional, Protocol, Tuple, Type, Union |
8 | 9 |
|
9 | 10 | import attr |
@@ -316,6 +317,77 @@ class DatabaseLogic: |
316 | 317 |
|
317 | 318 | extensions: List[str] = attr.ib(default=attr.Factory(list)) |
318 | 319 |
|
| 320 | + aggregation_mapping: Dict[str, Dict[str, Any]] = { |
| 321 | + "total_count": {"value_count": {"field": "id"}}, |
| 322 | + "collection_frequency": {"terms": {"field": "collection", "size": 100}}, |
| 323 | + "platform_frequency": {"terms": {"field": "properties.platform", "size": 100}}, |
| 324 | + "cloud_cover_frequency": { |
| 325 | + "range": { |
| 326 | + "field": "properties.eo:cloud_cover", |
| 327 | + "ranges": [ |
| 328 | + {"to": 5}, |
| 329 | + {"from": 5, "to": 15}, |
| 330 | + {"from": 15, "to": 40}, |
| 331 | + {"from": 40}, |
| 332 | + ], |
| 333 | + } |
| 334 | + }, |
| 335 | + "datetime_frequency": { |
| 336 | + "date_histogram": { |
| 337 | + "field": "properties.datetime", |
| 338 | + "calendar_interval": "month", |
| 339 | + } |
| 340 | + }, |
| 341 | + "datetime_min": {"min": {"field": "properties.datetime"}}, |
| 342 | + "datetime_max": {"max": {"field": "properties.datetime"}}, |
| 343 | + "grid_code_frequency": { |
| 344 | + "terms": { |
| 345 | + "field": "properties.grid:code", |
| 346 | + "missing": "none", |
| 347 | + "size": 10000, |
| 348 | + } |
| 349 | + }, |
| 350 | + "sun_elevation_frequency": { |
| 351 | + "histogram": {"field": "properties.view:sun_elevation", "interval": 5} |
| 352 | + }, |
| 353 | + "sun_azimuth_frequency": { |
| 354 | + "histogram": {"field": "properties.view:sun_azimuth", "interval": 5} |
| 355 | + }, |
| 356 | + "off_nadir_frequency": { |
| 357 | + "histogram": {"field": "properties.view:off_nadir", "interval": 5} |
| 358 | + }, |
| 359 | + "centroid_geohash_grid_frequency": { |
| 360 | + "geohash_grid": { |
| 361 | + "field": "properties.proj:centroid", |
| 362 | + "precision": 1, |
| 363 | + } |
| 364 | + }, |
| 365 | + "centroid_geohex_grid_frequency": { |
| 366 | + "geohex_grid": { |
| 367 | + "field": "properties.proj:centroid", |
| 368 | + "precision": 0, |
| 369 | + } |
| 370 | + }, |
| 371 | + "centroid_geotile_grid_frequency": { |
| 372 | + "geotile_grid": { |
| 373 | + "field": "properties.proj:centroid", |
| 374 | + "precision": 0, |
| 375 | + } |
| 376 | + }, |
| 377 | + "geometry_geohash_grid_frequency": { |
| 378 | + "geohash_grid": { |
| 379 | + "field": "geometry", |
| 380 | + "precision": 1, |
| 381 | + } |
| 382 | + }, |
| 383 | + "geometry_geotile_grid_frequency": { |
| 384 | + "geotile_grid": { |
| 385 | + "field": "geometry", |
| 386 | + "precision": 0, |
| 387 | + } |
| 388 | + }, |
| 389 | + } |
| 390 | + |
319 | 391 | """CORE LOGIC""" |
320 | 392 |
|
321 | 393 | async def get_all_collections( |
@@ -657,104 +729,41 @@ async def aggregate( |
657 | 729 | centroid_geotile_grid_precision: int, |
658 | 730 | geometry_geohash_grid_precision: int, |
659 | 731 | geometry_geotile_grid_precision: int, |
| 732 | + datetime_frequency_interval: str, |
660 | 733 | ignore_unavailable: Optional[bool] = True, |
661 | 734 | ): |
662 | 735 | """Return aggregations of STAC Items.""" |
663 | | - agg_2_es = { |
664 | | - "total_count": {"value_count": {"field": "id"}}, |
665 | | - "collection_frequency": {"terms": {"field": "collection", "size": 100}}, |
666 | | - "platform_frequency": { |
667 | | - "terms": {"field": "properties.platform", "size": 100} |
668 | | - }, |
669 | | - "cloud_cover_frequency": { |
670 | | - "range": { |
671 | | - "field": "properties.eo:cloud_cover", |
672 | | - "ranges": [ |
673 | | - {"to": 5}, |
674 | | - {"from": 5, "to": 15}, |
675 | | - {"from": 15, "to": 40}, |
676 | | - {"from": 40}, |
677 | | - ], |
678 | | - } |
679 | | - }, |
680 | | - "datetime_frequency": { |
681 | | - "date_histogram": { |
682 | | - "field": "properties.datetime", |
683 | | - "calendar_interval": "month", |
684 | | - } |
685 | | - }, |
686 | | - "datetime_min": {"min": {"field": "properties.datetime"}}, |
687 | | - "datetime_max": {"max": {"field": "properties.datetime"}}, |
688 | | - "grid_code_frequency": { |
689 | | - "terms": { |
690 | | - "field": "properties.grid:code", |
691 | | - "missing": "none", |
692 | | - "size": 10000, |
693 | | - } |
694 | | - }, |
695 | | - "sun_elevation_frequency": { |
696 | | - "histogram": {"field": "properties.view:sun_elevation", "interval": 5} |
697 | | - }, |
698 | | - "sun_azimuth_frequency": { |
699 | | - "histogram": {"field": "properties.view:sun_azimuth", "interval": 5} |
700 | | - }, |
701 | | - "off_nadir_frequency": { |
702 | | - "histogram": {"field": "properties.view:off_nadir", "interval": 5} |
703 | | - }, |
704 | | - } |
705 | | - |
706 | 736 | search_body: Dict[str, Any] = {} |
707 | 737 | query = search.query.to_dict() if search.query else None |
708 | 738 | if query: |
709 | 739 | search_body["query"] = query |
710 | 740 |
|
711 | 741 | logger.debug("Aggregations: %s", aggregations) |
712 | 742 |
|
713 | | - # include all aggregations specified |
714 | | - # this will ignore aggregations with the wrong names |
715 | | - search_body["aggregations"] = { |
716 | | - k: v for k, v in agg_2_es.items() if k in aggregations |
717 | | - } |
718 | | - |
719 | | - if "centroid_geohash_grid_frequency" in aggregations: |
720 | | - search_body["aggregations"]["centroid_geohash_grid_frequency"] = { |
721 | | - "geohash_grid": { |
722 | | - "field": "properties.proj:centroid", |
723 | | - "precision": centroid_geohash_grid_precision, |
724 | | - } |
725 | | - } |
726 | | - |
727 | | - if "centroid_geohex_grid_frequency" in aggregations: |
728 | | - search_body["aggregations"]["centroid_geohex_grid_frequency"] = { |
729 | | - "geohex_grid": { |
730 | | - "field": "properties.proj:centroid", |
731 | | - "precision": centroid_geohex_grid_precision, |
732 | | - } |
| 743 | + def _fill_aggregation_parameters(name: str, agg: dict) -> dict: |
| 744 | + [key] = agg.keys() |
| 745 | + agg_precision = { |
| 746 | + "centroid_geohash_grid_frequency": centroid_geohash_grid_precision, |
| 747 | + "centroid_geohex_grid_frequency": centroid_geohex_grid_precision, |
| 748 | + "centroid_geotile_grid_frequency": centroid_geotile_grid_precision, |
| 749 | + "geometry_geohash_grid_frequency": geometry_geohash_grid_precision, |
| 750 | + "geometry_geotile_grid_frequency": geometry_geotile_grid_precision, |
733 | 751 | } |
| 752 | + if name in agg_precision: |
| 753 | + agg[key]["precision"] = agg_precision[name] |
734 | 754 |
|
735 | | - if "centroid_geotile_grid_frequency" in aggregations: |
736 | | - search_body["aggregations"]["centroid_geotile_grid_frequency"] = { |
737 | | - "geotile_grid": { |
738 | | - "field": "properties.proj:centroid", |
739 | | - "precision": centroid_geotile_grid_precision, |
740 | | - } |
741 | | - } |
| 755 | + if key == "date_histogram": |
| 756 | + agg[key]["calendar_interval"] = datetime_frequency_interval |
742 | 757 |
|
743 | | - if "geometry_geohash_grid_frequency" in aggregations: |
744 | | - search_body["aggregations"]["geometry_geohash_grid_frequency"] = { |
745 | | - "geohash_grid": { |
746 | | - "field": "geometry", |
747 | | - "precision": geometry_geohash_grid_precision, |
748 | | - } |
749 | | - } |
| 758 | + return agg |
750 | 759 |
|
751 | | - if "geometry_geotile_grid_frequency" in aggregations: |
752 | | - search_body["aggregations"]["geometry_geotile_grid_frequency"] = { |
753 | | - "geotile_grid": { |
754 | | - "field": "geometry", |
755 | | - "precision": geometry_geotile_grid_precision, |
756 | | - } |
757 | | - } |
| 760 | + # include all aggregations specified |
| 761 | + # this will ignore aggregations with the wrong names |
| 762 | + search_body["aggregations"] = { |
| 763 | + k: _fill_aggregation_parameters(k, deepcopy(v)) |
| 764 | + for k, v in self.aggregation_mapping.items() |
| 765 | + if k in aggregations |
| 766 | + } |
758 | 767 |
|
759 | 768 | index_param = indices(collection_ids) |
760 | 769 | search_task = asyncio.create_task( |
|
0 commit comments