feat!: Save the converted dataset

/!\ Need to clear the cache folder
This commit is contained in:
Lucàs
2024-10-04 09:23:21 +02:00
parent 9428208d9f
commit f3f2234fdc
3 changed files with 23 additions and 20 deletions
+4 -5
View File
@@ -1,6 +1,5 @@
import { Router, Request, Response } from "express"; import { Router, Request, Response } from "express";
import { Dataset, DatasetCollection } from "../services/dataset"; import { DatasetCollection } from "../services/dataset";
import { Data } from "../services/data";
import DmnModdle from "dmn-moddle"; import DmnModdle from "dmn-moddle";
import { DMN } from "../services/dmn/DMN"; 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" }); 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); console.log(rootElement);
const data = await dataset.get(size); 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; export default router;
+16 -12
View File
@@ -1,28 +1,31 @@
import { pipeline, Transform } from "node:stream"; import { pipeline, Transform } from "node:stream";
import { promisify } from "node:util"; import { promisify } from "node:util";
import * as fs from "node:fs";
import * as readline from "node:readline";
import CacheService from "../CacheService"; import CacheService from "../CacheService";
import FileService from "../FileService"; import FileService from "../FileService";
import { ArchiveFactory, ArchiveType } from "../archive"; import { ArchiveFactory, ArchiveType } from "../archive";
import { ParserFactory } from "../parser"; import { ParserFactory } from "../parser";
import { DatasetType } from "./"; import { DatasetType } from "./";
import * as fs from "node:fs"; import { Data } from "../data";
import * as readline from "node:readline";
/** /**
* Represents a dataset that can be loaded and queried * Represents a dataset that can be loaded and queried
*/ */
class Dataset<D> { class Dataset {
readonly id: string; readonly id: string;
readonly url: string; readonly url: string;
readonly sourceFile: string; readonly sourceFile: string;
readonly archiveType: ArchiveType; readonly archiveType: ArchiveType;
readonly datasetType: DatasetType; readonly datasetType: DatasetType;
readonly cachePath: string; readonly cachePath: string;
private dConstructor: { new (rawData: any): D }; private dataConstructor: { new (rawData: object): Data };
/** /**
* Create a new dataset instance * 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 id - The unique identifier of the dataset
* @param url - The URL of the dataset * @param url - The URL of the dataset
* @param sourceFile - The file name of the dataset in the archive * @param sourceFile - The file name of the dataset in the archive
@@ -30,14 +33,14 @@ class Dataset<D> {
* @param datasetType - The type of the dataset * @param datasetType - The type of the dataset
*/ */
constructor( constructor(
dConstructor: new (rawData: any) => D, dataConstructor: new (rawData: any) => Data,
id: string, id: string,
url: string, url: string,
sourceFile: string, sourceFile: string,
archiveType: ArchiveType, archiveType: ArchiveType,
datasetType: DatasetType datasetType: DatasetType
) { ) {
this.dConstructor = dConstructor; this.dataConstructor = dataConstructor;
this.id = id; this.id = id;
this.url = url; this.url = url;
this.sourceFile = sourceFile; this.sourceFile = sourceFile;
@@ -73,7 +76,9 @@ class Dataset<D> {
new Transform({ new Transform({
objectMode: true, objectMode: true,
transform(chunk: object, _, callback) { 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"); this.push(JSON.stringify(data) + "\n");
callback(null, JSON.stringify(data) + "\n"); callback(null, JSON.stringify(data) + "\n");
}, },
@@ -86,10 +91,10 @@ class Dataset<D> {
* Get a number of data entries from the dataset * Get a number of data entries from the dataset
* @param length - The number of data entries to get (default: 10) * @param length - The number of data entries to get (default: 10)
*/ */
public get(length: number = 10): Promise<D[]> { public get(length: number = 10): Promise<Data[]> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let count: number = 0; let count: number = 0;
const results: D[] = []; const results: Data[] = [];
const stream = fs.createReadStream(this.cachePath, { encoding: "utf8" }); const stream = fs.createReadStream(this.cachePath, { encoding: "utf8" });
const rl = readline.createInterface({ const rl = readline.createInterface({
@@ -100,8 +105,7 @@ class Dataset<D> {
rl.on("line", (line) => { rl.on("line", (line) => {
if (count < length) { if (count < length) {
try { try {
const obj = JSON.parse(line); results.push(JSON.parse(line) as Data);
results.push(new this.dConstructor(obj));
count++; count++;
} catch (err) { } catch (err) {
console.error("Erreur lors du parsing de la ligne:", err); console.error("Erreur lors du parsing de la ligne:", err);
+3 -3
View File
@@ -1,10 +1,10 @@
import { Data, NudgerData } from "../data"; import { NudgerData } from "../data";
import { ArchiveType } from "../archive"; import { ArchiveType } from "../archive";
import { Dataset, DatasetType } from "./"; import { Dataset, DatasetType } from "./";
class DatasetCollection { class DatasetCollection {
public static datasets: Dataset<Data>[] = [ public static datasets: Dataset[] = [
new Dataset<NudgerData>( new Dataset(
NudgerData, NudgerData,
"nudger", "nudger",
"https://files.opendatarchives.fr/data.cquest.org/open4goods/gtin-open-data.zip", "https://files.opendatarchives.fr/data.cquest.org/open4goods/gtin-open-data.zip",