settle.go 869 B

123456789101112131415161718192021222324
  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. afterGroup := QuotaRound(cost * snap.GroupRatio)
  13. crossed := trace.MatchedTier != snap.EstimatedTier
  14. return TieredResult{
  15. ActualQuotaBeforeGroup: cost,
  16. ActualQuotaAfterGroup: afterGroup,
  17. MatchedTier: trace.MatchedTier,
  18. CrossedTier: crossed,
  19. }, nil
  20. }