Skip to content

Commit

Permalink
Merge branch 'master' into change-waste-ratio-to-input-mass
Browse files Browse the repository at this point in the history
  • Loading branch information
paulboosz committed Sep 23, 2024
2 parents 0c7e799 + c3fd9f2 commit ad68641
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
47 changes: 36 additions & 11 deletions bin/build-db
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,60 @@ try {
process.exit(1);
}

function getJson(path) {
function parseAndValidate(path, idKeyName = "id") {
const raw = JSON.parse(fs.readFileSync(path).toString());
if (idKeyName && Array.isArray(raw)) {
try {
validatePrimaryKeys(raw, idKeyName);
} catch (err) {
console.error(`🚨 ERROR building ${path}:\n ${err}`);
process.exit(1);
}
}
// Adapts a standard JSON string to what is expected to be the format
// used in Elm's template strings (`"""{}"""`).
return JSON.stringify(raw).replaceAll("\\", "\\\\");
}

/**
* Validates that unique identifiers are actually unique in provided datasource.
*/
function validatePrimaryKeys(records, idKeyName) {
const ids = records.map((record) => record[idKeyName]).sort();
const duplicates = ids.filter((item, index) => ids.indexOf(item) !== index);
if (duplicates.length > 0) {
throw new Error(`Duplicate ${idKeyName}: ${duplicates}`);
}
}

const targetDbFile = "src/Static/Json.elm";
const elmTemplate = fs.readFileSync(`${targetDbFile}-template`).toString();
const elmWithFixtures = elmTemplate
// Transverse JSON data
.replace("%countriesJson%", getJson("public/data/countries.json"))
.replace("%impactsJson%", getJson("public/data/impacts.json"))
.replace("%transportsJson%", getJson("public/data/transports.json"))
.replace("%countriesJson%", parseAndValidate("public/data/countries.json", "code"))
.replace("%impactsJson%", parseAndValidate("public/data/impacts.json"))
.replace("%transportsJson%", parseAndValidate("public/data/transports.json"))
// Food JSON data
.replace("%foodIngredientsJson%", getJson("public/data/food/ingredients.json"))
.replace("%foodIngredientsJson%", parseAndValidate("public/data/food/ingredients.json", "id"))
.replace(
"%foodProcessesJson%",
getJson(NODE_ENV === "test" ? dataFiles.foodDetailed : dataFiles.foodNoDetails),
parseAndValidate(NODE_ENV === "test" ? dataFiles.foodDetailed : dataFiles.foodNoDetails, "id"),
)
.replace("%foodProductExamplesJson%", getJson("public/data/food/examples.json"))
.replace("%foodProductExamplesJson%", parseAndValidate("public/data/food/examples.json", "id"))
// Textile JSON data
.replace("%textileMaterialsJson%", getJson("public/data/textile/materials.json"))
.replace("%textileMaterialsJson%", parseAndValidate("public/data/textile/materials.json", "id"))
.replace(
"%textileProcessesJson%",
getJson(NODE_ENV === "test" ? dataFiles.textileDetailed : dataFiles.textileNoDetails),
parseAndValidate(
NODE_ENV === "test" ? dataFiles.textileDetailed : dataFiles.textileNoDetails,
"uuid",
),
)
.replace(
"%textileProductExamplesJson%",
parseAndValidate("public/data/textile/examples.json", "id"),
)
.replace("%textileProductExamplesJson%", getJson("public/data/textile/examples.json"))
.replace("%textileProductsJson%", getJson("public/data/textile/products.json"));
.replace("%textileProductsJson%", parseAndValidate("public/data/textile/products.json", "id"));

const header =
"---- THIS FILE WAS GENERATED FROM THE FILE `Json.elm-template` BY THE `/bin/build-db` SCRIPT";
Expand Down
2 changes: 1 addition & 1 deletion public/data/textile/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@
}
},
{
"id": "f48d7a6b-68db-4d1c-9623-6ec0ab89cbdb",
"id": "6a2da9ff-9120-4a47-91ad-a8e72be45f4a",
"name": "Tshirt coton (150g) - Remanufacturé",
"category": "Tshirt / Polo",
"query": {
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Textile/Inputs.elm
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ getOutOfEuropeEOLProbability materialInputs =
|> getMaterialCategoryShare Origin.Synthetic
in
Split.fromFloat
(if Split.toPercent syntheticMaterialsShare >= 10 then
(if Split.toPercent syntheticMaterialsShare >= 50 then
0.121

else
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e-textile.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
"swe": 0.08000116470598892,
"tre": 0.5022110547143145,
"wtu": 35.180613817523366,
"ecs": 5039.336827561499,
"ecs": 4811.167813476992,
"pef": 3052.638092437028
}
},
Expand Down

0 comments on commit ad68641

Please sign in to comment.