fix
This commit is contained in:
1 parent
feebf7d3c1
commit
486502852e
41 files changed
+1311
-259
No files matched your search
@@ -36,9 +36,9 @@ const featureGatesCacheTTL = 30 * time.Second
|
||||
|
||||
type CreditsOverview struct {
|
||||
TotalCredits int `json:"total_credits"`
|
||||
UsedCredits int `json:"used_credits"`
|
||||
Remaining int `json:"remaining"`
|
||||
RemainingCredits int `json:"remaining_credits"`
|
||||
UsedCredits float64 `json:"used_credits"`
|
||||
Remaining float64 `json:"remaining"`
|
||||
RemainingCredits float64 `json:"remaining_credits"`
|
||||
LowCredits bool `json:"low_credits"`
|
||||
LowCreditsThreshold int `json:"low_credits_threshold"`
|
||||
ProductCount int `json:"product_count"`
|
||||
@@ -69,9 +69,10 @@ func (s *Service) CreditsOverview(ctx context.Context, companyID uuid.UUID, lowT
|
||||
lowThreshold = 100
|
||||
}
|
||||
var total, used int
|
||||
var fractional float64
|
||||
err := s.Pool.QueryRow(ctx, `
|
||||
SELECT total_credits, used_credits FROM credit_balances WHERE company_id = $1`, companyID).
|
||||
Scan(&total, &used)
|
||||
SELECT total_credits, used_credits, fractional_credits_used::double precision FROM credit_balances WHERE company_id = $1`, companyID).
|
||||
Scan(&total, &used, &fractional)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
_, _ = s.Pool.Exec(ctx, `INSERT INTO credit_balances (company_id) VALUES ($1) ON CONFLICT DO NOTHING`, companyID)
|
||||
total, used = 0, 0
|
||||
@@ -79,10 +80,13 @@ func (s *Service) CreditsOverview(ctx context.Context, companyID uuid.UUID, lowT
|
||||
return CreditsOverview{}, err
|
||||
}
|
||||
|
||||
remaining := RemainingCreditsClamped(total, used)
|
||||
remaining := float64(total-used) - fractional
|
||||
if remaining < 0 {
|
||||
remaining = 0
|
||||
}
|
||||
out := CreditsOverview{
|
||||
TotalCredits: total,
|
||||
UsedCredits: used,
|
||||
UsedCredits: float64(used) + fractional,
|
||||
Remaining: remaining,
|
||||
RemainingCredits: remaining,
|
||||
LowCreditsThreshold: lowThreshold,
|
||||
@@ -157,7 +161,7 @@ func (s *Service) CreditsOverview(ctx context.Context, companyID uuid.UUID, lowT
|
||||
planName = "Free"
|
||||
featuresRaw = []byte("{}")
|
||||
}
|
||||
ent := ComputeEntitlements(planName, monthlyVal, remaining, isTrial)
|
||||
ent := ComputeEntitlements(planName, monthlyVal, int(remaining+0.9999), isTrial)
|
||||
out.CanUseAI = ent.CanUseAI
|
||||
out.CanUseEPREL = ent.CanUseEPREL
|
||||
out.IsFreePlan = ent.IsFreePlan
|
||||
@@ -214,6 +218,9 @@ func (s *Service) AIBrandApplyAllowed(ctx context.Context, companyID uuid.UUID)
|
||||
type ProcessingGateOpts struct {
|
||||
// RequiresAI when true enforces can_use_ai + credit wallet (AI-only job types).
|
||||
RequiresAI bool
|
||||
// CreditsPerProduct is the effective per-output rate for this job.
|
||||
// Zero retains legacy one-credit minimum for callers without content pricing.
|
||||
CreditsPerProduct float64
|
||||
// RequiresEPREL when true enforces can_use_eprel (EPREL-only job types).
|
||||
RequiresEPREL bool
|
||||
}
|
||||
@@ -239,9 +246,15 @@ func (s *Service) AssertCanStartProcessing(ctx context.Context, companyID uuid.U
|
||||
if ent.RemainingCredits < 1 {
|
||||
return fmt.Errorf("%w: no credits remaining — upgrade your plan to continue AI processing", ErrInsufficientCredits)
|
||||
}
|
||||
if ent.RemainingCredits < batchSize {
|
||||
return fmt.Errorf("%w: need at least %d credits for this batch (have %d) — upgrade or select fewer products",
|
||||
ErrInsufficientCredits, batchSize, ent.RemainingCredits)
|
||||
needed := float64(batchSize)
|
||||
if opts.CreditsPerProduct > 0 {
|
||||
needed *= opts.CreditsPerProduct
|
||||
}
|
||||
// The wallet debit uses precise fractional values and remains authoritative.
|
||||
// The coarse entitlement is rounded up, so avoid rejecting a valid sub-credit job.
|
||||
if float64(ent.RemainingCredits)+0.000001 < needed {
|
||||
return fmt.Errorf("%w: need about %.2f credits for this batch (up to %d whole-credit units available)",
|
||||
ErrInsufficientCredits, needed, ent.RemainingCredits)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -385,7 +398,7 @@ func (s *Service) applyCreditDebit(ctx context.Context, tx pgx.Tx, companyID uui
|
||||
WITH upd AS (
|
||||
UPDATE credit_balances
|
||||
SET used_credits = used_credits + $2, updated_at = now()
|
||||
WHERE company_id = $1 AND (total_credits - used_credits) >= $2
|
||||
WHERE company_id = $1 AND (total_credits - used_credits - fractional_credits_used) >= $2
|
||||
RETURNING company_id
|
||||
),
|
||||
cyc AS (
|
||||
@@ -462,7 +475,7 @@ func (s *Service) lookupCost(ctx context.Context, feature string, fallback int)
|
||||
|
||||
var cost int
|
||||
err := s.Pool.QueryRow(ctx, `
|
||||
SELECT cost_per_unit FROM processing_costs
|
||||
SELECT cost_per_unit::integer FROM processing_costs
|
||||
WHERE feature_name = $1 AND is_active = true`, feature).Scan(&cost)
|
||||
if err != nil || cost <= 0 {
|
||||
return fallback
|
||||
@@ -884,7 +897,7 @@ func (s *Service) AssignPlan(ctx context.Context, companyID uuid.UUID, planID in
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO credit_balances (company_id, total_credits, used_credits, updated_at)
|
||||
VALUES ($1, $2, 0, now())
|
||||
ON CONFLICT (company_id) DO UPDATE SET total_credits = EXCLUDED.total_credits, used_credits = 0, updated_at = now()`,
|
||||
ON CONFLICT (company_id) DO UPDATE SET total_credits = EXCLUDED.total_credits, used_credits = 0, fractional_credits_used = 0, updated_at = now()`,
|
||||
companyID, alloc)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -931,9 +944,9 @@ type UsageDayPoint struct {
|
||||
type UsageSummary struct {
|
||||
CompanyID uuid.UUID `json:"company_id"`
|
||||
Range string `json:"range"`
|
||||
CreditsUsed int `json:"credits_used"`
|
||||
CreditsUsed float64 `json:"credits_used"`
|
||||
CreditsTotal int `json:"credits_total"`
|
||||
CreditsRemaining int `json:"credits_remaining"`
|
||||
CreditsRemaining float64 `json:"credits_remaining"`
|
||||
ProductsProcessed int `json:"products_processed"`
|
||||
ProductsTotal int `json:"products_total"`
|
||||
Tokens int64 `json:"tokens"`
|
||||
@@ -969,17 +982,21 @@ func (s *Service) UsageSummary(ctx context.Context, companyID uuid.UUID, rangeRa
|
||||
}
|
||||
|
||||
var total, used int
|
||||
var fractional float64
|
||||
err := s.Pool.QueryRow(ctx, `
|
||||
SELECT total_credits, used_credits FROM credit_balances WHERE company_id = $1`, companyID).
|
||||
Scan(&total, &used)
|
||||
SELECT total_credits, used_credits, fractional_credits_used::double precision FROM credit_balances WHERE company_id = $1`, companyID).
|
||||
Scan(&total, &used, &fractional)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
total, used = 0, 0
|
||||
} else if err != nil {
|
||||
return out, err
|
||||
}
|
||||
out.CreditsTotal = total
|
||||
out.CreditsUsed = used
|
||||
out.CreditsRemaining = RemainingCreditsClamped(total, used)
|
||||
out.CreditsUsed = float64(used) + fractional
|
||||
out.CreditsRemaining = float64(total-used) - fractional
|
||||
if out.CreditsRemaining < 0 {
|
||||
out.CreditsRemaining = 0
|
||||
}
|
||||
out.Notes = append(out.Notes,
|
||||
"Credits are the live wallet (credit_balances). Daily credit history is not ledgered yet — range filters apply to products and tokens only.")
|
||||
|
||||
@@ -1250,7 +1267,7 @@ func (s *Service) claimAndRollDueCompanyPlan(ctx context.Context, rowID int64) (
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO credit_balances (company_id, total_credits, used_credits, updated_at)
|
||||
VALUES ($1, $2, 0, now())
|
||||
ON CONFLICT (company_id) DO UPDATE SET total_credits = EXCLUDED.total_credits, used_credits = 0, updated_at = now()`,
|
||||
ON CONFLICT (company_id) DO UPDATE SET total_credits = EXCLUDED.total_credits, used_credits = 0, fractional_credits_used = 0, updated_at = now()`,
|
||||
companyID, monthly)
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
||||
Reference in new issue
Block a user