Currently cost logs only record the currency and the monetary cost. The cost in compute units, with an 'at risk' margin (risk_cost in CostLog) is calculated by the application when needed, based on the latest exchange rate, gbp to compute units conversion rate, and risk margin.
As the exchange rate and/or our conversions to compute units could change over time, we should instead calculate and include this figure in the cost logs as they are recorded.
This will require:
Creating a new migration to add the risk_cost field to cost logs
Moving the current logic in the CostLog risk_cost method elsewhere (e.g to a private or class method 'calculate_risk_cost'), so the value of this new attribute is used instead
Either updating the logic in the services AwsCostsRecorder and AzureCostsRecorder to explicitly calculate and include this value as part of their recording/updating logic, or add a mechanism in the CostLog class itself to automatically set this (e.g. use an after_initialize or before_create callback: https://guides.rubyonrails.org/active_record_callbacks.html)
Would also be good to have a data migration to calculate and add this for all existing cost logs
Currently cost logs only record the currency and the monetary cost. The cost in compute units, with an 'at risk' margin (
risk_cost
in CostLog) is calculated by the application when needed, based on the latest exchange rate, gbp to compute units conversion rate, and risk margin.As the exchange rate and/or our conversions to compute units could change over time, we should instead calculate and include this figure in the cost logs as they are recorded.
This will require:
risk_cost
field to cost logsrisk_cost
method elsewhere (e.g to a private or class method 'calculate_risk_cost'), so the value of this new attribute is used insteadAwsCostsRecorder
andAzureCostsRecorder
to explicitly calculate and include this value as part of their recording/updating logic, or add a mechanism in the CostLog class itself to automatically set this (e.g. use anafter_initialize
orbefore_create
callback: https://guides.rubyonrails.org/active_record_callbacks.html)