From f3f2234fdc903fa8f3cee38c0fc7dd2b174d6266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luc=C3=A0s?= <86352901+LucasVbr@users.noreply.github.com> Date: Fri, 4 Oct 2024 09:23:21 +0200 Subject: [PATCH] feat!: Save the converted dataset /!\ Need to clear the cache folder --- src/routes/randomize.ts | 9 ++++---- src/services/dataset/Dataset.ts | 28 +++++++++++++---------- src/services/dataset/DatasetCollection.ts | 6 ++--- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/routes/randomize.ts b/src/routes/randomize.ts index 857e0f3..57e0a36 100644 --- a/src/routes/randomize.ts +++ b/src/routes/randomize.ts @@ -1,6 +1,5 @@ import { Router, Request, Response } from "express"; -import { Dataset, DatasetCollection } from "../services/dataset"; -import { Data } from "../services/data"; +import { DatasetCollection } from "../services/dataset"; import DmnModdle from "dmn-moddle"; import { DMN } from "../services/dmn/DMN"; @@ -22,14 +21,14 @@ router.post("/randomize/:id", async (req: Request, res: Response) => { ); if (!dataset) return res.status(404).json({ status: "NOT_FOUND" }); - const a = await DMN.parse(req.body); + const a: any = await DMN.parse(req.body); - const { rootElement, warnings } = await new DmnModdle().fromXML(req.body); + const { rootElement } = await new DmnModdle().fromXML(req.body); console.log(rootElement); const data = await dataset.get(size); - return res.status(200).json({ status: "RANDOMIZED", data, a }); + return res.status(200).json({ status: "RANDOMIZED", data }); }); export default router; diff --git a/src/services/dataset/Dataset.ts b/src/services/dataset/Dataset.ts index 1bad306..4527405 100644 --- a/src/services/dataset/Dataset.ts +++ b/src/services/dataset/Dataset.ts @@ -1,28 +1,31 @@ import { pipeline, Transform } from "node:stream"; import { promisify } from "node:util"; +import * as fs from "node:fs"; +import * as readline from "node:readline"; + import CacheService from "../CacheService"; import FileService from "../FileService"; + import { ArchiveFactory, ArchiveType } from "../archive"; import { ParserFactory } from "../parser"; import { DatasetType } from "./"; -import * as fs from "node:fs"; -import * as readline from "node:readline"; +import { Data } from "../data"; /** * Represents a dataset that can be loaded and queried */ -class Dataset { +class Dataset { readonly id: string; readonly url: string; readonly sourceFile: string; readonly archiveType: ArchiveType; readonly datasetType: DatasetType; readonly cachePath: string; - private dConstructor: { new (rawData: any): D }; + private dataConstructor: { new (rawData: object): Data }; /** * Create a new dataset instance - * @param dConstructor - The constructor of the data class + * @param dataConstructor - The constructor of the data class * @param id - The unique identifier of the dataset * @param url - The URL of the dataset * @param sourceFile - The file name of the dataset in the archive @@ -30,14 +33,14 @@ class Dataset { * @param datasetType - The type of the dataset */ constructor( - dConstructor: new (rawData: any) => D, + dataConstructor: new (rawData: any) => Data, id: string, url: string, sourceFile: string, archiveType: ArchiveType, datasetType: DatasetType ) { - this.dConstructor = dConstructor; + this.dataConstructor = dataConstructor; this.id = id; this.url = url; this.sourceFile = sourceFile; @@ -73,7 +76,9 @@ class Dataset { new Transform({ objectMode: true, transform(chunk: object, _, callback) { - const data: D = new self.dConstructor(JSON.parse(chunk.toString())); + const data: Data = new self.dataConstructor( + JSON.parse(chunk.toString()) + ); this.push(JSON.stringify(data) + "\n"); callback(null, JSON.stringify(data) + "\n"); }, @@ -86,10 +91,10 @@ class Dataset { * Get a number of data entries from the dataset * @param length - The number of data entries to get (default: 10) */ - public get(length: number = 10): Promise { + public get(length: number = 10): Promise { return new Promise((resolve, reject) => { let count: number = 0; - const results: D[] = []; + const results: Data[] = []; const stream = fs.createReadStream(this.cachePath, { encoding: "utf8" }); const rl = readline.createInterface({ @@ -100,8 +105,7 @@ class Dataset { rl.on("line", (line) => { if (count < length) { try { - const obj = JSON.parse(line); - results.push(new this.dConstructor(obj)); + results.push(JSON.parse(line) as Data); count++; } catch (err) { console.error("Erreur lors du parsing de la ligne:", err); diff --git a/src/services/dataset/DatasetCollection.ts b/src/services/dataset/DatasetCollection.ts index d83a7d7..4c08836 100644 --- a/src/services/dataset/DatasetCollection.ts +++ b/src/services/dataset/DatasetCollection.ts @@ -1,10 +1,10 @@ -import { Data, NudgerData } from "../data"; +import { NudgerData } from "../data"; import { ArchiveType } from "../archive"; import { Dataset, DatasetType } from "./"; class DatasetCollection { - public static datasets: Dataset[] = [ - new Dataset( + public static datasets: Dataset[] = [ + new Dataset( NudgerData, "nudger", "https://files.opendatarchives.fr/data.cquest.org/open4goods/gtin-open-data.zip",