-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): cost surface: update costs
* chore(geoprocessing): spud and spucd model update * feat(api): cost surface: update costs * fix(geoprocessing): enable cascade remove on scenarios-pu-cost-data
- Loading branch information
Showing
11 changed files
with
448 additions
and
24 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
20 changes: 0 additions & 20 deletions
20
api/src/modules/analysis/providers/cost-surface/adapters/base-app-cost-surface.ts
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
...src/modules/analysis/providers/cost-surface/adapters/scenarios-pu-cost-data.geo.entity.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
Column, | ||
Entity, | ||
JoinColumn, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
RelationId, | ||
} from 'typeorm'; | ||
import { ScenariosPlanningUnitGeoEntity } from '../../../../scenarios-planning-unit/entities/scenarios-planning-unit.geo.entity'; | ||
|
||
@Entity({ | ||
name: 'scenarios_pu_cost_data', | ||
}) | ||
export class ScenariosPuCostDataGeo { | ||
@PrimaryGeneratedColumn('uuid') | ||
id!: string; | ||
|
||
@Column({ | ||
type: 'float8', | ||
nullable: false, | ||
name: 'output_results_data_id', | ||
}) | ||
planningUnitId!: string; | ||
|
||
@Column({ | ||
type: 'float8', | ||
nullable: false, | ||
default: 0, | ||
comment: `By default we will set them as unitary based on equal area`, | ||
}) | ||
cost!: number; | ||
|
||
@ManyToOne(() => ScenariosPlanningUnitGeoEntity, { | ||
onDelete: 'CASCADE', | ||
}) | ||
@JoinColumn({ | ||
referencedColumnName: 'id', | ||
name: 'scenarios_pu_data_id', | ||
}) | ||
scenariosPlanningUnit?: ScenariosPlanningUnitGeoEntity | null; | ||
|
||
@Column({ | ||
name: 'scenarios_pu_data_id', | ||
}) | ||
@RelationId((spud: ScenariosPuCostDataGeo) => spud.scenariosPlanningUnit) | ||
scenariosPuDataId!: string; | ||
} |
61 changes: 61 additions & 0 deletions
61
api/src/modules/analysis/providers/cost-surface/adapters/typeorm-cost-surface.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { Repository } from 'typeorm'; | ||
import { flatten } from 'lodash'; | ||
|
||
import { CostSurfaceRepo } from '../cost-surface-repo'; | ||
import { CostSurfaceInputDto } from '../../../entry-points/adjust-cost-surface-input'; | ||
import { ScenariosPuCostDataGeo } from './scenarios-pu-cost-data.geo.entity'; | ||
import { DbConnections } from '../../../../../ormconfig.connections'; | ||
|
||
type Success = true; | ||
|
||
@Injectable() | ||
export class TypeormCostSurface implements CostSurfaceRepo { | ||
constructor( | ||
@InjectRepository(ScenariosPuCostDataGeo, DbConnections.geoprocessingDB) | ||
private readonly costs: Repository<ScenariosPuCostDataGeo>, | ||
) { | ||
// | ||
} | ||
|
||
async applyCostSurface( | ||
_: string, | ||
values: CostSurfaceInputDto['planningUnits'], | ||
): Promise<Success> { | ||
const pairs = values.map<[string, number]>((pair) => [pair.id, pair.cost]); | ||
await this.costs.query( | ||
` | ||
UPDATE scenarios_pu_cost_data as spd | ||
set "cost" = pucd.new_cost | ||
from (values | ||
${this.generateParametrizedValues(pairs)} | ||
) as pucd(output_results_data_id, new_cost) | ||
where pucd.output_results_data_id = spd.output_results_data_id | ||
`, | ||
flatten(pairs), | ||
); | ||
return true; | ||
} | ||
|
||
/** | ||
* | ||
* generates parametrized input for: | ||
* ('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid, 5000::float) | ||
* | ||
* in form of: | ||
* ($1::uuid, $2::float), | ||
* ($3::uuid, $4::float), | ||
* ($5::uuid, $6::float), | ||
* ... | ||
* | ||
*/ | ||
private generateParametrizedValues(pairs: [string, number][]): string { | ||
return pairs | ||
.map( | ||
(_, index) => | ||
`($${(index + 1) * 2 - 1}::uuid, $${(index + 1) * 2}::float)`, | ||
) | ||
.join(','); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
api/test/integration/cost-surface-repo/cost-surface-update.e2e-spec.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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { TypeormCostSurface } from '../../../src/modules/analysis/providers/cost-surface/adapters/typeorm-cost-surface'; | ||
import { bootstrapApplication } from '../../utils/api-application'; | ||
import { CostSurfaceUpdateWorld, createWorld } from './world'; | ||
import { CostSurfaceRepo } from '../../../src/modules/analysis/providers/cost-surface/cost-surface-repo'; | ||
|
||
let app: INestApplication; | ||
let sut: TypeormCostSurface; | ||
let world: CostSurfaceUpdateWorld; | ||
|
||
beforeAll(async () => { | ||
app = await bootstrapApplication(); | ||
world = await createWorld(app); | ||
sut = app.get(CostSurfaceRepo); | ||
}); | ||
|
||
afterAll(async () => { | ||
await world.cleanup(); | ||
await app.close(); | ||
}); | ||
|
||
describe(`when updating some of the costs`, () => { | ||
let puCostDataIds: string[]; | ||
beforeEach(async () => { | ||
puCostDataIds = await world.GivenPuCostDataExists(); | ||
}); | ||
|
||
it(`applies new costs to given PU`, async () => { | ||
const costOf9999Id = puCostDataIds[0]; | ||
const costOf1Id = puCostDataIds[1]; | ||
const sameCostId = puCostDataIds[2]; | ||
|
||
await sut.applyCostSurface(world.scenarioId, [ | ||
{ | ||
cost: 9999, | ||
id: costOf9999Id, | ||
}, | ||
{ | ||
cost: 1, | ||
id: costOf1Id, | ||
}, | ||
]); | ||
|
||
const afterChanges = await world.GetPuCostsData(world.scenarioId); | ||
|
||
expect(afterChanges).toContainEqual({ | ||
scenario_id: world.scenarioId, | ||
cost: 9999, | ||
pu_id: costOf9999Id, | ||
}); | ||
|
||
expect(afterChanges).toContainEqual({ | ||
scenario_id: world.scenarioId, | ||
cost: 1, | ||
pu_id: costOf1Id, | ||
}); | ||
|
||
expect(afterChanges).toContainEqual({ | ||
scenario_id: world.scenarioId, | ||
cost: expect.any(Number), | ||
pu_id: sameCostId, | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { v4 } from 'uuid'; | ||
|
||
import { ScenariosPlanningUnitGeoEntity } from '../../../src/modules/scenarios-planning-unit/entities/scenarios-planning-unit.geo.entity'; | ||
import { GivenScenarioPuDataExists } from '../../steps/given-scenario-pu-data-exists'; | ||
import { ScenariosPuCostDataGeo } from '../../../src/modules/analysis/providers/cost-surface/adapters/scenarios-pu-cost-data.geo.entity'; | ||
import { DbConnections } from '../../../src/ormconfig.connections'; | ||
|
||
export interface CostSurfaceUpdateWorld { | ||
cleanup: () => Promise<void>; | ||
scenarioId: string; | ||
planningUnitsIds: string[]; | ||
GivenPuCostDataExists: () => Promise<string[]>; | ||
GetPuCostsData: ( | ||
scenarioId: string, | ||
) => Promise<{ scenario_id: string; cost: number; pu_id: string }[]>; | ||
} | ||
|
||
export const createWorld = async ( | ||
app: INestApplication, | ||
): Promise<CostSurfaceUpdateWorld> => { | ||
const scenarioId = v4(); | ||
const puCostRepoToken = getRepositoryToken( | ||
ScenariosPuCostDataGeo, | ||
DbConnections.geoprocessingDB, | ||
); | ||
const puDataRepoToken = getRepositoryToken( | ||
ScenariosPlanningUnitGeoEntity, | ||
DbConnections.geoprocessingDB, | ||
); | ||
const puDataRepo: Repository<ScenariosPlanningUnitGeoEntity> = app.get( | ||
puDataRepoToken, | ||
); | ||
const puCostDataRepo: Repository<ScenariosPuCostDataGeo> = app.get( | ||
puCostRepoToken, | ||
); | ||
const scenarioPuData = await GivenScenarioPuDataExists( | ||
puDataRepo, | ||
scenarioId, | ||
); | ||
|
||
const puIds = scenarioPuData.rows.map((row) => row.puGeometryId); | ||
|
||
return { | ||
GetPuCostsData: async ( | ||
scenarioId: string, | ||
): Promise<{ scenario_id: string; cost: number; pu_id: string }[]> => | ||
puCostDataRepo.query(` | ||
select spud.scenario_id, spucd."cost", spucd.output_results_data_id as pu_id from scenarios_pu_data as spud join scenarios_pu_cost_data as spucd on (spud."id" = spucd.scenarios_pu_data_id) | ||
where spud.scenario_id = '${scenarioId}' | ||
`), | ||
GivenPuCostDataExists: async () => | ||
puCostDataRepo | ||
.save( | ||
scenarioPuData.rows.map((scenarioPuData) => | ||
puCostDataRepo.create({ | ||
scenariosPuDataId: scenarioPuData.id, | ||
cost: 300, | ||
planningUnitId: scenarioPuData.puGeometryId, | ||
scenariosPlanningUnit: scenarioPuData, | ||
}), | ||
), | ||
) | ||
.then((rows) => rows.map((row) => row.planningUnitId)), | ||
planningUnitsIds: puIds, | ||
scenarioId, | ||
cleanup: async () => { | ||
await puDataRepo.delete({ | ||
scenarioId, | ||
}); | ||
}, | ||
}; | ||
}; |
32 changes: 32 additions & 0 deletions
32
geoprocessing/src/migrations/geoprocessing/1621843948965-ScenariosPuCostData.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class ScenariosPuCostData1621843948965 implements MigrationInterface { | ||
name = 'ScenariosPuCostData1621843948965'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
/** | ||
* backward compatibility; can probably removed in another migration | ||
*/ | ||
await queryRunner.query( | ||
`ALTER TABLE "scenarios_pu_cost_data" ALTER COLUMN "cost" SET DEFAULT '0'`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "scenarios_pu_cost_data" ALTER COLUMN "cost" SET NOT NULL`, | ||
); | ||
await queryRunner.query( | ||
`COMMENT ON COLUMN "scenarios_pu_cost_data"."cost" IS 'By default we will set them as unitary based on equal area'`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "scenarios_pu_cost_data" ALTER COLUMN "cost" DROP NOT NULL`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "scenarios_pu_cost_data" ALTER COLUMN "cost" DROP DEFAULT`, | ||
); | ||
await queryRunner.query( | ||
`COMMENT ON COLUMN "scenarios_pu_cost_data"."cost" IS NULL`, | ||
); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
geoprocessing/src/migrations/geoprocessing/1621847467456-ScenariosPuCostDataId.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class ScenariosPuCostDataId1621847467456 implements MigrationInterface { | ||
name = 'ScenariosPuCostDataId1621847467456'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "scenarios_pu_cost_data" ADD "scenarios_pu_data_id" uuid`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "scenarios_pu_cost_data" ADD CONSTRAINT "FK_21454fad6e954ba771262974ae7" FOREIGN KEY ("scenarios_pu_data_id") REFERENCES "scenarios_pu_data"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "scenarios_pu_cost_data" DROP CONSTRAINT "FK_21454fad6e954ba771262974ae7"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "scenarios_pu_cost_data" DROP COLUMN "scenarios_pu_data_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export enum LockStatus { | ||
Unstated = 'unstated', | ||
|
||
/** | ||
* is always guaranteed to be tagged as included in the planning solution | ||
*/ | ||
LockedIn = 'locked-in', | ||
LockedOut = 'locked-out', | ||
} |
Oops, something went wrong.