refactor(api, robot-server): add error for multiple CSV definitions and less reliance on file reading/creating #15956
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.
Overview
This PR follows up both #15855 and #15907 with further refinements and small changes to defining and setting CSV parameters, along with behind the scenes refactors.
The two main user/client facing changes of this PR are changing the HTTP status code when invalid data file IDs are sent to the
POST
/runs
,/protocols
and/protocols/{protocolId}/analyses
endpoints from 400 to 422, and raising a newIncompatibleParameterError
when multiple CSV file parameters are added to a single protocol. The change in status code is to reflect that the error is not because of a malformed request, but because there was something in the request that could not be processed (in this case being the data file ID). The new error was added to align with the original PRD and the current UI. While there's no inherent technical limitation for multiple CSV parameters (other than the theoretical limitations with memory, number of file handlers, etc), there are unexpected UI bugs when multiple ones are defined and this change enforces only one per protocol.The other major change of this PR is a non-user facing refactor of how we set the
CSVParameter
interface. Rather than passing thePath
of the file to the interface and then using aTemporaryFile
as the basis for all further access of the CSV file and it's contents, now the contents of the file (passed asbytes
) is what everything else is based off of, including CSV parsing inparse_as_csv
. With this change, we only ever make a temporary file handler when the user accesses.file
. With this change reducing the chance of there being an open file handler, a newfile_opened
property was added to the public interface to reduce needless file opening. This is technically user-facing code meant more for internal usage, but I see no danger in exposing it, though if needed we can tag it is a private non-guaranteed method.Test Plan and Hands on Testing
Tested the protocols in the previously mentioned PRs, along with ensuring the following one fails analysis.
Changelog
IncompatibleParameterError
whenadd_csv_file
in theParameterContext
is used more than once400
to422
CSVParameter
to only open temporary file handler when user requests oneReview requests
Should we label
file_opened
as private/use another way of preventing spurious temporary file creation just to close them?Risk assessment
Low.