Exemplo n.º 1
0
def _create_facet(collection, user, facet_id, facet_label, facet_field,
                  widget_type, window_size):
    properties = {
        'sort': 'desc',
        'canRange': False,
        'stacked': False,
        'limit': 10,
        'mincount': 0,
        'missing': False,
        'isDate': False,
        'slot': 0,
        'aggregate': {
            'function': 'unique',
            'formula': '',
            'plain_formula': '',
            'percentile': 50
        }
    }
    template = {
        "showFieldList": True,
        "showGrid": False,
        "showChart": True,
        "chartSettings": {
            'chartType':
            'pie' if widget_type == 'pie2-widget' else
            ('timeline' if widget_type == 'timeline-widget' else
             ('gradientmap' if widget_type == 'gradient-map-widget' else 'bars'
              )),
            'chartSorting':
            'none',
            'chartScatterGroup':
            None,
            'chartScatterSize':
            None,
            'chartScope':
            'world',
            'chartX':
            None,
            'chartYSingle':
            None,
            'chartYMulti': [],
            'chartData': [],
            'chartMapLabel':
            None,
            'chartSelectorType':
            'bar'
        },
        "fieldsAttributes": [],
        "fieldsAttributesFilter": "",
        "filteredAttributeFieldsAll": True,
        "fields": [],
        "fieldsSelected": [],
        "leafletmap": {
            'latitudeField': None,
            'longitudeField': None,
            'labelField': None
        },  # Use own?
        'leafletmapOn': False,
        'isGridLayout': False,
        "hasDataForChart": True,
        "rows": 25,
    }
    if widget_type in ('tree-widget', 'heatmap-widget', 'map-widget'):
        facet_type = 'pivot'
    elif widget_type == 'document-widget':
        # SQL query, 1 solr widget
        if collection['selectedDocument'].get('uuid'):
            properties['statementUuid'] = collection['selectedDocument'].get(
                'uuid')
            doc = Document2.objects.get_by_uuid(
                user=user,
                uuid=collection['selectedDocument']['uuid'],
                perm_type='read')
            snippets = doc.data_dict.get('snippets', [])
            properties['result'] = {
                'handle': {
                    'statement_id':
                    0,
                    'statements_count':
                    1,
                    'previous_statement_hash':
                    hashlib.sha224(str(uuid.uuid4())).hexdigest()
                }
            }
            if snippets:
                properties['engine'] = snippets[0]['type']
        else:
            properties['statementUuid'] = ''
        properties['statement'] = ''
        properties['uuid'] = facet_field
        properties['facets'] = [{
            'canRange': False,
            'field': 'blank',
            'limit': 10,
            'mincount': 0,
            'sort': 'desc',
            'aggregate': {
                'function': 'count'
            },
            'isDate': False,
            'type': 'field'
        }]
        facet_type = 'statement'
    else:
        api = get_engine(user, collection)
        range_properties = _new_range_facet(api, collection, facet_field,
                                            widget_type, window_size)

        if range_properties:
            facet_type = 'range'
            properties.update(range_properties)
            properties['initial_gap'] = properties['gap']
            properties['initial_start'] = properties['start']
            properties['initial_end'] = properties['end']
        else:
            facet_type = 'field'

        if widget_type in ('bucket-widget', 'pie2-widget', 'timeline-widget',
                           'tree2-widget', 'text-facet-widget', 'hit-widget',
                           'gradient-map-widget'):
            # properties = {'canRange': False, 'stacked': False, 'limit': 10} # TODO: Lighter weight top nested facet

            properties['facets_form'] = NESTED_FACET_FORM
            # Not supported on dim 2 currently
            properties['facets_form']['type'] = 'field'
            properties['facets_form']['canRange'] = False
            properties['facets_form']['isFacetForm'] = True

            facet = NESTED_FACET_FORM.copy()
            facet['field'] = facet_field
            facet['limit'] = 10
            facet['fieldLabel'] = facet_field
            facet['multiselect'] = True

            if widget_type == 'text-facet-widget':
                properties['type'] = facet_type
                if USE_GRIDSTER.get():
                    properties['limit'] = facet['limit'] = 100

            if range_properties:
                # TODO: timeline still uses properties from top properties
                facet.update(range_properties)
                facet['initial_gap'] = facet['gap']
                facet['initial_start'] = facet['start']
                facet['initial_end'] = facet['end']
                facet['stacked'] = False
                facet['type'] = 'range'
            else:
                facet['type'] = facet_type

            if collection.get('engine', 'solr') != 'solr':
                facet['sort'] = 'default'

            properties['facets'] = [facet]
            properties['domain'] = {'blockParent': [], 'blockChildren': []}
            properties['compare'] = COMPARE_FACET
            properties['filter'] = QUERY_FACET

            if widget_type == 'hit-widget':
                facet_type = 'function'
                facet['aggregate']['function'] = 'unique'
            else:
                facet_type = 'nested'
                facet['aggregate']['function'] = 'count'

            if widget_type == 'pie2-widget':
                properties['scope'] = 'stack'
                properties['timelineChartType'] = 'bar'
            elif widget_type == 'tree2-widget':
                properties['scope'] = 'tree'
                properties['facets_form']['limit'] = 5
                properties['isOldPivot'] = True
            elif widget_type == 'gradient-map-widget':
                properties['scope'] = 'world'
                facet['limit'] = 100
            else:
                properties['scope'] = 'stack'
                properties['timelineChartType'] = 'bar'

    if widget_type in ('tree-widget', 'heatmap-widget',
                       'map-widget') and widget_type != 'gradient-map-widget':
        properties['mincount'] = 1
        properties['facets'] = []
        properties['stacked'] = True
        properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 5}

        if widget_type == 'map-widget':
            properties['scope'] = 'world'
            properties['limit'] = 100
        else:
            properties[
                'scope'] = 'stack' if widget_type == 'heatmap-widget' else 'tree'

    if widget_type == 'histogram-widget':
        properties['enableSelection'] = True
        properties['timelineChartType'] = 'bar'
        properties['extraSeries'] = []

    return {
        'id': facet_id,
        'label': facet_label,
        'field': facet_field,
        'type': facet_type,
        'widgetType': widget_type,
        'properties': properties,
        # Hue 4+
        'template': template,
        'queryResult': {}
    }
