Commit 07ea192b authored by Valentin Le Gauche's avatar Valentin Le Gauche
Browse files

feat: create function that normalise damage values for several meeting scenarios

parent 3d05ffba
......@@ -100,6 +100,13 @@ const possibleJourneys = [
{ distance: 10, mean: 'CAR_ELECTRIC_ONE_KM', numberOfPeople: 4 }
]
const damageEndpoints = {
HUMAN_HEALTH: 'HUMAN_HEALTH',
ECOSYSTEM_QUALITY: 'ECOSYSTEM_QUALITY',
CLIMATE_CHANGE: 'CLIMATE_CHANGE',
RESOURCES: 'RESOURCES'
}
exports.hourToMinutes = hourToMinutes
exports.dayToHours = dayToHours
exports.minuteToSeconds = minuteToSeconds
......@@ -122,3 +129,5 @@ exports.transportationMeanSubCategories = transportationMeanSubCategories
exports.meetingCategoryDamage = meetingCategoryDamage
exports.possibleJourneys = possibleJourneys
exports.damageEndpoints = damageEndpoints
'used strict'
const assert = require('assert')
const normalise = require('../../utils/normalise')
const hardwareDatabase = require('../../database/meeting/hardware')
const softwareDatabase = require('../../database/meeting/software')
const transportDatabase = require('../../database/meeting/transportationMean')
const meetingScenarios = require('../../database/meeting/meetingScenarios')
const MeetingScenario = require('../../model/classes/meeting/MeetingScenario')
const {
meetingCategoryDamage,
bounds
} = require('../../constants/meeting')
const {
normalise,
normaliseDamages
} = require('../../utils/normalise')
describe('Normalise utilitary function', () => {
describe('#normalise()', () => {
......@@ -15,3 +27,106 @@ describe('Normalise utilitary function', () => {
})
})
})
describe('Normalise damages function', () => {
describe('#normaliseDamages()', () => {
it('should return an array with normalised values for several meeting scenarios', () => {
// All necessary data needed to compute meeting total damage
const damagePayload = {
[meetingCategoryDamage.HARDWARE]: { meetingDuration: 90, bound: bounds.UPPER },
[meetingCategoryDamage.SOFTWARE]: { instancesNumber: 5, bandwithBound: bounds.UPPER, networkBound: bounds.UPPER, meetingDuration: 120 },
[meetingCategoryDamage.JOURNEY]: {}
}
// The user who creates the meeting
const user = 'vlegauch'
// The meeting duration in minutes
const meetingDuration = 120
// Number of participants
const numberOfParticipants = 5
// The JSON object that enables to creates components linked to the meeting
const firstPayload = {
[meetingCategoryDamage.HARDWARE]: [
{ name: hardwareDatabase.DESKTOP.name },
{ name: hardwareDatabase.DESKTOP.name },
{ name: hardwareDatabase.DESKTOP.name },
{ name: hardwareDatabase.LAPTOP.name },
{ name: hardwareDatabase.LOGITECH_KIT.name },
{ name: hardwareDatabase.TV.name },
{ name: hardwareDatabase.TV.name },
{ name: hardwareDatabase.METAL_STRUCTURE.name }
],
[meetingCategoryDamage.SOFTWARE]: [{ name: softwareDatabase.SKYPE.name }],
[meetingCategoryDamage.JOURNEY]: [
{
passenger: 'Passenger 1',
mean: transportDatabase.CAR_ELECTRIC_ONE_KM.name,
distance: 120,
numberOfPeople: 4
},
{
passenger: 'Passenger 1',
mean: transportDatabase.BUS_LARGE_DISTANCE_ONE_PERSON_KM.name,
distance: 40,
numberOfPeople: 1
},
{
passenger: 'Passenger 2',
mean: transportDatabase.CAR_ELECTRIC_ONE_KM.name,
distance: 120,
numberOfPeople: 4
},
{
passenger: 'Passenger 2',
mean: transportDatabase.TRAIN_REGIONAL_ONE_PERSON_KM.name,
distance: 300,
numberOfPeople: 1
},
{
passenger: 'Passenger 2',
mean: transportDatabase.BIKE_ONE_PERSON_ONE_KM.name,
distance: 10,
numberOfPeople: 1
}
]
}
const firstMeetingScenario = MeetingScenario.create({ user, meetingDuration, numberOfParticipants, payload: firstPayload })
firstMeetingScenario.computeDamage(damagePayload)
const secoundPayload = {
[meetingCategoryDamage.HARDWARE]: [
{ name: hardwareDatabase.LAPTOP.name },
{ name: hardwareDatabase.LAPTOP.name },
{ name: hardwareDatabase.LAPTOP.name },
{ name: hardwareDatabase.PROJECTOR.name },
{ name: hardwareDatabase.PROJECTOR.name }
],
[meetingCategoryDamage.SOFTWARE]: [{ name: softwareDatabase.HANGOUTS.name }],
[meetingCategoryDamage.JOURNEY]: [
{
passenger: 'Passenger 1',
mean: transportDatabase.CAR_ELECTRIC_ONE_KM.name,
distance: 120,
numberOfPeople: 4
},
{
passenger: 'Passenger 2',
mean: transportDatabase.TRAIN_HIGH_SPEED_ONE_PERSON_KM.name,
distance: 300,
numberOfPeople: 1
},
{
passenger: 'Passenger 2',
mean: transportDatabase.TRAMWAY_ONE_PERSON_KM.name,
distance: 20,
numberOfPeople: 1
}
]
}
const secoundMeetingScenario = MeetingScenario.create({ user, meetingDuration, numberOfParticipants, payload: secoundPayload })
secoundMeetingScenario.computeDamage(damagePayload)
secoundMeetingScenario.generateAlternatives()
meetingScenarios.forEach(meetingScenario => {
meetingScenario.computeDamage(damagePayload)
})
normaliseDamages(meetingScenarios.values())
})
})
})
'use strict'
const {
damageEndpoints
} = require('../constants/meeting')
/**
* Normalise the array
* The biggest number is turned into 100, the other numbers are like percentages of the first one
......@@ -18,4 +22,89 @@ function normalise (numbers) {
return normalisedNumbers
}
module.exports = normalise
/**
* 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
* 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}
* ]
* The function is used to plot normalised damages.
* @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})
* normaised by their damage values and ordered.
*/
function normaliseDamages (meetingScenarios) {
let damages = []
// For each meeting scenario, get the values for each damage end point
// (human health, ecosysteme quality, climate change and resources) of its total damage
for (const meetingScenario of meetingScenarios) {
damages = damages.concat(
[{
damageEndpoint: damageEndpoints.HUMAN_HEALTH,
meetingScenario: meetingScenario.id,
value: meetingScenario.damage.totalDamage.humanHealth
}],
[{
damageEndpoint: damageEndpoints.ECOSYSTEM_QUALITY,
meetingScenario: meetingScenario.id,
value: meetingScenario.damage.totalDamage.ecosystemQuality
}],
[{
damageEndpoint: damageEndpoints.CLIMATE_CHANGE,
meetingScenario: meetingScenario.id,
value: meetingScenario.damage.totalDamage.climateChange
}],
[{
damageEndpoint: damageEndpoints.RESOURCES,
meetingScenario: meetingScenario.id,
value: meetingScenario.damage.totalDamage.resources
}]
)
}
// Sort the damage values array and get the maximum value
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 normalisedDamage = {
damageEndpoint: damage.damageEndpoint,
meetingScenario: damage.meetingScenario,
value: (damage.value / max) * 100
}
return normalisedDamage
})
return normalisedDamages
}
module.exports = { normalise, normaliseDamages }
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