-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat(api): allow to get scenarios solutions results #264
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
66e90cd
feat(api): allow to get scenarios solutions results
kgajowy 15a54c1
refactor(api): remove unused imports from scenario solution dto
kgajowy 78de3b1
refactor(api): replace deprecated decorator
kgajowy bed5b79
refactor(scenario-solutions): more expresive separation of best/most-…
kgajowy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
api/apps/api/src/modules/scenarios/dto/scenario-solution-result.dto.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,40 @@ | ||
import { ApiExtraModels, ApiProperty, getSchemaPath } from '@nestjs/swagger'; | ||
import { ScenariosOutputResultsGeoEntity } from '@marxan/scenarios-planning-unit'; | ||
|
||
class ScenarioSolutionsDataDto { | ||
@ApiProperty() | ||
type: 'solutions' = 'solutions'; | ||
|
||
@ApiProperty() | ||
id!: string; | ||
|
||
@ApiProperty({ | ||
isArray: true, | ||
type: () => ScenariosOutputResultsGeoEntity, | ||
}) | ||
attributes!: ScenariosOutputResultsGeoEntity[]; | ||
} | ||
|
||
class ScenarioSolutionDataDto { | ||
@ApiProperty() | ||
type: 'solution' = 'solution'; | ||
|
||
@ApiProperty() | ||
id!: string; | ||
|
||
@ApiProperty({ | ||
type: () => ScenariosOutputResultsGeoEntity, | ||
}) | ||
attributes!: ScenariosOutputResultsGeoEntity; | ||
} | ||
|
||
@ApiExtraModels(ScenarioSolutionsDataDto, ScenarioSolutionDataDto) | ||
export class ScenarioSolutionResultDto { | ||
@ApiProperty({ | ||
oneOf: [ | ||
{ $ref: getSchemaPath(ScenarioSolutionsDataDto) }, | ||
{ $ref: getSchemaPath(ScenarioSolutionDataDto) }, | ||
], | ||
}) | ||
data!: ScenarioSolutionsDataDto | ScenarioSolutionDataDto; | ||
} |
23 changes: 23 additions & 0 deletions
23
api/apps/api/src/modules/scenarios/dto/scenario-solution.serializer.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 { Injectable } from '@nestjs/common'; | ||
import { PaginationMeta } from '@marxan-api/utils/app-base.service'; | ||
import { ScenariosOutputResultsGeoEntity } from '@marxan/scenarios-planning-unit'; | ||
import { SolutionResultCrudService } from '../solutions-result/solution-result-crud.service'; | ||
|
||
@Injectable() | ||
export class ScenarioSolutionSerializer { | ||
constructor( | ||
private readonly scenariosSolutionsCrudService: SolutionResultCrudService, | ||
) {} | ||
|
||
async serialize( | ||
entities: | ||
| Partial<ScenariosOutputResultsGeoEntity> | ||
| (Partial<ScenariosOutputResultsGeoEntity> | undefined)[], | ||
paginationMeta?: PaginationMeta, | ||
): Promise<any> { | ||
return this.scenariosSolutionsCrudService.serialize( | ||
entities, | ||
paginationMeta, | ||
); | ||
} | ||
} |
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
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
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
72 changes: 72 additions & 0 deletions
72
api/apps/api/src/modules/scenarios/solutions-result/solution-result-crud.service.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,72 @@ | ||
import { Repository } from 'typeorm'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { | ||
AppBaseService, | ||
JSONAPISerializerConfig, | ||
} from '@marxan-api/utils/app-base.service'; | ||
|
||
import { AppInfoDTO } from '@marxan-api/dto/info.dto'; | ||
import { AppConfig } from '@marxan-api/utils/config.utils'; | ||
import { FetchSpecification } from 'nestjs-base-service'; | ||
import { ScenariosOutputResultsGeoEntity } from '@marxan/scenarios-planning-unit'; | ||
import { DbConnections } from '@marxan-api/ormconfig.connections'; | ||
|
||
@Injectable() | ||
export class SolutionResultCrudService extends AppBaseService< | ||
ScenariosOutputResultsGeoEntity, | ||
never, | ||
never, | ||
AppInfoDTO | ||
> { | ||
constructor( | ||
@InjectRepository( | ||
ScenariosOutputResultsGeoEntity, | ||
DbConnections.geoprocessingDB, | ||
) | ||
protected readonly repository: Repository<ScenariosOutputResultsGeoEntity>, | ||
) { | ||
super(repository, 'solution', 'solutions', { | ||
logging: { muteAll: AppConfig.get<boolean>('logging.muteAll', false) }, | ||
}); | ||
} | ||
|
||
get serializerConfig(): JSONAPISerializerConfig<ScenariosOutputResultsGeoEntity> { | ||
return { | ||
attributes: [ | ||
'id', | ||
'planningUnits', | ||
'missingValues', | ||
'cost', | ||
'score', | ||
'run', | ||
], | ||
keyForAttribute: 'camelCase', | ||
}; | ||
} | ||
|
||
async extendFindAllResults( | ||
entitiesAndCount: [ScenariosOutputResultsGeoEntity[], number], | ||
_fetchSpecification?: FetchSpecification, | ||
_info?: AppInfoDTO, | ||
): Promise<[ScenariosOutputResultsGeoEntity[], number]> { | ||
const extendedEntities: Promise<ScenariosOutputResultsGeoEntity>[] = entitiesAndCount[0].map( | ||
(entity) => this.extendGetByIdResult(entity), | ||
); | ||
return [await Promise.all(extendedEntities), entitiesAndCount[1]]; | ||
} | ||
|
||
async extendGetByIdResult( | ||
entity: ScenariosOutputResultsGeoEntity, | ||
_fetchSpecification?: FetchSpecification, | ||
_info?: AppInfoDTO, | ||
): Promise<ScenariosOutputResultsGeoEntity> { | ||
// TODO implement | ||
entity.planningUnits = 17; | ||
entity.missingValues = 13; | ||
entity.cost = 400; | ||
entity.score = 999; | ||
entity.run = 1; | ||
return entity; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
api/apps/api/test/scenario-solutions/get-all-solutions.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,47 @@ | ||
import { PromiseType } from 'utility-types'; | ||
import { createWorld } from './world'; | ||
|
||
let world: PromiseType<ReturnType<typeof createWorld>>; | ||
|
||
beforeAll(async () => { | ||
world = await createWorld(); | ||
}); | ||
|
||
describe(`When getting scenario solution results`, () => { | ||
beforeAll(async () => { | ||
await world.GivenScenarioHasSolutionsReady(); | ||
}); | ||
|
||
it(`should resolve solutions`, async () => { | ||
const response = await world.WhenGettingSolutions(); | ||
expect(response.body.meta).toMatchInlineSnapshot(` | ||
Object { | ||
"page": 1, | ||
"size": 25, | ||
"totalItems": 1, | ||
"totalPages": 1, | ||
} | ||
`); | ||
|
||
expect(response.body.data.length).toEqual(1); | ||
expect(response.body.data[0].attributes).toMatchInlineSnapshot( | ||
{ | ||
id: expect.any(String), | ||
}, | ||
` | ||
Object { | ||
"cost": 400, | ||
"id": Any<String>, | ||
"missingValues": 13, | ||
"planningUnits": 17, | ||
"run": 1, | ||
"score": 999, | ||
} | ||
`, | ||
); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await world?.cleanup(); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they seem to be mutually exclusive
maybe it's not a problem, though
also a dto could be used here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, they are exclusive (and returning different DTO). Any idea how to show it in code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
honestly I'd create another method and route for it, trying to show it in the code with one method is tricky (especially when you'd like to keep it in the openapi spec) and can be not worth the effort
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd go with @Dyostiq 's suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or make
bestSolution
andoneOfFiveMostDifferentSolutions
(or something like this) column-based props of the entity, which would allow to filter via?filter[bestSolution]=true
or?filter[oneOfBlaBla]=true
.but semantically I prefer separate endpoints - if we expose these props as part of the entity then API consumers could as well just filter client-side, IMO.
:id/marxan/run/:runId/solutions/best
:id/marxan/run/:runId/solutions/most-different
at the expense of some controller handler boilerplate duplication, though a minor evil here IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kgajowy should we remove in a new PR the
best
andmost-different
queries and relative code paths in this controller, or did you want to leave these in to let API clients query for best and most different solutions in either way? (I am fine either way, though I'd prefer to have only one way to query for best and most efficient, via their own endpoint).