From 4a04814901a373d23fcb1be74950d93060471fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luc=C3=A0s?= <86352901+LucasVbr@users.noreply.github.com> Date: Mon, 14 Oct 2024 17:36:46 +0200 Subject: [PATCH] feat: General Endpoint return randomly the mix of all data --- insomnia/Insomnia_2024-10-14.json | 179 +++++++++++++++++++++++++++++- src/routes/randomize.ts | 27 ++++- 2 files changed, 204 insertions(+), 2 deletions(-) diff --git a/insomnia/Insomnia_2024-10-14.json b/insomnia/Insomnia_2024-10-14.json index 3ba5eca..6140ce6 100644 --- a/insomnia/Insomnia_2024-10-14.json +++ b/insomnia/Insomnia_2024-10-14.json @@ -1 +1,178 @@ -{"_type":"export","__export_format":4,"__export_date":"2024-10-14T10:07:13.189Z","__export_source":"insomnia.desktop.app:v10.0.0","resources":[{"_id":"req_55b9623b1d824b52b34d13279176c354","parentId":"wrk_b6f7ea92d2f443b2bf923a99dc6fca88","modified":1727867887432,"created":1727867720980,"url":"http://localhost:4321/randomize","name":"randomize","description":"","method":"POST","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"insomnia/10.0.0"}],"authentication":{},"metaSortKey":-1727867720980,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"wrk_b6f7ea92d2f443b2bf923a99dc6fca88","parentId":null,"modified":1727867693579,"created":1727867693579,"name":"Compo-Service-Log-Project","description":"","scope":"collection","_type":"workspace"},{"_id":"req_cc7e8367ddb843da91ef50d9d0df6ed2","parentId":"wrk_b6f7ea92d2f443b2bf923a99dc6fca88","modified":1728038602629,"created":1727867896038,"url":"http://localhost:4321/randomize/nudger","name":"randomize/nudger","description":"","method":"POST","body":{"mimeType":"application/xml","text":"\n\n \n \n \n \n \n \n \n \n \n \n string length(?)=13 and starts with(?, \"3\") and not matches(?, \"^3[89]\")\n \n \n \"France-Monaco\"\n \n \n \n \n matches(?, \"^46\\d{11}$\")\n \n \n \"Russia\"\n \n \n \n \n string length(?)=13 and starts with(?, \"560\")\n \n \n \"Portugal\"\n \n \n \n \n \nmatches(?, \"^9[0-1]{1}\\d{10}$\")\n \n \n \"AT\"\n \n \n \n \n matches(?, \"^9[0-1]{1}\\d{10}$\")\n \n \n \"Austria\"\n \n \n \n \n\n"},"parameters":[{"id":"pair_d55b3a61f8fc4ad89303e5b70fc19df6","name":"size","value":"1000","description":"","disabled":false}],"headers":[{"name":"Content-Type","value":"application/xml"},{"name":"User-Agent","value":"insomnia/10.0.0"}],"authentication":{},"metaSortKey":-1727867720930,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_bfff90a801714354a8362a22e7a635d9","parentId":"wrk_b6f7ea92d2f443b2bf923a99dc6fca88","modified":1728319714698,"created":1728319701558,"url":"http://localhost:4321/randomize/openfoodfacts","name":"randomize/openfoodfacts","description":"","method":"POST","body":{"mimeType":"application/xml","text":"\n\n \n \n \n \n \n \n \n \n \n \n string length(?)=13 and starts with(?, \"3\") and not matches(?, \"^3[89]\")\n \n \n \"France-Monaco\"\n \n \n \n \n matches(?, \"^46\\d{11}$\")\n \n \n \"Russia\"\n \n \n \n \n string length(?)=13 and starts with(?, \"560\")\n \n \n \"Portugal\"\n \n \n \n \n \nmatches(?, \"^9[0-1]{1}\\d{10}$\")\n \n \n \"AT\"\n \n \n \n \n matches(?, \"^9[0-1]{1}\\d{10}$\")\n \n \n \"Austria\"\n \n \n \n \n\n"},"parameters":[{"id":"pair_d55b3a61f8fc4ad89303e5b70fc19df6","name":"size","value":"1000","description":"","disabled":false}],"headers":[{"name":"Content-Type","value":"application/xml"},{"name":"User-Agent","value":"insomnia/10.0.0"}],"authentication":{},"metaSortKey":-1727867720880,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_53d1a1f0862d4cb99e5572c1dd30967b","parentId":"wrk_b6f7ea92d2f443b2bf923a99dc6fca88","modified":1728900408292,"created":1728900124584,"url":"http://localhost:4321/randomize/world-cities","name":"randomize/world-cities","description":"","method":"POST","body":{"mimeType":"application/xml","text":"\n\n \n \n \n \n GeonameID\n \n \n \n \n Cities with geonameId between 2967245 and 3038789 are in France\n \n includes(2967245, 3038789)\n \n \n \"France\"\n \n \n \n Cities with geonameId between 3104316 and 3130616 are in Spain\n \n includes(3104316, 3130616)\n \n \n \"Spain\"\n \n \n \n Cities with geonameId between 5881791 and 6185607 are in Spain\n \n includes(5881791,6185607)\n \n \n \"Canada\"\n \n \n \n \n\n"},"parameters":[{"id":"pair_d55b3a61f8fc4ad89303e5b70fc19df6","name":"size","value":"1000","description":"","disabled":false}],"headers":[{"name":"Content-Type","value":"application/xml"},{"name":"User-Agent","value":"insomnia/10.0.0"}],"authentication":{},"metaSortKey":-1727867720830,"isPrivate":false,"pathParameters":[],"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"env_025a059466ea4c5d1a14c9fd8028c0bb99039906","parentId":"wrk_b6f7ea92d2f443b2bf923a99dc6fca88","modified":1727867693581,"created":1727867693581,"name":"Base Environment","data":{},"dataPropertyOrder":null,"color":null,"isPrivate":false,"metaSortKey":1727867693581,"_type":"environment"},{"_id":"jar_025a059466ea4c5d1a14c9fd8028c0bb99039906","parentId":"wrk_b6f7ea92d2f443b2bf923a99dc6fca88","modified":1727867693582,"created":1727867693582,"name":"Default Jar","cookies":[],"_type":"cookie_jar"}]} \ No newline at end of file +{ + "_type": "export", + "__export_format": 4, + "__export_date": "2024-10-14T10:07:13.189Z", + "__export_source": "insomnia.desktop.app:v10.0.0", + "resources": [ + { + "_id": "req_55b9623b1d824b52b34d13279176c354", + "parentId": "wrk_b6f7ea92d2f443b2bf923a99dc6fca88", + "modified": 1727867887432, + "created": 1727867720980, + "url": "http://localhost:4321/randomize", + "name": "randomize", + "description": "", + "method": "POST", + "body": {}, + "parameters": [], + "headers": [{ "name": "User-Agent", "value": "insomnia/10.0.0" }], + "authentication": {}, + "metaSortKey": -1727867720980, + "isPrivate": false, + "pathParameters": [], + "settingStoreCookies": true, + "settingSendCookies": true, + "settingDisableRenderRequestBody": false, + "settingEncodeUrl": true, + "settingRebuildPath": true, + "settingFollowRedirects": "global", + "_type": "request" + }, + { + "_id": "wrk_b6f7ea92d2f443b2bf923a99dc6fca88", + "parentId": null, + "modified": 1727867693579, + "created": 1727867693579, + "name": "Compo-Service-Log-Project", + "description": "", + "scope": "collection", + "_type": "workspace" + }, + { + "_id": "req_cc7e8367ddb843da91ef50d9d0df6ed2", + "parentId": "wrk_b6f7ea92d2f443b2bf923a99dc6fca88", + "modified": 1728038602629, + "created": 1727867896038, + "url": "http://localhost:4321/randomize/nudger", + "name": "randomize/nudger", + "description": "", + "method": "POST", + "body": { + "mimeType": "application/xml", + "text": "\n\n \n \n \n \n \n \n \n \n \n \n string length(?)=13 and starts with(?, \"3\") and not matches(?, \"^3[89]\")\n \n \n \"France-Monaco\"\n \n \n \n \n matches(?, \"^46\\d{11}$\")\n \n \n \"Russia\"\n \n \n \n \n string length(?)=13 and starts with(?, \"560\")\n \n \n \"Portugal\"\n \n \n \n \n \nmatches(?, \"^9[0-1]{1}\\d{10}$\")\n \n \n \"AT\"\n \n \n \n \n matches(?, \"^9[0-1]{1}\\d{10}$\")\n \n \n \"Austria\"\n \n \n \n \n\n" + }, + "parameters": [ + { + "id": "pair_d55b3a61f8fc4ad89303e5b70fc19df6", + "name": "size", + "value": "1000", + "description": "", + "disabled": false + } + ], + "headers": [ + { "name": "Content-Type", "value": "application/xml" }, + { "name": "User-Agent", "value": "insomnia/10.0.0" } + ], + "authentication": {}, + "metaSortKey": -1727867720930, + "isPrivate": false, + "pathParameters": [], + "settingStoreCookies": true, + "settingSendCookies": true, + "settingDisableRenderRequestBody": false, + "settingEncodeUrl": true, + "settingRebuildPath": true, + "settingFollowRedirects": "global", + "_type": "request" + }, + { + "_id": "req_bfff90a801714354a8362a22e7a635d9", + "parentId": "wrk_b6f7ea92d2f443b2bf923a99dc6fca88", + "modified": 1728319714698, + "created": 1728319701558, + "url": "http://localhost:4321/randomize/openfoodfacts", + "name": "randomize/openfoodfacts", + "description": "", + "method": "POST", + "body": { + "mimeType": "application/xml", + "text": "\n\n \n \n \n \n \n \n \n \n \n \n string length(?)=13 and starts with(?, \"3\") and not matches(?, \"^3[89]\")\n \n \n \"France-Monaco\"\n \n \n \n \n matches(?, \"^46\\d{11}$\")\n \n \n \"Russia\"\n \n \n \n \n string length(?)=13 and starts with(?, \"560\")\n \n \n \"Portugal\"\n \n \n \n \n \nmatches(?, \"^9[0-1]{1}\\d{10}$\")\n \n \n \"AT\"\n \n \n \n \n matches(?, \"^9[0-1]{1}\\d{10}$\")\n \n \n \"Austria\"\n \n \n \n \n\n" + }, + "parameters": [ + { + "id": "pair_d55b3a61f8fc4ad89303e5b70fc19df6", + "name": "size", + "value": "1000", + "description": "", + "disabled": false + } + ], + "headers": [ + { "name": "Content-Type", "value": "application/xml" }, + { "name": "User-Agent", "value": "insomnia/10.0.0" } + ], + "authentication": {}, + "metaSortKey": -1727867720880, + "isPrivate": false, + "pathParameters": [], + "settingStoreCookies": true, + "settingSendCookies": true, + "settingDisableRenderRequestBody": false, + "settingEncodeUrl": true, + "settingRebuildPath": true, + "settingFollowRedirects": "global", + "_type": "request" + }, + { + "_id": "req_53d1a1f0862d4cb99e5572c1dd30967b", + "parentId": "wrk_b6f7ea92d2f443b2bf923a99dc6fca88", + "modified": 1728900408292, + "created": 1728900124584, + "url": "http://localhost:4321/randomize/world-cities", + "name": "randomize/world-cities", + "description": "", + "method": "POST", + "body": { + "mimeType": "application/xml", + "text": "\n\n \n \n \n \n GeonameID\n \n \n \n \n Cities with geonameId between 2967245 and 3038789 are in France\n \n includes(2967245, 3038789)\n \n \n \"France\"\n \n \n \n Cities with geonameId between 3104316 and 3130616 are in Spain\n \n includes(3104316, 3130616)\n \n \n \"Spain\"\n \n \n \n Cities with geonameId between 5881791 and 6185607 are in Spain\n \n includes(5881791,6185607)\n \n \n \"Canada\"\n \n \n \n \n\n" + }, + "parameters": [ + { + "id": "pair_d55b3a61f8fc4ad89303e5b70fc19df6", + "name": "size", + "value": "1000", + "description": "", + "disabled": false + } + ], + "headers": [ + { "name": "Content-Type", "value": "application/xml" }, + { "name": "User-Agent", "value": "insomnia/10.0.0" } + ], + "authentication": {}, + "metaSortKey": -1727867720830, + "isPrivate": false, + "pathParameters": [], + "settingStoreCookies": true, + "settingSendCookies": true, + "settingDisableRenderRequestBody": false, + "settingEncodeUrl": true, + "settingRebuildPath": true, + "settingFollowRedirects": "global", + "_type": "request" + }, + { + "_id": "env_025a059466ea4c5d1a14c9fd8028c0bb99039906", + "parentId": "wrk_b6f7ea92d2f443b2bf923a99dc6fca88", + "modified": 1727867693581, + "created": 1727867693581, + "name": "Base Environment", + "data": {}, + "dataPropertyOrder": null, + "color": null, + "isPrivate": false, + "metaSortKey": 1727867693581, + "_type": "environment" + }, + { + "_id": "jar_025a059466ea4c5d1a14c9fd8028c0bb99039906", + "parentId": "wrk_b6f7ea92d2f443b2bf923a99dc6fca88", + "modified": 1727867693582, + "created": 1727867693582, + "name": "Default Jar", + "cookies": [], + "_type": "cookie_jar" + } + ] +} diff --git a/src/routes/randomize.ts b/src/routes/randomize.ts index 148fe41..108eb5c 100644 --- a/src/routes/randomize.ts +++ b/src/routes/randomize.ts @@ -7,7 +7,32 @@ import { Data } from "../services/data"; const router = Router(); router.post("/randomize", (req: Request, res: Response) => { - res.status(200).json({ status: "RANDOMIZED", data: [{}] }); + const size: number = req.query.size + ? parseInt(req.query.size as string) + : 1000; + + const datasetID = DatasetCollection.datasets.map((dataset) => dataset.id); + + Promise.all( + datasetID.map((id) => { + const url: URL = new URL(`http://localhost:4321/randomize/${id}`); + url.searchParams.append("size", size.toString()); + return fetch(url, { + method: "POST", + body: req.body, + headers: { "Content-Type": "application/xml" }, + }) + .then((response) => response.json()) + .then((json: any) => json.data); + }) + ).then((r) => { + const data = r + .flat() + .sort(() => Math.random() - 0.5) + .slice(0, size); + + res.status(200).json({ status: "RANDOMIZED", data }); + }); }); router.post("/randomize/:id", async (req: Request, res: Response) => {