Skip to content

Commit

Permalink
refactor: move scenario features data to shared lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Jul 22, 2021
1 parent fdc6a42 commit 63cf654
Show file tree
Hide file tree
Showing 32 changed files with 259 additions and 88 deletions.
10 changes: 5 additions & 5 deletions api/apps/api/src/modules/geo-features/geo-feature-set.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Scenario } from '../scenarios/scenario.api.entity';
import { EntityManager, Repository } from 'typeorm';
import { GeoFeaturePropertySetService } from './geo-feature-property-sets.service';
import { RemoteScenarioFeaturesData } from '../scenarios-features/entities/remote-scenario-features-data.geo.entity';
import { ScenarioFeaturesData } from '@marxan/features';
import { flattenDeep } from 'lodash';
import { GeoprocessingOpSplitV1 } from './types/geo-feature.geoprocessing-operations.type';

Expand Down Expand Up @@ -87,7 +87,7 @@ export class GeoFeatureSetService {
): Promise<void> {
await this.entityManager.transaction(async (transactionalEntityManager) => {
const repository = transactionalEntityManager.getRepository(
RemoteScenarioFeaturesData,
ScenarioFeaturesData,
);
await repository.delete({ scenarioId });
// First process features which can be used as they are (plain)
Expand Down Expand Up @@ -120,10 +120,10 @@ export class GeoFeatureSetService {
featuresDataId: string;
fpf: number;
prop: number | undefined;
} & RemoteScenarioFeaturesData)[]
} & ScenarioFeaturesData)[]
> {
const repository = transactionalEntityManager.getRepository(
RemoteScenarioFeaturesData,
ScenarioFeaturesData,
);
return Promise.all(
featureSpecification
Expand Down Expand Up @@ -163,7 +163,7 @@ export class GeoFeatureSetService {
// prop: number | undefined;
// } & RemoteScenarioFeaturesData)[]
const _repository = transactionalEntityManager.getRepository(
RemoteScenarioFeaturesData,
ScenarioFeaturesData,
);
return Promise.all(
featureSpecification
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Column, Entity, PrimaryColumn } from 'typeorm';
import { BaseServiceResource } from '@marxan-api/types/resource.interface';
import { FeatureTags } from '@marxan/features';

export const geoFeatureResource: BaseServiceResource = {
className: 'GeoFeature',
Expand All @@ -11,11 +12,6 @@ export const geoFeatureResource: BaseServiceResource = {
moduleControllerPrefix: 'geo-features',
};

export enum FeatureTags {
bioregional = 'bioregional',
species = 'species',
}

export interface GeoFeatureProperty {
key: string;
distinctValues: Array<string | number>;
Expand Down
4 changes: 2 additions & 2 deletions api/apps/api/src/modules/geo-features/geo-features.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import {
EntityManagerToken,
GeoFeatureSetService,
} from './geo-feature-set.service';
import { RemoteScenarioFeaturesData } from '../scenarios-features/entities/remote-scenario-features-data.geo.entity';
import { ScenarioFeaturesData } from '@marxan/features';
import { GeoFeaturePropertySetService } from './geo-feature-property-sets.service';

@Module({
imports: [
TypeOrmModule.forFeature(
[GeoFeatureGeometry, GeoFeaturePropertySet, RemoteScenarioFeaturesData],
[GeoFeatureGeometry, GeoFeaturePropertySet, ScenarioFeaturesData],
apiConnections.geoprocessingDB.name,
),
TypeOrmModule.forFeature([GeoFeature, Project, Scenario]),
Expand Down
7 changes: 2 additions & 5 deletions api/apps/api/src/modules/geo-features/geo-features.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ import {
AppBaseService,
JSONAPISerializerConfig,
} from '@marxan-api/utils/app-base.service';
import {
FeatureTags,
GeoFeature,
GeoFeatureProperty,
} from './geo-feature.api.entity';
import { GeoFeature, GeoFeatureProperty } from './geo-feature.api.entity';
import { FetchSpecification } from 'nestjs-base-service';
import { Project } from '@marxan-api/modules/projects/project.api.entity';
import { apiConnections } from '@marxan-api/ormconfig';
import { AppConfig } from '@marxan-api/utils/config.utils';
import { Scenario } from '../scenarios/scenario.api.entity';
import { GeoFeaturePropertySetService } from './geo-feature-property-sets.service';
import { FeatureTags } from '@marxan/features';

const geoFeatureFilterKeyNames = [
'featureClassName',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { RemoteFeaturesData } from '../entities/remote-features-data.geo.entity';
import { RemoteScenarioFeaturesData } from '../entities/remote-scenario-features-data.geo.entity';
import {
FeatureTags,
GeoFeature,
} from '../../geo-features/geo-feature.api.entity';
import { FeatureTags, ScenarioFeaturesData } from '@marxan/features';
import { GeoFeature } from '../../geo-features/geo-feature.api.entity';

const featureIdMet = `feature-uuid-1-criteria-met`;
const featureIdFailed = `feature-uuid-2-criteria-failed`;
Expand All @@ -12,7 +9,7 @@ const metaFeatureIdMet = `meta-feature-uuid-1-criteria-met`;
const metaFeatureIdFailed = `meta-feature-uuid-1-criteria-failed`;

type RawRemoteScenarioFeaturesData = Pick<
RemoteScenarioFeaturesData,
ScenarioFeaturesData,
| 'id'
| 'target'
| 'scenarioId'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { TypeOrmModule } from '@nestjs/typeorm';

import { GeoFeature } from '@marxan-api/modules/geo-features/geo-feature.api.entity';

import { remoteConnectionName } from './entities/remote-connection-name';
import { RemoteScenarioFeaturesData } from './entities/remote-scenario-features-data.geo.entity';
import { ScenarioFeaturesData } from '@marxan/features';
import { RemoteFeaturesData } from './entities/remote-features-data.geo.entity';
import { DbConnections } from '@marxan-api/ormconfig.connections';

@Module({
imports: [
TypeOrmModule.forFeature([GeoFeature]),
TypeOrmModule.forFeature(
[RemoteScenarioFeaturesData, RemoteFeaturesData],
remoteConnectionName,
[ScenarioFeaturesData, RemoteFeaturesData],
DbConnections.geoprocessingDB,
),
],
providers: [ScenarioFeaturesService],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ScenarioFeaturesService } from './scenario-features.service';
import { Repository } from 'typeorm';
import { RemoteScenarioFeaturesData } from './entities/remote-scenario-features-data.geo.entity';
import { ScenarioFeaturesData } from '@marxan/features';
import { Test } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { remoteConnectionName } from './entities/remote-connection-name';
import { fakeQueryBuilder } from '../../utils/__mocks__/fake-query-builder';
import { RemoteFeaturesData } from './entities/remote-features-data.geo.entity';
import { GeoFeature } from '../geo-features/geo-feature.api.entity';
Expand All @@ -12,6 +11,7 @@ import {
getValidNonGeoData,
getValidRemoteFeatureData,
} from './__mocks__/scenario-features.view-data';
import { DbConnections } from '@marxan-api/ormconfig.connections';

let sut: ScenarioFeaturesService;
let geoFeatureRepoMock: jest.Mocked<Repository<GeoFeature>>;
Expand All @@ -23,12 +23,12 @@ beforeAll(async () => {
fakeResultResolver = jest.fn();
const geoFeatureToken = getRepositoryToken(GeoFeature);
const geoRemoteScenarioFeaturesRepoToken = getRepositoryToken(
RemoteScenarioFeaturesData,
remoteConnectionName,
ScenarioFeaturesData,
DbConnections.geoprocessingDB,
);
const geoRemoteFeaturesRepoToken = getRepositoryToken(
RemoteFeaturesData,
remoteConnectionName,
DbConnections.geoprocessingDB,
);
const sandbox = await Test.createTestingModule({
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@ import {
JSONAPISerializerConfig,
} from '../../utils/app-base.service';

import { remoteConnectionName } from './entities/remote-connection-name';
import { GeoFeature } from '../geo-features/geo-feature.api.entity';
import { RemoteScenarioFeaturesData } from './entities/remote-scenario-features-data.geo.entity';
import { ScenarioFeaturesData } from '@marxan/features';
import { RemoteFeaturesData } from './entities/remote-features-data.geo.entity';
import { UserSearchCriteria } from './search-criteria';
import { AppConfig } from '../../utils/config.utils';
import { DbConnections } from '@marxan-api/ormconfig.connections';

@Injectable()
export class ScenarioFeaturesService extends AppBaseService<
RemoteScenarioFeaturesData,
ScenarioFeaturesData,
never,
never,
UserSearchCriteria
> {
constructor(
@InjectRepository(GeoFeature)
private readonly features: Repository<GeoFeature>,
@InjectRepository(RemoteFeaturesData, remoteConnectionName)
@InjectRepository(RemoteFeaturesData, DbConnections.geoprocessingDB)
private readonly remoteFeature: Repository<RemoteFeaturesData>,
@InjectRepository(RemoteScenarioFeaturesData, remoteConnectionName)
private readonly remoteScenarioFeatures: Repository<RemoteScenarioFeaturesData>,
@InjectRepository(ScenarioFeaturesData, DbConnections.geoprocessingDB)
private readonly remoteScenarioFeatures: Repository<ScenarioFeaturesData>,
) {
super(remoteScenarioFeatures, 'scenario_features', 'scenario_feature', {
logging: { muteAll: AppConfig.get<boolean>('logging.muteAll', false) },
});
}

setFilters(
query: SelectQueryBuilder<RemoteScenarioFeaturesData>,
query: SelectQueryBuilder<ScenarioFeaturesData>,
filters?: FiltersSpecification['filter'],
info?: UserSearchCriteria,
): SelectQueryBuilder<RemoteScenarioFeaturesData> {
): SelectQueryBuilder<ScenarioFeaturesData> {
const scenarioId = info?.params?.scenarioId;
if (scenarioId) {
return query.andWhere(`${this.alias}.scenario_id = :scenarioId`, {
Expand All @@ -51,7 +51,7 @@ export class ScenarioFeaturesService extends AppBaseService<
async extendFindAllResults(
entitiesAndCount: [any[], number],
): Promise<[any[], number]> {
const scenarioFeaturesData = entitiesAndCount[0] as RemoteScenarioFeaturesData[];
const scenarioFeaturesData = entitiesAndCount[0] as ScenarioFeaturesData[];
const featuresDataIds = scenarioFeaturesData.map(
(rsfd) => rsfd.featuresDataId,
);
Expand Down Expand Up @@ -98,7 +98,7 @@ export class ScenarioFeaturesService extends AppBaseService<
];
}

get serializerConfig(): JSONAPISerializerConfig<RemoteScenarioFeaturesData> {
get serializerConfig(): JSONAPISerializerConfig<ScenarioFeaturesData> {
return {
attributes: [
'description',
Expand All @@ -116,9 +116,9 @@ export class ScenarioFeaturesService extends AppBaseService<
}

#injectAndCompute = (
base: RemoteScenarioFeaturesData,
base: ScenarioFeaturesData,
assign: GeoFeature,
): RemoteScenarioFeaturesData => {
): ScenarioFeaturesData => {
const metArea = base?.currentArea ?? 0;
const totalArea = base?.totalArea ?? 0;
const targetPct = (base?.target ?? 0) / 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { DbConnections } from '@marxan-api/ormconfig.connections';
import { PlanningUnitsGeom } from '@marxan-jobs/planning-unit-geometry';
import { ScenarioPuvsprGeoEntity } from '@marxan/scenario-puvspr';
import { RemoteScenarioFeaturesData } from '@marxan-api/modules/scenarios-features/entities/remote-scenario-features-data.geo.entity';
import { ScenarioFeaturesData } from '@marxan/features';
import {
ScenariosPlanningUnitGeoEntity,
ScenariosPuCostDataGeo,
Expand All @@ -27,7 +27,7 @@ import { InputFilesArchiverService } from './input-files-archiver.service';
[
PlanningUnitsGeom,
ScenarioPuvsprGeoEntity,
RemoteScenarioFeaturesData,
ScenarioFeaturesData,
ScenariosPlanningUnitGeoEntity,
ScenariosPuCostDataGeo,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { RemoteScenarioFeaturesData } from '@marxan-api/modules/scenarios-features/entities/remote-scenario-features-data.geo.entity';
import { ScenarioFeaturesData } from '@marxan/features';
import { Repository } from 'typeorm';
import { DbConnections } from '@marxan-api/ormconfig.connections';

@Injectable()
export class SpecDatService {
constructor(
@InjectRepository(RemoteScenarioFeaturesData, DbConnections.geoprocessingDB)
private readonly scenarioFeaturesData: Repository<RemoteScenarioFeaturesData>,
@InjectRepository(ScenarioFeaturesData, DbConnections.geoprocessingDB)
private readonly scenarioFeaturesData: Repository<ScenarioFeaturesData>,
) {}

async getSpecDatContent(scenarioId: string): Promise<string> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RemoteScenarioFeaturesData } from '@marxan-api/modules/scenarios-features/entities/remote-scenario-features-data.geo.entity';
import { ScenarioFeaturesData } from '@marxan/features';
import { Repository } from 'typeorm';
import { Test } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
Expand All @@ -7,11 +7,11 @@ import { DbConnections } from '@marxan-api/ormconfig.connections';
import { SpecDatService } from './spec.dat.service';

let sut: SpecDatService;
let dataRepo: jest.Mocked<Repository<RemoteScenarioFeaturesData>>;
let dataRepo: jest.Mocked<Repository<ScenarioFeaturesData>>;

beforeEach(async () => {
const token = getRepositoryToken(
RemoteScenarioFeaturesData,
ScenarioFeaturesData,
DbConnections.geoprocessingDB,
);
const sandbox = await Test.createTestingModule({
Expand Down Expand Up @@ -50,7 +50,7 @@ describe(`when there is data available`, () => {
fpf: 0.33,
featuresDataId: '...',
prop: 0.25,
} as RemoteScenarioFeaturesData,
} as ScenarioFeaturesData,
{
scenarioId: 'id',
featureId: 1,
Expand All @@ -64,7 +64,7 @@ describe(`when there is data available`, () => {
metadata: {
sepdistance: 4000,
},
} as RemoteScenarioFeaturesData,
} as ScenarioFeaturesData,
]);
});

Expand Down
6 changes: 3 additions & 3 deletions api/apps/api/src/modules/scenarios/scenarios.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
import { CreateScenarioDTO } from './dto/create.scenario.dto';
import { UpdateScenarioDTO } from './dto/update.scenario.dto';
import { RequestWithAuthenticatedUser } from '@marxan-api/app.controller';
import { RemoteScenarioFeaturesData } from '../scenarios-features/entities/remote-scenario-features-data.geo.entity';
import { ScenarioFeaturesData } from '@marxan/features';
import { UpdateScenarioPlanningUnitLockStatusDto } from './dto/update-scenario-planning-unit-lock-status.dto';
import { uploadOptions } from '@marxan-api/utils/file-uploads.utils';
import { ShapefileGeoJSONResponseDTO } from './dto/shapefile.geojson.response.dto';
Expand Down Expand Up @@ -284,12 +284,12 @@ export class ScenariosController {

@ApiOperation({ description: `Resolve scenario's features pre-gap data.` })
@ApiOkResponse({
type: RemoteScenarioFeaturesData,
type: ScenarioFeaturesData,
})
@Get(':id/features')
async getScenarioFeatures(
@Param('id', ParseUUIDPipe) id: string,
): Promise<Partial<RemoteScenarioFeaturesData>[]> {
): Promise<Partial<ScenarioFeaturesData>[]> {
return this.scenarioFeatureSerializer.serialize(
await this.service.getFeatures(id),
);
Expand Down
Loading

0 comments on commit 63cf654

Please sign in to comment.