Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve puvspr.dat queries #397

Merged
merged 4 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
import { Repository } from 'typeorm';
import { Connection, getConnection } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { ScenarioPuvsprGeoEntity } from '@marxan/scenario-puvspr';
import { DbConnections } from '@marxan-api/ormconfig.connections';

import { InjectConnection } from '@nestjs/typeorm';
@Injectable()
export class PuvsprDatService {
constructor(
@InjectRepository(ScenarioPuvsprGeoEntity, DbConnections.geoprocessingDB)
private readonly puvsprRepo: Repository<ScenarioPuvsprGeoEntity>,
@InjectConnection(DbConnections.geoprocessingDB)
private readonly connection: Connection,
Comment on lines +8 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just use puvsprRepo.query instead creating new connection.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I know - I did so because with this PR the puvsprRepo should become redundant and we should be able to drop it altogether: I left it in the current PR in order not to change too much in case the new approach ends up not working out in practice, but all going well we should be able to drop the repo.

) {}

async getPuvsprDatContent(scenarioId: string): Promise<string> {
const rows = await this.puvsprRepo.find({
where: {
scenarioId,
},
});
/**
* @TODO further performance savings: limiting scans to planning_units_geom
* by partition (we need to get the grid shape from the parent project); use
* && operator instead of st_intersects() for bbox-based calculation of
* intersections.
*/
const rows: {
scenario_id: string;
pu_id: number;
feature_id: number;
amount: number;
}[] = await this.connection.query(`
select pu.scenario_id as scenario_id, puid as pu_id, feature_id, ST_Area(ST_Transform(st_intersection(species.the_geom, pu.the_geom),3410)) as amount
from
(
select scenario_id, the_geom, sfd.feature_id
from scenario_features_data sfd
inner join features_data fd on sfd.feature_class_id = fd.id where sfd.scenario_id = '${scenarioId}'
) species,
(
select the_geom, spd.puid, spd.scenario_id
from planning_units_geom pug
inner join scenarios_pu_data spd on pug.id = spd.pu_geom_id where spd.scenario_id = '${scenarioId}' order by puid asc
) pu
where pu.scenario_id = '${scenarioId}' and species.the_geom && pu.the_geom
order by puid, feature_id asc;
`);
return (
'species\tpu\tamount\n' +
rows
.map((row) => `${row.featureId}\t${row.puId}\t${row.amount.toFixed(6)}`)
.map(
(row) => `${row.feature_id}\t${row.pu_id}\t${row.amount.toFixed(6)}`,
)
.join('\n')
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Repository } from 'typeorm';
import { ScenarioPuvsprGeoEntity } from '@marxan/scenario-puvspr';
import { getRepositoryToken } from '@nestjs/typeorm';
import { getConnectionToken, getRepositoryToken } from '@nestjs/typeorm';
import { Test } from '@nestjs/testing';
import { DbConnections } from '@marxan-api/ormconfig.connections';

Expand All @@ -10,17 +10,14 @@ let sut: PuvsprDatService;
let dataRepo: jest.Mocked<Repository<ScenarioPuvsprGeoEntity>>;

beforeEach(async () => {
const token = getRepositoryToken(
ScenarioPuvsprGeoEntity,
DbConnections.geoprocessingDB,
);
const token = getConnectionToken(DbConnections.geoprocessingDB);
const sandbox = await Test.createTestingModule({
providers: [
PuvsprDatService,
{
provide: token,
useValue: {
find: jest.fn(),
query: jest.fn(),
} as any,
},
],
Expand All @@ -32,7 +29,7 @@ beforeEach(async () => {

describe(`when there are no rows`, () => {
beforeEach(() => {
dataRepo.find.mockImplementationOnce(async () => []);
dataRepo.query.mockImplementationOnce(async () => []);
});

it(`should return headers only`, async () => {
Expand All @@ -44,24 +41,24 @@ describe(`when there are no rows`, () => {

describe(`when there is data available`, () => {
beforeEach(() => {
dataRepo.find.mockImplementationOnce(async () => [
dataRepo.query.mockImplementationOnce(async () => [
{
amount: 1000.0,
scenarioId: 'scenarioId',
featureId: 'feature-1',
puId: 'pu-1,',
scenario_id: 'scenarioId',
feature_id: 'feature-1',
pu_id: 'pu-1,',
},
{
amount: 0.001,
scenarioId: 'scenarioId',
featureId: 'feature-1',
puId: 'pu-2,',
scenario_id: 'scenarioId',
feature_id: 'feature-1',
pu_id: 'pu-2,',
},
{
amount: 99.995,
scenarioId: 'scenarioId',
featureId: 'feature-1',
puId: 'pu-3,',
scenario_id: 'scenarioId',
feature_id: 'feature-1',
pu_id: 'pu-3,',
},
]);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddIndexesForPuVSprQueries1627980508000
implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
create index scenario_features_data__scenario_id__idx on scenario_features_data(scenario_id);
create index scenarios_pu_data__scenario_id__idx on scenarios_pu_data(scenario_id);
create index scenario_features_data__feature_class_id__idx on scenario_features_data(feature_class_id);
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
drop index scenario_features_data__scenario_id__idx;
drop index scenarios_pu_data__scenario_id__idx;
drop index scenario_features_data__feature_class_id__idx;
`);
}
}
5 changes: 5 additions & 0 deletions api/libs/scenario-puvspr/src/scenario-puvspr.geo.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ViewColumn, ViewEntity } from 'typeorm';

/**
* @deprecated Due to performance limitations of the approach using a db view,
* we have moved this logic to a query which is run directly from the service
* responsible for retrieving this data.
*/
@ViewEntity({
expression: `
select pu.scenario_id as scenario_id, puid as pu_id, feature_id, ST_Area(ST_Transform(st_intersection(species.the_geom, pu.the_geom),3410)) as amount
Expand Down