Implement filtering

This commit is contained in:
hodasemi 2024-04-20 10:28:45 +02:00
parent f04b0bff0d
commit 83a8183e15
4 changed files with 144 additions and 25 deletions

View file

@ -101,12 +101,7 @@ mod test {
use std::collections::HashMap; use std::collections::HashMap;
#[test_case("../laufzettel/CEPI-method_Laufzettel_Part I_V22.8_Metsä Board_Muster1.xlsx")] #[test_case("../laufzettel/CEPI-method_Laufzettel_Part I_V23.07_Ranpak_Muster2.xlsx")]
#[test_case("../laufzettel/CEPI-method_Laufzettel_Part I_V22.9_Adapack_Muster 1.xlsx")]
#[test_case("../laufzettel/CEPI-method_Laufzettel_Part I_V23.02_Cascogne_Muster1.xlsx")]
#[test_case(
"../laufzettel/CEPI-method_Laufzettel_Part I_V23.04_Cats Flexible Packaging_M1.xlsx"
)]
fn verify_cepi(file: &str) -> Result<()> { fn verify_cepi(file: &str) -> Result<()> {
let cepi_reader = PythonObject::new("../python-cepi-master/cepi_read_function.py")?; let cepi_reader = PythonObject::new("../python-cepi-master/cepi_read_function.py")?;
@ -116,6 +111,8 @@ mod test {
|r: HashMap<String, HashMap<String, String>>| Ok(r), |r: HashMap<String, HashMap<String, String>>| Ok(r),
)?; )?;
println!("result: {result:#?}");
Ok(()) Ok(())
} }
} }

View file

@ -14,7 +14,7 @@ const pool = mariadb.createPool({
supportBigNumbers: true, supportBigNumbers: true,
}) })
const PORT = 80 const PORT = 9000
function convert_rights(rights) { function convert_rights(rights) {
switch (rights) { switch (rights) {
@ -401,28 +401,146 @@ async function resultList(cb_filter, rights) {
return result; return result;
} }
app.get('/ddtrust/cepi/:rights', async (request, response) => { function createFilters() {
// column | variable name | values
// ---------------------------|-----------------------
// B | finished_product | without-finishing, partial-finishing, intermediate, final, finished
// C | base_material | paper, carton, corrugated, solid, fibre, mix, other
// D | coating | no, one-side, two-side
// E | fillers | no, yes
// F | polymers | no, low, high
// G | artificial_fibre | no, yes
// H | ??? |
// I | printed | no, yes
// J | varnish | no, yes
// K | glue | no, yes
// L | sealing | no, yes
let filters = [
{
key: "finished_product",
values: ["without-finishing", "partial-finishing", "intermediate", "final", "finished"]
},
{
key: "base_material",
values: ["paper", "carton", "corrugated", "solid", "fibre", "mix", "other"]
},
{
key: "coating",
values: ["no", "one-side", "two-side"]
},
{
key: "fillers",
values: ["no", "yes"]
},
{
key: "polymers",
values: ["no", "low", "high"]
},
{
key: "artificial_fibre",
values: ["no", "yes"]
},
{
key: "printed",
values: ["no", "yes"]
},
{
key: "varnish",
values: ["no", "yes"]
},
{
key: "glue",
values: ["no", "yes"]
},
{
key: "sealing",
values: ["no", "yes"]
}
];
return filters
}
// searches through the provided filter and returns a list of them
function getFilter(query) {
// get static list of implemented filters
let filters = createFilters();
let result = [];
// iterate over every filter and check for correct syntax
filters.forEach(function (filter) {
let res = query[filter.key];
if (res) {
if (filter.values.find(element => element == res)) {
result.push({ key: filter.key, value: res });
} else {
throw "unknown value (" + res + ") for key (" + filter.key + "). Possible values: " + filter.values;
}
}
});
return result;
}
function applyFilter(result_list, filters) {
let results = [];
// check every result if every filter can be applied
result_list.forEach(function (result) {
let filter_success = true;
// check all filter
filters.forEach(function (filter) {
let value = result[filter.key];
// check if result even holds the key (and value for it)
if (value) {
if (value !== filter.value) {
filter_success = false;
}
} else {
filter_success = false;
}
});
// only add result if filters could successfully be applied
if (filter_success == true) {
results.push(result);
}
});
return results;
}
app.get('/cepi/list/:rights', async (request, response) => {
let result_list = await resultList("%Cepi%", request.params.rights); let result_list = await resultList("%Cepi%", request.params.rights);
response.json(result_list) response.json(result_list)
}) })
app.get('ddtrust/cepi/:rights/filter', async (request, response) => { app.get('/cepi/filters', async (_request, response) => {
let result_list = await resultList("%Cepi%", request.params.rights); response.json(createFilters())
})
// column | variable name app.get('/cepi/list/:rights/filter', async (request, response) => {
// ---------------------- let results;
// B | finished_product
// C | base_material try {
// D | coating let result_list = await resultList("%Cepi%", request.params.rights);
// E | fillers let filters = getFilter(request.query);
// F | polymers
// G | artificial_fibre results = applyFilter(result_list, filters);
// H | ??? }
// I | printed catch (err) {
// J | varnish return response.status(400).send({
// K | glue message: err
// L | sealing });
}
response.json(results)
}) })
app.listen(PORT, () => { app.listen(PORT, () => {

View file

@ -14,7 +14,7 @@
"author": "", "author": "",
"dependencies": { "dependencies": {
"bson": "^5.3.0", "bson": "^5.3.0",
"express": "^4.18.2", "express": "^4.19.2",
"mariadb": "^3.1.2" "mariadb": "^3.3.0"
} }
} }

View file

@ -310,6 +310,10 @@ def read(filepath):
#verified #verified
elif version == "V23.07":
print("23.07 needs extra testing")
return results
#If version not supported #If version not supported
else: else:
version = "" version = ""