Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
Fix excluded countries in historical availability
Browse files Browse the repository at this point in the history
  • Loading branch information
brew committed Jun 26, 2017
1 parent 6f5d371 commit b994f73
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
32 changes: 26 additions & 6 deletions explorer/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,31 @@ const COUNTRY_EXCLUDE_LIST = [
const loadDataset = function () {
assert(_EXPLORER_DATASET !== null, 'Failed to load dataset.')

const pruneCountry = function (countryList, excluded, year) {
// Store excluded countries
_EXPLORER_DATASET.excluded_country = []
_EXPLORER_DATASET.excluded_country_old = []

const pruneCountry = function (countryList, excludedCountryList, excluded, year) {
/*
Removes `db_${year}` from the passed `excluded` country. If no
`db_${year}`s are left, remove the country.
Remove `db_${year}` from the passed `excluded` country. If no `db_${year}`
properties are left, remove the country. Excluded country data is added to
an `_excluded_country[_old]` array, so pages have access to it if necessary
(e.g. availabilityHistorical.js uses it).
*/

const country = _.find(countryList, c => c.alpha2 === excluded)
delete country[`db_${year}`]
if (country[`db_${year}`] !== undefined) {
let excludedCountry = _.find(excludedCountryList, c => c.alpha2 === excluded)
if (!excludedCountry) {
excludedCountry = {
alpha2: country.alpha2
, name: country.name
}
excludedCountryList.push(excludedCountry)
}
excludedCountry[`db_${year}`] = _.clone(country[`db_${year}`])
delete country[`db_${year}`]
}

const countryKeys = _.keys(country)
if (!_.some(countryKeys, k => _s.startsWith(k, 'db_'))) {
Expand All @@ -62,9 +80,11 @@ const loadDataset = function () {
// Remove excluded country/years from `country_old`
_.each(COUNTRY_EXCLUDE_LIST, excluded => {
_EXPLORER_DATASET.country
= pruneCountry(_EXPLORER_DATASET.country, excluded[0], excluded[1])
= pruneCountry(_EXPLORER_DATASET.country, _EXPLORER_DATASET.excluded_country,
excluded[0], excluded[1])
_EXPLORER_DATASET.country_old
= pruneCountry(_EXPLORER_DATASET.country_old, excluded[0], excluded[1])
= pruneCountry(_EXPLORER_DATASET.country_old, _EXPLORER_DATASET.excluded_country_old,
excluded[0], excluded[1])
})

// 2015 survey dataset
Expand Down
22 changes: 18 additions & 4 deletions explorer/views/page/availabilityHistorical.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ class ProjectPage extends Backbone.View {
this._repaint()
}

_findScore(countries, country, year) {
return _.find(countries, x => (x.alpha2 === country))[`db_${year}`].roundobi
_findScore(countries, excludedCountries, country, year) {
/*
Find the score for the country/year in either `countries` or `excludedCountries`.
*/
try {
return _.find(countries, x => (x.alpha2 === country))[`db_${year}`].roundobi
} catch (err) {
return _.find(excludedCountries, x => (x.alpha2 === country))[`db_${year}`].roundobi
}
}

_repaint(dataset = reportGenerator.dataset,
Expand All @@ -70,25 +77,31 @@ class ProjectPage extends Backbone.View {
let datasetRegions
let datasetAv
let countries
let countriesExcluded
let datasetAvCompare
let countriesCompare
let countriesCompareExcluded
if (this.year !== '2015') {
datasetRegions = _EXPLORER_DATASET.regions_old
datasetAv = _EXPLORER_DATASET.availability_old
countries = _EXPLORER_DATASET.country_old
countriesExcluded = _EXPLORER_DATASET.excluded_country_old
} else {
datasetRegions = _EXPLORER_DATASET.regions
datasetAv = _EXPLORER_DATASET.availability
countries = _EXPLORER_DATASET.country
countriesExcluded = _EXPLORER_DATASET.excluded_country
}

if (compareYear) {
if (compareYear !== '2015') {
datasetAvCompare = _EXPLORER_DATASET.availability_old
countriesCompare = _EXPLORER_DATASET.country_old
countriesCompareExcluded = _EXPLORER_DATASET.excluded_country_old
} else {
datasetAvCompare = _EXPLORER_DATASET.availability
countriesCompare = _EXPLORER_DATASET.country
countriesCompareExcluded = _EXPLORER_DATASET.excluded_country
}
}

Expand All @@ -115,13 +128,14 @@ class ProjectPage extends Backbone.View {
)
if (comparisonRow) {
compareObj = comparisonRow[comparisonYearKey]
compareObj.score = this._findScore(countriesCompare, country, compareYear)
compareObj.score = this._findScore(countriesCompare, countriesCompareExcluded,
country, compareYear)
}
}

const context = {}
context.obj = row[yearKey]
context.obj.score = this._findScore(countries, country, this.year)
context.obj.score = this._findScore(countries, countriesExcluded, country, this.year)
context.obj.year = this.year
if (compareObj) {
context.compareObj = compareObj
Expand Down

0 comments on commit b994f73

Please sign in to comment.