fixes
This commit is contained in:
1 parent
6586722652
commit
1e7c7bbfbb
65 files changed
+1345
-640
No files matched your search
@@ -334,10 +334,10 @@ func (s *Service) consumeCredits(ctx context.Context, tx pgx.Tx, companyID uuid.
|
||||
}
|
||||
_ = s.EnsureDefaultCosts(ctx)
|
||||
|
||||
featureCost := s.lookupCost(ctx, featureName, 1)
|
||||
featureCost := s.lookupCostForCompany(ctx, companyID, featureName, 1)
|
||||
tokenKCost := 1
|
||||
if tokenCount > 0 {
|
||||
tokenKCost = s.lookupCost(ctx, "openai_token_k", 1)
|
||||
tokenKCost = s.lookupCostForCompany(ctx, companyID, "openai_token_k", 1)
|
||||
}
|
||||
var debit int
|
||||
if productCount == 1 {
|
||||
@@ -477,6 +477,43 @@ func (s *Service) lookupCost(ctx context.Context, feature string, fallback int)
|
||||
return cost
|
||||
}
|
||||
|
||||
// lookupCostForCompany resolves credit pricing without caching tenant-specific rules.
|
||||
// Overrides must be positive; the existing credit engine enforces a minimum of 1.
|
||||
// An unconfigured/missing migration retains the global default behavior.
|
||||
func (s *Service) lookupCostForCompany(ctx context.Context, companyID uuid.UUID, feature string, fallback int) int {
|
||||
var cost int
|
||||
err := s.Pool.QueryRow(ctx, `
|
||||
SELECT COALESCE(
|
||||
(SELECT cc.cost_per_unit FROM company_processing_costs cc
|
||||
WHERE cc.company_id = $1 AND cc.feature_name = $2),
|
||||
(SELECT pc.cost_per_unit FROM plan_processing_costs pc
|
||||
JOIN company_plans cp ON cp.plan_id = pc.plan_id
|
||||
WHERE cp.company_id = $1 AND cp.is_active = true AND pc.feature_name = $2
|
||||
ORDER BY cp.created_at DESC LIMIT 1),
|
||||
(SELECT gc.cost_per_unit FROM processing_costs gc
|
||||
WHERE gc.feature_name = $2 AND gc.is_active = true),
|
||||
$3
|
||||
)::integer`, companyID, feature, fallback).Scan(&cost)
|
||||
if err == nil && cost >= 0 {
|
||||
return cost
|
||||
}
|
||||
return s.lookupCost(ctx, feature, fallback)
|
||||
}
|
||||
|
||||
// EstimateDebitForCompany uses the same company/plan pricing rules as the actual debit.
|
||||
func (s *Service) EstimateDebitForCompany(ctx context.Context, companyID uuid.UUID, feature string, tokenCount int) int {
|
||||
if feature == "" {
|
||||
feature = "product_processing"
|
||||
}
|
||||
_ = s.EnsureDefaultCosts(ctx)
|
||||
base := s.lookupCostForCompany(ctx, companyID, feature, 1)
|
||||
perK := 1
|
||||
if tokenCount > 0 {
|
||||
perK = s.lookupCostForCompany(ctx, companyID, "openai_token_k", 1)
|
||||
}
|
||||
return DebitAmount(base, perK, tokenCount)
|
||||
}
|
||||
|
||||
// EnsureDefaultCosts seeds plan-aligned processing cost rows (idempotent).
|
||||
// After a successful seed in this process, subsequent calls no-op (worker seeds at boot).
|
||||
func (s *Service) EnsureDefaultCosts(ctx context.Context) error {
|
||||
@@ -900,6 +937,10 @@ type UsageSummary struct {
|
||||
ProductsProcessed int `json:"products_processed"`
|
||||
ProductsTotal int `json:"products_total"`
|
||||
Tokens int64 `json:"tokens"`
|
||||
InputTokens int64 `json:"input_tokens"`
|
||||
CachedInputTokens int64 `json:"cached_input_tokens"`
|
||||
OutputTokens int64 `json:"output_tokens"`
|
||||
AICalls int64 `json:"ai_calls"`
|
||||
FeedsInput int `json:"feeds_input"`
|
||||
FeedsExport int `json:"feeds_export"`
|
||||
JobsTotal int `json:"jobs_total"`
|
||||
@@ -1010,6 +1051,21 @@ func (s *Service) UsageSummary(ctx context.Context, companyID uuid.UUID, rangeRa
|
||||
out.ProductsProcessed = out.ProductsTotal
|
||||
}
|
||||
|
||||
// Detailed token usage comes from the durable per-call ledger, not processed
|
||||
// product rows (a single product can make several model calls).
|
||||
// A missing old ledger should not prevent viewing the rest of billing.
|
||||
if err := s.Pool.QueryRow(ctx, `
|
||||
SELECT COALESCE(SUM(prompt_tokens),0)::bigint,
|
||||
COALESCE(SUM(cached_prompt_tokens),0)::bigint,
|
||||
COALESCE(SUM(completion_tokens),0)::bigint,
|
||||
COALESCE(SUM(calls),0)::bigint
|
||||
FROM ai_usage_daily
|
||||
WHERE company_id = $1 AND ($2::timestamptz IS NULL OR day >= $2::date)
|
||||
AND ($3::timestamptz IS NULL OR day < $3::date)`,
|
||||
companyID, since, until).Scan(&out.InputTokens, &out.CachedInputTokens, &out.OutputTokens, &out.AICalls); err != nil {
|
||||
out.Notes = append(out.Notes, "Detailed token usage unavailable; check the AI usage ledger migration.")
|
||||
}
|
||||
|
||||
if rangeKey == "7d" || rangeKey == "30d" || (rangeKey == "cycle" && since != nil) {
|
||||
seriesSince := now.Truncate(24*time.Hour).AddDate(0, 0, -29)
|
||||
days := 30
|
||||
|
||||
Reference in new issue
Block a user