diff --git a/services/backend/src/service/environmental_authority.js b/services/backend/src/service/environmental_authority.js index 2be0c37b..5e15903a 100644 --- a/services/backend/src/service/environmental_authority.js +++ b/services/backend/src/service/environmental_authority.js @@ -32,8 +32,13 @@ module.exports = (eaPersistence, seService) => { values[key] += area.sum; } }); + const areaAdd = values.reduce((prev, nex) => prev + nex, 0); - return keys.map((fc, idx) => ({ key: fc, area: values[idx] })); + return keys.map((fc, idx) => ({ + key: fc, + area: values[idx], + percentage: values[idx] / areaAdd, + })); }, /** @@ -45,7 +50,13 @@ module.exports = (eaPersistence, seService) => { */ getAreaByBioticUnit: async (envAuthorityId) => { const data = await eaPersistence.findAreaByBioticUnit(envAuthorityId); - return data.map((datum) => ({ ...datum, area: Number(datum.area) })); + const dataToNumber = data.map((e) => Number(e.area)); + const areaAdd = dataToNumber.reduce((prev, nex) => prev + nex); + return data.map((datum) => ({ + ...datum, + area: Number(datum.area), + percentage: Number(datum.area) / areaAdd, + })); }, /** @@ -55,7 +66,11 @@ module.exports = (eaPersistence, seService) => { * * @returns {Object[]} total area for each biome */ - getAreaByBiome: async (envAuthorityId) => eaPersistence.findAreaByBiome(envAuthorityId), + getAreaByBiome: async (envAuthorityId) => { + const data = await eaPersistence.findAreaByBiome(envAuthorityId); + const areaAdd = data.reduce((pre, nex) => pre + nex.area, 0); + return data.map((e) => ({ area: e.area, key: e.key, percentage: e.area / areaAdd })); + }, /** * Get total area grouped by sub-basin given environmental authority filtered by a biome diff --git a/services/forest/src/service/forestLP.js b/services/forest/src/service/forestLP.js index 54fb1ada..5f71c202 100644 --- a/services/forest/src/service/forestLP.js +++ b/services/forest/src/service/forestLP.js @@ -27,7 +27,7 @@ module.exports = (ForestLPPersistence, restAPI) => { const totalArea = item.map((o) => o.area).reduce((prev, next) => prev + next); const periodData = item.map((period) => ({ ...period, - percentage: period.area / totalArea, + percentage: (period.area / totalArea) * 100, })); data.push({ id: periods[i],