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

JSON-ify credit values with perfect precision if we introduce job types with fractional costs #2105

Open
jtherrmann opened this issue Feb 24, 2024 · 2 comments
Labels
Jira Story Create a Jira Story for this issue

Comments

@jtherrmann
Copy link
Contributor

jtherrmann commented Feb 24, 2024

Jira: https://asfdaac.atlassian.net/browse/TOOL-2609

Note: The above link is accessible only to members of ASF.


As part of the configurable job cost implementation in #2072, we've settled on always handling credit values as decimal.Decimal objects so that we always perform credit calculations using fixed-point arithmetic rather than floating-point arithmetic. This is how banks and other financial institutions perform calculations with currency, and will be important for avoiding precision errors if we ever introduce a job type with a fractional cost, which would almost certainly be less than 1.0.

Currently, all of our API responses include at least one credits value:

  • /costs returns a table of credit costs
  • /jobs endpoints include a credit_cost field for each job
  • /user includes a remaining_credits field

The responses are all serialized as JSON using flask.jsonify. By default, jsonify would serialize Decimal objects as str, but we define a CustomEncoder class in apps/api/src/hyp3_api/routes.py, which converts Decimal to float.

This should be fine for now, as we don't have any fractional job costs. However, if we ever introduce any job types with fractional costs, we should revisit how to serialize credit values in our API responses. For example, the default jsonify behavior of converting Decimal to str may be more appropriate (though we wouldn't want to break how we serialize float job parameter values). We want to ensure that credit values are serialized with perfect precision in our API responses, and that they can be de-serialized with perfect precision by the client.

@jtherrmann jtherrmann added the Jira Story Create a Jira Story for this issue label Feb 24, 2024
@jtherrmann
Copy link
Contributor Author

This would also require updating our SDK functions and associated unit tests.

@jtherrmann
Copy link
Contributor Author

We should also update the SDK to always handle credit values as Decimal rather than float.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Jira Story Create a Jira Story for this issue
Projects
None yet
Development

No branches or pull requests

1 participant