Skip to content

Commit

Permalink
Refactor to replace puvsprCalculations with featureAmountsPerPlanning…
Browse files Browse the repository at this point in the history
…Units

Refactor to replace puvsprCalculations with featureAmountsPerPlanningUnits
  • Loading branch information
yulia-bel committed Oct 20, 2023
1 parent 5424986 commit 97eecac
Show file tree
Hide file tree
Showing 33 changed files with 331 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const getFixtures = async () => {
ClonePiece.ProjectCustomProtectedAreas,
ClonePiece.ProjectCostSurfaces,
ClonePiece.ProjectCustomFeatures,
ClonePiece.ProjectPuvsprCalculations,
ClonePiece.ProjectFeatureAmountsPerPlanningUnit,
];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export class ExportResourcePiecesAdapter implements ExportResourcePieces {
ExportComponent.newOne(id, ClonePiece.ProjectCustomProtectedAreas),
ExportComponent.newOne(id, ClonePiece.ProjectCostSurfaces),
ExportComponent.newOne(id, ClonePiece.ProjectCustomFeatures),
ExportComponent.newOne(id, ClonePiece.ProjectPuvsprCalculations),
ExportComponent.newOne(
id,
ClonePiece.ProjectFeatureAmountsPerPlanningUnit,
),
...scenarioPieces,
];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ProjectSourcesEnum } from '@marxan/projects';
import {
MemoryPuvsprCalculationsRepository,
PuvsprCalculationsRepository,
PuvsprCalculationsService,
} from '@marxan/puvspr-calculations';
MemoryFeatureAmountsPerPlanningUnitRepository,
FeatureAmountsPerPlanningUnitRepository,
FeatureAmountsPerPlanningUnitService,
} from '@marxan/feature-amounts-per-planning-unit';
import { FixtureType } from '@marxan/utils/tests/fixture-type';
import { Test } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
Expand Down Expand Up @@ -43,11 +43,11 @@ const getFixtures = async () => {
useValue: { find: findProjectMock },
},
{
provide: PuvsprCalculationsRepository,
useClass: MemoryPuvsprCalculationsRepository,
provide: FeatureAmountsPerPlanningUnitRepository,
useClass: MemoryFeatureAmountsPerPlanningUnitRepository,
},
{
provide: PuvsprCalculationsService,
provide: FeatureAmountsPerPlanningUnitService,
useValue: {
computeMarxanAmountPerPlanningUnit:
computeMarxanAmountPerPlanningUnitMock,
Expand All @@ -60,8 +60,8 @@ const getFixtures = async () => {
await sandbox.init();

const sut = sandbox.get(ComputeArea);
const puvsprCalculationsRepo: MemoryPuvsprCalculationsRepository =
sandbox.get(PuvsprCalculationsRepository);
const featureAmountsPerPlanningUnitRepo: MemoryFeatureAmountsPerPlanningUnitRepository =
sandbox.get(FeatureAmountsPerPlanningUnitRepository);

const expectedPuid = v4();
const expectedAmount = 20;
Expand Down Expand Up @@ -106,7 +106,7 @@ const getFixtures = async () => {
featureId: string,
) => {
const savedCalculations =
await puvsprCalculationsRepo.getAmountPerPlanningUnitAndFeature(
await featureAmountsPerPlanningUnitRepo.getAmountPerPlanningUnitAndFeature(
projectId,
[featureId],
);
Expand All @@ -124,7 +124,7 @@ const getFixtures = async () => {
) => {
expect(computeMarxanAmountPerPlanningUnitMock).not.toHaveBeenCalled();
const hasBeenSaved =
await puvsprCalculationsRepo.areAmountPerPlanningUnitAndFeatureSaved(
await featureAmountsPerPlanningUnitRepo.areAmountPerPlanningUnitAndFeatureSaved(
projectId,
featureId,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ProjectSourcesEnum } from '@marxan/projects';
import {
PuvsprCalculationsRepository,
PuvsprCalculationsService,
} from '@marxan/puvspr-calculations';
FeatureAmountsPerPlanningUnitRepository,
FeatureAmountsPerPlanningUnitService,
} from '@marxan/feature-amounts-per-planning-unit';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
Expand All @@ -11,8 +11,8 @@ import { Project } from '../projects/project.api.entity';
@Injectable()
export class ComputeArea {
constructor(
private readonly puvsprCalculationsRepo: PuvsprCalculationsRepository,
private readonly puvsprCalculations: PuvsprCalculationsService,
private readonly featureAmountsPerPlanningUnitRepo: FeatureAmountsPerPlanningUnitRepository,
private readonly featureAmountsPerPlanningUnit: FeatureAmountsPerPlanningUnitService,
@InjectRepository(Project)
private readonly projectsRepo: Repository<Project>,
) {}
Expand All @@ -26,20 +26,20 @@ export class ComputeArea {
if (isLegacyProject) return;

const alreadyComputed =
await this.puvsprCalculationsRepo.areAmountPerPlanningUnitAndFeatureSaved(
await this.featureAmountsPerPlanningUnitRepo.areAmountPerPlanningUnitAndFeatureSaved(
projectId,
featureId,
);

if (alreadyComputed) return;

const amountPerPlanningUnitOfFeature =
await this.puvsprCalculations.computeMarxanAmountPerPlanningUnit(
await this.featureAmountsPerPlanningUnit.computeMarxanAmountPerPlanningUnit(
featureId,
scenarioId,
);

return this.puvsprCalculationsRepo.saveAmountPerPlanningUnitAndFeature(
return this.featureAmountsPerPlanningUnitRepo.saveAmountPerPlanningUnitAndFeature(
projectId,
amountPerPlanningUnitOfFeature.map(
({ featureId, projectPuId, amount }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { AccessControlModule } from '@marxan-api/modules/access-control';
import { ComputeArea } from './compute-area.service';
import { LegacyProjectImportRepositoryModule } from '../legacy-project-import/infra/legacy-project-import.repository.module';
import { PuvsprCalculationsModule } from '@marxan/puvspr-calculations';
import { FeatureAmountsPerPlanningUnitModule } from '@marxan/feature-amounts-per-planning-unit';
import { SplitFeatureConfigMapper } from '../scenarios/specification/split-feature-config.mapper';
import { FeatureHashModule } from '../features-hash/features-hash.module';
import { SplitCreateFeatures } from './split/split-create-features.service';
Expand All @@ -47,7 +47,7 @@ import { Project } from '../projects/project.api.entity';
DbConnections.geoprocessingDB,
),
FeatureHashModule,
PuvsprCalculationsModule.for(DbConnections.geoprocessingDB),
FeatureAmountsPerPlanningUnitModule.for(DbConnections.geoprocessingDB),
ProjectsModule,
ApiEventsModule,
IntersectWithPuModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeoFeature } from '@marxan-api/modules/geo-features/geo-feature.api.entity';
import { PuvsprCalculationsService } from '@marxan/puvspr-calculations';
import { FeatureAmountsPerPlanningUnitService } from '@marxan/feature-amounts-per-planning-unit';
import { Injectable } from '@nestjs/common';
import { FeatureAmountPerPlanningUnitId, PuvsprDat } from './puvsrpr.dat';
import { InjectRepository } from '@nestjs/typeorm';
Expand All @@ -8,7 +8,7 @@ import { Repository, In } from 'typeorm';
@Injectable()
export class PuvsprDatLegacyProject implements PuvsprDat {
constructor(
private readonly puvsprCalculations: PuvsprCalculationsService,
private readonly featureAmountsPerPlanningUnit: FeatureAmountsPerPlanningUnitService,
@InjectRepository(GeoFeature)
private readonly featuresRepo: Repository<GeoFeature>,
) {}
Expand Down Expand Up @@ -57,7 +57,7 @@ export class PuvsprDatLegacyProject implements PuvsprDat {
): Promise<FeatureAmountPerPlanningUnitId[]> {
const legacyFeaturesComputations = await Promise.all(
featureIds.map((featureId) =>
this.puvsprCalculations.computeLegacyAmountPerPlanningUnit(
this.featureAmountsPerPlanningUnit.computeLegacyAmountPerPlanningUnit(
featureId,
scenarioId,
),
Expand All @@ -76,7 +76,7 @@ export class PuvsprDatLegacyProject implements PuvsprDat {
): Promise<FeatureAmountPerPlanningUnitId[]> {
const marxanFeaturesComputations = await Promise.all(
featureIds.map((featureId) =>
this.puvsprCalculations.computeMarxanAmountPerPlanningUnit(
this.featureAmountsPerPlanningUnit.computeMarxanAmountPerPlanningUnit(
featureId,
scenarioId,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DbConnections } from '@marxan-api/ormconfig.connections';
import { ProjectsPuEntity } from '@marxan-jobs/planning-unit-geometry';
import { PuvsprCalculationsRepository } from '@marxan/puvspr-calculations';
import { FeatureAmountsPerPlanningUnitRepository } from '@marxan/feature-amounts-per-planning-unit';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
Expand All @@ -9,7 +9,7 @@ import { FeatureAmountPerPlanningUnitId, PuvsprDat } from './puvsrpr.dat';
@Injectable()
export class PuvsprDatMarxanProject implements PuvsprDat {
constructor(
private readonly puvsprCalculationsRepo: PuvsprCalculationsRepository,
private readonly featureAmountsPerPlanningUnitRepo: FeatureAmountsPerPlanningUnitRepository,
@InjectRepository(ProjectsPuEntity, DbConnections.geoprocessingDB)
private readonly projectsPusRepo: Repository<ProjectsPuEntity>,
) {}
Expand All @@ -19,7 +19,7 @@ export class PuvsprDatMarxanProject implements PuvsprDat {
featureIds: string[],
): Promise<FeatureAmountPerPlanningUnitId[]> {
const amountPerPlanningUnitOfFeature =
await this.puvsprCalculationsRepo.getAmountPerPlanningUnitAndFeature(
await this.featureAmountsPerPlanningUnitRepo.getAmountPerPlanningUnitAndFeature(
projectId,
featureIds,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SpecificationAdaptersModule } from '@marxan-api/modules/specification/a
import { DbConnections } from '@marxan-api/ormconfig.connections';
import { ScenarioFeaturesData } from '@marxan/features';
import { GeoFeatureGeometry } from '@marxan/geofeatures';
import { PuvsprCalculationsModule } from '@marxan/puvspr-calculations';
import { FeatureAmountsPerPlanningUnitModule } from '@marxan/feature-amounts-per-planning-unit';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Scenario } from '../../scenario.api.entity';
Expand All @@ -19,7 +19,7 @@ import { ProjectsPuEntity } from '@marxan-jobs/planning-unit-geometry';

@Module({
imports: [
PuvsprCalculationsModule.for(DbConnections.geoprocessingDB),
FeatureAmountsPerPlanningUnitModule.for(DbConnections.geoprocessingDB),
FeatureHashModule,
TypeOrmModule.forFeature([Scenario, GeoFeature]),
TypeOrmModule.forFeature(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { SpecificationRepository } from '@marxan-api/modules/specification/application/specification.repository';
import { Specification } from '@marxan-api/modules/specification/domain';
import { SingleSplitConfigFeatureValue } from '@marxan/features-hash';
import { PuvsprCalculationsService } from '@marxan/puvspr-calculations';
import { FeatureAmountsPerPlanningUnitService } from '@marxan/feature-amounts-per-planning-unit';
import { SpecificationOperation } from '@marxan/specification';
import { Injectable } from '@nestjs/common';
import { InjectEntityManager } from '@nestjs/typeorm';
Expand All @@ -38,7 +38,7 @@ export class PuvsprDatProcessor {
private readonly scenarioSpecificationsRepo: ScenarioSpecificationRepo,
private readonly specificationsRepo: SpecificationRepository,
private readonly geoFeatureMapper: GeoFeatureDtoMapper,
private readonly puvsprCalculationsService: PuvsprCalculationsService,
private readonly featureAmountsPerPlanningUnitService: FeatureAmountsPerPlanningUnitService,
private readonly splitConfigHasher: SingleConfigFeatureValueHasher,
private readonly splitFeatureConfigMapper: SplitFeatureConfigMapper,
private readonly puvsprDatFactory: PuvrsprDatFactory,
Expand All @@ -58,7 +58,7 @@ export class PuvsprDatProcessor {
const featuresIds = await this.getFeaturesIds(specification);

const featuresIdsWithSpeciesId =
await this.puvsprCalculationsService.computeSpeciesId(
await this.featureAmountsPerPlanningUnitService.computeSpeciesId(
featuresIds,
scenarioId,
);
Expand Down
4 changes: 2 additions & 2 deletions api/apps/api/test/jest-e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
"@marxan/planning-units-grid": "<rootDir>/../../../libs/planning-units-grid/src",
"@marxan/protected-areas/(.*)": "<rootDir>/../../../libs/protected-areas/src/$1",
"@marxan/protected-areas": "<rootDir>/../../../libs/protected-areas/src",
"@marxan/puvspr-calculations/(.*)": "<rootDir>/../../../libs/puvspr-calculations/src/$1",
"@marxan/puvspr-calculations": "<rootDir>/../../../libs/puvspr-calculations/src",
"@marxan/feature-amounts-per-planning-unit/(.*)": "<rootDir>/../../../libs/feature-amounts-per-planning-unit/src/$1",
"@marxan/feature-amounts-per-planning-unit": "<rootDir>/../../../libs/feature-amounts-per-planning-unit/src",
"@marxan/specification/(.*)": "<rootDir>/../../../libs/specification/src/$1",
"@marxan/specification": "<rootDir>/../../../libs/specification/src",
"@marxan/tiles/(.*)": "<rootDir>/../../../libs/tiles/src/$1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
MarxanExecutionMetadataGeoEntity,
OutputScenariosFeaturesDataGeoEntity,
} from '@marxan/marxan-output';
import { PuvsprCalculationsModule } from '@marxan/puvspr-calculations';
import { FeatureAmountsPerPlanningUnitModule } from '@marxan/feature-amounts-per-planning-unit';
import { Logger, Module } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { TypeOrmModule } from '@nestjs/typeorm';
Expand All @@ -21,7 +21,7 @@ import { PlanningUnitsGridPieceExporter } from './planning-units-grid.piece-expo
import { ProjectCustomFeaturesPieceExporter } from './project-custom-features.piece-exporter';
import { ProjectCustomProtectedAreasPieceExporter } from './project-custom-protected-areas.piece-exporter';
import { ProjectMetadataPieceExporter } from './project-metadata.piece-exporter';
import { ProjectPuvsprCalculationsPieceExporter } from './project-puvspr-calculations.piece-exporter';
import { ProjectFeatureAmountsPerPlanningUnitPieceExporter } from './project-feature-amounts-per-planning-unit.piece-exporter';
import { ScenarioFeaturesDataPieceExporter } from './scenario-features-data.piece-exporter';
import { ScenarioFeaturesSpecificationPieceExporter } from './scenario-features-specification.piece-exporter';
import { ScenarioInputFolderPieceExporter } from './scenario-input-folder.piece-exporter';
Expand All @@ -45,7 +45,9 @@ import { ProjectCostSurfacesPieceExporter } from '@marxan-geoprocessing/export/p
],
geoprocessingConnections.default,
),
PuvsprCalculationsModule.for(geoprocessingConnections.default.name!),
FeatureAmountsPerPlanningUnitModule.for(
geoprocessingConnections.default.name!,
),
HttpModule,
],
providers: [
Expand All @@ -69,7 +71,7 @@ import { ProjectCostSurfacesPieceExporter } from '@marxan-geoprocessing/export/p
ScenarioOutputFolderPieceExporter,
ScenarioFeaturesSpecificationPieceExporter,
MarxanExecutionMetadataPieceExporter,
ProjectPuvsprCalculationsPieceExporter,
ProjectFeatureAmountsPerPlanningUnitPieceExporter,
Logger,
],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { ClonePiece, ExportJobInput, ExportJobOutput } from '@marxan/cloning';
import { CloningFilesRepository } from '@marxan/cloning-files-repository';
import { ComponentLocation, ResourceKind } from '@marxan/cloning/domain';
import { ClonePieceRelativePathResolver } from '@marxan/cloning/infrastructure/clone-piece-data';
import { ProjectPuvsprCalculationsContent } from '@marxan/cloning/infrastructure/clone-piece-data/project-puvspr-calculations';
import { ProjectFeatureAmountsPerPlanningUnitContent } from '@marxan/cloning/infrastructure/clone-piece-data/project-feature-amounts-per-planning-unit';
import { SingleConfigFeatureValueStripped } from '@marxan/features-hash';
import {
FeatureAmountPerProjectPlanningUnit,
PuvsprCalculationsRepository,
} from '@marxan/puvspr-calculations';
FeatureAmountsPerPlanningUnitRepository,
} from '@marxan/feature-amounts-per-planning-unit';
import { SpecificationOperation } from '@marxan/specification';
import { Injectable, Logger } from '@nestjs/common';
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
Expand All @@ -36,16 +36,16 @@ type FeatureByIdMap = Record<string, Omit<FeaturesSelectResult, 'id'>>;

@Injectable()
@PieceExportProvider()
export class ProjectPuvsprCalculationsPieceExporter
export class ProjectFeatureAmountsPerPlanningUnitPieceExporter
implements ExportPieceProcessor
{
private readonly logger: Logger = new Logger(
ProjectPuvsprCalculationsPieceExporter.name,
ProjectFeatureAmountsPerPlanningUnitPieceExporter.name,
);

constructor(
private readonly fileRepository: CloningFilesRepository,
private readonly puvsprCalculationsRepo: PuvsprCalculationsRepository,
private readonly featureAmountsPerPlanningUnitRepo: FeatureAmountsPerPlanningUnitRepository,
@InjectRepository(ProjectsPuEntity)
private readonly projectPusRepo: Repository<ProjectsPuEntity>,
@InjectEntityManager(geoprocessingConnections.apiDB)
Expand All @@ -54,7 +54,7 @@ export class ProjectPuvsprCalculationsPieceExporter

isSupported(piece: ClonePiece, kind: ResourceKind): boolean {
return (
piece === ClonePiece.ProjectPuvsprCalculations &&
piece === ClonePiece.ProjectFeatureAmountsPerPlanningUnit &&
kind === ResourceKind.Project
);
}
Expand All @@ -63,7 +63,7 @@ export class ProjectPuvsprCalculationsPieceExporter
const projectId = input.resourceId;

const featureAmountPerPlanningUnit =
await this.puvsprCalculationsRepo.getAmountPerPlanningUnitAndFeatureInProject(
await this.featureAmountsPerPlanningUnitRepo.getAmountPerPlanningUnitAndFeatureInProject(
projectId,
);

Expand All @@ -76,13 +76,13 @@ export class ProjectPuvsprCalculationsPieceExporter
const projectFeaturesGeoOperations =
await this.getProjectFeaturesGeoOperations(projectId);

const fileContent: ProjectPuvsprCalculationsContent = {
puvsprCalculations: featuresAmountPerPlanningUnitParsed,
const fileContent: ProjectFeatureAmountsPerPlanningUnitContent = {
featureAmountsPerPlanningUnit: featuresAmountPerPlanningUnitParsed,
projectFeaturesGeoOperations,
};

const relativePath = ClonePieceRelativePathResolver.resolveFor(
ClonePiece.ProjectPuvsprCalculations,
ClonePiece.ProjectFeatureAmountsPerPlanningUnit,
);

const outputFile = await this.fileRepository.saveCloningFile(
Expand All @@ -92,7 +92,7 @@ export class ProjectPuvsprCalculationsPieceExporter
);

if (isLeft(outputFile)) {
const errorMessage = `${ProjectPuvsprCalculationsPieceExporter.name} - Project - couldn't save file - ${outputFile.left.description}`;
const errorMessage = `${ProjectFeatureAmountsPerPlanningUnitPieceExporter.name} - Project - couldn't save file - ${outputFile.left.description}`;
this.logger.error(errorMessage);
throw new Error(errorMessage);
}
Expand Down
Loading

0 comments on commit 97eecac

Please sign in to comment.