Skip to content

Commit

Permalink
Merge pull request #2198 from WikiWatershed/tt/bigcz-filter-cuahsi-re…
Browse files Browse the repository at this point in the history
…sults

BiG-CZ Add Search Option Support, Filter CUAHSI Results

Connects #2133
  • Loading branch information
rajadain committed Aug 30, 2017
2 parents fb09659 + a7222e3 commit fc39a91
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 13 deletions.
35 changes: 31 additions & 4 deletions src/mmw/apps/bigcz/clients/cuahsi/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,35 @@
DATE_MAX = date(2100, 1, 1)
DATE_FORMAT = '%m/%d/%Y'

GRIDDED = [
'NWS-WGRFC_Hourly_MPE',
'NWS_WGRFC_Daily_MPE_Recent_Values',
'LMRFC_Data',
'GLDAS_NOAH',
'NLDAS_FORA',
'NLDAS_NOAH',
'TRMM_3B42_7',
]

client = Client(CATALOG_URL, timeout=settings.BIGCZ_CLIENT_TIMEOUT)


def filter_networkIDs(services, gridded=False):
"""
Transforms list of services to list of ServiceIDs, with respect to
given options.
If gridded=False, then GRIDDED services will not be included.
If no filters apply, we return an empty list to disable filtering.
"""
if not gridded:
return [str(s.ServiceID) for s in services
if s.NetworkName not in GRIDDED]

return []


def parse_geom(record):
lat = record['latitude']
lng = record['longitude']
Expand Down Expand Up @@ -173,7 +198,7 @@ def get_services_in_box(box):
return []


def get_series_catalog_in_box(box, from_date, to_date):
def get_series_catalog_in_box(box, from_date, to_date, networkIDs):
from_date = from_date or DATE_MIN
to_date = to_date or DATE_MAX

Expand All @@ -183,7 +208,7 @@ def get_series_catalog_in_box(box, from_date, to_date):
ymin=box.ymin,
ymax=box.ymax,
conceptKeyword='',
networkIDs='',
networkIDs=','.join(networkIDs),
beginDate=from_date.strftime(DATE_FORMAT),
endDate=to_date.strftime(DATE_FORMAT))

Expand All @@ -201,6 +226,7 @@ def search(**kwargs):
bbox = kwargs.get('bbox')
to_date = kwargs.get('to_date')
from_date = kwargs.get('from_date')
gridded = 'gridded' in kwargs.get('options')

