Skip to content

Commit

Permalink
POC allow to launch a recice from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mestachs committed Oct 7, 2023
1 parent 9028a8b commit 87ce529
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 30 deletions.
87 changes: 87 additions & 0 deletions cli/taskr
Original file line number Diff line number Diff line change
@@ -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()
27 changes: 1 addition & 26 deletions src/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions src/support/DataSets.js
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions src/support/Dhis2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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 =
Expand Down

0 comments on commit 87ce529

Please sign in to comment.