From 87ce52975dc2efe5ea931785ce836e8712300173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Mestach?= Date: Sat, 7 Oct 2023 21:05:18 +0200 Subject: [PATCH] POC allow to launch a recice from cli --- cli/taskr | 87 +++++++++++++++++++++++++++++++++++++++++ src/Editor.js | 27 +------------ src/support/DataSets.js | 29 ++++++++++++++ src/support/Dhis2.js | 8 ++-- 4 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 cli/taskr create mode 100644 src/support/DataSets.js diff --git a/cli/taskr b/cli/taskr new file mode 100644 index 0000000..64c0df2 --- /dev/null +++ b/cli/taskr @@ -0,0 +1,87 @@ + +import DatePeriods from "../src/support/DatePeriods"; +import JSONApi from "../src/support/JSONApi"; +import XlsxPopulate from "../src/support/XlsxPopulateOpenAsBlob" +import Dhis2 from "../src/support/Dhis2"; +import * as turf from "@turf/turf"; +import papaparse from 'papaparse' +import DataSets from "../src/support/DataSets"; + + +const recipe = { + id: "D5a1DVMw7F1", + name: "empty", + editable: true, + code: ` + const api = await dhis2.api(); + const ou = await api.get("organisationUnits", { + fields: "id,name,ancestors[id,name],geometry" + }); + + return _.flattenObjects(ou.organisationUnits, ["geometry"]); + `, + } + +const AsyncFunction = Object.getPrototypeOf(async function() {}).constructor; + + +const startup = async () => { + const dhis2 = new Dhis2(); + const parameters = {} + const report = new DataSets() + + + const workbook = await XlsxPopulate.fromFileAsync(process.argv[2]) + console.log(workbook.sheets().map(s => s.name())) + + + const body = recipe.code.includes("return ") + ? recipe.code + : "return " + recipe.code; + const libs = [ + { identifier: "dhis2", entryPoint: async () => dhis2 }, + + { + identifier: "_", + entryPoint: async () => { + const lodash = await import("../src/support/lodash"); + return lodash.default; + }, + }, + { + identifier: "turf", + entryPoint: async () => { + return turf.default; + }, + }, + { + identifier: "Fuse", + entryPoint: async () => { + const Fuse = await import("fuse.js"); + return Fuse.default; + }, + }, + + { identifier: "XlsxPopulate", entryPoint: async () => XlsxPopulate }, + { identifier: "DatePeriods", entryPoint: async () => DatePeriods }, + { identifier: "parameters", entryPoint: async () => parameters }, + { identifier: "report", entryPoint: async () => report }, + { identifier: "JSONApi", entryPoint: async () => JSONApi }, + ]; + const entryPoints = []; + for (let entryPoint of libs.map((l) => l.entryPoint)) { + entryPoints.push(await entryPoint()); + } + + + console.log(body) + const results = await new AsyncFunction( + ...libs.map((l) => l.identifier), + body + )(...entryPoints); + console.log(results) + +} +console.log("hello", process.argv) + +startup() \ No newline at end of file diff --git a/src/Editor.js b/src/Editor.js index 0cf3d03..e2ba4f1 100644 --- a/src/Editor.js +++ b/src/Editor.js @@ -35,6 +35,7 @@ import FormControl from "@material-ui/core/FormControl"; import MenuItem from "@material-ui/core/MenuItem"; import Select from "@material-ui/core/Select"; +import DataSets from "./support/DataSets"; import DeleteButton from "./DeleteButton"; @@ -64,33 +65,7 @@ const interceptor = FetchInterceptor.register({ const markup = ``; -class DataSets { - constructor() { - this.registeredCount = 0; - this.datasets = {}; - } - register(datasetName, data) { - this.datasets[datasetName] = data; - this.registeredCount += 1; - return this; - } - reset(mode) { - for (var member in this.datasets) { - delete this.datasets[member]; - } - this.registeredCount = 0; - if (mode == "run" && this.reRun) { - this.reRun(); - } - if (mode == "clear" && this.clearResults) { - this.clearResults(); - } - } - asVars() { - return this.datasets; - } -} function Editor({ recipe, dhis2, onSave, editable, autorun }) { const [showEditor, setShowEditor] = useState(recipe.editable); diff --git a/src/support/DataSets.js b/src/support/DataSets.js new file mode 100644 index 0000000..eb08a3b --- /dev/null +++ b/src/support/DataSets.js @@ -0,0 +1,29 @@ +class DataSets { + constructor() { + this.registeredCount = 0; + this.datasets = {}; + } + register(datasetName, data) { + this.datasets[datasetName] = data; + this.registeredCount += 1; + return this; + } + reset(mode) { + for (var member in this.datasets) { + delete this.datasets[member]; + } + this.registeredCount = 0; + if (mode == "run" && this.reRun) { + this.reRun(); + } + if (mode == "clear" && this.clearResults) { + this.clearResults(); + } + } + + asVars() { + return this.datasets; + } +} + +export default DataSets diff --git a/src/support/Dhis2.js b/src/support/Dhis2.js index 48cdeb8..ee2d47f 100644 --- a/src/support/Dhis2.js +++ b/src/support/Dhis2.js @@ -10,9 +10,9 @@ class Dhis2 { */ constructor(argOptions) { const options = argOptions || {}; - this.url = options.url || process.env.REACT_APP_DHIS2_URL; - this.user = options.user || process.env.REACT_APP_USER; - this.password = options.password || process.env.REACT_APP_PASSWORD; + this.url = options.url || process.env.REACT_APP_DHIS2_URL || process.env.DHIS2_URL; + this.user = options.user || process.env.REACT_APP_USER || process.env.DHIS2_USER + this.password = options.password || process.env.REACT_APP_PASSWORD || process.env.DHIS2_PASSWORD; this.contractGroupId = options.contractGroupId || process.env.REACT_APP_CONTRACT_OU_GROUP; this.cache = []; @@ -39,7 +39,7 @@ class Dhis2 { Authorization: "Basic " + btoa(this.user + ":" + this.password) } : null; - console.info("Using node env: " + process.env.NODE_ENV); + console.info("Using node env: " + process.env.NODE_ENV, this.url); this.d2 = getManifest("./manifest.webapp") .then(manifest => { let baseUrl =