Repository: AlexsLemonade/refinebio

Issue:
Engagement bot has gone away

### Problem or idea
Engagement bot hasn't been posting since 27th Dec

### Solution or next step

Fix the engagement bot


Discussion:

Resolving patch:
diff --git a/api/data_refinery_api/views.py b/api/data_refinery_api/views.py
--- a/api/data_refinery_api/views.py
+++ b/api/data_refinery_api/views.py
@@ -540,16 +540,10 @@ def perform_update(self, serializer):
                             city = "COULD_NOT_DETERMINE"
 
                         user_agent = self.request.META.get("HTTP_USER_AGENT", None)
-                        total_samples = len(
-                            set(
-                                [
-                                    accession_code
-                                    for experiment in new_data.values()
-                                    for accession_code in experiment
-                                ]
-                            )
-                        )
-                        response = requests.post(
+                        total_downloads = Dataset.objects.filter(
+                            email_address=supplied_email_address
+                        ).count()
+                        requests.post(
                             settings.ENGAGEMENTBOT_WEBHOOK,
                             json={
                                 "channel": "ccdl-general",  # Move to robots when we get sick of these
@@ -561,7 +555,7 @@ def perform_update(self, serializer):
                                         "title": "New dataset download",
                                         "fallback": "New dataset download",
                                         "title_link": "http://www.refine.bio/dataset/{0}".format(
-                                            old_object.id
+                                            obj.id
                                         ),
                                         "text": "New user {0} from {1} downloaded a dataset!".format(
                                             supplied_email_address, city
@@ -571,17 +565,15 @@ def perform_update(self, serializer):
                                         ),
                                         "footer_icon": "https://s3.amazonaws.com/refinebio-email/logo-2x.png",
                                         "fields": [
-                                            {"title": "Dataset id", "value": str(old_object.id),},
+                                            {"title": "Dataset id", "value": str(obj.id),},
                                             {
                                                 "title": "Total downloads",
-                                                "value": Dataset.objects.filter(
-                                                    email_address=supplied_email_address
-                                                ).count(),
+                                                "value": total_downloads,
                                                 "short": True,
                                             },
                                             {
                                                 "title": "Samples",
-                                                "value": total_samples,
+                                                "value": obj.get_total_samples(),
                                                 "short": True,
                                             },
                                         ],
diff --git a/common/data_refinery_common/models/models.py b/common/data_refinery_common/models/models.py
--- a/common/data_refinery_common/models/models.py
+++ b/common/data_refinery_common/models/models.py
@@ -1416,6 +1416,19 @@ def get_samples(self):
 
         return Sample.objects.filter(accession_code__in=all_samples)
 
+    def get_total_samples(self):
+        """ Returns the total number of samples, this counts the number of unique
+        accession codes in `data`. """
+        return len(
+            set(
+                [
+                    accession_code
+                    for experiment in self.data.values()
+                    for accession_code in experiment
+                ]
+            )
+        )
+
     def get_experiments(self):
         """ Retuns all of the Experiments objects in this Dataset """
         all_experiments = self.data.keys()


Test patch: