Skip to content

Action

datastore_query_extent(data_dict)

Performs a basic query extent analysis on one resource with parameters and return value compatible with CKAN's datastore.

Just calls vds_basic_extent.

Source code in ckanext/versioned_datastore/logic/basic/action.py
123
124
125
126
127
128
129
130
131
@action(schema.vds_basic_query(), helptext.vds_basic_extent, get=True)
def datastore_query_extent(data_dict: dict):
    """
    Performs a basic query extent analysis on one resource with parameters and return
    value compatible with CKAN's datastore.

    Just calls vds_basic_extent.
    """
    return vds_basic_extent(data_dict)

Performs a basic query on one resource with parameters and return value compatible with CKAN's datastore.

Just calls vds_basic_query.

Source code in ckanext/versioned_datastore/logic/basic/action.py
17
18
19
20
21
22
23
24
25
26
27
28
29
@action(schema.vds_basic_query(), helptext.vds_basic_query, get=True)
def datastore_search(
    data_dict: dict,
    resource_id: str,
    run_query: Optional[bool] = True,
):
    """
    Performs a basic query on one resource with parameters and return value compatible
    with CKAN's datastore.

    Just calls vds_basic_query.
    """
    return vds_basic_query(data_dict, resource_id, run_query)

vds_basic_autocomplete(data_dict, field, term)

Provides autocomplete for values on the given field in the given resource under the given query and version restrictions. Use after and limit to paginate through results.

Parameters:

Name Type Description Default
data_dict dict

the data dict passed to this action

required
field str

the field to autocomplete values on

required
term str

a prefix to filter field values by

required

Returns:

Type Description

a dict containing the values and optionally an after key for pagination

Source code in ckanext/versioned_datastore/logic/basic/action.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@action(schema.vds_basic_autocomplete(), helptext.vds_basic_autocomplete, get=True)
def vds_basic_autocomplete(data_dict: dict, field: str, term: str):
    """
    Provides autocomplete for values on the given field in the given resource under the
    given query and version restrictions. Use after and limit to paginate through
    results.

    :param data_dict: the data dict passed to this action
    :param field: the field to autocomplete values on
    :param term: a prefix to filter field values by
    :returns: a dict containing the values and optionally an after key for pagination
    """
    # extract the limit but default to a size of 20 if it's not present
    size = data_dict.pop('limit', 20)
    if not 0 < size <= 20:
        raise toolkit.ValidationError('Size must be 0 < size <= 20')
    # grab the after as we need to use it for the agg, not the query
    after = data_dict.pop('after', None)

    request = make_request(data_dict)
    request.set_no_results()
    # add the autocomplete part of the query
    request.extra_filter &= Q('prefix', **{keyword(field): term})

    # add the aggregation which gets the field values
    agg_options = dict(field=keyword(field), order='asc')
    if after is not None:
        agg_options['after'] = {field: after}
    request.add_agg(
        'field_values',
        'composite',
        size=size,
        sources={field: A('terms', **agg_options)},
    )

    response = request.run()
    agg_result = response.aggs['field_values']
    return_dict = {
        'values': [bucket['key'][field] for bucket in agg_result['buckets']],
    }
    if 'after_key' in agg_result:
        return_dict['after'] = agg_result['after_key'][field]
    return return_dict

vds_basic_count(data_dict)

Counts the number of records that meet the given query on the resource with the given ID and returns this number.

Parameters:

Name Type Description Default
data_dict dict

the data dict passed to this action

required

Returns:

Type Description

an integer >= 0

Source code in ckanext/versioned_datastore/logic/basic/action.py
62
63
64
65
66
67
68
69
70
71
72
73
74
@action(schema.vds_basic_count(), helptext.vds_basic_query, get=True)
def vds_basic_count(data_dict: dict):
    """
    Counts the number of records that meet the given query on the resource with the
    given ID and returns this number.

    :param data_dict: the data dict passed to this action
    :returns: an integer >= 0
    """
    request = make_request(data_dict)
    request.set_no_results()
    response = request.run()
    return response.count

vds_basic_extent(data_dict)

Calculates the geographic extent of the given resource using the given query. Returns a dict containing the overall number of records which matched the query, the number of records with geo data that matched the query and then the bounds as a pair of coordinates (top left, bottom right).

Parameters:

Name Type Description Default
data_dict dict

the data dict passed to this action

required

Returns:

Type Description

a dict

Source code in ckanext/versioned_datastore/logic/basic/action.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@action(schema.vds_basic_extent(), helptext.vds_basic_extent, get=True)
def vds_basic_extent(data_dict: dict):
    """
    Calculates the geographic extent of the given resource using the given query.
    Returns a dict containing the overall number of records which matched the query, the
    number of records with geo data that matched the query and then the bounds as a pair
    of coordinates (top left, bottom right).

    :param data_dict: the data dict passed to this action
    :returns: a dict
    """
    request = make_request(data_dict)
    request.set_no_results()

    # get the total number of records first before we do anything else
    total = request.run().count

    # add a filter to get only records with geo data
    request.extra_filter &= Q('exists', field=ALL_POINTS)
    # add our bounds aggregation
    request.add_agg('bounds', 'geo_bounds', field=ALL_POINTS, wrap_longitude=False)

    response = request.run()

    result = {
        'total_count': total,
        'geom_count': response.count,
    }
    # extract and add the bounds info from the aggregations if there is any
    if response.count > 0:
        top_left = response.aggs['bounds']['bounds']['top_left']
        bottom_right = response.aggs['bounds']['bounds']['bottom_right']
        result['bounds'] = [[p['lat'], p['lon']] for p in (top_left, bottom_right)]

    return result

vds_basic_query(data_dict, resource_id, run_query=True)

Performs a basic query on one resource using a simple query language which is in line with CKAN's default datastore extension.

Parameters:

Name Type Description Default
data_dict dict

the data dict passed to this action

required
resource_id str

the ID of the resource to search

required
run_query Optional[bool]

whether to run the query, or just return information about what would have been run

True

Returns:

Type Description

a dict

Source code in ckanext/versioned_datastore/logic/basic/action.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@action(schema.vds_basic_query(), helptext.vds_basic_query, get=True)
def vds_basic_query(
    data_dict: dict,
    resource_id: str,
    run_query: Optional[bool] = True,
):
    """
    Performs a basic query on one resource using a simple query language which is in
    line with CKAN's default datastore extension.

    :param data_dict: the data dict passed to this action
    :param resource_id: the ID of the resource to search
    :param run_query: whether to run the query, or just return information about what
        would have been run
    :returns: a dict
    """
    request = make_request(data_dict)
    if not run_query:
        return {'indexes': request.indexes(), 'search': request.to_search().to_dict()}

    response = request.run()
    return {
        'total': response.count,
        'records': response.data,
        'facets': format_facets(response.aggs),
        'fields': get_fields(resource_id, request.query.version),
        'after': response.next_after,
    }