浏览代码

HUE-5116 [search] Support of dimension N of Gradient Map

Romain Rigaux 9 年之前
父节点
当前提交
80b95a2

+ 16 - 5
apps/search/src/search/models.py

@@ -881,8 +881,19 @@ def augment_solr_response(response, collection, query):
             counts = []
         elif collection_facet['properties'].get('isOldPivot'):
           facet_fields = [collection_facet['field']] + [f['field'] for f in collection_facet['properties'].get('facets', []) if f['aggregate']['function'] == 'count']
-          count = response['facets'][name]
-          _convert_nested_to_augmented_pivot_nd(facet_fields, facet['id'], count, selected_values, dimension=2)
+ 
+          column = 'count'
+          agg_keys = [key for key, value in counts[0].items() if key.lower().startswith('agg_') or key.lower().startswith('dim_')]
+          agg_keys.sort(key=lambda a: a[4:])
+
+          if len(agg_keys) == 1 and agg_keys[0].lower().startswith('dim_'):
+            agg_keys.insert(0, 'count')
+          counts = _augment_stats_2d(name, facet, counts, selected_values, agg_keys, rows)
+#             _augment_stats_2d(name, facet, counts, selected_values, agg_keys, rows)
+          
+          print counts
+#           count = response['facets'][name]
+#           _convert_nested_to_augmented_pivot_nd(facet_fields, facet['id'], count, selected_values, dimension=2)
           dimension = len(facet_fields)
         elif not collection_facet['properties']['facets'] or (collection_facet['properties']['facets'][0]['aggregate']['function'] != 'count' and len(collection_facet['properties']['facets']) == 1):
           # Dimension 1 with 1 count or agg
@@ -894,13 +905,13 @@ def augment_solr_response(response, collection, query):
             legend = agg_keys[0].split(':', 2)[1]
             column = agg_keys[0]
           else:
-            legend = facet['field'] # 'count(%s)' % legend
+            legend = facet['field']
             agg_keys = [column]
 
           _augment_stats_2d(name, facet, counts, selected_values, agg_keys, rows)
 
-          counts = [_v for _f in counts for _v in (_f['val'], _f[column])] # TODO: Create additional ordered dict for table view + download
-          counts = pairwise2(legend, selected_values.get(facet['id'], []), counts) # TODO use 'cat' for legend in graph
+          counts = [_v for _f in counts for _v in (_f['val'], _f[column])]
+          counts = pairwise2(legend, selected_values.get(facet['id'], []), counts)
         else:
           # Dimension 2 with analytics or 1 with N aggregates
           dimension = 2

+ 8 - 6
apps/search/src/search/static/search/js/search.ko.js

@@ -659,6 +659,8 @@ var Collection = function (vm, collection) {
         return _fields;
       });
 
+      facet.properties.facets_form.aggregate.percentiles = ko.mapping.fromJS([{'value': 50}]);
+
       facet.template.fieldsSelected.subscribe(function(newValue) { // Could be more efficient as we don't need to research, just redraw
         vm.getFacetFromQuery(facet.id()).resultHash('');
         vm.search();
@@ -877,13 +879,13 @@ var Collection = function (vm, collection) {
       'aggregate': ko.mapping.toJS(facet.properties.facets_form.aggregate),
     });
 
-      facet.properties.facets_form.field(null);
-      facet.properties.facets_form.limit(5);
-      facet.properties.facets_form.mincount(1);
+    facet.properties.facets_form.field(null);
+    facet.properties.facets_form.limit(5);
+    facet.properties.facets_form.mincount(1);
 
-      facet.properties.facets_form.aggregate.function('count');
-      facet.properties.facets_form.aggregate.ops.removeAll();
-      facet.properties.facets_form.aggregate.percentiles(ko.mapping.fromJS([{'value': 50}]));
+    facet.properties.facets_form.aggregate.function('count');
+    facet.properties.facets_form.aggregate.ops.removeAll();
+    facet.properties.facets_form.aggregate.percentiles(ko.mapping.fromJS([{'value': 50}]));
 
     if (pivot != null) {
       pivot.aggregate.function.subscribe(function() {

+ 39 - 2
apps/search/src/search/templates/common_search.mako

@@ -1627,11 +1627,11 @@ ${ dashboard.layout_skeleton() }
         <!-- ko if: widgetType() == 'gradient-map-widget' -->
           <!-- ko with: $parent -->
             <div data-bind="mapChart: {data: {counts: counts(), scope: $parent.properties.scope()},
-              transformer: mapChartDataTransformer,
+              transformer: gradientMapChartDataTransformer,
               maxWidth: 750,
               isScale: true,
               onClick: function(d) {
-                $root.query.togglePivotFacet({facet: {'fq_fields': d.cat, 'fq_values': d.value}, widget_id: id()});
+                $root.query.togglePivotFacet({facet: {'fq_fields': d.fields, 'fq_values': d.value}, widget_id: id()});
               },
               onComplete: function(){ var widget = viewModel.getWidgetById($parent.id()); if (widget != null) { widget.isLoading(false)}; } }" />
            <!-- /ko -->
@@ -3142,6 +3142,7 @@ function mapChartDataTransformer(data) {
     item.values = item.pivot ? item.pivot[0].fq_values : item.fq_values;
     item.counts = item.pivot ? item.pivot[0].count : item.count; // unused yet
     item.is2d = item.pivot ? true : false; // unused yet
+
     if (item.value != null && item.value != "" && item.value.length < 4) {
       var _label = data.scope == "world" ? HueGeo.getISOAlpha3(item.value) : item.value.toUpperCase();
       var _found = false;
@@ -3164,6 +3165,42 @@ function mapChartDataTransformer(data) {
   return _data;
 }
 
+
+function gradientMapChartDataTransformer(data) {
+  var _data = [];
+  $(data.counts).each(function (cnt, item) {
+    item.is2d = item.fq_fields ? true : false;
+    item.fields = item.is2d ? item.fq_fields : [item.cat];
+    item.values = item.is2d ? item.fq_values : [item.value];
+    item.counts = item.count;
+    item.value = item.is2d ? item.fq_values[0] : item.value;
+    item.pivot = [];
+
+    if (item.value != null && item.value != "" && item.value.length < 4) {
+      var _label = data.scope == "world" ? HueGeo.getISOAlpha3(item.value) : item.value.toUpperCase();
+      var _found = false;
+      for (var i = 0; i < _data.length; i++) { // we group lower and upper cases together
+        if (_data[i].label == _label) {
+          _data[i].value += item.counts;
+          _data[i].obj.pivot.push({count: item.counts, value: item.is2d ? (/\)/.test(item.cat) ? item.cat : item.fq_values[item.fq_values.length - 1]) : item.value});
+          _found = true;
+
+          break;
+        }
+      }
+      if (! _found) {
+        item.pivot = [{count: item.count, value: item.is2d ? (/\)/.test(item.cat) ? item.cat : item.fq_values[item.fq_values.length - 1]) : item.value}];
+        _data.push({
+          label: _label,
+          value: item.counts,
+          obj: item
+        });
+      }
+    }
+  });
+  return _data;
+}
+
 function leafletMapChartDataTransformer(data) {
   var _data = [];
   if (!$.isEmptyObject(data.counts) && data.counts.response.docs && viewModel.collection.template.leafletmap.latitudeField() != "" && viewModel.collection.template.leafletmap.longitudeField() != "") {