Skip to content

Commit

Permalink
feat(api): aggregate features in spec.dat and puvspr.dat
Browse files Browse the repository at this point in the history
  • Loading branch information
Dyostiq committed Sep 22, 2021
1 parent 385346f commit 850b83a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Connection, getConnection } from 'typeorm';
import { Connection } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { DbConnections } from '@marxan-api/ormconfig.connections';
import { InjectConnection } from '@nestjs/typeorm';
Expand All @@ -25,9 +25,10 @@ export class PuvsprDatService {
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
select st_union(the_geom), min(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}'
group by fd.feature_id
) species,
(
select the_geom, spd.puid, spd.scenario_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@ export class SpecDataTsvFile {
#optionalColumns = new Set<OptionalKeys<SpecDataEntry>>([
'target2',
'targetocc',
'name',
'sepnum',
'sepdistance',
]);

#rows: SpecDataEntry[] = [];

addRow(data: ScenarioFeaturesData) {
addRow(
data: Pick<
ScenarioFeaturesData,
| 'featureId'
| 'target'
| 'prop'
| 'fpf'
| 'target2'
| 'targetocc'
| 'sepNum'
| 'metadata'
>,
) {
let sepdistance: string | undefined;
if (typeof data.metadata?.sepdistance === 'number')
sepdistance = data.metadata.sepdistance.toFixed(2);
Expand All @@ -41,7 +52,6 @@ export class SpecDataTsvFile {
spf: data.fpf?.toFixed(2),
target2: data.target2?.toFixed(2),
targetocc: data.targetocc?.toFixed(2),
name: data.name ?? undefined,
sepnum: data.sepNum?.toFixed(2),
sepdistance: sepdistance,
};
Expand Down
42 changes: 34 additions & 8 deletions api/apps/api/src/modules/scenarios/input-files/spec.dat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,40 @@ export class SpecDatService {
) {}

async getSpecDatContent(scenarioId: string): Promise<string> {
const rows = await this.scenarioFeaturesData.find({
where: {
scenarioId,
},
order: {
featureId: 'ASC',
},
});
const rows: Pick<
ScenarioFeaturesData,
| 'featureId'
| 'target'
| 'prop'
| 'fpf'
| 'target2'
| 'targetocc'
| 'sepNum'
| 'metadata'
>[] = await this.scenarioFeaturesData.query(
`
with grouped_feature as (
select min(sfd.feature_id) as min_feature_id
from public.scenario_features_data as sfd
inner join features_data fd on fd.id = sfd.feature_class_id
where sfd.scenario_id = $1
group by fd.feature_id
)
select
feature_id as "featureId",
target,
prop,
fpf,
target2,
targetocc,
sepnum as "sepNum",
metadata
from grouped_feature
left join scenario_features_data as sfd on feature_id = grouped_feature.min_feature_id
order by feature_id
`,
[scenarioId],
);

const specDatFile = new SpecDataTsvFile();
for (const row of rows) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ beforeEach(async () => {
{
provide: token,
useValue: {
find: jest.fn(),
query: jest.fn(),
} as any,
},
],
Expand All @@ -32,18 +32,18 @@ beforeEach(async () => {

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

it(`should return headers only`, async () => {
expect(await sut.getSpecDatContent('scenario-id')).toEqual(
`id\ttarget\tprop\tspf\ttarget2\ttargetocc\tname\tsepnum\tsepdistance`,
`id\ttarget\tprop\tspf\ttarget2\ttargetocc\tsepnum\tsepdistance`,
);
});
});
describe(`when there is data available`, () => {
beforeEach(() => {
dataRepo.find.mockImplementationOnce(async () => [
dataRepo.query.mockImplementationOnce(async () => [
{
scenarioId: 'id',
featureId: 0,
Expand Down Expand Up @@ -79,7 +79,7 @@ describe(`when there is data available`, () => {

describe(`when there is data available`, () => {
beforeEach(() => {
dataRepo.find.mockImplementationOnce(async () => [
dataRepo.query.mockImplementationOnce(async () => [
{
scenarioId: 'id',
featureId: 1,
Expand All @@ -90,7 +90,6 @@ describe(`when there is data available`, () => {
target2: 32,
sepNum: 10,
targetocc: 999,
name: 'entry',
metadata: {
sepdistance: 4000,
},
Expand All @@ -100,8 +99,8 @@ describe(`when there is data available`, () => {

it(`should return content`, async () => {
expect(await sut.getSpecDatContent('scenario-id')).toMatchInlineSnapshot(`
"id target prop spf target2 targetocc name sepnum sepdistance
1 30.00 0.25 1.66 32.00 999.00 entry 10.00 4000.00"
"id target prop spf target2 targetocc sepnum sepdistance
1 30.00 0.25 1.66 32.00 999.00 10.00 4000.00"
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe(`when getting input.zip`, () => {
describe(`when getting spec.dat`, () => {
it(`should resolve text/*`, async () => {
expect((await world.WhenGettingSpecDat()).text).toMatchInlineSnapshot(
`"id target prop spf target2 targetocc name sepnum sepdistance"`,
`"id target prop spf target2 targetocc sepnum sepdistance"`,
);
});
});
Expand Down

0 comments on commit 850b83a

Please sign in to comment.