Skip to content

Commit

Permalink
Display "*about* 1000000 records" on the frontend when estimated
Browse files Browse the repository at this point in the history
  • Loading branch information
David Read committed Oct 5, 2018
1 parent 4cad664 commit 7760a56
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
12 changes: 7 additions & 5 deletions ckanext/datastore/logic/action.py
Expand Up @@ -406,12 +406,14 @@ def datastore_search(context, data_dict):
(optional, default: true)
:type include_total: bool
:param total_estimation_threshold: By default, the "total" returned is a
precise count of the query result, which can be computationally
precise count of the query results, which can be computationally
expensive for large results (e.g. >100,000k rows). By setting this
value to non-zero, you can set a threshold estimated total, above
which it doesn't do the count - it just returns the estimated
total, generated by quick sampling. NB Total estimation is not
compatible with filter or distinct options. (optional, default: 0)
value to non-zero, and the estimated total is above this value,
then datastore_search will skip the precise count - it just returns
the estimated total. The estimated total is generated by EXPLAIN,
which is triggered by Express Loader or DataPusher as part of
loading the data. NB Total estimation is not compatible when
specifying 'filters' or 'distinct' options. (optional, default: 0)
:type total_estimation_threshold: int
:param records_format: the format for the records return value:
'objects' (default) list of {fieldname1: value1, ...} dicts,
Expand Down
18 changes: 10 additions & 8 deletions ckanext/reclineview/theme/public/vendor/ckan.js/ckan.js
Expand Up @@ -10,7 +10,7 @@ if (isNodeModule) {
}

(function(my) {
my.Client = function(endpoint, apiKey) {
my.Client = function(endpoint, apiKey) {
this.endpoint = _getEndpoint(endpoint);
this.apiKey = apiKey;
};
Expand Down Expand Up @@ -51,7 +51,8 @@ if (isNodeModule) {
var out = {
total: results.result.total,
fields: fields,
hits: results.result.records
hits: results.result.records,
total_was_estimated: results.result.total_was_estimated
};
cb(null, out);
});
Expand All @@ -67,7 +68,7 @@ if (isNodeModule) {
'bool': 'boolean',
};

//
//
my.jsonTableSchema2CkanTypes = {
'string': 'text',
'number': 'float',
Expand Down Expand Up @@ -128,7 +129,7 @@ if (isNodeModule) {
code: obj.status,
message: obj.responseText
}
cb(err);
cb(err);
}
if (options.headers) {
options.beforeSend = function(req) {
Expand All @@ -147,7 +148,8 @@ if (isNodeModule) {
q: queryObj.q,
filters: {},
limit: queryObj.size || 10,
offset: queryObj.from || 0
offset: queryObj.from || 0,
total_estimation_threshold: 1000
};

if (queryObj.sort && queryObj.sort.length > 0) {
Expand Down Expand Up @@ -188,21 +190,21 @@ if (isNodeModule) {
// This provides connection to the CKAN DataStore (v2)
//
// General notes
//
//
// We need 2 things to make most requests:
//
// 1. CKAN API endpoint
// 2. ID of resource for which request is being made
//
// There are 2 ways to specify this information.
//
// EITHER (checked in order):
// EITHER (checked in order):
//
// * Every dataset must have an id equal to its resource id on the CKAN instance
// * The dataset has an endpoint attribute pointing to the CKAN API endpoint
//
// OR:
//
//
// Set the url attribute of the dataset to point to the Resource on the CKAN instance. The endpoint and id will then be automatically computed.
var recline = recline || {};
recline.Backend = recline.Backend || {};
Expand Down
9 changes: 8 additions & 1 deletion ckanext/reclineview/theme/public/vendor/recline/recline.js
Expand Up @@ -422,6 +422,7 @@ my.Dataset = Backbone.Model.extend({
};
this.facets = new my.FacetList();
this.recordCount = null;
this.recordCountWasEstimated = null;
this.queryState = new my.Query();
this.queryState.bind('change facet:add', function () {
self.query(); // We want to call query() without any arguments.
Expand Down Expand Up @@ -602,6 +603,7 @@ my.Dataset = Backbone.Model.extend({
_handleQueryResult: function(queryResult) {
var self = this;
self.recordCount = queryResult.total;
self.recordCountWasEstimated = queryResult.total_was_estimated;
var docs = _.map(queryResult.hits, function(hit) {
var _doc = new my.Record(hit);
_doc.fields = self.fields;
Expand All @@ -626,6 +628,7 @@ my.Dataset = Backbone.Model.extend({
toTemplateJSON: function() {
var data = this.toJSON();
data.recordCount = this.recordCount;
data.recordCountWasEstimated = this.recordCountWasEstimated;
data.fields = this.fields.toJSON();
return data;
},
Expand Down Expand Up @@ -2619,7 +2622,10 @@ my.MultiView = Backbone.View.extend({
</div> \
</div> \
<div class="recline-results-info"> \
<span class="doc-count">{{recordCount}}</span> records\
{{#recordCountWasEstimated}} \
<span class="doc-count-approx">about</span> \
{{/recordCountWasEstimated}} \
<span class="doc-count">{{recordCount}}</span> records \
</div> \
<div class="menu-right"> \
<div class="btn-group" data-toggle="buttons-checkbox"> \
Expand Down Expand Up @@ -2716,6 +2722,7 @@ my.MultiView = Backbone.View.extend({
this.listenTo(this.model, 'query:done', function() {
self.clearNotifications();
self.$el.find('.doc-count').text(self.model.recordCount || 'Unknown');
self.$el.find('.doc-count-approx').text(self.model.recordCountWasEstimated && 'about' || '');
});
this.listenTo(this.model, 'query:fail', function(error) {
self.clearNotifications();
Expand Down

0 comments on commit 7760a56

Please sign in to comment.