Skip to content

Commit

Permalink
feat: add log 10 logic (#193)
Browse files Browse the repository at this point in the history
* add gitpod

* feat: add log10

* format

* fix format
  • Loading branch information
DanielHabenicht authored Jun 19, 2021
1 parent 6aa72dd commit 184c924
Show file tree
Hide file tree
Showing 56 changed files with 460 additions and 504 deletions.
13 changes: 13 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/
tasks:
- name: Prepare
init: |
npm install
cd covquestions-editor-app
npm run installAndLink
command: echo 'start script'

# List the ports to expose. Learn more https://www.gitpod.io/docs/config-ports/
ports:
- port: 3000
onOpen: notify
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": false
"singleQuote": false,
"printWidth": 120
}
6 changes: 1 addition & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}/covquestions-js",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/covquestions-js/node_modules/jest/bin/jest.js",
"--runInBand"
],
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/covquestions-js/node_modules/jest/bin/jest.js", "--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
Expand Down
54 changes: 54 additions & 0 deletions api/v1/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,30 @@
"title": "LogicConvertToDateString",
"type": "object"
},
"Log10": {
"properties": {
"log10": {
"anyOf": [
{
"$ref": "#/components/schemas/LogicExpression"
},
{
"items": {
"$ref": "#/components/schemas/LogicExpression"
},
"maxItems": 1,
"minItems": 1,
"type": "array"
}
]
}
},
"required": [
"log10"
],
"title": "Log10",
"type": "object"
},
"Round": {
"properties": {
"round": {
Expand Down Expand Up @@ -868,6 +892,9 @@
{
"$ref": "#/components/schemas/LogicConvertToDateString"
},
{
"$ref": "#/components/schemas/Log10"
},
{
"$ref": "#/components/schemas/Round"
}
Expand Down Expand Up @@ -1889,6 +1916,9 @@
{
"$ref": "#/components/schemas/LogicConvertToDateString"
},
{
"$ref": "#/components/schemas/Log10"
},
{
"$ref": "#/components/schemas/Round"
}
Expand Down Expand Up @@ -2020,6 +2050,30 @@
"title": "LogicConvertToDateString",
"type": "object"
},
"Log10": {
"properties": {
"log10": {
"anyOf": [
{
"$ref": "#/components/schemas/LogicExpression"
},
{
"items": {
"$ref": "#/components/schemas/LogicExpression"
},
"maxItems": 1,
"minItems": 1,
"type": "array"
}
]
}
},
"required": [
"log10"
],
"title": "Log10",
"type": "object"
},
"Round": {
"properties": {
"round": {
Expand Down
24 changes: 4 additions & 20 deletions api/v1/scripts/pre_build_add_new_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ import { convert } from "xmlbuilder2";
import * as glob from "fast-glob";
import { validate } from "../src/validate";
import { SOURCE_PATHS } from "../src/index";
import {
md5,
doOnEachTranslation,
getStringRessource,
TranslationMap,
readI18nFile,
} from "../src/utility";
import { md5, doOnEachTranslation, getStringRessource, TranslationMap, readI18nFile } from "../src/utility";
import { Questionnaire } from "../src/models/Questionnaire.generated";

const xmlBase = {
Expand All @@ -32,17 +26,12 @@ const xmlBase = {
* @param inputGlob Input Folder, where to import the questionnaire from
* @param outDir The data Folder, where the consolidated Questionnaires are versioned
*/
export function pre_build_add_new_file(
inputGlob = "./input/*.json",
outDir = "./src/data"
) {
export function pre_build_add_new_file(inputGlob = "./input/*.json", outDir = "./src/data") {
let newQuestionnairePaths = glob.sync(inputGlob);
newQuestionnairePaths.forEach((q) => validate(q));

newQuestionnairePaths.forEach((path) => {
let srcQuestionnaire = JSON.parse(
fs.readFileSync(path, { encoding: "utf-8" })
) as Questionnaire;
let srcQuestionnaire = JSON.parse(fs.readFileSync(path, { encoding: "utf-8" })) as Questionnaire;
let dataQuestionnairePath = `${outDir}${SOURCE_PATHS.QUESTIONNAIRES}/${srcQuestionnaire.id}/`;
let dataI18nMainFile = `${dataQuestionnairePath}i18n/translation.${srcQuestionnaire.language}.xlf`;
let dataPath = `${dataQuestionnairePath}${srcQuestionnaire.version}/${srcQuestionnaire.id}-${srcQuestionnaire.version}.json`;
Expand Down Expand Up @@ -108,12 +97,7 @@ export function writeI18nFile(
fs.outputFileSync(path, convert(xmlBase, { prettyPrint: true }));
}

export function addTranslationToMap(
map: TranslationMap,
str: string,
obj: Object,
key: string
) {
export function addTranslationToMap(map: TranslationMap, str: string, obj: Object, key: string) {
// We should't use md5 from source, if source is updated all other references are lost.
// TODO: Make sure every object with strings for translation has a unqiue id (this makes reusing the objects easier aswell)
let [trans, id] = getStringRessource(str);
Expand Down
14 changes: 3 additions & 11 deletions api/v1/scripts/pre_build_fixate_translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import { i18n_extract, writeI18nFile } from "./pre_build_add_new_file";
export function pre_build_fixate_translations(dataDir = "./src/data") {
let allQuestionnaires = glob.sync(`${dataDir}/**/*.json`);
allQuestionnaires.forEach((questionnairePath) => {
let questionnaire = JSON.parse(
fs.readFileSync(questionnairePath, { encoding: "utf-8" })
) as Questionnaire;
let questionnaire = JSON.parse(fs.readFileSync(questionnairePath, { encoding: "utf-8" })) as Questionnaire;
// Example "data/questionnaires/questionnaireId"
let questionnaireDir = questionnairePath.split("/").slice(0, -2).join("/");
// Example "data/questionnaires/questionnaireId/version/"
Expand All @@ -33,14 +31,8 @@ export function pre_build_fixate_translations(dataDir = "./src/data") {
let filteredSourceMap = replaceTranslationMap(sourceMap, source);
let filteredTargetMap = replaceTranslationMap(sourceMap, target);

if (
Object.keys(filteredSourceMap).every(
(t) => filteredSourceMap[t] == undefined
)
) {
console.warn(
"WARNING: The created Translations are empty. Did you use the wrong source language?"
);
if (Object.keys(filteredSourceMap).every((t) => filteredSourceMap[t] == undefined)) {
console.warn("WARNING: The created Translations are empty. Did you use the wrong source language?");
}

writeI18nFile(
Expand Down
18 changes: 5 additions & 13 deletions api/v1/src/data/questionnaires/covapp_new/1/covapp_new-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"language": "de",
"title": "CovApp@@9e7328e5d5c11038c69b727bc7efcd8d",
"meta": {
"availableLanguages": [
"de"
],
"availableLanguages": ["de"],
"author": "Alexander Thieme",
"creationDate": "2021-05-06"
},
Expand Down Expand Up @@ -2279,9 +2277,7 @@
"q_pcrtest": "no",
"q_antigentestresult": "negative",
"q_contact_tracing_risk_contacts": "yes_high_risk",
"q_immunosuppressants": [
"chemo"
],
"q_immunosuppressants": ["chemo"],
"q_antigentest": "yes",
"q_vaccine": "yes",
"q_vaccine_completed": "yes",
Expand Down Expand Up @@ -2492,9 +2488,7 @@
"q_antigentestresult": "positive",
"q_age": "yes",
"q_pcrtest": "no",
"q_symptoms": [
"chills"
],
"q_symptoms": ["chills"],
"q_symptoms_date": 1620259200
},
"results": {
Expand All @@ -2512,9 +2506,7 @@
"q_antigentest": "no",
"q_age": "yes",
"q_pcrtest": "no",
"q_symptoms": [
"chills"
],
"q_symptoms": ["chills"],
"q_symptoms_date": 1620259200
},
"results": {
Expand Down Expand Up @@ -2545,4 +2537,4 @@
}
}
]
}
}
67 changes: 13 additions & 54 deletions api/v1/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import * as fs from "fs-extra";
import {
ISOLanguage,
Question,
Questionnaire,
QuestionnaireMeta,
} from "./models/Questionnaire.generated";
import { ISOLanguage, Question, Questionnaire, QuestionnaireMeta } from "./models/Questionnaire.generated";
import * as glob from "fast-glob";
import { validate } from "./validate";
import {
doOnEachTranslation,
getDirectories,
getStringRessource,
readI18nFile,
writeJSONFile,
} from "./utility";
import { doOnEachTranslation, getDirectories, getStringRessource, readI18nFile, writeJSONFile } from "./utility";

class TranslationNotCompleteError extends Error {
constructor(m: string) {
Expand Down Expand Up @@ -41,19 +30,10 @@ export function main(pwd: string = "./src/data", outputDir: string = "./dist") {

// Build all Questionnaires
console.log("Building the static API:");
let questionnaireDirs = getDirectories(
`${pwd}${SOURCE_PATHS.QUESTIONNAIRES}`
);
let questionnaireDirs = getDirectories(`${pwd}${SOURCE_PATHS.QUESTIONNAIRES}`);
questionnaireDirs.forEach((questionnaireId) => {
console.log(` - Building "${questionnaireId}"`);
index = [
...index,
...buildQuestionnaire(
`${pwd}${SOURCE_PATHS.QUESTIONNAIRES}`,
questionnaireId,
outputDir
),
];
index = [...index, ...buildQuestionnaire(`${pwd}${SOURCE_PATHS.QUESTIONNAIRES}`, questionnaireId, outputDir)];
});

// Index Document
Expand All @@ -73,10 +53,7 @@ export function main(pwd: string = "./src/data", outputDir: string = "./dist") {
Object.keys(indexMap).map((key) =>
translateObject(
indexMap[key],
index.find(
(q) =>
q.id === indexMap[key].id && q.version === indexMap[key].version
).languages.en
index.find((q) => q.id === indexMap[key].id && q.version === indexMap[key].version).languages.en
)
)
);
Expand Down Expand Up @@ -106,23 +83,17 @@ export function buildQuestionnaire(
questionnaireId: string,
outputPath: string
): QuestionnaireWithLanguages[] {
let questionnaireFilePaths = glob.sync(
`${sourceBaseDir}/${questionnaireId}/**/*.json`
);
let questionnaireFilePaths = glob.sync(`${sourceBaseDir}/${questionnaireId}/**/*.json`);

let index: Questionnaire[] = [];
let indexWithLanguages: QuestionnaireWithLanguages[] = [];
/**
* Generate the questionnaire JSON files
*/
questionnaireFilePaths.forEach((path) => {
let questionnaire: Questionnaire = JSON.parse(
fs.readFileSync(path, "utf-8")
);
let questionnaire: Questionnaire = JSON.parse(fs.readFileSync(path, "utf-8"));

let translationFilePaths = glob.sync(
`${path.split("/").slice(0, -1).join("/")}/*.xlf`
);
let translationFilePaths = glob.sync(`${path.split("/").slice(0, -1).join("/")}/*.xlf`);

// Retrieving available languages
let languages: Language[] = translationFilePaths.map((p) => {
Expand All @@ -145,19 +116,14 @@ export function buildQuestionnaire(

// Test for same Ids
if (questionnaire.id != questionnaireId) {
throw new Error(
`Id of Folder ("${questionnaireId}") does not match id "${questionnaire.id}" of ${path}`
);
throw new Error(`Id of Folder ("${questionnaireId}") does not match id "${questionnaire.id}" of ${path}`);
}
questionnaire.meta.availableLanguages = languages.map((l) => l.id);

// Write Language Specific Questionnaire Files
languages.forEach((lang) => {
try {
const translatedQuestionnaire = translateQuestionnaire(
questionnaire,
lang
);
const translatedQuestionnaire = translateQuestionnaire(questionnaire, lang);

index.push(JSON.parse(JSON.stringify(questionnaire)));
indexWithLanguages.push(
Expand Down Expand Up @@ -206,9 +172,7 @@ export function buildQuestionnaire(
/**
* Generate Main Language Files
*/
let translationFilePaths = glob.sync(
`${sourceBaseDir}/${questionnaireId}/i18n/*.xlf`
);
let translationFilePaths = glob.sync(`${sourceBaseDir}/${questionnaireId}/i18n/*.xlf`);

// Retrieving available languages
let languages: Language[] = translationFilePaths.map((p) => {
Expand Down Expand Up @@ -237,10 +201,7 @@ export function translateQuestionnaire(
return translateObject(q, lang);
}

export function translateObject<T = any>(
o: T,
lang: Language = { id: "none", path: null, translations: {} }
): T {
export function translateObject<T = any>(o: T, lang: Language = { id: "none", path: null, translations: {} }): T {
// Dereferrencing the object so there are no side effects
o = JSON.parse(JSON.stringify(o));

Expand All @@ -255,9 +216,7 @@ export function translateObject<T = any>(
let translation = lang.translations[id];
if (translation == null) {
throw new TranslationNotCompleteError(
`Questionnaire with id "${
(o as any).id
}" could not be translated, because there is no translation in "${
`Questionnaire with id "${(o as any).id}" could not be translated, because there is no translation in "${
lang.id
}" for "${key}" (${value})`
);
Expand Down
4 changes: 4 additions & 0 deletions api/v1/src/models/Questionnaire.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export type LogicOperator =
| LogicGreater
| LogicLess
| LogicConvertToDateString
| Log10
| Round;
export type LogicConstant = number | string | boolean;
/**
Expand Down Expand Up @@ -394,6 +395,9 @@ export interface LogicLess {
export interface LogicConvertToDateString {
convert_to_date_string: [LogicExpression, LogicExpression];
}
export interface Log10 {
log10: LogicExpression | [LogicExpression];
}
export interface Round {
round: LogicExpression | [LogicExpression];
}
Expand Down
Loading

0 comments on commit 184c924

Please sign in to comment.