Skip to content

Commit

Permalink
chore(api): scenario-cost-surface
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Jun 17, 2021
1 parent bc16c71 commit 8726239
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import { Module } from '@nestjs/common';
import { CostSurfaceViewService } from './cost-surface-view.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PlanningUnitsGeom } from '@marxan-jobs/planning-unit-geometry';
import { ScenariosPlanningUnitGeoEntity } from '@marxan/scenarios-planning-unit';
import {
ScenariosPlanningUnitGeoEntity,
ScenariosPuCostDataGeo,
} from '@marxan/scenarios-planning-unit';
import { DbConnections } from '@marxan-api/ormconfig.connections';

@Module({
imports: [
TypeOrmModule.forFeature(
[PlanningUnitsGeom, ScenariosPlanningUnitGeoEntity],
[
PlanningUnitsGeom,
ScenariosPlanningUnitGeoEntity,
ScenariosPuCostDataGeo,
],
DbConnections.geoprocessingDB,
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { ScenariosPlanningUnitGeoEntity } from '@marxan/scenarios-planning-unit'

@Injectable()
export class CostSurfaceViewService {
readonly #separator = '\t';

constructor(
@InjectRepository(
ScenariosPlanningUnitGeoEntity,
Expand All @@ -15,11 +17,12 @@ export class CostSurfaceViewService {
private readonly spuDataRepo: Repository<ScenariosPlanningUnitGeoEntity>,
) {}

async read(scenarioId: string, stream: stream.Writable): Promise<void> {
stream.write('id\tcost\tstatus');
async read(
scenarioId: string,
responseStream: stream.Writable,
): Promise<void> {
responseStream.write([`id`, `cost`, `status`].join(this.#separator));

// TODO make some changes upon lockin_status
// TODO add _costs rows
const query = await this.spuDataRepo
.createQueryBuilder('spu')
.select(['spu.puid', 'spu.lockin_status', 'spucd.cost'])
Expand All @@ -31,34 +34,24 @@ export class CostSurfaceViewService {
.where(`spu.scenario_id = :scenarioId`, { scenarioId });

const queryStream = await query.stream();
// typeorm query runner/builder -> stream

queryStream.on('data', (data) => {
const row = (data as unknown) as {
// "pipe" does not seem to trigger
queryStream.on(
'data',
(data: {
puid: number;
lockin_status: number | null;
cost: number | null;
};
const tsvRow = [row.puid, row.lockin_status, row.cost].join('\t');
stream.write(`\n`);
stream.write(tsvRow);
});
queryStream.on('result', (data) => {
console.log(`-result`, data);
});
queryStream.on('end', () => {
// queryRunner.release();
stream.end();
console.log(`-end`);
});
queryStream.on('error', (error) => {
// queryRunner.release();
stream.destroy(error);
console.log(`-error`, error);
});
spucd_cost: number | null;
}) => {
const tsvRow = [data.puid, data.spucd_cost, data.lockin_status].join(
this.#separator,
);
responseStream.write(`\n`);
responseStream.write(tsvRow);
},
);

stream.on(`finish`, () => {
console.log(`finished?`);
queryStream.on('end', () => {
responseStream.end();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ describe(`When scenario has PUs with cost and lock status`, () => {

it(`returns relevant data`, async () => {
const result = await world.WhenGettingMarxanData();
expect(result).toMatchInlineSnapshot(`
"id cost status
0
1
2
3 "
const [headers, ...costAndStatus] = result.split('\n');

expect(headers).toEqual('id\tcost\tstatus');
expect(costAndStatus).toMatchInlineSnapshot(`
Array [
"0 200 ",
"1 400 1",
"2 600 2",
"3 800 2",
]
`);
});
});
Expand Down
32 changes: 29 additions & 3 deletions api/apps/api/test/scenario-cost-surface/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { GivenUserIsLoggedIn } from '../steps/given-user-is-logged-in';
import * as request from 'supertest';
import { In, Repository } from 'typeorm';
import { getRepositoryToken } from '@nestjs/typeorm';
import { ScenariosPlanningUnitGeoEntity } from '@marxan/scenarios-planning-unit';
import {
LockStatus,
ScenariosPlanningUnitGeoEntity,
ScenariosPuCostDataGeo,
} from '@marxan/scenarios-planning-unit';
import { Polygon } from 'geojson';
import { v4 } from 'uuid';
import { DbConnections } from '@marxan-api/ormconfig.connections';
import {
PlanningUnitsGeom,
Expand All @@ -14,6 +17,7 @@ import {
import { GivenProjectExists } from '../steps/given-project';
import { ScenariosTestUtils } from '../utils/scenarios.test.utils';
import { ScenarioType } from '@marxan-api/modules/scenarios/scenario.api.entity';
import { v4 } from 'uuid';

export const createWorld = async () => {
const app = await bootstrapApplication();
Expand All @@ -30,6 +34,7 @@ export const createWorld = async () => {
})
).data.id;
const geometries: string[] = [];
const scenariosPuData: string[] = [];

const puGeometryRepo: Repository<PlanningUnitsGeom> = app.get(
getRepositoryToken(PlanningUnitsGeom, DbConnections.geoprocessingDB),
Expand All @@ -40,11 +45,17 @@ export const createWorld = async () => {
DbConnections.geoprocessingDB,
),
);
const scenarioPuDataCostRepo: Repository<ScenariosPuCostDataGeo> = app.get(
getRepositoryToken(ScenariosPuCostDataGeo, DbConnections.geoprocessingDB),
);

return {
cleanup: async () => {
await ScenariosTestUtils.deleteScenario(app, jwt, scenarioId);
await projectCleanup();
await scenarioPuDataCostRepo.delete({
scenariosPuDataId: In(scenariosPuData),
});
await puGeometryRepo.delete({
id: In(geometries),
});
Expand Down Expand Up @@ -77,15 +88,30 @@ export const createWorld = async () => {
).identifiers;

geometries.push(...geoRows.map((geo) => geo.id));
await scenarioPuDataRepo.save(
const scenarioPuData = await scenarioPuDataRepo.save(
geometries.map((id, index) =>
scenarioPuDataRepo.create({
puGeometryId: id,
scenarioId,
planningUnitMarxanId: index,
lockStatus:
index === 0
? LockStatus.Unstated
: index === 1
? LockStatus.LockedIn
: LockStatus.LockedOut,
}),
),
);
scenariosPuData.push(...scenarioPuData.map((spud) => spud.id));
await scenarioPuDataCostRepo.save(
scenarioPuData.map((spud, index) => ({
cost: (index + 1) * 200,
scenariosPuDataId: spud.id,
scenariosPlanningUnit: spud,
planningUnitId: v4(),
})),
);
},
WhenGettingMarxanData: async () =>
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { flatten } from 'lodash';

import { CostSurfacePersistencePort } from '../ports/persistence/cost-surface-persistence.port';
import { PlanningUnitCost } from '../ports/planning-unit-cost';
import { ScenariosPuCostDataGeo } from '../../scenarios/scenarios-pu-cost-data.geo.entity';
import { ScenariosPuCostDataGeo } from '@marxan/scenarios-planning-unit';

@Injectable()
export class TypeormCostSurface implements CostSurfacePersistencePort {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import { ShapefileConverterPort } from './ports/shapefile-converter/shapefile-co

import { TypeormCostSurface } from './adapters/typeorm-cost-surface';
import { ShapefileConverter } from './adapters/shapefile-converter';
import { ScenariosPuCostDataGeo } from '../scenarios/scenarios-pu-cost-data.geo.entity';
import { PuCostExtractor } from './adapters/pu-cost-extractor';
import { AvailablePlanningUnitsRepository } from './adapters/available-planning-units-repository';
import { ScenariosPuCostDataGeo } from '@marxan/scenarios-planning-unit';

@Module({
imports: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { v4 } from 'uuid';

import { ScenariosPuCostDataGeo } from '@marxan-geoprocessing/modules/scenarios/scenarios-pu-cost-data.geo.entity';
import { ScenariosPlanningUnitGeoEntity } from '@marxan/scenarios-planning-unit';
import {
ScenariosPlanningUnitGeoEntity,
ScenariosPuCostDataGeo,
} from '@marxan/scenarios-planning-unit';

import { GivenScenarioPuDataExists } from '../../steps/given-scenario-pu-data-exists';

Expand Down
1 change: 1 addition & 0 deletions api/libs/scenarios-planning-unit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { LockStatus } from './lock-status.enum';
export { ScenariosPlanningUnitGeoEntity } from './scenarios-planning-unit.geo.entity';
export { ScenariosOutputResultsGeoEntity } from './scenarios-output-results.geo.entity';
export { ScenariosPuCostDataGeo } from './scenarios-pu-cost-data.geo.entity';
export * from './domain';

0 comments on commit 8726239

Please sign in to comment.