From cc1e078da9f93db9ff79959ac69e6c803a226902 Mon Sep 17 00:00:00 2001 From: abram axel booth Date: Wed, 2 Oct 2024 09:59:53 -0400 Subject: [PATCH] fix: handle (and test) reporting with no indexes --- osf/metrics/reporters/public_item_usage.py | 5 ++++- .../metrics/reporters/test_public_item_usage_reporter.py | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/osf/metrics/reporters/public_item_usage.py b/osf/metrics/reporters/public_item_usage.py index 4a202e2880b..740d7308f88 100644 --- a/osf/metrics/reporters/public_item_usage.py +++ b/osf/metrics/reporters/public_item_usage.py @@ -244,7 +244,10 @@ def _iter_composite_buckets(search: edsl.Search, composite_agg_name: str): ''' while True: _page_response = search.execute(ignore_cache=True) - _agg_result = _page_response.aggregations[composite_agg_name] + try: + _agg_result = _page_response.aggregations[composite_agg_name] + except KeyError: + return # no data; all done yield from _agg_result.buckets # update the search for the next page try: diff --git a/osf_tests/metrics/reporters/test_public_item_usage_reporter.py b/osf_tests/metrics/reporters/test_public_item_usage_reporter.py index d7869970753..58a5e1cb5e7 100644 --- a/osf_tests/metrics/reporters/test_public_item_usage_reporter.py +++ b/osf_tests/metrics/reporters/test_public_item_usage_reporter.py @@ -152,6 +152,11 @@ def busy_month_item2(self, ym_busy): action_labels=['download'], ) + def test_no_data(self, ym_empty): + _reporter = PublicItemUsageReporter() + _empty = list(_reporter.report(ym_empty)) + assert _empty == [] + def test_reporter(self, ym_empty, ym_sparse, ym_busy, sparse_month_usage, busy_month_item0, busy_month_item1, busy_month_item2): _reporter = PublicItemUsageReporter() _empty = list(_reporter.report(ym_empty))