Skip to content

Commit

Permalink
Merge pull request #3533 from Vizzuality/fix/staging-analysis
Browse files Browse the repository at this point in the history
Update staging api endpoints
  • Loading branch information
edbrett committed Aug 2, 2018
2 parents 9e95552 + 3771cfd commit b913a12
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 107 deletions.
4 changes: 2 additions & 2 deletions app/assets/javascripts/map/services/AnalysisNewService.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ define(

var GET_REQUEST_ID = 'AnalysisService:get';

var APIURL = 'https://production-api.globalforestwatch.org';
var APIURLV2 = 'https://production-api.globalforestwatch.org' + '/v3';
var APIURLV2 = window.gfw.config.GFW_API + '/v3';
var APIURL = window.gfw.config.GFW_API;

var APIURLS = {
draw: '/{dataset}{?geostore,period,thresh,gladConfirmOnly}',
Expand Down
118 changes: 54 additions & 64 deletions app/assets/javascripts/map/services/CountryService.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,100 @@
/**
* CountryService provides access to information about countries.
*/
define([
'Class',
'uri',
'bluebird',
'map/services/DataService'
], function (Class, UriTemplate, Promise, ds) {

/* eslint-disable */
define(['Class', 'uri', 'bluebird', 'map/services/DataService'], function(
Class,
UriTemplate,
Promise,
ds
) {
'use strict';

var GET_REQUEST_ID = 'CountryService:get',
SHOW_REQUEST_ID = 'CountryService:show';
SHOW_REQUEST_ID = 'CountryService:show';

var URL = window.gfw.config.GFW_API_OLD + '/countries/{id}';


var CountryService = Class.extend({

get: function() {
return new Promise(function(resolve, reject) {

var url = new UriTemplate(URL).fillFromObject({});

ds.define(GET_REQUEST_ID, {
cache: {type: 'persist', duration: 1, unit: 'days'},
cache: { type: 'persist', duration: 1, unit: 'days' },
// cache: false,
url: url,
type: 'GET',
dataType: 'json',
contentType: 'application/json; charset=utf-8',

decoder: function ( data, status, xhr, success, error ) {
if ( status === "success" ) {
data.countries = _.filter(data.countries, function(country){
return country.iso !== null && country.enabled == true
decoder: function(data, status, xhr, success, error) {
if (status === 'success') {
data.countries = _.filter(data.countries, function(country) {
return country.iso !== null && country.enabled == true;
});
success( data, xhr );
} else if ( status === "fail" || status === "error" ) {
error( JSON.parse(xhr.responseText) );
} else if ( status === "abort") {

success(data, xhr);
} else if (status === 'fail' || status === 'error') {
error(JSON.parse(xhr.responseText));
} else if (status === 'abort') {
} else {
error( JSON.parse(xhr.responseText) );
error(JSON.parse(xhr.responseText));
}
}

});

var requestConfig = {
resourceId: GET_REQUEST_ID,
success: function(data, status) {
resolve(data,status);
resolve(data, status);
},
error: function(errors) {
reject(errors);
}
};

ds.request(requestConfig);

});
},

show: function(id) {
return new Promise(function(resolve, reject) {

var url = new UriTemplate(URL).fillFromObject({id: id});

ds.define(SHOW_REQUEST_ID, {
cache: {type: 'persist', duration: 1, unit: 'days'},
url: url,
type: 'GET',
dataType: 'json',
contentType: 'application/json; charset=utf-8',

// TO-DO We should move this to the DataService
decoder: function ( data, status, xhr, success, error ) {
if ( status === "success" ) {
success( data, xhr );
} else if ( status === "fail" || status === "error" ) {
error( JSON.parse(xhr.responseText) );
} else if ( status === "abort") {

} else {
error( JSON.parse(xhr.responseText) );
return new Promise(
function(resolve, reject) {
var url = new UriTemplate(URL).fillFromObject({ id: id });
ds.define(SHOW_REQUEST_ID, {
cache: { type: 'persist', duration: 1, unit: 'days' },
url: url,
type: 'GET',
dataType: 'json',
contentType: 'application/json; charset=utf-8',

// TO-DO We should move this to the DataService
decoder: function(data, status, xhr, success, error) {
if (status === 'success') {
success(data, xhr);
} else if (status === 'fail' || status === 'error') {
error(JSON.parse(xhr.responseText));
} else if (status === 'abort') {
} else {
error(JSON.parse(xhr.responseText));
}
}
}

});

var requestConfig = {
resourceId: SHOW_REQUEST_ID,
success: function(data, status) {
resolve(data,status);
},
error: function(errors) {
reject(errors);
}
};

ds.request(requestConfig);

}.bind(this));
},
});

var requestConfig = {
resourceId: SHOW_REQUEST_ID,
success: function(data, status) {
resolve(data, status);
},
error: function(errors) {
reject(errors);
}
};

ds.request(requestConfig);
}.bind(this)
);
}
});

var service = new CountryService();
Expand Down
30 changes: 11 additions & 19 deletions app/assets/javascripts/map/services/GeostoreService.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
define([
'Class',
'uri',
'bluebird',
'map/services/DataService'
], function(Class, UriTemplate, Promise, ds) {

/* eslint-disable */
define(['Class', 'uri', 'bluebird', 'map/services/DataService'], function(
Class,
UriTemplate,
Promise,
ds
) {
'use strict';

var GET_REQUEST_ID = 'GeostoreService:get',
SAVE_REQUEST_ID = 'GeostoreService:save';
SAVE_REQUEST_ID = 'GeostoreService:save';

var URL = 'https://production-api.globalforestwatch.org' + '/geostore/{id}';
var URL = window.gfw.config.GFW_API;
+'/geostore/{id}';

var GeostoreService = Class.extend({

get: function(id) {
return new Promise(function(resolve, reject) {

var url = new UriTemplate(URL).fillFromObject({id: id});
var url = new UriTemplate(URL).fillFromObject({ id: id });

ds.define(GET_REQUEST_ID, {
cache: false,
Expand All @@ -31,13 +30,11 @@ define([
};

ds.request(requestConfig);

});
},

save: function(geojson) {
return new Promise(function(resolve, reject) {

var url = new UriTemplate(URL).fillFromObject({});

ds.define(SAVE_REQUEST_ID, {
Expand All @@ -60,13 +57,11 @@ define([
};

ds.request(requestConfig);

});
},

use: function(provider) {
return new Promise(function(resolve, reject) {

var url = new UriTemplate(URL).fillFromObject({});

ds.define(SAVE_REQUEST_ID, {
Expand All @@ -89,12 +84,9 @@ define([
};

ds.request(requestConfig);

});
}

});

return new GeostoreService();

});
40 changes: 18 additions & 22 deletions app/assets/javascripts/services/CountryService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ define(['Class', 'uri', 'bluebird', 'map/services/DataService'], function(
'use strict';

var CONFIG = {
countriesConfigDataset: '134caa0a-21f7-451d-a7fe-30db31a424aa',
countriesConfigTable: 'gfw_countries_config',
countriesDataset: 'a8dc9474-ba42-4ae3-a7d3-d8df5f1e78df',
countriesTable: 'gadm36_countries',
regionsDataset: '8f22dec5-2aea-49d6-8a7b-c494dbb8095c',
regionsTable: 'gadm36_adm1',
subRegionsDataset: '7cc6ac21-c8ef-4dd8-a181-8967721a15a4',
subRegionsTable: 'gadm36_adm2'
};

Expand All @@ -26,20 +22,19 @@ define(['Class', 'uri', 'bluebird', 'map/services/DataService'], function(
GET_REQUEST_SUBREGIONS_LIST_ID = 'CountryService:getSubRegionsList',
SHOW_REQUEST_SUBREGION_ID = 'CountryService:showSubRegion';

var APIURL = window.gfw.config.GFW_API;
var CARTO_API = window.gfw.config.CARTO_API;

var APIURLS = {
getCountryConfig:
"/query/{countriesConfigDataset}?sql=SELECT iso, indepth FROM {countriesConfigTable} WHERE iso='{iso}' AND iso != 'XCA' AND iso != 'TWN'",
"/sql?q=SELECT iso, indepth FROM {countriesConfigTable} WHERE iso='{iso}' AND iso != 'XCA' AND iso != 'TWN'",
getCountriesList:
"/query/{countriesDataset}?sql=SELECT name_engli as name, iso FROM {countriesTable} WHERE iso != 'XCA' AND iso != 'TWN' ORDER BY name",
"/sql?q=SELECT name_engli as name, iso FROM {countriesTable} WHERE iso != 'XCA' AND iso != 'TWN' ORDER BY name",
showCountry:
"/query/{countriesDataset}?sql=SELECT name_engli as name, iso, ST_AsGeoJSON(ST_Simplify(the_geom,0.1)) AS geojson FROM {countriesTable} WHERE iso='{iso}'",
"/sql?q=SELECT name_engli as name, iso, ST_AsGeoJSON(ST_Simplify(the_geom,0.1)) AS geojson FROM {countriesTable} WHERE iso='{iso}'",
getRegionsList:
"/query/{regionsDataset}?sql=SELECT cartodb_id, iso, bbox as bounds, gid_1 as id_1, name_1 FROM {regionsTable} WHERE iso='{iso}' AND iso != 'XCA' AND iso != 'TWN' ORDER BY name_1",
"/sql?q=SELECT cartodb_id, iso, bbox as bounds, gid_1 as id_1, name_1 FROM {regionsTable} WHERE iso='{iso}' AND iso != 'XCA' AND iso != 'TWN' ORDER BY name_1",
showRegion:
"/query/{regionsDataset}?sql=SELECT gid_1 as id_1, name_1, ST_AsGeoJSON(the_geom) AS geojson FROM {regionsTable} WHERE iso='{iso}' AND gid_1='{region}' ORDER BY name_1",
"/sql?q=SELECT gid_1 as id_1, name_1, ST_AsGeoJSON(the_geom) AS geojson FROM {regionsTable} WHERE iso='{iso}' AND gid_1='{region}' ORDER BY name_1",
getSubRegionsList:
"/sql?q=SELECT gid_2 as id, name_2 as name FROM gadm36_adm2 WHERE iso = '{iso}' AND iso != 'XCA' AND iso != 'TWN' AND gid_1 = '{region}' ORDER BY name",
showSubRegion:
Expand Down Expand Up @@ -70,15 +65,15 @@ define(['Class', 'uri', 'bluebird', 'map/services/DataService'], function(
function(resolve, reject) {
var status = _.extend({}, CONFIG, params);
var url = new UriTemplate(
APIURL + APIURLS.getCountryConfig
CARTO_API + APIURLS.getCountryConfig
).fillFromObject(status);

this.defineRequest(datasetId, url);

var requestConfig = {
resourceId: datasetId,
success: function(res, status) {
resolve(res.data, status);
resolve(res.rows, status);
},
error: function(errors) {
reject(errors);
Expand All @@ -94,15 +89,15 @@ define(['Class', 'uri', 'bluebird', 'map/services/DataService'], function(
return new Promise(
function(resolve, reject) {
var url = new UriTemplate(
APIURL + APIURLS.getCountriesList
CARTO_API + APIURLS.getCountriesList
).fillFromObject(CONFIG);

this.defineRequest(GET_REQUEST_COUNTRIES_LIST_ID, url);

var requestConfig = {
resourceId: GET_REQUEST_COUNTRIES_LIST_ID,
success: function(res, status) {
resolve(res.data, status);
resolve(res.rows, status);
},
error: function(errors) {
reject(errors);
Expand All @@ -126,15 +121,15 @@ define(['Class', 'uri', 'bluebird', 'map/services/DataService'], function(
function(countryConfig) {
var status = _.extend({}, CONFIG, params);
var url = new UriTemplate(
APIURL + APIURLS.showCountry
CARTO_API + APIURLS.showCountry
).fillFromObject(status);
this.defineRequest(datasetId, url);
var requestConfig = {
resourceId: datasetId,
success: function(res, status) {
var dataCountryConfig =
countryConfig.length >= 0 ? countryConfig[0] : [];
var dataCountry = res.data.length >= 0 ? res.data[0] : [];
var dataCountry = res.rows.length >= 0 ? res.rows[0] : [];
var data = _.extend({}, dataCountry, dataCountryConfig);
resolve(data, status);
},
Expand All @@ -160,15 +155,15 @@ define(['Class', 'uri', 'bluebird', 'map/services/DataService'], function(
function(resolve, reject) {
var status = _.extend({}, CONFIG, params);
var url = new UriTemplate(
APIURL + APIURLS.getRegionsList
CARTO_API + APIURLS.getRegionsList
).fillFromObject(status);

this.defineRequest(GET_REQUEST_REGIONS_LIST_ID, url);

var requestConfig = {
resourceId: GET_REQUEST_REGIONS_LIST_ID,
success: function(res, status) {
var data = res.data.map(d => {
var data = res.rows.map(d => {
var ids = parseGadm36Id(d.id_1);
return {
iso: d.iso,
Expand Down Expand Up @@ -200,15 +195,16 @@ define(['Class', 'uri', 'bluebird', 'map/services/DataService'], function(
var status = _.extend({}, CONFIG, params, {
region: buildGadm36Id(params.iso, params.region)
});
var url = new UriTemplate(APIURL + APIURLS.showRegion).fillFromObject(
status
);
var url = new UriTemplate(
CARTO_API + APIURLS.showRegion
).fillFromObject(status);
this.defineRequest(datasetId, url);

var requestConfig = {
resourceId: datasetId,
success: function(res, status) {
var data = res.data.length >= 0 ? res.data[0] : [];
var data = res.rows.length >= 0 ? res.rows[0] : [];
console.log(data);
resolve(data, status);
},
error: function(errors) {
Expand Down

0 comments on commit b913a12

Please sign in to comment.