-
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
- Loading branch information
Showing
6 changed files
with
216 additions
and
26 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.
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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
import { flatten, Injectable } from '@nestjs/common'; | ||
import { CostSurfaceRepo } from '../cost-surface-repo'; | ||
import { CostSurfaceInputDto } from '../../../entry-points/adjust-cost-surface-input'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { ScenariosPuCostDataGeo } from './scenarios-pu-cost-data.geo.entity'; | ||
import { DbConnections } from '../../../../../ormconfig.connections'; | ||
import { Repository } from 'typeorm'; | ||
|
||
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,80 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { ScenariosPlanningUnitGeoEntity } from '../../../src/modules/scenarios-planning-unit/entities/scenarios-planning-unit.geo.entity'; | ||
import { GivenScenarioPuDataExists } from '../../steps/given-scenario-pu-data-exists'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { ScenariosPuCostDataGeo } from '../../../src/modules/analysis/providers/cost-surface/adapters/scenarios-pu-cost-data.geo.entity'; | ||
import { DbConnections } from '../../../src/ormconfig.connections'; | ||
import { In, Repository } from 'typeorm'; | ||
import { v4 } from 'uuid'; | ||
|
||
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 puDataIds = scenarioPuData.rows.map((row) => row.id); | ||
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 puCostDataRepo.delete({ | ||
scenariosPlanningUnit: { | ||
id: In(puDataIds), | ||
}, | ||
}); | ||
await puDataRepo.delete({ | ||
scenarioId, | ||
}); | ||
}, | ||
}; | ||
}; |