-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3213 paneuropean linked nodes shows wrong data 1 (#3218)
* generate meta cache for all assessments * refactor generate meta cache context * use Objects.setInPath in member * fix Objects.setInPath * add cross cycle dependant/dependencies * update jest config
- Loading branch information
Showing
8 changed files
with
150 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
...r/controller/assessment/generateMetaCache/dependencyEvaluator/evalDependencies/context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { ExpressionContext } from '@openforis/arena-core' | ||
|
||
import { AssessmentMetaCache, Row } from 'meta/assessment' | ||
import { Assessment, AssessmentName, CycleName, RowCache } from 'meta/assessment' | ||
|
||
export interface Context extends ExpressionContext { | ||
assessmentMetaCache: AssessmentMetaCache | ||
row: Row | ||
tableName: string | ||
assessments: Array<Assessment> | ||
assessmentName: AssessmentName | ||
cycleName: CycleName | ||
row: RowCache | ||
type: 'calculations' | 'validations' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 76 additions & 48 deletions
124
src/server/controller/assessment/generateMetaCache/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,100 @@ | ||
import { Assessment, AssessmentMetaCache, Cycle } from 'meta/assessment' | ||
import { Objects } from 'utils/objects' | ||
import { Promises } from 'utils/promises' | ||
|
||
import { BaseProtocol } from 'server/db' | ||
import { AssessmentMetaCache, AssessmentName, RowCache } from 'meta/assessment' | ||
|
||
import { BaseProtocol, DB } from 'server/db' | ||
import { AssessmentRepository } from 'server/repository/assessment/assessment' | ||
import { RowRepository } from 'server/repository/assessment/row' | ||
import { ValueAggregateRepository } from 'server/repository/assessmentCycle/valueAggregate' | ||
|
||
import { DependencyEvaluator } from './dependencyEvaluator' | ||
|
||
type Props = { | ||
assessment: Assessment | ||
cycle: Cycle | ||
} | ||
/** | ||
* This method generates meta cache for all assessments | ||
*/ | ||
export const generateMetaCache = async (client: BaseProtocol = DB): Promise<void> => { | ||
// 1. init assessments meta cache and rows | ||
const assessments = await AssessmentRepository.getAll({}, client) | ||
const rows: Record<AssessmentName, Array<RowCache>> = {} | ||
await Promises.each(assessments, async (assessment) => { | ||
rows[assessment.props.name] = (await RowRepository.getManyCache({ assessment }, client)).filter( | ||
(row) => | ||
Boolean(row.props.validateFns || row.props.calculateFn) || | ||
Boolean(row.cols.find((col) => Boolean(col.props.validateFns || col.props.calculateFn))) | ||
) | ||
|
||
export const generateMetaCache = async (props: Props, client: BaseProtocol): Promise<void> => { | ||
const { assessment, cycle } = props | ||
// init cycle meta cache | ||
await Promises.each(assessment.cycles, async (cycle) => { | ||
const [variables, valueAggregate] = await Promise.all([ | ||
RowRepository.getVariablesCache({ assessment, cycle }, client), | ||
ValueAggregateRepository.getVariablesCache({ assessment, cycle }, client), | ||
]) | ||
const metaCache: AssessmentMetaCache = { | ||
calculations: { dependants: {}, dependencies: {} }, | ||
validations: { dependants: {}, dependencies: {} }, | ||
variablesByTable: { ...variables, ...valueAggregate }, | ||
} | ||
Objects.setInPath({ obj: assessment, path: ['metaCache', cycle.uuid], value: metaCache }) | ||
}) | ||
}) | ||
|
||
const [variables, valueAggregate] = await Promise.all([ | ||
RowRepository.getVariablesCache({ assessment, cycle }, client), | ||
ValueAggregateRepository.getVariablesCache({ assessment, cycle }, client), | ||
]) | ||
// 2. generate assessments meta cache | ||
assessments.forEach((assessment) => { | ||
const assessmentName = assessment.props.name | ||
|
||
const assessmentMetaCache: AssessmentMetaCache = { | ||
calculations: { dependants: {}, dependencies: {} }, | ||
validations: { dependants: {}, dependencies: {} }, | ||
variablesByTable: { ...variables, ...valueAggregate }, | ||
} | ||
assessment.cycles.forEach((cycle) => { | ||
const cycleName = cycle.name | ||
|
||
const rows = (await RowRepository.getManyCache({ assessment }, client)).filter( | ||
(row) => | ||
Boolean(row.props.validateFns || row.props.calculateFn) || | ||
Boolean(row.cols.find((col) => Boolean(col.props.validateFns || col.props.calculateFn))) | ||
) | ||
rows[assessmentName].forEach((row) => { | ||
const context = { assessments, assessmentName, cycleName, row } | ||
|
||
rows.forEach(({ tableName, ...row }) => { | ||
const context = { row, tableName, assessmentMetaCache } | ||
if (row.props.calculateFn?.[cycle.uuid]) { | ||
DependencyEvaluator.evalDependencies(row.props.calculateFn[cycle.uuid], { ...context, type: 'calculations' }) | ||
if (row.props.calculateIf?.[cycle.uuid]) { | ||
DependencyEvaluator.evalDependencies(row.props.calculateIf[cycle.uuid], { ...context, type: 'calculations' }) | ||
} | ||
} else { | ||
row.cols.forEach((col) => { | ||
if (col.props.calculateFn?.[cycle.uuid]) { | ||
DependencyEvaluator.evalDependencies(col.props.calculateFn[cycle.uuid], { ...context, type: 'calculations' }) | ||
if (row.props.calculateFn?.[cycle.uuid]) { | ||
DependencyEvaluator.evalDependencies(row.props.calculateFn[cycle.uuid], { ...context, type: 'calculations' }) | ||
if (row.props.calculateIf?.[cycle.uuid]) { | ||
DependencyEvaluator.evalDependencies(row.props.calculateIf[cycle.uuid], { | ||
...context, | ||
type: 'calculations', | ||
}) | ||
} | ||
} else { | ||
row.cols.forEach((col) => { | ||
if (col.props.calculateFn?.[cycle.uuid]) { | ||
DependencyEvaluator.evalDependencies(col.props.calculateFn[cycle.uuid], { | ||
...context, | ||
type: 'calculations', | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
if (row.props.validateFns?.[cycle.uuid]) { | ||
row.props.validateFns[cycle.uuid].forEach((validateFn) => | ||
DependencyEvaluator.evalDependencies(validateFn, { ...context, type: 'validations' }) | ||
) | ||
} else { | ||
row.cols.forEach((col) => { | ||
if (col.props.validateFns?.[cycle.uuid]) { | ||
col.props.validateFns?.[cycle.uuid].forEach((validateFn) => { | ||
if (row.props.validateFns?.[cycle.uuid]) { | ||
row.props.validateFns[cycle.uuid].forEach((validateFn) => | ||
DependencyEvaluator.evalDependencies(validateFn, { ...context, type: 'validations' }) | ||
) | ||
} else { | ||
row.cols.forEach((col) => { | ||
if (col.props.validateFns?.[cycle.uuid]) { | ||
col.props.validateFns?.[cycle.uuid].forEach((validateFn) => { | ||
DependencyEvaluator.evalDependencies(validateFn, { ...context, type: 'validations' }) | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
}) | ||
|
||
return client.query( | ||
` | ||
await Promise.all( | ||
assessments.map((assessment) => | ||
client.query<void>( | ||
` | ||
update assessment | ||
set meta_cache = jsonb_set(meta_cache, '{${cycle.uuid}}', $1::jsonb) | ||
set meta_cache = $1::jsonb | ||
where id = $2 | ||
`, | ||
[JSON.stringify(assessmentMetaCache), assessment.id] | ||
[assessment.metaCache, assessment.id] | ||
) | ||
) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters