Skip to content

Commit

Permalink
Adapt ecoindex calcul (set scale between 0 and 100)
Browse files Browse the repository at this point in the history
  • Loading branch information
didierfred committed Sep 10, 2022
1 parent 10a2bb3 commit acc0334
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
20 changes: 10 additions & 10 deletions script/ecoIndex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 [email protected]
* Copyright (C) 2019 - 2022 [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
Expand All @@ -15,6 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


let quantiles_dom = [0, 47, 75, 159, 233, 298, 358, 417, 476, 537, 603, 674, 753, 843, 949, 1076, 1237, 1459, 1801, 2479, 594601];
let quantiles_req = [0, 2, 15, 25, 34, 42, 49, 56, 63, 70, 78, 86, 95, 105, 117, 130, 147, 170, 205, 281, 3920];
let quantiles_size = [0, 1.37, 144.7, 319.53, 479.46, 631.97, 783.38, 937.91, 1098.62, 1265.47, 1448.32, 1648.27, 1876.08, 2142.06, 2465.37, 2866.31, 3401.59, 4155.73, 5400.08, 8037.54, 223212.26];
Expand All @@ -30,28 +31,27 @@ const q_dom= computeQuantile(quantiles_dom,dom);
const q_req= computeQuantile(quantiles_req,req);
const q_size= computeQuantile(quantiles_size,size);


return 100 - 5 * (3*q_dom + 2*q_req + q_size)/6;
}

function computeQuantile(quantiles,value)
{
for (let i=1;i<quantiles.length;i++)
{
if (value<quantiles[i]) return (i + (value-quantiles[i-1])/(quantiles[i] -quantiles[i-1]));
if (value<quantiles[i]) return (i -1 + (value-quantiles[i-1])/(quantiles[i] -quantiles[i-1]));
}
return quantiles.length;
return quantiles.length -1;
}


function getEcoIndexGrade(ecoIndex)
{
if (ecoIndex > 75) return "A";
if (ecoIndex > 65) return "B";
if (ecoIndex > 50) return "C";
if (ecoIndex > 35) return "D";
if (ecoIndex > 20) return "E";
if (ecoIndex > 5) return "F";
if (ecoIndex > 80) return "A";
if (ecoIndex > 70) return "B";
if (ecoIndex > 55) return "C";
if (ecoIndex > 40) return "D";
if (ecoIndex > 25) return "E";
if (ecoIndex > 10) return "F";
return "G";
}

Expand Down
53 changes: 31 additions & 22 deletions tests/spec/EcoIndex_Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,40 @@ describe("ecoIndex.js", function() {
beforeEach(function() {
});

it(" 100 , 100 ,100 should return 67", function() {
expect(Math.round(computeEcoIndex(100,100,100))).toEqual(67);
it(" 100 , 100 ,100 should return 72", function() {
expect(Math.round(computeEcoIndex(100,100,100))).toEqual(72);
});

it(" 100 , 100 ,1000 should return 62", function() {
expect(Math.round(computeEcoIndex(100,100,1000))).toEqual(62);
it(" 100 , 100 ,1000 should return 67", function() {
expect(Math.round(computeEcoIndex(100,100,1000))).toEqual(67);
});

it(" 100 , 100 ,10000 should return 53", function() {
expect(Math.round(computeEcoIndex(100,100,10000))).toEqual(53);
it(" 100 , 100 ,10000 should return 58", function() {
expect(Math.round(computeEcoIndex(100,100,10000))).toEqual(58);
});

it(" 200 , 200 ,10000 should return 53", function() {
expect(Math.round(computeEcoIndex(200,200,10000))).toEqual(41);
it(" 200 , 200 ,10000 should return 46", function() {
expect(Math.round(computeEcoIndex(200,200,10000))).toEqual(46);
});


it(" 2355 , 267 ,2493 should return 5", function() {
expect(Math.round(computeEcoIndex(2355,267,2493))).toEqual(5);
it(" 2355 , 267 ,2493 should return 10", function() {
expect(Math.round(computeEcoIndex(2355,267,2493))).toEqual(10);
});

it(" 240 , 20 ,331 should return 78", function() {
expect(Math.round(computeEcoIndex(240,20,331))).toEqual(78);
it(" 240 , 20 ,331 should return 83", function() {
expect(Math.round(computeEcoIndex(240,20,331))).toEqual(83);
});

it(" 6000 , 4000 ,300000 should return 0", function() {
expect(Math.round(computeEcoIndex(600000,4000,300000))).toEqual(0);
});

it(" 0 , 0 , 0 should return 100", function() {
expect(Math.round(computeEcoIndex(0,0,0))).toEqual(100);
});


afterEach(function() {
});
});
Expand All @@ -44,24 +53,24 @@ describe("ecoIndex.js", function() {
expect(getEcoIndexGrade(2)).toEqual("G");
});

it(" 10 should return F ", function() {
expect(getEcoIndexGrade(10)).toEqual("F");
it(" 15 should return F ", function() {
expect(getEcoIndexGrade(15)).toEqual("F");
});

it(" 25 should return E ", function() {
expect(getEcoIndexGrade(25)).toEqual("E");
it(" 30 should return E ", function() {
expect(getEcoIndexGrade(30)).toEqual("E");
});

it(" 40 should return D ", function() {
expect(getEcoIndexGrade(40)).toEqual("D");
it(" 45 should return D ", function() {
expect(getEcoIndexGrade(45)).toEqual("D");
});

it(" 50.2 should return C ", function() {
expect(getEcoIndexGrade(50.2)).toEqual("C");
it(" 55.2 should return C ", function() {
expect(getEcoIndexGrade(55.2)).toEqual("C");
});

it(" 75 should return B ", function() {
expect(getEcoIndexGrade(75)).toEqual("B");
it(" 80 should return B ", function() {
expect(getEcoIndexGrade(80)).toEqual("B");
});

it(" 100 should return A ", function() {
Expand Down

0 comments on commit acc0334

Please sign in to comment.