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

[NM-82] download results state refactor #1036

Merged
merged 5 commits into from
Nov 21, 2024
Merged

[NM-82] download results state refactor #1036

merged 5 commits into from
Nov 21, 2024

Conversation

M-Kusumgar
Copy link
Contributor

@M-Kusumgar M-Kusumgar commented Nov 20, 2024

Description

This one was painful. Ill remove AGYW in the next PR to see how easy/hard it is to modify this new construction, almost 2000 lines gone, damn.

There was a handy enum in the types file called DownloadType so i thought way not roll with that, everything is constructed from downloadConfig.ts, using Object.values on a string enum (does not work well on a usual enum, can try it if you want to know a stupid typescript decision) gives all the values and we can loop over it to do everything

The tests were especially painful, i suspect there'll be small simplifications when we remove AGYW

The other thing is I had to add a payload handler to the api service because i needed finer control over the payload the api need to commit to the state (needed to add type so that it knows which DownloadType to commit to)

ideally, to add another button you just need to extend the 3 things in downloadConfig.ts

Edit: the e2e test failure is a real one! i am removing AGYW in #1037 which im merging into here so will just fix the e2e test there

Copy link

@M-Kusumgar M-Kusumgar requested a review from r-ash November 20, 2024 20:25
Copy link
Collaborator

@r-ash r-ash left a comment

Choose a reason for hiding this comment

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

This looks really great, so good to see so much duplication removed.

Couple of comments on a few bits would be nice to get your thoughts on. But I think this looks good to go

@@ -89,6 +89,7 @@
import {RootState} from "../../root";
import {DownloadResultsState} from "../../store/downloadResults/downloadResults";
import {defineComponent} from "vue";
import { DownloadType } from "../../store/downloadResults/downloadConfig";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Bit of weird indenting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahhh my auto import extension is trippin, fixed

Comment on lines -38 to -113
async downloadComparisonReport(context) {
const {state, commit} = context
commit({type: DownloadResultsMutation.ComparisonDownloadError, payload: null})
await api(context)
.ignoreSuccess()
.withError(DownloadResultsMutation.ComparisonDownloadError)
.download(`download/result/${state.comparison.downloadId}`)
},

async downloadSpectrumOutput(context) {
const {state, commit} = context
commit({type: DownloadResultsMutation.SpectrumOutputDownloadError, payload: null})
await api(context)
.ignoreSuccess()
.withError(DownloadResultsMutation.SpectrumOutputDownloadError)
.download(`download/result/${state.spectrum.downloadId}`)
},

async downloadSummaryReport(context) {
const {state, commit} = context
commit({type: DownloadResultsMutation.SummaryOutputDownloadError, payload: null})
await api(context)
.ignoreSuccess()
.withError(DownloadResultsMutation.SummaryOutputDownloadError)
.download(`download/result/${state.summary.downloadId}`)
},

async downloadCoarseOutput(context) {
const {state, commit} = context
commit({type: DownloadResultsMutation.CoarseOutputDownloadError, payload: null})
await api(context)
.ignoreSuccess()
.withError(DownloadResultsMutation.CoarseOutputDownloadError)
.download(`download/result/${state.coarseOutput.downloadId}`)
},

async downloadAgywTool(context) {
const {state, commit} = context
commit({type: DownloadResultsMutation.AgywDownloadError, payload: null})
await api(context)
.ignoreSuccess()
.withError(DownloadResultsMutation.AgywDownloadError)
.download(`download/result/${state.agyw.downloadId}`)
},

async prepareCoarseOutput(context) {
const {state, dispatch, rootState, commit} = context
if (!state.coarseOutput.downloadId && !state.coarseOutput.fetchingDownloadId) {
commit({type: "SetFetchingDownloadId", payload: DOWNLOAD_TYPE.COARSE});
const calibrateId = rootState.modelCalibrate.calibrateId
const response = await api<DownloadResultsMutation, DownloadResultsMutation>(context)
.withSuccess(DownloadResultsMutation.PreparingCoarseOutput)
.withError(DownloadResultsMutation.CoarseOutputError)
.postAndReturn(`download/submit/coarse-output/${calibrateId}`, {})

if (response) {
await dispatch("poll", DOWNLOAD_TYPE.COARSE)
}
}
},

