Repository: AlexsLemonade/refinebio

Issue:
Weekly stats shows 0 downloads for some users
### Context

A lot of changes has gone into prod recently. One of them is the EngagementBot weekly stats updates.

### Problem or idea

The most recent summary contains lines like "0 downloads from " indicating some (potential) stats inaccuracy.

### Solution or next step

Figure out why the value is empty and fix the issue. If everything is right then just hide those 0 downloads items.



Discussion:

Resolving patch:
diff --git a/api/data_refinery_api/management/commands/post_downloads_summary.py b/api/data_refinery_api/management/commands/post_downloads_summary.py
--- a/api/data_refinery_api/management/commands/post_downloads_summary.py
+++ b/api/data_refinery_api/management/commands/post_downloads_summary.py
@@ -66,7 +66,6 @@ def get_user_location(ip_address):
 
 def post_downloads_summary(days, channel, top_countries=5):
     """Posts downloads summary to Slack channel."""
-
     start_time = timezone.now() - datetime.timedelta(days=days)
     datasets = Dataset.processed_filtered_objects.filter(
         created_at__gt=start_time
@@ -75,7 +74,7 @@ def post_downloads_summary(days, channel, top_countries=5):
     users_emails = set(dataset.email_address for dataset in datasets)
 
     locations = set()
-    locations_cache = dict()
+    locations_cache = {}
     for annotation in annotations:
         if "location" not in annotation.data:
             ip_address = annotation.data["ip"]
@@ -94,8 +93,10 @@ def post_downloads_summary(days, channel, top_countries=5):
     for user_email in users_emails:
         user_annotations = annotations.filter(dataset__email_address=user_email)
         user_downloads = user_annotations.count()
-        downloads_total += user_downloads
+        if user_downloads == 0:
+            continue
 
+        downloads_total += user_downloads
         user_locations = set()
         for user_annotation in user_annotations:
             user_locations.add(user_annotation.data["location"])
@@ -110,18 +111,18 @@ def post_downloads_summary(days, channel, top_countries=5):
 
         is_returning_user = Dataset.processed_filtered_objects.filter(
             created_at__lt=start_time, email_address=user_email
-        )
+        ).exists()
         if is_returning_user:
             returning_users.append(user_data)
         else:
             new_users.append(user_data)
 
-    if downloads_total:
+    if downloads_total > 0:
         locations_count = len(locations)
-        users_emails_count = len(users_emails)
+        users_count = len(new_users) + len(returning_users)
         fallback_text = (
-            f"In the last {days} day{pluralize(days)}, {users_emails_count} "
-            f"user{pluralize(users_emails_count)} downloaded {downloads_total} "
+            f"In the last {days} day{pluralize(days)}, {users_count} "
+            f"user{pluralize(users_count)} downloaded {downloads_total} "
             f"dataset{pluralize(downloads_total)} from {locations_count} "
             f"location{pluralize(locations_count)}."
         )
@@ -129,14 +130,20 @@ def post_downloads_summary(days, channel, top_countries=5):
         fallback_text = f"There were no downloads in the last {days} day{pluralize(days)}."
 
     blocks = [
-        {"type": "section", "text": {"type": "plain_text", "emoji": True, "text": fallback_text}}
+        {
+            "type": "section",
+            "text": {"type": "plain_text", "emoji": True, "text": fallback_text},
+        }
     ]
 
     if new_users:
         blocks.append(
             {
                 "type": "section",
-                "text": {"type": "mrkdwn", "text": format_user_data("*New users*", new_users)},
+                "text": {
+                    "type": "mrkdwn",
+                    "text": format_user_data("*New users*", new_users),
+                },
             }
         )
 


Test patch:
diff --git a/api/tests/management/commands/test_post_downloads_summary.py b/api/tests/management/commands/test_post_downloads_summary.py
--- a/api/tests/management/commands/test_post_downloads_summary.py
+++ b/api/tests/management/commands/test_post_downloads_summary.py
@@ -9,24 +9,38 @@
 
 
 class DownloadsPostTestCase(TestCase):
+    @patch("data_refinery_api.management.commands.post_downloads_summary.requests.post")
+    def test_post_download_summary_no_downloads(self, requests_post):
+        Dataset(email_address="test@gmail.com", is_processed=True).save()
+
+        post_downloads_summary(7, "ccdl-general-test")
+        json = requests_post.mock_calls[0][2]["json"]
+
+        self.assertEqual("There were no downloads in the last 7 days.", json["text"])
+
     @patch("data_refinery_api.management.commands.post_downloads_summary.requests.post")
     def test_post_download_summary_one_download(self, requests_post):
         dataset = Dataset(email_address="test@gmail.com", is_processed=True)
         dataset.save()
         DatasetAnnotation(dataset=dataset, data={"location": "Testyville, TSA"}).save()
 
+        # Shouldn't be counted with no annotation.
+        Dataset(email_address="test_nd@gmail.com", is_processed=True).save()
+
         post_downloads_summary(7, "ccdl-general-test")
         json = requests_post.mock_calls[0][2]["json"]
 
         self.assertEqual(
-            "In the last 7 days, 1 user downloaded 1 dataset from 1 location.", json["text"]
+            "In the last 7 days, 1 user downloaded 1 dataset from 1 location.",
+            json["text"],
         )
         self.assertEqual(
             "\n".join(("*New users*", "test@gmail.com | 1 download from Testyville, TSA")),
             json["blocks"][1]["text"]["text"],
         )
         self.assertEqual(
-            "\n".join(("*Top 1 country*", "TSA: 1 download")), json["blocks"][2]["text"]["text"]
+            "\n".join(("*Top 1 country*", "TSA: 1 download")),
+            json["blocks"][2]["text"]["text"],
         )
 
     @patch("data_refinery_api.management.commands.post_downloads_summary.requests.post")
@@ -47,7 +61,8 @@ def test_post_download_summary_two_downloads(self, requests_post):
         json = requests_post.mock_calls[0][2]["json"]
 
         self.assertEqual(
-            "In the last 7 days, 2 users downloaded 3 datasets from 2 locations.", json["text"]
+            "In the last 7 days, 2 users downloaded 3 datasets from 2 locations.",
+            json["text"],
         )
         self.assertEqual(
             "\n".join(
@@ -89,7 +104,8 @@ def test_post_download_summary_ordering(self, requests_post):
         json = requests_post.mock_calls[0][2]["json"]
 
         self.assertEqual(
-            "In the last 7 days, 4 users downloaded 8 datasets from 4 locations.", json["text"]
+            "In the last 7 days, 4 users downloaded 8 datasets from 4 locations.",
+            json["text"],
         )
         self.assertEqual(
             "\n".join(
@@ -132,7 +148,8 @@ def test_post_download_summary_filtered(self, requests_post):
         post_downloads_summary(7, "ccdl-general-test")
         json = requests_post.mock_calls[0][2]["json"]
         self.assertEqual(
-            "In the last 7 days, 1 user downloaded 1 dataset from 1 location.", json["text"]
+            "In the last 7 days, 1 user downloaded 1 dataset from 1 location.",
+            json["text"],
         )
 
     @patch("data_refinery_api.management.commands.post_downloads_summary.requests.post")