Skip to content

Commit

Permalink
Cache all calculated variables (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grafikart authored Mar 8, 2024
1 parent d6c9fd4 commit 1f4a36e
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/use-lunatic/commons/variables/lunatic-variables-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ export class LunaticVariablesStore {
if (name === 'yAxis') return 1;
return undefined;
};
store.set('1', 1); // Fake variable to use on the shapeFrom, we will use "variableDimension" in the future
for (const variable of source.variables) {
switch (variable.variableType) {
case 'CALCULATED':
store.setCalculated(variable.name, variable.expression.value, {
dependencies: variable.bindingDependencies,
iterationDepth: getIterationDepth(variable.name),
shapeFrom: variable.shapeFrom,
shapeFrom: variable.shapeFrom ?? '1',
});
break;
case 'COLLECTED':
Expand Down Expand Up @@ -193,6 +194,28 @@ export class LunaticVariablesStore {
get interpretCount() {
return interpretCount;
}

// Displays a table of the most calculated variable (useful for debug)
debug() {
console.table(
Array.from(this.dictionary.values())
.sort((a, b) => b.calculatedCount - a.calculatedCount)
.slice(0, 25)
.map((v) => ({
name: v.name,
calculations: v.calculatedCount,
expression: v.expression,
}))
);
console.log(
'Total calculations : ' +
Array.from(this.dictionary.values()).reduce(
(acc, v) => acc + v.calculatedCount,
0
)
);
Array.from(this.dictionary.values()).map((v) => (v.calculatedCount = 0));
}
}

class LunaticVariable {
Expand All @@ -205,15 +228,17 @@ class LunaticVariable {
// List of dependencies, ex: ['FIRSTNAME', 'LASTNAME']
private dependencies?: string[];
// Expression for calculated variable
private readonly expression?: string;
public readonly expression?: string;
// Dictionary holding all the available variables
private readonly dictionary?: Map<string, LunaticVariable>;
// Specific iteration depth to get value from dependencies (used for yAxis for instance)
private readonly iterationDepth?: number;
// For calculated variable, shape is copied from another variable
private readonly shapeFrom?: string;
// Keep a record of variable name (optional, used for debug)
private readonly name?: string;
public readonly name?: string;
// Count the number of calculation
public calculatedCount = 0;

constructor(
args: {
Expand Down Expand Up @@ -274,6 +299,8 @@ class LunaticVariable {
if (isTestEnv()) {
interpretCount++;
}
// Uncomment this if you want to track the number of calculation
// this.calculatedCount++;
// Remember the value
try {
this.setValue(interpretVTL(this.expression, bindings), iteration);
Expand Down

0 comments on commit 1f4a36e

Please sign in to comment.