101_add_account_stats_pricing.sql 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. -- Account statistics pricing: allow channels to configure custom pricing for account cost tracking.
  2. -- 1. Channel-level toggle
  3. ALTER TABLE channels ADD COLUMN IF NOT EXISTS apply_pricing_to_account_stats BOOLEAN NOT NULL DEFAULT FALSE;
  4. -- 2. Account stats pricing rules (ordered list per channel)
  5. CREATE TABLE IF NOT EXISTS channel_account_stats_pricing_rules (
  6. id BIGSERIAL PRIMARY KEY,
  7. channel_id BIGINT NOT NULL REFERENCES channels(id) ON DELETE CASCADE,
  8. name VARCHAR(100) NOT NULL DEFAULT '',
  9. group_ids BIGINT[] NOT NULL DEFAULT '{}',
  10. account_ids BIGINT[] NOT NULL DEFAULT '{}',
  11. sort_order INT NOT NULL DEFAULT 0,
  12. created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  13. updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
  14. );
  15. CREATE INDEX IF NOT EXISTS idx_cas_pricing_rules_channel_id ON channel_account_stats_pricing_rules(channel_id);
  16. -- 3. Model pricing for each rule (same structure as channel_model_pricing)
  17. CREATE TABLE IF NOT EXISTS channel_account_stats_model_pricing (
  18. id BIGSERIAL PRIMARY KEY,
  19. rule_id BIGINT NOT NULL REFERENCES channel_account_stats_pricing_rules(id) ON DELETE CASCADE,
  20. platform VARCHAR(50) NOT NULL DEFAULT '',
  21. models JSONB NOT NULL DEFAULT '[]',
  22. billing_mode VARCHAR(20) NOT NULL DEFAULT 'token',
  23. input_price NUMERIC(20,10),
  24. output_price NUMERIC(20,10),
  25. cache_write_price NUMERIC(20,10),
  26. cache_read_price NUMERIC(20,10),
  27. image_output_price NUMERIC(20,10),
  28. per_request_price NUMERIC(20,10),
  29. created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  30. updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
  31. );
  32. CREATE INDEX IF NOT EXISTS idx_cas_model_pricing_rule_id ON channel_account_stats_model_pricing(rule_id);
  33. -- 4. Usage logs: pre-computed account stats cost (NULL = use default formula)
  34. ALTER TABLE usage_logs ADD COLUMN IF NOT EXISTS account_stats_cost NUMERIC(20,10);