Browse Source

HUE-7766 [dashboard] Skeleton to compare with past data intervals in counter widget

Romain Rigaux 8 years ago
parent
commit
131fc3e

+ 2 - 1
desktop/libs/dashboard/src/dashboard/api.py

@@ -35,7 +35,7 @@ from dashboard.data_export import download as export_download
 from dashboard.decorators import allow_viewer_only
 from dashboard.decorators import allow_viewer_only
 from dashboard.facet_builder import _guess_gap, _zoom_range_facet, _new_range_facet
 from dashboard.facet_builder import _guess_gap, _zoom_range_facet, _new_range_facet
 from dashboard.models import Collection2, augment_solr_response, pairwise2, augment_solr_exception,\
 from dashboard.models import Collection2, augment_solr_response, pairwise2, augment_solr_exception,\
-  NESTED_FACET_FORM
+  NESTED_FACET_FORM, COMPARE_FACET
 
 
 
 
 LOG = logging.getLogger(__name__)
 LOG = logging.getLogger(__name__)
@@ -428,6 +428,7 @@ def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_t
 
 
       properties['facets'] = [facet]
       properties['facets'] = [facet]
       properties['domain'] = {'blockParent': [], 'blockChildren': []}
       properties['domain'] = {'blockParent': [], 'blockChildren': []}
+      properties['compare'] = COMPARE_FACET
 
 
       if widget_type == 'hit-widget':
       if widget_type == 'hit-widget':
         facet_type = 'function'
         facet_type = 'function'

+ 18 - 1
desktop/libs/dashboard/src/dashboard/models.py

@@ -14,6 +14,7 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # See the License for the specific language governing permissions and
 # limitations under the License.
 # limitations under the License.
+from __future__ import division
 
 
 import collections
 import collections
 import itertools
 import itertools
@@ -44,6 +45,7 @@ NESTED_FACET_FORM = {
     'isDate': False,
     'isDate': False,
     'aggregate': {'function': 'unique', 'formula': '', 'plain_formula': '', 'percentile': 50}
     'aggregate': {'function': 'unique', 'formula': '', 'plain_formula': '', 'percentile': 50}
 }
 }
+COMPARE_FACET = {'is_enabled': False, 'gap': '1WEEK', 'use_percentage': False, 'cohort_number': 1}
 
 
 
 
 class Collection2(object):
 class Collection2(object):
@@ -145,6 +147,9 @@ class Collection2(object):
         properties['facets'] = []
         properties['facets'] = []
         properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 5}
         properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 5}
 
 
+      if 'compare' not in properties:
+        properties['compare'] = COMPARE_FACET
+
     if 'qdefinitions' not in props['collection']:
     if 'qdefinitions' not in props['collection']:
       props['collection']['qdefinitions'] = []
       props['collection']['qdefinitions'] = []
 
 
@@ -480,8 +485,20 @@ def augment_solr_response(response, collection, query):
       name = facet['id'] # Nested facets can only have one name
       name = facet['id'] # Nested facets can only have one name
 
 
       if category == 'function' and name in response['facets']:
       if category == 'function' and name in response['facets']:
-        value = response['facets'][name]
         collection_facet = get_facet_field(category, name, collection['facets'])
         collection_facet = get_facet_field(category, name, collection['facets'])
+
+        if collection_facet['properties']['compare']['is_enabled']:
+          orignal_number, final_number = response['facets'][name]['buckets'][0][name], response['facets'][name]['buckets'][1][name]
+          if collection_facet['properties']['compare']['use_percentage']:
+            if orignal_number != 0:
+              value = (final_number - orignal_number) / orignal_number * 100.0
+            else:
+              value = None 
+          else:
+            value = final_number - orignal_number
+        else:
+          value = response['facets'][name]
+
         facet = {
         facet = {
           'id': collection_facet['id'],
           'id': collection_facet['id'],
           'query': name,
           'query': name,

+ 9 - 0
desktop/libs/dashboard/src/dashboard/static/dashboard/js/search.ko.js

@@ -745,6 +745,15 @@ var Collection = function (vm, collection) {
       }
       }
     }
     }
 
 
