Skip to content

Commit

Permalink
Merge pull request #1133 from Vizzuality/fix/legacy-piece-importers
Browse files Browse the repository at this point in the history
fix: legacy piece importers
  • Loading branch information
angelhigueraacid authored Jun 13, 2022
2 parents 0a5835d + 3b51a96 commit 35e9672
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,29 @@ export class InputLegacyProjectPieceImporter

const marxanInputParameters = marxanInputParametersOrError.right;

const [metadata] = await this.apiEntityManager
.createQueryBuilder()
.select('metadata')
.from('scenarios', 's')
.where('id = :scenarioId', { scenarioId })
.execute();
const legacyProjectImportIncludesSolutions = files.some(
(file) => file.type === LegacyProjectImportFileType.Output,
);

const updatedMetadata = {
scenarioEditingMetadata: metadata?.scenarioEditingMetadata ?? undefined,
const scenarioMetadata = {
scenarioEditingMetadata: {
tab: 'planning-unit',
subtab: null,
status: {
'planning-unit': 'draft',
features: 'draft',
parameters: 'draft',
solutions: legacyProjectImportIncludesSolutions ? 'draft' : 'empty',
},
lastJobCheck: new Date().getTime(),
},
marxanInputParameterFile: marxanInputParameters,
};

await this.apiEntityManager
.createQueryBuilder()
.update('scenarios', {
metadata: JSON.stringify({
...updatedMetadata,
}),
metadata: JSON.stringify(scenarioMetadata),
})
.where('id = :scenarioId', { scenarioId })
.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class SolutionsLegacyProjectPieceImporter
}

private async insertOutputScenariosSummaries(
em: EntityManager,
outputSumPath: string,
planningUnitsState: PlanningUnitsSelectionState,
scenarioId: string,
Expand All @@ -137,7 +138,7 @@ export class SolutionsLegacyProjectPieceImporter
planningUnitsState,
);

await this.apiEntityManager.save(
await em.save(
ScenariosOutputResultsApiEntity,
results.map(({ score, cost, ...runSummary }) => ({
...runSummary,
Expand All @@ -148,6 +149,20 @@ export class SolutionsLegacyProjectPieceImporter
);
}

private async updateScenario(
em: EntityManager,
scenarioId: string,
): Promise<void> {
await em
.createQueryBuilder()
.update('scenarios')
.set({
ran_at_least_once: true,
})
.where('id = :scenarioId', { scenarioId })
.execute();
}

async run(
input: LegacyProjectImportJobInput,
): Promise<LegacyProjectImportJobOutput> {
Expand Down Expand Up @@ -215,11 +230,15 @@ export class SolutionsLegacyProjectPieceImporter
await this.insertOutputScenarioFeaturesData(em, scenarioFeatureRunData);
await this.insertOutputScenariosPuData(em, planningUnitsState);

await this.insertOutputScenariosSummaries(
outputSumPath,
planningUnitsState,
scenarioId,
);
await this.apiEntityManager.transaction(async (apiEm) => {
await this.insertOutputScenariosSummaries(
apiEm,
outputSumPath,
planningUnitsState,
scenarioId,
);
await this.updateScenario(apiEm, scenarioId);
});
});

return input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ describe(InputLegacyProjectPieceImporter, () => {
.WhenPieceImporterIsInvoked(job)
.ThenScenarioMetadataDataShouldBeUpdated();
});

it('updates successfully scenario metadata when job has solution files', async () => {
const fileLocation = await fixtures.GivenValidInputDatFile();
const job = fixtures.GivenJobInput({ fileLocation, withSolutions: true });
await fixtures.GivenProjectWithScenarioExist();

await fixtures
.WhenPieceImporterIsInvoked(job)
.ThenScenarioMetadataDataShouldBeUpdated({ withSolutions: true });
});
});

const getFixtures = async () => {
Expand Down Expand Up @@ -129,23 +139,36 @@ const getFixtures = async () => {
GivenJobInput: (
{
missingInputDatFile,
withSolutions,
fileLocation,
}: {
missingInputDatFile?: boolean;
withSolutions?: boolean;
fileLocation?: string;
} = { missingInputDatFile: false, fileLocation: 'foo/input.dat' },
} = {
missingInputDatFile: false,
withSolutions: false,
fileLocation: 'foo/input.dat',
},
): LegacyProjectImportJobInput => {
const files = [
{
id: v4(),
location: fileLocation ?? '',
type: fileType,
},
];

if (withSolutions)
files.push({
id: v4(),
location: '',
type: LegacyProjectImportFileType.Output,
});

return {
piece: LegacyProjectImportPiece.Input,
files: missingInputDatFile
? []
: [
{
id: v4(),
location: fileLocation ?? '',
type: fileType,
},
],
files: missingInputDatFile ? [] : files,
pieceId: v4(),
projectId,
scenarioId,
Expand Down Expand Up @@ -192,7 +215,11 @@ const getFixtures = async () => {
/invalid variables values in input.dat file/gi,
);
},
ThenScenarioMetadataDataShouldBeUpdated: async () => {
ThenScenarioMetadataDataShouldBeUpdated: async (
{ withSolutions }: { withSolutions: boolean } = {
withSolutions: false,
},
) => {
const result = await sut.run(input);

expect(result).toBeDefined();
Expand All @@ -208,6 +235,17 @@ const getFixtures = async () => {

expect(scenario.metadata).toEqual({
marxanInputParameterFile: expectedInputParameterFile,
scenarioEditingMetadata: {
tab: 'planning-unit',
subtab: null,
status: {
'planning-unit': 'draft',
features: 'draft',
parameters: 'draft',
solutions: withSolutions ? 'draft' : 'empty',
},
lastJobCheck: expect.any(Number),
},
});
},
};
Expand Down

0 comments on commit 35e9672

Please sign in to comment.