Skip to content

Commit

Permalink
feature(Feature Amounts): Unifies source of info for PUVSRP.dat gener…
Browse files Browse the repository at this point in the history
…ated files
  • Loading branch information
KevSanchez committed Oct 25, 2023
1 parent d30303b commit db2a838
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 161 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import { FeatureAmountsPerPlanningUnitRepository } from '@marxan/feature-amounts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FeatureAmountPerPlanningUnitId, PuvsprDat } from './puvsrpr.dat';

export type FeatureAmountPerPlanningUnitId = {
featureId: string;
amount: number;
puId: number;
};

@Injectable()
export class PuvsprDatMarxanProject implements PuvsprDat {
export class PuvsprDatFeatureAmountsService {
constructor(
private readonly featureAmountsPerPlanningUnitRepo: FeatureAmountsPerPlanningUnitRepository,
@InjectRepository(ProjectsPuEntity, DbConnections.geoprocessingDB)
private readonly projectsPusRepo: Repository<ProjectsPuEntity>,
) {}
public async getAmountPerPlanningUnitAndFeature(
projectId: string,
scenarioId: string,
featureIds: string[],
): Promise<FeatureAmountPerPlanningUnitId[]> {
const amountPerPlanningUnitOfFeature =
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { Scenario } from '../../scenario.api.entity';
import { GeoFeatureDtoMapper } from '../../specification/geo-feature-dto.mapper';
import { SplitFeatureConfigMapper } from '../../specification/split-feature-config.mapper';
import { PuvrsprDatFactory } from './puvspr.dat.factory';
import { PuvsprDatLegacyProject } from './puvspr.dat.legacy-project';
import { PuvsprDatMarxanProject } from './puvspr.dat.marxan-project';
import { PuvsprDatProcessor } from './puvspr.dat.processor';
import { FeatureHashModule } from '@marxan-api/modules/features-hash/features-hash.module';
import { ProjectsPuEntity } from '@marxan-jobs/planning-unit-geometry';
import { PuvsprDatFeatureAmountsService } from '@marxan-api/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.feature-amounts.service';

@Module({
imports: [
Expand All @@ -32,9 +30,7 @@ import { ProjectsPuEntity } from '@marxan-jobs/planning-unit-geometry';
providers: [
GeoFeatureDtoMapper,
PuvsprDatProcessor,
PuvrsprDatFactory,
PuvsprDatMarxanProject,
PuvsprDatLegacyProject,
PuvsprDatFeatureAmountsService,
SplitFeatureConfigMapper,
],
exports: [PuvsprDatProcessor],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { isLeft, left, right } from 'fp-ts/lib/Either';
import { EntityManager } from 'typeorm';
import { GeoFeatureDtoMapper } from '../../specification/geo-feature-dto.mapper';
import { SplitFeatureConfigMapper } from '../../specification/split-feature-config.mapper';
import { PuvrsprDatFactory } from './puvspr.dat.factory';
import { PuvsprDatFeatureAmountsService } from '@marxan-api/modules/scenarios/input-files/puvspr.dat.processor/puvspr.dat.feature-amounts.service';

export type PuvrsprDatRow = {
speciesId: number;
Expand All @@ -41,11 +41,10 @@ export class PuvsprDatProcessor {
private readonly featureAmountsPerPlanningUnitService: FeatureAmountsPerPlanningUnitService,
private readonly splitConfigHasher: SingleConfigFeatureValueHasher,
private readonly splitFeatureConfigMapper: SplitFeatureConfigMapper,
private readonly puvsprDatFactory: PuvrsprDatFactory,
private readonly puvsprFeatureAmounts: PuvsprDatFeatureAmountsService,
) {}

async getPuvsprDatRows(
isLegacy: boolean,
scenarioId: string,
projectId: string,
): Promise<PuvrsprDatRow[]> {
Expand All @@ -64,10 +63,8 @@ export class PuvsprDatProcessor {
);

const featuresAmountPerPlanningUnit =
await this.getAmountPerPlanningUnitAndFeature(
isLegacy,
await this.puvsprFeatureAmounts.getAmountPerPlanningUnitAndFeature(
projectId,
scenarioId,
featuresIds,
);

Expand Down Expand Up @@ -133,21 +130,6 @@ export class PuvsprDatProcessor {
return copyFeatureIds.concat(splitFeatureIds);
}

private async getAmountPerPlanningUnitAndFeature(
isLegacy: boolean,
projectId: string,
scenarioId: string,
fetaureIds: string[],
) {
const puvspr = this.puvsprDatFactory.getPuvsrDat(isLegacy);

return puvspr.getAmountPerPlanningUnitAndFeature(
projectId,
scenarioId,
fetaureIds,
);
}

private getCopyFeatureIds(
featureConfigs: (SpecificationFeatureCopy | SpecificationFeatureSplit)[],
) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Project } from '@marxan-api/modules/projects/project.api.entity';
import { ProjectSourcesEnum } from '@marxan/projects';
import { assertDefined } from '@marxan/utils';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { isRight } from 'fp-ts/lib/Either';
import { Repository } from 'typeorm';
import { Scenario } from '../scenario.api.entity';
import {
Expand All @@ -29,25 +27,14 @@ export class PuvsprDatService {

const projectId = scenario.projectId;

const isLegacyProject = await this.isLegacyProject(projectId);

const puvsprRows = await this.puvsprDatProcessor.getPuvsprDatRows(
isLegacyProject,
scenarioId,
projectId,
);

return this.getFileContent(puvsprRows);
}

private async isLegacyProject(projectId: string) {
const [project] = await this.projectsRepo.find({
where: { id: projectId },
});
assertDefined(project);
return project.sources === ProjectSourcesEnum.legacyImport;
}

private getFileContent(rows: PuvrsprDatRow[]) {
return (
'species\tpu\tamount\n' +
Expand Down

0 comments on commit db2a838

Please sign in to comment.