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

Store excel formulae for calculated intervention data rows/cells #351

Open
bgsandan opened this issue Feb 17, 2023 · 3 comments
Open

Store excel formulae for calculated intervention data rows/cells #351

bgsandan opened this issue Feb 17, 2023 · 3 comments
Labels
2023-Sprint-1 area::costings Related to costs of micronutrient programs and internventions Priority::1 User story that we want to focus on

Comments

@bgsandan
Copy link
Contributor

bgsandan commented Feb 17, 2023

Whilst the definitive values for the intervention calculations should be the ones in the database (re-calculated on updates to the individual values in intervention_data via @andy-bevan's script, we should also encode the relationships in a manner that they can be sent to the front-end.

This will allow (for example), fields that contain totals to be updated in the UI on the fly when values are updated (multiplying no. of factories by number of inspections etc).

To facilitate this, the intention is to utilise jsonLogic (http://jsonlogic.com) to encode the relationships in a format that can be sent in the API response and parsed and calculated on the front-end. @bgsandan has already managed to demonstrate a proof-of-concept of automatically converting an Excel formula e.g. =(D57+D60+D63)*D73+D70*D74 into its jsonLogic representation which can then utilised in the front-end:

{  "+": [
    {
      "*": [
        {
          "+": [
            {
              "+": [
                {
                  "var": "row57.year0"
                },
                {
                  "var": "row60.year0"
                }
              ]
            },
            {
              "var": "row63.year0"
            }
          ]
        },
        {
          "var": "row73.year0"
        }
      ]
    },
    {
      "*": [
        {
          "var": "row70.year0"
        },
        {
          "var": "row74.year0"
        }
      ]
    }
  ]
}

If the database can store the input formulae for fields with field types of totals or calculatedvalues then these formulae can be exposed in the appropriate views allowing this conversion to jsonLogic to be handled at the API layer.

See https://kwvmxgit.ad.nerc.ac.uk/-/snippets/125 / https://kwvmxgit.ad.nerc.ac.uk/-/snippets/126 for more info

@bgsandan bgsandan added the area::costings Related to costs of micronutrient programs and internventions label Feb 17, 2023
@bgsandan
Copy link
Contributor Author

Also worth noting that the code used to go from Excel to JsonLogic goes via an Abstract Syntax Tree (AST) representation of the formula.  It may be possible to tap into that to go back from the AST to statements that are valid PLPGSql for automating some of the function generation

@bgsandan bgsandan added Priority::2 Priority::1 User story that we want to focus on and removed Priority::2 labels Feb 17, 2023
@bgsandan
Copy link
Contributor Author

Ref #272

@rbroth
Copy link
Collaborator

rbroth commented Jul 26, 2023

I think this can be closed as completed

  • We are extracting excel formulas from the spreadsheets and storing them in intervention_cell_formula
  • We decided in a meeting at some point that it would be "easiest" to have all calculation logic encoded as JSON logic and use Javascript to do the calculation; that way, we can do calculations in the frontend and the API with the same code and won't have to maintain two codebases (JS for the frontend, PL/SQL for the db) to do the calculations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2023-Sprint-1 area::costings Related to costs of micronutrient programs and internventions Priority::1 User story that we want to focus on
Projects
None yet
Development

No branches or pull requests

2 participants