settle.go 951 B

12345678910111213141516171819202122232425
  1. package billingexpr
  2. // ComputeTieredQuota runs the Expr from a frozen BillingSnapshot against
  3. // actual token counts and returns the settlement result.
  4. func ComputeTieredQuota(snap *BillingSnapshot, params TokenParams) (TieredResult, error) {
  5. return ComputeTieredQuotaWithRequest(snap, params, RequestInput{})
  6. }
  7. func ComputeTieredQuotaWithRequest(snap *BillingSnapshot, params TokenParams, request RequestInput) (TieredResult, error) {
  8. cost, trace, err := RunExprByHashWithRequest(snap.ExprString, snap.ExprHash, params, request)
  9. if err != nil {
  10. return TieredResult{}, err
  11. }
  12. quotaBeforeGroup := cost / 1_000_000 * snap.QuotaPerUnit
  13. afterGroup := QuotaRound(quotaBeforeGroup * snap.GroupRatio)
  14. crossed := trace.MatchedTier != snap.EstimatedTier
  15. return TieredResult{
  16. ActualQuotaBeforeGroup: quotaBeforeGroup,
  17. ActualQuotaAfterGroup: afterGroup,
  18. MatchedTier: trace.MatchedTier,
  19. CrossedTier: crossed,
  20. }, nil
  21. }