Implement filtering
This commit is contained in:
parent
f04b0bff0d
commit
83a8183e15
4 changed files with 144 additions and 25 deletions
|
@ -101,12 +101,7 @@ mod test {
|
|||
|
||||
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_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"
|
||||
)]
|
||||
#[test_case("../laufzettel/CEPI-method_Laufzettel_Part I_V23.07_Ranpak_Muster2.xlsx")]
|
||||
fn verify_cepi(file: &str) -> Result<()> {
|
||||
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),
|
||||
)?;
|
||||
|
||||
println!("result: {result:#?}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ const pool = mariadb.createPool({
|
|||
supportBigNumbers: true,
|
||||
})
|
||||
|
||||
const PORT = 80
|
||||
const PORT = 9000
|
||||
|
||||
function convert_rights(rights) {
|
||||
switch (rights) {
|
||||
|
@ -401,28 +401,146 @@ async function resultList(cb_filter, rights) {
|
|||
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);
|
||||
|
||||
response.json(result_list)
|
||||
})
|
||||
|
||||
app.get('ddtrust/cepi/:rights/filter', async (request, response) => {
|
||||
let result_list = await resultList("%Cepi%", request.params.rights);
|
||||
app.get('/cepi/filters', async (_request, response) => {
|
||||
response.json(createFilters())
|
||||
})
|
||||
|
||||
// column | variable name
|
||||
// ----------------------
|
||||
// B | finished_product
|
||||
// C | base_material
|
||||
// D | coating
|
||||
// E | fillers
|
||||
// F | polymers
|
||||
// G | artificial_fibre
|
||||
// H | ???
|
||||
// I | printed
|
||||
// J | varnish
|
||||
// K | glue
|
||||
// L | sealing
|
||||
app.get('/cepi/list/:rights/filter', async (request, response) => {
|
||||
let results;
|
||||
|
||||
try {
|
||||
let result_list = await resultList("%Cepi%", request.params.rights);
|
||||
let filters = getFilter(request.query);
|
||||
|
||||
results = applyFilter(result_list, filters);
|
||||
}
|
||||
catch (err) {
|
||||
return response.status(400).send({
|
||||
message: err
|
||||
});
|
||||
}
|
||||
|
||||
response.json(results)
|
||||
})
|
||||
|
||||
app.listen(PORT, () => {
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
"author": "",
|
||||
"dependencies": {
|
||||
"bson": "^5.3.0",
|
||||
"express": "^4.18.2",
|
||||
"mariadb": "^3.1.2"
|
||||
"express": "^4.19.2",
|
||||
"mariadb": "^3.3.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -310,6 +310,10 @@ def read(filepath):
|
|||
|
||||
#verified
|
||||
|
||||
elif version == "V23.07":
|
||||
print("23.07 needs extra testing")
|
||||
return results
|
||||
|
||||
#If version not supported
|
||||
else:
|
||||
version = ""
|
||||
|
|
Loading…
Reference in a new issue