async prepareSummaryReport(context) {
const {state, dispatch, rootState, commit} = context
if (!state.summary.downloadId && !state.summary.fetchingDownloadId) {
commit({type: "SetFetchingDownloadId", payload: DOWNLOAD_TYPE.SUMMARY});
const calibrateId = rootState.modelCalibrate.calibrateId
const response = await api<DownloadResultsMutation, DownloadResultsMutation>(context)
.withSuccess(DownloadResultsMutation.PreparingSummaryReport)
.withError(DownloadResultsMutation.SummaryError)
.postAndReturn(`download/submit/summary/${calibrateId}`, {})

if (response) {
await dispatch("poll", DOWNLOAD_TYPE.SUMMARY)
}
}
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Love to see it

@@ -3,273 +3,83 @@ import {RootState} from "../../root";
import {DownloadResultsState} from "./downloadResults";
import {api, ResponseWithType} from "../../apiService";
import {DownloadResultsMutation} from "./mutations";
import {ModelStatusResponse} from "../../generated";
import {DOWNLOAD_TYPE} from "../../types";
import {Error, ModelStatusResponse} from "../../generated";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Error not used 🤓

Copy link
Contributor Author

@M-Kusumgar M-Kusumgar Nov 21, 2024

Choose a reason for hiding this comment

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

yep removed, neither was switches in the next line! you can tell i did a couple of drafts before settling on the final version haha

Comment on lines +26 to +30
[DownloadType.SPECTRUM]: { url: "spectrum", body: (store) => store.rootGetters.projectState },
[DownloadType.COARSE]: { url: "coarse-output", body: () => ({}) },
[DownloadType.SUMMARY]: { url: "summary", body: () => ({}) },
[DownloadType.COMPARISON]: { url: "comparison", body: () => ({}) },
[DownloadType.AGYW]: { url: "agyw", body: () => ({}) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Small point but I think I would favour adding the full URL (minus the ID) here instead of some here and some in prepareOutput. Makes it a little easier to find if in one place

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed, in fact lets do a bit more generally, lets just make both the body and url functions that take in the store, that would be good!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

AgywError = "AgywError",
AgywStatusUpdated = "AgywStatusUpdated",
AgywDownloadError = "AgywDownloadError",
Preparing = "Preparing",
Copy link
Collaborator

Choose a reason for hiding this comment

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

So good, fewer than half the original number

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yh these were definitely satisfying files!

<error-alert v-if="spectrum.downloadError" :error="spectrum.downloadError"></error-alert>
<template v-for="type in Object.values(DownloadType)" :key="type">
<div :id="`${type}-download`" v-if="switches[type]">
<download :translate-key="`${type}Download`"
Copy link
Collaborator

@r-ash r-ash Nov 21, 2024

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 put this into the downloadConfig.ts like the switches. A map of type to translation key. I do like all the download stuff being in the single config file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmmm do you mean because theres duplication between this and the locales.ts type? because otherwise everything is auto generated, also the types in the locales file ensures we have the matching translation key

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, only that when you're adding a new file type you have to look here to see what the key is. It is {type}Download" instead of just being able to see the full string in the downloadConfig.ts` ? That make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done extracted

@@ -111,23 +112,23 @@ export const actions: ActionTree<ADRUploadState, RootState> & ADRUploadActions =
requestParams["resourceId"] = resourceId
}
if (resourceType === rootState.adr.schemas?.outputSummary) {
downloadId = rootState.downloadResults.summary.downloadId
downloadId = rootState.downloadResults[DownloadType.SUMMARY].downloadId
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you also think about handling this through the downloadConfig? Have some list of files which need to go to the ADR, and here look through the DownloadType and include those which are in the list?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could use it above in UploadModal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the reason i didnt do this here is because this requires me to refactor the ADR upload stuff because we need some way of matching up if "outputSummary" exists, then include DownloadType.SUMMARY, i say we do a smaller refactor in a separate PR and sort out the adrUpload stuff and there we can put these mappings, infact some of the adrUpload stuff can probably just come from the downloadConfig itself

in the UploadModal we have this same mapping issue and also have outputFileError which has a weird control flow which does something different if both spectrum and summary have metadata errors vs just one of them, i would like to properly investigate that and see if theres a pattern that we can extract

i can create a ticket for these two if you agree!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep sounds great doing that separately, yes please create the tickets.

@@ -141,6 +136,12 @@ export const mutations: MutationTree<RootState> = {

};

const stopPollingAllDownloadTypes = (state: RootState) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️

Comment on lines +73 to +76
withError = (type: E, root = false, payloadFunc?: (e: Error) => any) => {
this._onError = (failure: Response) => {
this._commit({type: type, payload: APIService.getFirstErrorFromFailure(failure)}, {root});
const error = APIService.getFirstErrorFromFailure(failure);
this._commit({ type, payload: payloadFunc ? payloadFunc(error) : error }, { root });
Copy link
Collaborator

Choose a reason for hiding this comment

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

So this is what I had previously added withErrorCallback for. But I think what you've done is better. Could you remove the withErrorCallback helper and tidy up uses of that to use this instead?

But actually, I wonder if we should have 2 functions withError and withErrorHandler? Or some better name? Whatever you think!

Copy link
Contributor Author

@M-Kusumgar M-Kusumgar Nov 21, 2024

Choose a reason for hiding this comment

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

ohhhh damn sorry i didnt even see that, i was looking for that type of functionality, yh im happy to remove it, i think its nice to declare the mutation you want and all that, i personally think its nicer if we just have one function with an optional arg, it feels like exactly the same responsibility, just with pre commit step or not

im basically not sure what we gain by having 2 separate functions XD

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nope that's fine, happy with 1 function if you want.

@r-ash r-ash mentioned this pull request Nov 21, 2024
@M-Kusumgar
Copy link
Contributor Author

have addressed comments in this commit: 481b209

ADR upload refactor ticket is created!: https://www.notion.so/unaids-naomi/Upload-ADR-state-refactor-f1c71f2f341c49aa98bb9f3bad88e99c?pvs=4

@r-ash r-ash self-requested a review November 21, 2024 20:50
Copy link
Collaborator

@r-ash r-ash left a comment

Choose a reason for hiding this comment

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

Thanks for that, this is looking great. Just merged main into it and updated the download test

Copy link
Collaborator

@r-ash r-ash left a comment

Choose a reason for hiding this comment

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

Thanks for that, I just merged in #1034 and then removed downloadOutput action. We don't need that any more as we're now downloading via an href instead of an action

@r-ash r-ash merged commit fa30f27 into main Nov 21, 2024
7 of 9 checks passed
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.

2 participants