if not bbox:
raise ValidationError({
Expand All @@ -209,9 +235,10 @@ def search(**kwargs):
box = BBox(bbox)
world = BBox('-180,-90,180,90')

series = get_series_catalog_in_box(box, from_date, to_date)
series = group_series_by_location(series)
services = get_services_in_box(world)
networkIDs = filter_networkIDs(services, gridded)
series = get_series_catalog_in_box(box, from_date, to_date, networkIDs)
series = group_series_by_location(series)
results = sorted(parse_records(series, services),
key=attrgetter('end_date'),
reverse=True)
Expand Down
1 change: 1 addition & 0 deletions src/mmw/apps/bigcz/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _do_search(request):
'to_date': parse_date(request.query_params.get('to_date')),
'from_date': parse_date(request.query_params.get('from_date')),
'bbox': request.query_params.get('bbox'),
'options': request.query_params.get('options', ''),
'page': page,
}

Expand Down
3 changes: 2 additions & 1 deletion src/mmw/js/src/data_catalog/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ var DataCatalogController = {
new models.Catalog({
id: 'cuahsi',
name: 'WDC',
description: 'Optional catalog description here...',
has_filters: true,
options: new models.SearchOptions([{ id: 'gridded' }]),
is_pageable: false,
results: new models.Results(null, { catalog: 'cuahsi' }),
})
Expand Down
38 changes: 35 additions & 3 deletions src/mmw/js/src/data_catalog/models.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var $ = require('jquery'),
_ = require('underscore'),
_ = require('lodash'),
Backbone = require('../../shim/backbone'),
turfIntersect = require('turf-intersect'),
App = require('../app'),
Expand All @@ -11,6 +11,17 @@ var REQUEST_TIMED_OUT_CODE = 408;
var DESCRIPTION_MAX_LENGTH = 100;
var PAGE_SIZE = settings.get('data_catalog_page_size');

var SearchOption = Backbone.Model.extend({
defaults: {
id: '',
active: false,
},
});

var SearchOptions = Backbone.Collection.extend({
model: SearchOption,
});

var Catalog = Backbone.Model.extend({
defaults: {
id: '',
Expand All @@ -24,6 +35,8 @@ var Catalog = Backbone.Model.extend({
active: false,
results: null, // Results collection
resultCount: 0,
has_filters: false, // If local filters apply
options: null, // SearchOptions collection
is_pageable: true,
page: 1,
error: '',
Expand All @@ -35,6 +48,14 @@ var Catalog = Backbone.Model.extend({
this.get('results').on('change:show_detail', function() {
self.set('detail_result', self.get('results').getDetail());
});

// Initialize and listen to options for changes
if (this.get('options') === null) {
this.set({ options: new SearchOptions() });
}
this.get('options').on('change:active', function() {
self.startSearch(1);
});
},

searchIfNeeded: function(query, fromDate, toDate, bbox) {
Expand Down Expand Up @@ -76,6 +97,9 @@ var Catalog = Backbone.Model.extend({
startSearch: function(page) {
var lastPage = Math.ceil(this.get('resultCount') / PAGE_SIZE),
thisPage = parseInt(page) || 1,
has_filters = this.get('has_filters'),
options = this.get('options'),
id = function(option) { return option.id; },
data = {
catalog: this.id,
query: this.get('query'),
Expand All @@ -88,6 +112,12 @@ var Catalog = Backbone.Model.extend({
_.assign(data, { page: thisPage });
}

if (has_filters && options) {
_.assign(data, {
options: options.where({ active: true }).map(id).join(',')
});
}

this.set('loading', true);
this.set('error', false);

Expand Down Expand Up @@ -160,8 +190,8 @@ var Result = Backbone.Model.extend({
description: '',
geom: null, // GeoJSON
links: null, // Array
created: '',
updated: '',
created_at: '',
updated_at: '',
show_detail: false // Show this result as the detail view?
},

Expand Down Expand Up @@ -260,6 +290,8 @@ var SearchForm = Backbone.Model.extend({
});

module.exports = {
SearchOption: SearchOption,
SearchOptions: SearchOptions,
Catalog: Catalog,
Catalogs: Catalogs,
Result: Result,
Expand Down
13 changes: 13 additions & 0 deletions src/mmw/js/src/data_catalog/templates/tabContent.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
{% if has_filters %}
<div class="filters-region">
{% if id == 'cuahsi' %}
<div class="checkbox">
<label>
<input name="gridded" type="checkbox" id="gridded">
Include Gridded
</label>
</div>
{% endif %}
</div>
{% endif %}

{% if description %}
<div class="catalog-description">
{{ description }}
Expand Down
23 changes: 18 additions & 5 deletions src/mmw/js/src/data_catalog/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,22 +331,35 @@ var TabContentView = Marionette.LayoutView.extend({
},

onShow: function() {
var model = this.model;

this.toggleActiveClass();

this.resultRegion.show(new ResultsView({
collection: this.model.get('results'),
catalog: this.model.id,
collection: model.get('results'),
catalog: model.id,
}));

this.errorRegion.show(new ErrorView({
model: this.model,
model: model,
}));

if (this.model.get('is_pageable')) {
if (model.get('is_pageable')) {
this.pagerRegion.show(new PagerView({
model: this.model,
model: model,
}));
}

if (model.get('has_filters')) {
// TODO Generalize for different types of filters
if (model.id === 'cuahsi') {
$('input#gridded').change(function() {
model.get('options')
.findWhere({ id: 'gridded' })
.set({ active: this.checked });
});
}
}
},

toggleActiveClass: function() {
Expand Down
4 changes: 4 additions & 0 deletions src/mmw/sass/pages/_data-catalog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
}
}

.filters-region {
padding: 6px 10px 0 10px;
}

.result-region {
height: auto !important;
width: auto !important;
Expand Down

0 comments on commit fc39a91

Please sign in to comment.