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

[api] Add cost surface piece exporter and importer [MRXN23-111] [MRXN23-19] [MRXN23-112] [MRXN23-20] #1503

Merged

Conversation

yulia-bel
Copy link
Contributor

Add cost surface piece exporter

Overview

Please write a description. If the PR is hard to understand, provide a quick explanation of the code.

Designs

Link to the related design prototypes (if applicable)

Testing instructions

Please explain how to test the PR: ID of a dataset, steps to reach the feature, etc.

Feature relevant tickets

Link to the related task manager tickets


Checklist before submitting

  • Meaningful commits and code rebased on develop.
  • If this PR adds feature that should be tested for regressions when
    deploying to staging/production, please add brief testing instructions
    to the deploy checklist (docs/deployment-checklist.md)
  • Update CHANGELOG file

@yulia-bel yulia-bel added the WIP Work in progress label Sep 18, 2023
@vercel
Copy link

vercel bot commented Sep 18, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
marxan ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 5, 2023 11:39am

@yulia-bel yulia-bel force-pushed the chore/api/MRXN23-111-add-cost-surface-piece-exporter branch from a7b1036 to a7730dc Compare September 26, 2023 13:56
@yulia-bel yulia-bel changed the title [api] Add cost surface piece exporter [MRXN23-111] [api] Add cost surface piece exporter and importer [MRXN23-111] [MRXN23-19] [MRXN23-112] [MRXN23-20] Sep 27, 2023
@yulia-bel yulia-bel removed the WIP Work in progress label Sep 27, 2023
@yulia-bel yulia-bel requested a review from hotzevzl September 27, 2023 08:41
Copy link
Member

@hotzevzl hotzevzl left a comment

Choose a reason for hiding this comment

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

@yulia-bel thanks, and apologies for taking so long to review this - I've been mostly down the rabbit hole of trying to make the functionality of this feature to work in practice in a variety of contexts (projects with or without scenarios, with run scenarios, etc.) and as you know this has been a royal headache in its own right because of the moving pieces in related PRs 😬

As it stands, the code itself looks fine, but:

  • please rebase it on the latest develop and consider what to do with the check that I highlighted inline in my review, as after rebasing we'd be "guaranteed" (except if any bugs, etc) that by the time we clone a project, this would have at least the default cost surface
  • check the functionality after rebasing - so far I have only been able to get cloning to work with a very specific combination of factors

const costSurfacesIds = costSurfaces.map((costSurface) => costSurface.id);
let costSurfaceData: CostSurfaceDataSelectResult[] = [];
let projectPusMap: Record<string, number> = {};
if (costSurfacesIds.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should just give up in case we don't have any cost surfaces, or even skip this check and go ahead (in which case we'd have an empty fileContent later on.

In practice, once rebasing this PR on the latest develop, we should never have projects without any cost surface (at least the default one must be created for the project to be ready). I don't think we should necessarily check here if a is ready/well formed, because we have these checks in place elsewhere and we should not be able to even clone a project if the project is not fully ready.

On the other hand, if we did not care here (basically avoiding altogether the if (costSurfacesIds.length > 0) check), we'd have a malformed export that should fail on import (garbage in, garbage out).

But, with the current implementation, more or less we end up with some kind of inconsistent state if costSurfacesIds.length === 0, so I am not sure. I'd err on avoiding the check altogether, and maybe logging an error. Or making the export fail altogether.

Comment on lines +84 to +96
const fileContent: ProjectCostSurfacesContent = {
costSurfaces: costSurfaces.map(({ id, ...costSurface }) => ({
...costSurface,
data: costSurfaceData
.filter(
(data: CostSurfaceDataSelectResult) => data.cost_surface_id === id,
)
.map(({ cost_surface_id, projects_pu_id, ...data }) => {
const puid = projectPusMap[projects_pu_id];
return { puid, ...data };
}),
})),
};
Copy link
Member

Choose a reason for hiding this comment

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

just a note on style - my recommendation for these kinds of "augmentations" is to use named functions to make what we're doing via array operations (filter, map, etc.), spreads and so on clearer.

In practice here we're combining a lookup of cost surface costs with the setter part of a lens, using lower-level primitives: which is ok and works, but makes even a tiny snippet of code like this less legible than something like const fileContent: ProjectCostSurfacesContent = addCostDataToCostSurfaces(costSurfaces, costSurfaceCostData)

but no need to do anything here! (and as always, these kinds of style comments can be highly opinable)

@yulia-bel yulia-bel force-pushed the chore/api/MRXN23-111-add-cost-surface-piece-exporter branch from e2a944b to 29e3b93 Compare October 5, 2023 08:51
@hotzevzl hotzevzl merged commit 3abab91 into develop Oct 6, 2023
52 checks passed
@hotzevzl hotzevzl deleted the chore/api/MRXN23-111-add-cost-surface-piece-exporter branch October 6, 2023 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants