2026-03-16 16:00:22 +08:00
|
|
|
package billingexpr
|
|
|
|
|
|
2026-03-17 15:29:43 +08:00
|
|
|
// quotaConversion converts raw expression output to quota based on the
|
|
|
|
|
// expression version. This is the central dispatch point for future versions
|
|
|
|
|
// that may use a different conversion formula.
|
|
|
|
|
func quotaConversion(exprOutput float64, snap *BillingSnapshot) float64 {
|
|
|
|
|
switch snap.ExprVersion {
|
|
|
|
|
default: // v1: coefficients are $/1M tokens prices
|
|
|
|
|
return exprOutput / 1_000_000 * snap.QuotaPerUnit
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 16:00:22 +08:00
|
|
|
// ComputeTieredQuota runs the Expr from a frozen BillingSnapshot against
|
|
|
|
|
// actual token counts and returns the settlement result.
|
|
|
|
|
func ComputeTieredQuota(snap *BillingSnapshot, params TokenParams) (TieredResult, error) {
|
|
|
|
|
return ComputeTieredQuotaWithRequest(snap, params, RequestInput{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ComputeTieredQuotaWithRequest(snap *BillingSnapshot, params TokenParams, request RequestInput) (TieredResult, error) {
|
|
|
|
|
cost, trace, err := RunExprByHashWithRequest(snap.ExprString, snap.ExprHash, params, request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return TieredResult{}, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 15:29:43 +08:00
|
|
|
quotaBeforeGroup := quotaConversion(cost, snap)
|
2026-03-16 20:11:55 +08:00
|
|
|
afterGroup := QuotaRound(quotaBeforeGroup * snap.GroupRatio)
|
2026-03-16 16:00:22 +08:00
|
|
|
crossed := trace.MatchedTier != snap.EstimatedTier
|
|
|
|
|
|
|
|
|
|
return TieredResult{
|
2026-03-16 20:11:55 +08:00
|
|
|
ActualQuotaBeforeGroup: quotaBeforeGroup,
|
2026-03-16 16:00:22 +08:00
|
|
|
ActualQuotaAfterGroup: afterGroup,
|
|
|
|
|
MatchedTier: trace.MatchedTier,
|
|
|
|
|
CrossedTier: crossed,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|