+    if (facet.properties.compare) {
+      facet.properties.compare.is_enabled.subscribe(function () {
+        vm.search();
+      });
+      facet.properties.compare.use_percentage.subscribe(function () {
+        vm.search();
+      });
+     }
+
     // For Solr 5+  only
     // For Solr 5+  only
     if (typeof facet.template != 'undefined') {
     if (typeof facet.template != 'undefined') {
       facet.template.filteredAttributeFields = ko.computed(function() { // Dup of template.filteredAttributeFields
       facet.template.filteredAttributeFields = ko.computed(function() { // Dup of template.filteredAttributeFields

+ 40 - 0
desktop/libs/dashboard/src/dashboard/templates/common_search.mako

@@ -1525,6 +1525,7 @@ ${ dashboard.layout_skeleton(suffix='search') }
         <!-- ko if: widgetType() == 'hit-widget' -->
         <!-- ko if: widgetType() == 'hit-widget' -->
           <!-- ko with: $parent -->
           <!-- ko with: $parent -->
             <!-- ko if: counts().length > 0 -->
             <!-- ko if: counts().length > 0 -->
+              <i class="fa fa-rotate-90" data-bind="css: { 'fa-caret-down': counts()[0].count < 0, 'fa-caret-up': counts()[0].count >= 0 }"></i>
               <span class="big-counter" data-bind="textSqueezer: counts()[0].count"></span>
               <span class="big-counter" data-bind="textSqueezer: counts()[0].count"></span>
             <!-- /ko -->
             <!-- /ko -->
           <!-- /ko -->
           <!-- /ko -->
@@ -2010,9 +2011,48 @@ ${ dashboard.layout_skeleton(suffix='search') }
   <div class="row-fluid" data-bind="with: $root.getFacetFromQuery(id())">
   <div class="row-fluid" data-bind="with: $root.getFacetFromQuery(id())">
     <div data-bind="with: $root.collection.getFacetById($parent.id())">
     <div data-bind="with: $root.collection.getFacetById($parent.id())">
       <span data-bind="template: { name: 'facet-toggle2' }"></span>
       <span data-bind="template: { name: 'facet-toggle2' }"></span>
+
+      <span class="pull-right" data-bind="template: { name: 'facet-compare', data: properties.compare }"></span>
     </div>
     </div>
 
 
+    <i class="fa big-counter" data-bind="css: { 'fa-caret-down': counts() < 0, 'fa-caret-up': counts() >= 0 }"></i>
     <span class="big-counter" data-bind="textSqueezer: counts"></span>
     <span class="big-counter" data-bind="textSqueezer: counts"></span>
+
+    <span data-bind="with: $root.collection.getFacetById($parent.id())">
+      <span class="big-counter" data-bind="visible: properties.compare.use_percentage">
+        ${ '%' }
+      </span>
+    </span>
+  </div>
+  <!-- /ko -->
+</script>
+
+
+<script type="text/html" id="facet-compare">
+  <input type="checkbox" data-bind="checked: is_enabled"/> ${ _('Compare') }
+
+  <!-- ko if: is_enabled -->
+  <div class="facet-field-cnt">
+    <span class="spinedit-cnt">
+      <span class="facet-field-label">
+        ${ _('Cohorts') }
+      </span>
+      <input type="text"/>
+      <input type="number"/>
+    </span>
+  </div>
+
+  <div class="facet-field-cnt">
+    <span class="spinedit-cnt">
+      <span class="facet-field-label">
+        ${ _('Percentage') }
+      </span>
+      <input type="checkbox" data-bind="checked: use_percentage"/>
+      <span class="facet-field-label">
+        ${ _('Display both') }
+      </span>
+      <input type="checkbox"/>
+    </span>
   </div>
   </div>
   <!-- /ko -->
   <!-- /ko -->
 </script>
 </script>

+ 11 - 9
desktop/libs/libsolr/src/libsolr/api.py

@@ -177,15 +177,17 @@ class SolrApi(object):
             json_facets[facet['id']] = _f['facet'][dim_key]
             json_facets[facet['id']] = _f['facet'][dim_key]
         elif facet['type'] == 'function':
         elif facet['type'] == 'function':
           if facet['properties']['facets']:
           if facet['properties']['facets']:
-#             json_facets[facet['id']] = self._get_aggregate_function(facet['properties']['facets'][0])
-            json_facets[facet['id']] = {
-    'type' : "range",
-    'field' : "time",
-    'start' : "2014-05-04T06:35:00Z",
-    'end' : "2014-05-04T14:15:00Z",
-    'gap' : "+30MINUTES",
-    'facet': {facet['id']: self._get_aggregate_function(facet['properties']['facets'][0])}
-  }
+            if facet['properties']['compare']['is_enabled']:
+              json_facets[facet['id']] = {
+                'type' : "range",
+                'field' : "time",
+                'start' : "2014-05-04T01:35:00Z/HOURS",
+                'end' : "2014-05-04T14:15:00Z/HOURS",
+                'gap' : "+12HOURS",
+                'facet': {facet['id']: self._get_aggregate_function(facet['properties']['facets'][0])}
+              }
+            else:
+              json_facets[facet['id']] = self._get_aggregate_function(facet['properties']['facets'][0])
             json_facets['processEmpty'] = True
             json_facets['processEmpty'] = True
         elif facet['type'] == 'pivot':
         elif facet['type'] == 'pivot':
           if facet['properties']['facets'] or facet['widgetType'] == 'map-widget':
           if facet['properties']['facets'] or facet['widgetType'] == 'map-widget':