Skip to content

Commit

Permalink
[x]: Removed API call to bigquery for filters, fetching filter from r…
Browse files Browse the repository at this point in the history
…esource object
  • Loading branch information
shubham-mahajan committed Mar 24, 2021
1 parent 4187c14 commit 014c9e5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
19 changes: 15 additions & 4 deletions ckanext/nhs/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ckan.common import config, is_flask_request, c, request
from ckan.plugins import toolkit
import logging
import ast
log = logging.getLogger(__name__)

def _get_action(action, context_dict, data_dict):
Expand Down Expand Up @@ -64,7 +65,7 @@ def get_resource_data_dictionary(pkg_dict):
try:
if not pkg_dict:
return []
tableschema = pkg_dict.get('schema', [])
tableschema = pkg_dict.get('schema', [])
if tableschema:
try:
return json.loads(tableschema)['fields']
Expand All @@ -74,7 +75,17 @@ def get_resource_data_dictionary(pkg_dict):
return []
except Exception as ex:
log.error("get_resource_data_dictionary - {}".format(str(ex)))



def resource_convert_schema(schema):
try:
result = ast.literal_eval(schema)
fields = [field['name'] for field in result.get('fields', [])]
log.info("fields: {}".format(fields))
return sorted(fields)
except Exception as ex:
log.error("Error converting schema to list - {}".format(str(ex)))
return []

def resource_view_get_fields(resource):
'''Returns sorted list of text and time fields of a datastore resource.'''
Expand All @@ -84,7 +95,7 @@ def resource_view_get_fields(resource):
'limit': 0
}
log.warning("resource_view_get_fields - data: {}".format(data))

result = logic.get_action('datastore_search')({}, data)

fields = [field['id'] for field in result.get('fields', [])]
Expand Down Expand Up @@ -124,7 +135,7 @@ def get_latest_resources():
resources = _get_action('resource_search', context, data_dict)['results']

from operator import itemgetter
resources_sorted = sorted(resources, key=itemgetter('last_modified','created'), reverse=True)
resources_sorted = sorted(resources, key=itemgetter('last_modified','created'), reverse=True)

return resources_sorted[:5]

Expand Down
5 changes: 3 additions & 2 deletions ckanext/nhs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def get_helpers(self):
'get_latest_resources': helpers.get_latest_resources,
'get_cookie_control_config': helpers.get_cookie_control_config,
'get_googleanalytics_config': helpers.get_googleanalytics_config,
'resource_view_get_fields' : helpers.resource_view_get_fields
'resource_view_get_fields' : helpers.resource_view_get_fields,
'resource_convert_schema' : helpers.resource_convert_schema,
}

# IRoutes
Expand Down Expand Up @@ -167,4 +168,4 @@ def update_config(self, config_):

def configure(self, config_):
self.config = config_
self.backend.configure(config_)
self.backend.configure(config_)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
class="resource-view-filters"
data-module="resource-view-filters-override"
data-module-resource-id="{{ resource['bq_table_name'] }}"
data-module-fields="{{ h.dump_json(h.resource_view_get_fields(resource)) }}"
data-module-fields="{{ h.dump_json(h.resource_convert_schema(resource['schema'])) }}"
>
</div>

0 comments on commit 014c9e5

Please sign in to comment.