Exemplo n.º 2
0
def _create_facet(collection, user, facet_id, facet_label, facet_field, widget_type):
  properties = {
    'sort': 'desc',
    'canRange': False,
    'stacked': False,
    'limit': 10,
    'mincount': 1,
    'isDate': False,
    'aggregate': {'function': 'unique', 'formula': '', 'plain_formula': '', 'percentile': 50}
  }

  if widget_type in ('tree-widget', 'heatmap-widget', 'map-widget'):
    facet_type = 'pivot'
  elif widget_type == 'document-widget':
    properties['uuid'] = facet_field
    properties['subfacet'] = {}
  else:
    api = get_engine(user, collection)
    range_properties = _new_range_facet(api, collection, facet_field, widget_type)

    if range_properties:
      facet_type = 'range'
      properties.update(range_properties)
      properties['initial_gap'] = properties['gap']
      properties['initial_start'] = properties['start']
      properties['initial_end'] = properties['end']
    else:
      facet_type = 'field'

    if widget_type in ('bucket-widget', 'pie2-widget', 'timeline-widget', 'tree2-widget', 'text-facet-widget', 'hit-widget', 'gradient-map-widget'):
      # properties = {'canRange': False, 'stacked': False, 'limit': 10} # TODO: Lighter weight top nested facet

      if widget_type == 'text-facet-widget':
        properties['type'] = facet_type

      properties['facets_form'] = NESTED_FACET_FORM
      # Not supported on dim 2 currently
      properties['facets_form']['type'] = 'field'
      properties['facets_form']['canRange'] = False
      properties['facets_form']['isFacetForm'] = True

      facet = NESTED_FACET_FORM.copy()
      facet['field'] = facet_field
      facet['limit'] = 10

      if range_properties:
        # TODO: timeline still uses properties from top properties
        facet.update(range_properties)
        facet['initial_gap'] = facet['gap']
        facet['initial_start'] = facet['start']
        facet['initial_end'] = facet['end']
        facet['stacked'] = False
        facet['type'] = 'range'
      else:
        facet['type'] = facet_type

      if collection.get('engine', 'solr') != 'solr':
        facet['sort'] = 'default'

      properties['facets'] = [facet]
      properties['domain'] = {'blockParent': [], 'blockChildren': []}

      if widget_type == 'hit-widget':
        facet_type = 'function'
        facet['aggregate']['function'] = 'unique'
      else:
        facet_type = 'nested'
        facet['aggregate']['function'] = 'count'

      if widget_type == 'pie2-widget':
        properties['scope'] = 'stack'
        properties['timelineChartType'] = 'bar'
      elif widget_type == 'tree2-widget':
        properties['scope'] = 'tree'
        properties['facets_form']['limit'] = 5
        properties['isOldPivot'] = True
      elif widget_type == 'gradient-map-widget':
        properties['scope'] = 'world'
        facet['limit'] = 100
      else:
        properties['scope'] = 'stack'
        properties['timelineChartType'] = 'bar'

  if widget_type in ('tree-widget', 'heatmap-widget', 'map-widget') and widget_type != 'gradient-map-widget':
    properties['mincount'] = 1
    properties['facets'] = []
    properties['stacked'] = True
    properties['facets_form'] = {'field': '', 'mincount': 1, 'limit': 5}

    if widget_type == 'map-widget':
      properties['scope'] = 'world'
      properties['limit'] = 100
    else:
      properties['scope'] = 'stack' if widget_type == 'heatmap-widget' else 'tree'

  if widget_type == 'histogram-widget':
    properties['enableSelection'] = True
    properties['timelineChartType'] = 'bar'
    properties['extraSeries'] = []

  return {
    'id': facet_id,
    'label': facet_label,
    'field': facet_field,
    'type': facet_type,
    'widgetType': widget_type,
    'properties': properties,
    # Hue 4+
    'template': {
        "showFieldList": True,
        "showGrid": False,
        "showChart": True,
        "chartSettings" : {
          'chartType': 'pie' if widget_type == 'pie2-widget' else ('timeline' if widget_type == 'timeline-widget' else ('gradientmap' if widget_type == 'gradient-map-widget' else 'bars')),
          'chartSorting': 'none',
          'chartScatterGroup': None,
          'chartScatterSize': None,
          'chartScope': 'world',
          'chartX': None,
          'chartYSingle': None,
          'chartYMulti': [],
          'chartData': [],
          'chartMapLabel': None,
        },
        "fieldsAttributes": [],
        "fieldsAttributesFilter": "",
        "filteredAttributeFieldsAll": True,
        "fields": [],
        "fieldsSelected": [],
        "leafletmap": {'latitudeField': None, 'longitudeField': None, 'labelField': None}, # Use own?
        'leafletmapOn': False,
        'isGridLayout': False,
        "hasDataForChart": True,
        "rows": 25,
    },
    'queryResult': {}
  }