-
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.
Revert "3213 paneuropean linked nodes shows wrong data 1 (#3218)"
This reverts commit eedd721.
- Loading branch information
Showing
8 changed files
with
125 additions
and
150 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: 4 additions & 5 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,11 +1,10 @@ | ||
import { ExpressionContext } from '@openforis/arena-core' | ||
|
||
import { Assessment, AssessmentName, CycleName, RowCache } from 'meta/assessment' | ||
import { AssessmentMetaCache, Row } from 'meta/assessment' | ||
|
||
export interface Context extends ExpressionContext { | ||
assessments: Array<Assessment> | ||
assessmentName: AssessmentName | ||
cycleName: CycleName | ||
row: RowCache | ||
assessmentMetaCache: AssessmentMetaCache | ||
row: Row | ||
tableName: string | ||
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: 48 additions & 76 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,100 +1,72 @@ | ||
import { Objects } from 'utils/objects' | ||
import { Promises } from 'utils/promises' | ||
import { Assessment, AssessmentMetaCache, Cycle } from 'meta/assessment' | ||
|
||
import { AssessmentMetaCache, AssessmentName, RowCache } from 'meta/assessment' | ||
|
||
import { BaseProtocol, DB } from 'server/db' | ||
import { AssessmentRepository } from 'server/repository/assessment/assessment' | ||
import { BaseProtocol } from 'server/db' | ||
import { RowRepository } from 'server/repository/assessment/row' | ||
import { ValueAggregateRepository } from 'server/repository/assessmentCycle/valueAggregate' | ||
|
||
import { DependencyEvaluator } from './dependencyEvaluator' | ||
|
||
/** | ||
* 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))) | ||
) | ||
type Props = { | ||
assessment: Assessment | ||
cycle: Cycle | ||
} | ||
|
||
// 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 }) | ||
}) | ||
}) | ||
export const generateMetaCache = async (props: Props, client: BaseProtocol): Promise<void> => { | ||
const { assessment, cycle } = props | ||
|
||
// 2. generate assessments meta cache | ||
assessments.forEach((assessment) => { | ||
const assessmentName = assessment.props.name | ||
const [variables, valueAggregate] = await Promise.all([ | ||
RowRepository.getVariablesCache({ assessment, cycle }, client), | ||
ValueAggregateRepository.getVariablesCache({ assessment, cycle }, client), | ||
]) | ||
|
||
assessment.cycles.forEach((cycle) => { | ||
const cycleName = cycle.name | ||
const assessmentMetaCache: AssessmentMetaCache = { | ||
calculations: { dependants: {}, dependencies: {} }, | ||
validations: { dependants: {}, dependencies: {} }, | ||
variablesByTable: { ...variables, ...valueAggregate }, | ||
} | ||
|
||
rows[assessmentName].forEach((row) => { | ||
const context = { assessments, assessmentName, cycleName, row } | ||
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))) | ||
) | ||
|
||
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', | ||
}) | ||
} | ||
}) | ||
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.validateFns?.[cycle.uuid]) { | ||
row.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' }) | ||
) | ||
} else { | ||
row.cols.forEach((col) => { | ||
if (col.props.validateFns?.[cycle.uuid]) { | ||
col.props.validateFns?.[cycle.uuid].forEach((validateFn) => { | ||
DependencyEvaluator.evalDependencies(validateFn, { ...context, type: 'validations' }) | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
}) | ||
|
||
await Promise.all( | ||
assessments.map((assessment) => | ||
client.query<void>( | ||
` | ||
return client.query( | ||
` | ||
update assessment | ||
set meta_cache = $1::jsonb | ||
set meta_cache = jsonb_set(meta_cache, '{${cycle.uuid}}', $1::jsonb) | ||
where id = $2 | ||
`, | ||
[assessment.metaCache, assessment.id] | ||
) | ||
) | ||
[JSON.stringify(assessmentMetaCache), 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