Skip to content

Commit

Permalink
updates contextual layers
Browse files Browse the repository at this point in the history
  • Loading branch information
sorodrigo committed Nov 11, 2019
1 parent 763036b commit fc36280
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 45 deletions.
46 changes: 33 additions & 13 deletions frontend/gis/cartodb/named-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ const getCartoUrl = endpoint =>

const spinner = ora();

function readEnv() {
const envFlag = process.argv[2];
if (!envFlag || !envFlag.includes('--env=')) {
throw new Error('You need to define --env');
}
const env = envFlag.split('--env=')[1];
const typeFlag = process.argv[3];
const type = (typeFlag && typeFlag.split('--type=')[1]) || DEFAULT_TYPE;
return { env, type };
}

function classifyTemplates(templates) {
const { env } = readEnv();
spinner.start('Fetching existing templates');
const getTemplates = (existingTemplates = []) => {
const envTemplates = existingTemplates.filter(template => template.endsWith(`__${env}`));
const result = templates.reduce(
(acc, next) => {
const exists = existingTemplates.find(t => next.name === t);
const exists = envTemplates.find(t => next.name === t);
return {
...acc,
toUpdate: exists ? [...acc.toUpdate, next] : acc.toUpdate,
Expand All @@ -26,6 +39,8 @@ function classifyTemplates(templates) {
},
{ toUpdate: [], toCreate: [] }
);
const toKeep = [...result.toUpdate, result.toCreate].map(i => i.name);
result.toRemove = envTemplates.filter(template => !toKeep.includes(template));
spinner.succeed();
return result;
};
Expand All @@ -36,17 +51,6 @@ function classifyTemplates(templates) {
.catch(e => console.error(e));
}

function readEnv() {
const envFlag = process.argv[2];
if (!envFlag || !envFlag.includes('--env=')) {
throw new Error('You need to define --env');
}
const env = envFlag.split('--env=')[1];
const typeFlag = process.argv[3];
const type = (typeFlag && typeFlag.split('--type=')[1]) || DEFAULT_TYPE;
return { env, type };
}

function getTemplatesByEnvironment() {
const { env, type } = readEnv();
spinner.start(`Reading templates from file system env: ${env} type: ${type}`);
Expand Down Expand Up @@ -104,6 +108,21 @@ function instanciate(templates) {
return Promise.all(updates).then(res => spinner.succeed() && res);
}

function remove(templateNames) {
spinner.start(`Removing templates: ${JSON.stringify(templateNames, null, ' ')}`);
const updates = templateNames.map(templateName =>
fetch(getCartoUrl(`api/v1/map/named/${templateName}`), {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
})
.then(res => res.text())
.catch(console.error)
);
return Promise.all(updates).then(res => spinner.succeed() && res);
}

function saveTemplates(templates, namedMaps) {
spinner.start('Saving templates to file system');
const { env, type } = readEnv();
Expand Down Expand Up @@ -142,10 +161,11 @@ function saveTemplates(templates, namedMaps) {
}

function applyTemplates(classified) {
const { toUpdate, toCreate } = classified;
const { toUpdate, toCreate, toRemove } = classified;
console.log(toUpdate);
const updated = toUpdate.length > 0 ? update(toUpdate) : [];
const created = toCreate.length > 0 ? create(toCreate) : [];
if (toRemove.length > 0) remove(toRemove).catch(console.error);
Promise.all([updated, created])
.then(() => instanciate([...toUpdate, ...toCreate]))
.then(namedMaps => saveTemplates([...toUpdate, ...toCreate], namedMaps))
Expand Down
64 changes: 32 additions & 32 deletions frontend/scripts/named-maps/tool_named_maps_carto.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ module.exports = {
uid: 'brazil_states__production',
layergroupid: 'p2cs-sei@4f456911@94bb9db1cc2bbb6f823b52891ea85a4b:1536754740910'
},
brazil_biomes: {
uid: 'brazil_biomes__production',
layergroupid: 'p2cs-sei@ee8b4bf2@53eff313f97bb4d6e956f5078a2ce178:1536754747512'
},
brazil_water_scarcity: {
uid: 'brazil_water_scarcity__production',
layergroupid: 'p2cs-sei@ce96a651@bd75df185965352f558793d64c6c76af:1536754755124'
Expand All @@ -25,18 +21,10 @@ module.exports = {
uid: 'brazil_defor_alerts__production',
layergroupid: 'p2cs-sei@c4807a94@9495c323e40ff63e0c59e0fc6ffbebcd:1538401614852'
},
paraguay_ecoregions_2018_11_14: {
uid: 'paraguay_ecoregions_2018_11_14__production',
layergroupid: 'p2cs-sei@8bc16e3e@920a70ce8cd9698d474541a13571868e:1543380503816'
},
paraguay_protected_areas_2018_11_14: {
uid: 'paraguay_protected_areas_2018_11_14__production',
layergroupid: 'p2cs-sei@7fb7d231@e3a7e457c532b2340f2fa76d05c84160:1542212474275'
},
paraguay_indigenous_areas_2018_11_14: {
uid: 'paraguay_indigenous_areas_2018_11_14__production',
layergroupid: 'p2cs-sei@93c53b34@286bbb73f3bcdfa206205acef80ec835:1542212565695'
},
py_deforestation_2013_2017_20190131: {
uid: 'py_deforestation_2013_2017_20190131__production',
layergroupid: 'p2cs-sei@9d6ad933@07588ca1af7caf72f3f838cdac1cac8f:1548926222884'
Expand All @@ -48,6 +36,18 @@ module.exports = {
colombia_protected_areas: {
uid: 'colombia_protected_areas__production',
layergroupid: 'p2cs-sei@bd5844b5@7068763bcb727c705c489c838b6deadb:1536312121956'
},
brazil_biomes: {
uid: 'brazil_biomes__production',
layergroupid: 'p2cs-sei@ee8b4bf2@53eff313f97bb4d6e956f5078a2ce178:1536754747512'
},
paraguay_ecoregions_2018_11_14: {
uid: 'paraguay_ecoregions_2018_11_14__production',
layergroupid: 'p2cs-sei@8bc16e3e@920a70ce8cd9698d474541a13571868e:1543380503816'
},
paraguay_indigenous_areas_2018_11_14: {
uid: 'paraguay_indigenous_areas_2018_11_14__production',
layergroupid: 'p2cs-sei@93c53b34@286bbb73f3bcdfa206205acef80ec835:1542212565695'
}
},
development: {
Expand Down Expand Up @@ -101,22 +101,34 @@ module.exports = {
}
},
sandbox: {
brazil_states: {
uid: 'brazil_states__sandbox',
layergroupid: 'p2cs-sei@813fddd3@cc7c9866165bd98a1fdd4113ec31ac49:1536754740910'
},
brazil_biomes: {
uid: 'brazil_biomes__sandbox',
layergroupid: 'p2cs-sei@c93b6cad@5a3d352564249dd7eaf1f5a2271b471d:1536754747512'
},
brazil_water_scarcity: {
uid: 'brazil_water_scarcity__sandbox',
layergroupid: 'p2cs-sei@51aab8e1@4ee3434d572f8071c4ee8395cd84c069:1536754755124'
},
brazil_indigenous_areas: {
uid: 'brazil_indigenous_areas__sandbox',
layergroupid: 'p2cs-sei@99f56118@ef8eae3b879a710a3772c7a5f1e34eae:1538034854446'
},
paraguay_ecoregions_2018_11_14: {
uid: 'paraguay_ecoregions_2018_11_14__sandbox',
layergroupid: 'p2cs-sei@8d7b2903@e25bfafe50ad1cde7cee552c474151bb:1543380503816'
},
colombia_regional_autonomous_corps: {
uid: 'colombia_regional_autonomous_corps__sandbox',
layergroupid: 'p2cs-sei@ac06e412@8920938abbeed970d70f57bec86dcd00:1536313111531'
},
colombia_protected_areas: {
uid: 'colombia_protected_areas__sandbox',
layergroupid: 'p2cs-sei@dba9560a@d9b798b42ae0e7bbb677bfbe46d49413:1536312121956'
},
brazil_states: {
uid: 'brazil_states__sandbox',
layergroupid: 'p2cs-sei@813fddd3@cc7c9866165bd98a1fdd4113ec31ac49:1536754740910'
},
brazil_water_scarcity: {
uid: 'brazil_water_scarcity__sandbox',
layergroupid: 'p2cs-sei@51aab8e1@4ee3434d572f8071c4ee8395cd84c069:1536754755124'
},
brazil_protected: {
uid: 'brazil_protected__sandbox',
layergroupid: 'p2cs-sei@94358e00@27478e79b3971e23f86bccc8aae4b0e1:1537979981544'
Expand All @@ -125,10 +137,6 @@ module.exports = {
uid: 'brazil_defor_alerts__sandbox',
layergroupid: 'p2cs-sei@d3572a6b@119855d12a08daf809c7911fb721a04c:1538401614852'
},
paraguay_ecoregions_2018_11_14: {
uid: 'paraguay_ecoregions_2018_11_14__sandbox',
layergroupid: 'p2cs-sei@8d7b2903@e25bfafe50ad1cde7cee552c474151bb:1543380503816'
},
paraguay_protected_areas_2018_11_14: {
uid: 'paraguay_protected_areas_2018_11_14__sandbox',
layergroupid: 'p2cs-sei@6f1f4672@d067ffc51eb3571426f0270c531418f4:1542212474275'
Expand All @@ -140,14 +148,6 @@ module.exports = {
py_deforestation_2013_2017_20190131: {
uid: 'py_deforestation_2013_2017_20190131__sandbox',
layergroupid: 'p2cs-sei@1d3f021a@0117a7789472bd8d3ec12e4d5600e7ab:1548926222884'
},
colombia_regional_autonomous_corps: {
uid: 'colombia_regional_autonomous_corps__sandbox',
layergroupid: 'p2cs-sei@ac06e412@8920938abbeed970d70f57bec86dcd00:1536313111531'
},
colombia_protected_areas: {
uid: 'colombia_protected_areas__sandbox',
layergroupid: 'p2cs-sei@dba9560a@d9b798b42ae0e7bbb677bfbe46d49413:1536312121956'
}
},
demo: {},
Expand Down

0 comments on commit fc36280

Please sign in to comment.