Эх сурвалжийг харах

[search] Adding histogram widget

Romain Rigaux 11 жил өмнө
parent
commit
336e3a9cd7

+ 12 - 1
apps/search/src/search/api.py

@@ -109,6 +109,17 @@ class SolrApi(object):
       for facet in collection['facets']:
         if facet['type'] == 'query':
           params += (('facet.query', '%s' % facet['field']),)
+        if facet['type'] == 'range':
+          if not facet['properties']:
+            facet['properties']['start'] = '2014-02-25T15:00:00Z'
+            facet['properties']['end'] = '2014-02-26T12:00:00Z'
+            facet['properties']['gap'] = '+1HOURS'
+          params += tuple([
+             ('facet.range', facet['field']),
+             ('f.%s.facet.range.start' % facet['field'], facet['properties']['start']),
+             ('f.%s.facet.range.end' % facet['field'], facet['properties']['end']),
+             ('f.%s.facet.range.gap' % facet['field'], facet['properties']['gap']),]
+          )          
         elif facet['type'] == 'field':
           params += (('facet.field', '{!ex=%s}%s' % (facet['field'], facet['field'])),)
 
@@ -120,7 +131,7 @@ class SolrApi(object):
       # If we do this, need to parse the template and fill up the fields list
       #params += (('fl', urllib.unquote(utf_quoter(','.join(collection['fields'])))),)
     params += (('fl', '*'),)
-    # To parameterize
+
     params += (
       ('hl', 'true'),
       ('hl.fl', '*'),

+ 13 - 4
apps/search/src/search/models.py

@@ -480,10 +480,10 @@ def augment_solr_response2(response, collection, solr_query):
       category = facet['type']
       if category == 'pie-widget': # could be facet_range
         category = 'field'
-        
+      
       if category == 'field' and response['facet_counts']['facet_fields']:
-        for name in response['facet_counts']['facet_fields']:
-          selected_field = fq.get(name, '') # todo with multi
+        for name in response['facet_counts']['facet_fields']: # todo get from the list
+          selected_field = fq.get(name, '') # todo with multi filter
           collection_facet = get_facet_field(category, name, collection['facets'])
           facet = {
             'id': collection_facet['id'],
@@ -495,7 +495,16 @@ def augment_solr_response2(response, collection, solr_query):
           }
           normalized_facets.append(facet)
       elif category == 'range' and response['facet_counts']['facet_ranges']:
-        pass
+          name = facet['field']
+          collection_facet = get_facet_field(category, name, collection['facets'])          
+          facet = {
+            'id': collection_facet['id'],
+            'field': name,
+            'type': category,
+            'label': collection_facet['label'],
+            'counts': response['facet_counts']['facet_ranges'][name]['counts'],
+          }
+          normalized_facets.append(facet)
       elif category == 'query' and response['facet_counts']['facet_queries']:
         for name, value in response['facet_counts']['facet_queries'].iteritems():
           collection_facet = get_facet_field(category, name, collection['facets'])

+ 17 - 3
apps/search/src/search/templates/search2.mako

@@ -195,7 +195,7 @@ ${ commonheader(_('Search'), "search", user, "60px") | n,unicode }
     <div class="draggable-widget" data-bind="draggable: draggablePie" title="${_('Pie Chart')}" rel="tooltip" data-placement="top"><a href="#"><i class="hcha hcha-pie-chart"></i></a></div>
     <div class="draggable-widget" data-bind="draggable: draggableHit" title="${_('Hit Count')}" rel="tooltip" data-placement="top"><a href="#"><i class="fa fa-tachometer"></i></a></div>
     <div class="draggable-widget" data-bind="draggable: draggableArea" title="${_('Line Chart')}" rel="tooltip" data-placement="top"><a href="#"><i class="hcha hcha-area-chart"></i></a></div>
-    <div class="draggable-widget" data-bind="draggable: draggableBar" title="${_('Timeline')}" rel="tooltip" data-placement="top"><a href="#"><i class="hcha hcha-bar-chart"></i></a></div>
+    <div class="draggable-widget" data-bind="draggable: draggableHistogram" title="${_('Timeline')}" rel="tooltip" data-placement="top"><a href="#"><i class="hcha hcha-bar-chart"></i></a></div>
     <div class="draggable-widget" data-bind="draggable: draggableLine" title="${_('Filter Bar')}" rel="tooltip" data-placement="top"><a href="#"><i class="fa fa-filter"></i></a></div>
     <div class="draggable-widget" data-bind="draggable: draggableMap" title="${_('Map')}" rel="tooltip" data-placement="top"><a href="#"><i class="hcha hcha-map-chart"></i></a></div>    
   </div>
@@ -436,8 +436,22 @@ ${ commonheader(_('Search'), "search", user, "60px") | n,unicode }
   <span data-bind="text: $data.response.numFound"></span> ${ _(' results') } <i class="fa fa-arrow-right"></i>
 </script>
 
-<script type="text/html" id="timeline-widget">
-  This is the timeline widget
+<script type="text/html" id="histogram-widget">
+  <!-- ko ifnot: $root.getFacetFromQuery(id) -->
+    <a data-bind="click: showAddFacetModal" class="btn" href="javascript:void(0)"><i class="fa fa-plus"></i></a>
+  <!-- /ko -->
+
+  <!-- ko if: $root.getFacetFromQuery(id) -->
+  <div class="row-fluid" data-bind="with: $root.getFacetFromQuery(id)">
+    <div data-bind="visible: $root.isEditing, with: $root.collection.getFacetById($parent.id())" style="margin-bottom: 20px">      
+      ${ _('Label') }: <input type="text" data-bind="value: label" />
+      <br/>      
+      ${ _('Field') }: <input type="text" data-bind="value: field" />
+    </div>  
+
+    <span data-bind="text: counts" />
+  </div>
+  <!-- /ko -->
 </script>
 
 <script type="text/html" id="bar-widget">

+ 4 - 3
apps/search/static/js/search.ko.js

@@ -232,12 +232,13 @@ var Collection = function (vm, collection) {
   self.fields = ko.mapping.fromJS(collection.fields);
 
   self.addFacet = function (facet_json) {
-	var facetType = facet_json.widgetType == 'hit-widget' ? 'query' : 'field';
+	var facetType = facet_json.widgetType == 'hit-widget' ? 'query' : (facet_json.widgetType == 'histogram-widget' ? 'range' : 'field');
     var facet = ko.mapping.fromJS({
         "id": facet_json.widget_id,
         "label": facet_json.name,
         "field": facet_json.name,
-        "type": facetType
+        "type": facetType,
+        "properties": {}
     });
     facet.field.subscribe(function () {
       vm.search();
@@ -341,7 +342,7 @@ var SearchViewModel = function (collection_json, query_json) {
   self.draggableHit = ko.observable(new Widget(12, UUID(), "Hit Count", "hit-widget"));
   self.draggableFacet = ko.observable(new Widget(12, UUID(), "Facet", "facet-widget"));
   self.draggableResultset = ko.observable(new Widget(12, UUID(), "Results", "resultset-widget"));
-  self.draggableBar = ko.observable(new Widget(12, UUID(), "Bar Chart", "bar-widget"));
+  self.draggableHistogram = ko.observable(new Widget(12, UUID(), "Histogram", "histogram-widget"));
   self.draggableArea = ko.observable(new Widget(12, UUID(), "Area Chart", "area-widget"));
   self.draggableMap = ko.observable(new Widget(12, UUID(), "Map", "map-widget"));
   self.draggableLine = ko.observable(new Widget(12, UUID(), "Line Chart", "line-widget"));