Repository: AlexsLemonade/refinebio

Issue:
API is Serializing Far Too Much Information
### Context

Database queries are much faster, but now it's slow because of how much information we're serializing. 

### Problem or idea

I suggest we stop serializing complete sample lists in the Search and Experiment and just return accession codes so that the individual samples can be queried by the separate Samples API.





Discussion:
Ref Sentry issue: [REFINEBIO-FRONTEND-27](https://sentry.io/greenelab/refinebio-frontend/issues/707106748/)

Seems that some requests to the search endpoint were dropped.

Resolving patch:
diff --git a/api/data_refinery_api/serializers.py b/api/data_refinery_api/serializers.py
--- a/api/data_refinery_api/serializers.py
+++ b/api/data_refinery_api/serializers.py
@@ -146,36 +146,6 @@ class Meta:
                     'last_modified',
                 )
 
-class SearchSampleSerializer(serializers.ModelSerializer):
-
-    class Meta:
-        model = Sample
-        fields = (
-                    'id',
-                    'title',
-                    'accession_code',
-                    'source_database',
-                    'platform_accession_code',
-                    'platform_name',
-                    'pretty_platform',
-                    'technology',
-                    'sex',
-                    'age',
-                    'specimen_part',
-                    'genotype',
-                    'disease',
-                    'disease_stage',
-                    'cell_line',
-                    'treatment',
-                    'race',
-                    'subject',
-                    'compound',
-                    'time',
-                    'is_processed',
-                    'created_at',
-                    'last_modified',
-                )
-
 class SampleAnnotationSerializer(serializers.ModelSerializer):
 
     class Meta:
@@ -233,13 +203,14 @@ class Meta:
 
 class ExperimentSerializer(serializers.ModelSerializer):
     organisms = serializers.StringRelatedField(many=True, read_only=True)
-    samples = SearchSampleSerializer(many=True, read_only=True)
+    platforms = serializers.ReadOnlyField()
+    processed_samples = serializers.StringRelatedField(many=True)
     total_samples_count = serializers.IntegerField(
                         read_only=True
                     )
-    processed_samples_count = serializers.IntegerField(
-                        read_only=True
-                    )
+    sample_metadata = serializers.ReadOnlyField(source='get_sample_metadata_fields')
+    technologies = serializers.ReadOnlyField(source='get_sample_technologies')
+    pretty_platforms = serializers.ReadOnlyField()
 
     class Meta:
         model = Experiment
@@ -251,18 +222,21 @@ class Meta:
                     'alternate_accession_code',
                     'source_database',
                     'source_url',
+                    'platforms',
+                    'pretty_platforms',
+                    'processed_samples',
                     'has_publication',
                     'publication_title',
                     'publication_doi',
                     'publication_authors',
                     'pubmed_id',
-                    'samples',
                     'total_samples_count',
-                    'processed_samples_count',
                     'organisms',
                     'submitter_institution',
                     'created_at',
                     'last_modified',
+                    'sample_metadata',
+                    'technologies'
                 )
 
     def setup_eager_loading(queryset):
@@ -287,9 +261,21 @@ class Meta:
                     'last_modified',
                 )
 
+class DetailedExperimentSampleSerializer(serializers.ModelSerializer):
+
+    class Meta:
+        model = Sample
+        fields = (
+                    'accession_code',
+                    'platform_name',
+                    'pretty_platform',
+                    'technology',
+                    'is_processed',
+                )
+
 class DetailedExperimentSerializer(serializers.ModelSerializer):
     annotations = ExperimentAnnotationSerializer(many=True, source='experimentannotation_set')
-    samples = SampleSerializer(many=True)
+    samples = DetailedExperimentSampleSerializer(many=True)
     organisms = OrganismSerializer(many=True)
     sample_metadata = serializers.ReadOnlyField(source='get_sample_metadata_fields')
 
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
@@ -326,10 +326,13 @@ def get_sample_metadata_fields(self):
         possible_fields = ['sex', 'age', 'specimen_part', 'genotype', 'disease', 'disease_stage',
                            'cell_line', 'treatment', 'race', 'subject', 'compound', 'time']
 
+        samples = self.samples.all()
+
         for field in possible_fields:
-            filter = {"age__isnull": True} if field == 'age' else {'%s__exact' % field: ''}
-            if len(self.samples.exclude(**filter)) > 0:
-                fields.append(field)
+            for sample in samples:
+                if getattr(sample, field) != None and getattr(sample, field) != '':
+                    fields.append(field)
+                    break
         return fields
 
     def get_sample_technologies(self):


Test patch: