From a142920d22810392977e21e8b4f72d4d4d277aec Mon Sep 17 00:00:00 2001
From: Megan Henning <meganhenning@flywheel.io>
Date: Wed, 26 Apr 2017 16:33:29 -0500
Subject: [PATCH] Minor updates to facet endpoint

---
 api/handlers/dataexplorerhandler.py | 80 +++++++++++------------------
 bin/dicom_doctype.py                |  2 +-
 2 files changed, 30 insertions(+), 52 deletions(-)

diff --git a/api/handlers/dataexplorerhandler.py b/api/handlers/dataexplorerhandler.py
index 9b32e4f0..b6dca740 100644
--- a/api/handlers/dataexplorerhandler.py
+++ b/api/handlers/dataexplorerhandler.py
@@ -47,49 +47,6 @@ BASE_QUERY = {
   }
 }
 
-OLD_FACET_QUERY = {
-    "size": 0,
-    "aggs" : {
-        "Series Description" : {
-            "terms" : {
-                "field" : "dicom_header.SeriesDescription_term",
-                "size" : 5
-            }
-        },
-        "Series Description Fragment" : {
-            "terms" : {
-                "field" : "dicom_header.SeriesDescription",
-                "size" : 5
-            }
-        },
-        "Patient Name" : {
-            "terms" : {
-                "field" : "dicom_header.PatientName_term",
-                "size" : 5
-            }
-        },
-        "Patient ID" : {
-            "terms" : {
-                "field" : "dicom_header.PatientID_term",
-                "size" : 5
-            }
-        },
-        "Modality" : {
-            "terms" : {
-                "field" : "dicom_header.Modality_term",
-                "size" : 5
-            }
-        },
-        "Study Date" : {
-            "date_histogram" : {
-                "field" : "dicom_header.StudyDate",
-                "interval" : "day"
-            }
-        }
-    }
-}
-
-
 FACET_QUERY = {
     "size": 0,
     "aggs" : {
@@ -98,7 +55,7 @@ FACET_QUERY = {
             "aggs": {
                 "subect.sex" : {
                     "terms" : {
-                        "field" : "subect.sex.raw",
+                        "field" : "subject.sex.raw",
                         "size" : 15
                     }
                 },
@@ -131,11 +88,27 @@ FACET_QUERY = {
                         "field" : "session.created",
                         "interval" : "month"
                     }
-                },
+                }
+            }
+        },
+        "session_age": {
+            "filter": {
+                "bool" : {
+                  "must" : [
+                     {"range": {"subject.age": {"gte": -31556952, "lte": 3155695200}}},
+                     {"term": {"container_type": "session"}}
+                  ]
+                }
+            },
+            "aggs": {
                 "subject.age" : {
                     "histogram" : {
                         "field" : "subject.age",
-                        "interval" : 31556952
+                        "interval" : 31556952,
+                        "extended_bounds" : {
+                            "min" : -31556952,
+                            "max" : 3155695200
+                        }
                     }
                 }
             }
@@ -270,11 +243,16 @@ class DataExplorerHandler(base.RequestHandler):
         )
         return { 'results': results['hits']['hits'], 'result_count': results['hits']['total']}
 
+    @require_login
     def get_facets(self):
-        results = config.es.search(
+        aggs = config.es.search(
             index='data_explorer',
-            doc_type='file',
-            body=FACET_QUERY,
-            size=10000
+            doc_type='flywheel',
+            body=FACET_QUERY
         )['aggregations']
-        return {'facets': results}
+
+        # This aggregation needs an extra filter to filter out outliers (only shows ages between -1 and 100)
+        # Add it back in to the session aggregation node
+        age_node = aggs.pop('session_age')
+        aggs['session']['subject.age'] = age_node['subject.age']
+        return {'facets': aggs}
diff --git a/bin/dicom_doctype.py b/bin/dicom_doctype.py
index 3be9adf2..e42b9fa8 100755
--- a/bin/dicom_doctype.py
+++ b/bin/dicom_doctype.py
@@ -396,7 +396,7 @@ if __name__ == '__main__':
     count = 1
     group_count_total = groups.count()
     for g in groups:
-        print 'Loading group {} ({} of {})'.format(g['label'], count, group_count_total)
+        print 'Loading group {} ({} of {})'.format(g['name'], count, group_count_total)
         count += 1
 
         remove_blacklisted_keys(g)
-- 
GitLab