Commit 4c00cc82 authored by Valentin Le Gauche's avatar Valentin Le Gauche
Browse files

Merge branch '58-modifier-la-fonction-de-normalisation-des-damages' into 'master'

Resolve "Modifier la fonction de normalisation des damages."

Closes #58

See merge request tx-techno-num/impactometre!60
parents aee9da6a 138d7d18
...@@ -19,8 +19,9 @@ ...@@ -19,8 +19,9 @@
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"morgan": "~1.9.1", "morgan": "~1.9.1",
"node-sass-middleware": "^0.11.0", "node-sass-middleware": "^0.11.0",
"postcss-middleware": "^1.1.4",
"postcss-cli": "7.1.0", "postcss-cli": "7.1.0",
"postcss-middleware": "^1.1.4",
"round-to": "^4.1.0",
"uniqid": "^5.2.0" "uniqid": "^5.2.0"
}, },
"devDependencies": { "devDependencies": {
......
'use strict' 'use strict'
const roundTo = require('round-to')
const { const {
damageEndpoints damageEndpoints,
meetingCategoryDamage
} = require('../constants/meeting') } = require('../constants/meeting')
/** /**
...@@ -26,81 +28,111 @@ function normalise (numbers) { ...@@ -26,81 +28,111 @@ function normalise (numbers) {
* Normalised the damages values of several meeting scenarios. * Normalised the damages values of several meeting scenarios.
* The biggest number is turned into 100, the other numbers are like percentages of the first one * The biggest number is turned into 100, the other numbers are like percentages of the first one
* example: * example:
* [ * [
* {HUMAN_HEALTH, meetingScenario1, 20}, * {HUMAN_HEALTH, meetingScenario1, 20, 5, 5, 10},
* {ECOSYSTEM_QUALITY, meetingScenario1, 18}, * {ECOSYSTEM_QUALITY, meetingScenario1, 18, 2, 2, 14},
* {CLIMATE_CHANGE, meetingScenario1, 14}, * {CLIMATE_CHANGE, meetingScenario1, 14, 5, 2, 7},
* {RESOURCES, meetingScenario1, 6}, * {RESOURCES, meetingScenario1, 6, 2, 2, 2},
* {HUMAN_HEALTH, meetingScenario2, 2} * {HUMAN_HEALTH, meetingScenario2, 2, 2, 0, 0}
* ] => [ * ] => [
* {HUMAN_HEALTH, meetingScenario1, 100}, * {HUMAN_HEALTH, meetingScenario1, 100, 25, 25, 50},
* {ECOSYSTEM_QUALITY, meetingScenario1, 90}, * {ECOSYSTEM_QUALITY, meetingScenario1, 90, 10, 10, 70},
* {CLIMATE_CHANGE, meetingScenario1, 70}, * {CLIMATE_CHANGE, meetingScenario1, 70, 25, 10, 35},
* {RESOURCES, meetingScenario1, 30}, * {RESOURCES, meetingScenario1, 30, 10, 10, 10},
* {HUMAN_HEALTH, meetingScenario2, 10} * {HUMAN_HEALTH, meetingScenario2, 10, 10, 0, 0}
* ] * ]
* The function is used to plot normalised damages. * The function is used to plot normalised damages.
* @param {Object} meetingScenarios - Iterable object thats contains meetingScenarios we want to normalise the damage values * @param {Object} meetingScenarios - Iterable object thats contains meetingScenarios we want to normalise the damage values
* @returns An array that contains JSON objects (like {ECOSYSTEM_QUALITY, meetingScenario1, 90}) * @returns An array that contains JSON objects (like {ECOSYSTEM_QUALITY, meetingScenario1, 90})
* normaised by their damage values and ordered. * normaised by their damage values and ordered.
*/ */
function normaliseDamages (meetingScenarios) { function normaliseDamages (meetingScenarios) {
let damages = [] let humanHealthDamages = []
let ecosystemQualityDamages = []
let climateChangeDamages = []
let resourcesDamages = []
// For each meeting scenario, get the values for each damage end point // For each meeting scenario, get the values for each damage end point
// (human health, ecosysteme quality, climate change and resources) of its total damage // (human health, ecosysteme quality, climate change and resources) of its total damage
// and its values for all damage category (hardware, software, jounrney)
for (const meetingScenario of meetingScenarios) { for (const meetingScenario of meetingScenarios) {
damages = damages.concat( humanHealthDamages = humanHealthDamages.concat(
[{ [{
damageEndpoint: damageEndpoints.HUMAN_HEALTH, damageEndpoint: damageEndpoints.HUMAN_HEALTH,
meetingScenario: meetingScenario.id, meetingScenario: meetingScenario.id,
value: meetingScenario.damage.totalDamage.humanHealth value: meetingScenario.damage.totalDamage.humanHealth,
}], [meetingCategoryDamage.HARDWARE]: meetingScenario.damage.hardwareDamage.totalDamage.humanHealth,
[meetingCategoryDamage.SOFTWARE]: meetingScenario.damage.softwareDamage.totalDamage.humanHealth,
[meetingCategoryDamage.JOURNEY]: meetingScenario.damage.journeyDamage.totalDamage.humanHealth
}]
)
ecosystemQualityDamages = ecosystemQualityDamages.concat(
[{ [{
damageEndpoint: damageEndpoints.ECOSYSTEM_QUALITY, damageEndpoint: damageEndpoints.ECOSYSTEM_QUALITY,
meetingScenario: meetingScenario.id, meetingScenario: meetingScenario.id,
value: meetingScenario.damage.totalDamage.ecosystemQuality value: meetingScenario.damage.totalDamage.ecosystemQuality,
}], [meetingCategoryDamage.HARDWARE]: meetingScenario.damage.hardwareDamage.totalDamage.ecosystemQuality,
[meetingCategoryDamage.SOFTWARE]: meetingScenario.damage.softwareDamage.totalDamage.ecosystemQuality,
[meetingCategoryDamage.JOURNEY]: meetingScenario.damage.journeyDamage.totalDamage.ecosystemQuality
}]
)
climateChangeDamages = climateChangeDamages.concat(
[{ [{
damageEndpoint: damageEndpoints.CLIMATE_CHANGE, damageEndpoint: damageEndpoints.CLIMATE_CHANGE,
meetingScenario: meetingScenario.id, meetingScenario: meetingScenario.id,
value: meetingScenario.damage.totalDamage.climateChange value: meetingScenario.damage.totalDamage.climateChange,
}], [meetingCategoryDamage.HARDWARE]: meetingScenario.damage.hardwareDamage.totalDamage.climateChange,
[meetingCategoryDamage.SOFTWARE]: meetingScenario.damage.softwareDamage.totalDamage.climateChange,
[meetingCategoryDamage.JOURNEY]: meetingScenario.damage.journeyDamage.totalDamage.climateChange
}]
)
resourcesDamages = resourcesDamages.concat(
[{ [{
damageEndpoint: damageEndpoints.RESOURCES, damageEndpoint: damageEndpoints.RESOURCES,
meetingScenario: meetingScenario.id, meetingScenario: meetingScenario.id,
value: meetingScenario.damage.totalDamage.resources value: meetingScenario.damage.totalDamage.resources,
[meetingCategoryDamage.HARDWARE]: meetingScenario.damage.hardwareDamage.totalDamage.resources,
[meetingCategoryDamage.SOFTWARE]: meetingScenario.damage.softwareDamage.totalDamage.resources,
[meetingCategoryDamage.JOURNEY]: meetingScenario.damage.journeyDamage.totalDamage.resources
}] }]
) )
} }
// Sort the damage values array and get the maximum value const damages = [humanHealthDamages, ecosystemQualityDamages, climateChangeDamages, resourcesDamages]
damages.sort((a, b) => b.value - a.value)
const max = damages[0].value
/* Normalised the damage values
The biggest number is turned into 100, the other numbers are like percentages of the first one
example:
[
{HUMAN_HEALTH, meetingScenario1, 20},
{ECOSYSTEM_QUALITY, meetingScenario1, 18},
{CLIMATE_CHANGE, meetingScenario1, 14},
{RESOURCES, meetingScenario1, 6},
{HUMAN_HEALTH, meetingScenario2, 2}
] => [
{HUMAN_HEALTH, meetingScenario1, 100},
{ECOSYSTEM_QUALITY, meetingScenario1, 90},
{CLIMATE_CHANGE, meetingScenario1, 70},
{RESOURCES, meetingScenario1, 30},
{HUMAN_HEALTH, meetingScenario2, 10}
]
*/
const normalisedDamages = damages.map(function (damage) { const normalisedDamages = damages.map(function (damage) {
const normalisedDamage = { // Sort the damage values array and get the maximum value
damageEndpoint: damage.damageEndpoint, damage.sort((a, b) => b.value - a.value)
meetingScenario: damage.meetingScenario, const max = damage[0].value
value: (damage.value / max) * 100
} /* Normalised the damage values
The biggest number is turned into 100, the other numbers are like percentages of the first one
example:
[
{HUMAN_HEALTH, meetingScenario1, 20, 5, 5, 10},
{ECOSYSTEM_QUALITY, meetingScenario1, 18, 2, 2, 14},
{CLIMATE_CHANGE, meetingScenario1, 14, 5, 2, 7},
{RESOURCES, meetingScenario1, 6, 2, 2, 2},
{HUMAN_HEALTH, meetingScenario2, 2, 2, 0, 0}
] => [
{HUMAN_HEALTH, meetingScenario1, 100, 25, 25, 50},
{ECOSYSTEM_QUALITY, meetingScenario1, 90, 10, 10, 70},
{CLIMATE_CHANGE, meetingScenario1, 70, 25, 10, 35},
{RESOURCES, meetingScenario1, 30, 10, 10, 10},
{HUMAN_HEALTH, meetingScenario2, 10, 10, 0, 0}
]
*/
const normalisedDamage = damage.map(function (damage) {
const x = {
damageEndpoint: damage.damageEndpoint,
meetingScenario: damage.meetingScenario,
value: roundTo((damage.value / max) * 100, 2),
[meetingCategoryDamage.HARDWARE]: roundTo((damage.hardware / max) * 100, 2),
[meetingCategoryDamage.SOFTWARE]: roundTo((damage.software / max) * 100, 2),
[meetingCategoryDamage.JOURNEY]: roundTo((damage.journey / max) * 100, 2)
}
return x
})
return normalisedDamage return normalisedDamage
}) })
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment