예제 #1
0
def index_fields_dynamic(request):
    result = {'status': -1, 'message': 'Error'}

    try:
        name = request.POST.get('name')
        engine = request.POST.get('engine')
        source = request.POST.get('source')

        dynamic_fields = get_engine(request.user, engine,
                                    source=source).luke(name)

        result['message'] = ''
        result['fields'] = [
            Collection2._make_field(name, properties)
            for name, properties in dynamic_fields['fields'].items()
            if 'dynamicBase' in properties
        ]
        result['gridlayout_header_fields'] = [
            Collection2._make_gridlayout_header_field(
                {
                    'name': name,
                    'type': properties.get('type')
                }, True)
            for name, properties in dynamic_fields['fields'].items()
            if 'dynamicBase' in properties
        ]
        result['status'] = 0
    except Exception as e:
        result['message'] = force_unicode(e)

    return JsonResponse(result)
예제 #2
0
파일: api.py 프로젝트: yanjingtu/hue
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'):
            doc = Document2.objects.get_by_uuid(
                user=user,
                uuid=collection['selectedDocument']['uuid'],
                perm_type='read')
            snippets = doc.data_dict.get('snippets', [])
            if snippets:
                table_metadata = get_api(MockRequest(user, '""'),
                                         snippets[0]).autocomplete(
                                             {
                                                 'source': 'query',
                                                 'type': snippets[0]['type']
                                             }, doc.id)
                template['fieldsAttributes'] = [
                    Collection2._make_gridlayout_header_field(field)
                    for field in table_metadata['extended_columns']
                ]
                properties['engine'] = snippets[0]['type']
                if snippets[0]['result']['handle']:
                    handle = snippets[0]['result']['handle']
                    # Replace previous_statement_hash so that we rerun current statement
                    properties['result'] = {
                        'handle': {
                            'statement_id':
                            handle['statement_id'],
                            'statements_count':
                            handle['statements_count'],
                            'previous_statement_hash':
                            hashlib.sha224(str(uuid.uuid4())).hexdigest()
                        }
                    }
                else:
                    properties['result'] = {'handle': {}}
                properties['statement'] = snippets[0]['statement_raw']
            else:
                properties['result'] = {'handle': {}}
                properties['statement'] = ''
        else:  # Demo data for now
            properties['statement'] = 'select * from customers'
            properties['result'] = {'handle': {}}

        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': {}
    }