Example #1
0
def main():
    facet_id = "habitat_type.substrate.id"
    facet_label = "Substrates"
    facet_type = "multiselect"
    id_field = "habitat_type.substrate.id"
    label_field = "habitat_type.substrate.name"
    value_field = "area"
    filters = []
    aggregate_func = "sum"

    choice_facet = habitat_services.get_choice_facet(
        id_field=id_field,
        value_field=value_field,
        label_field=label_field,
        filters=filters,
        aggregate_func=aggregate_func,
    )
    # print choice_facet

    numeric_facet = habitat_services.get_numeric_facet(
        grouping_field={"id": "z", "transform": "-1 * {field}"},
        value_field={"id": "area"},
        filters=[{"field": "habitat_type.substrate.id", "op": "in", "value": ["S1"]}],
    )

    totals = habitat_services.get_totals(value_field="area", filters=[])
    # print totals

    # Test aggregates.
    value_fields = [{"id": "area", "aggregate_funcs": ["sum"]}]

    grouping_fields = [
        # {'id': "habitat_type.substrate.id", 'label': 'substrate_id', 'label_field': {'id': 'habitat_type.substrate.name'}, 'all_values': True},
        {"id": "z", "transform": "-1 * {field}", "as_histogram": True, "all_values": True}
    ]
    filters = []
    aggregates = habitat_services.get_aggregates(
        value_fields=value_fields, grouping_fields=grouping_fields, filters=filters
    )
    # print aggregates

    # Test aggregates w/ unfiltered.
    uf_aggregates = habitat_services.get_aggregates(
        value_fields=value_fields,
        grouping_fields=grouping_fields,
        filters=[{"field": "habitat_type.substrate.id", "op": "in", "value": ["S1"]}],
        with_unfiltered=True,
    )
Example #2
0
def get_choice_facet():

    id_field = request.args.get("ID_FIELD", "")
    label_field = request.args.get("LABEL_FIELD", "")
    value_field = request.args.get("VALUE_FIELD", "")
    aggregate_func = request.args.get("AGGREGATE_FUNC", "sum")

    filters_json = request.args.get("FILTERS", "[]")
    if filters_json:
        filters = json.loads(filters_json)
    else:
        filters = []

    facet = habitat_services.get_choice_facet(
        id_field=id_field,
        value_field=value_field,
        label_field=label_field,
        filters=filters,
        aggregate_func=aggregate_func,
    )

    return Response(json.dumps(facet, indent=2), mimetype="application/json")