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

Open
#2,105 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
flask, python
Domain
api

Research direction

Start by reading apps/api/src/hyp3_api/routes.py and tracing how /costs, /jobs, and /user responses serialize credit values with flask.jsonify; then check how clients deserialize those values. The issue asks for a representation that preserves credit precision through serialization and deserialization while keeping float job parameters compatible. Done means credit values round-trip without precision loss across these API responses.

Written by the indexing model from the issue text.

Description

Jira Story

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 https://github.com/ASFHyP3/hyp3/pull/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.

Dominant language
Python
Stars
53
Forks
13
Avg merge
1d 2h
Merged PRs (30d)
20

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ASFHyP3/hyp3

All issues in ASFHyP3/hyp3

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.