2023-05-15 08:51:32 +08:00
|
|
|
from enum import Enum
|
|
|
|
|
2024-02-06 13:21:13 +08:00
|
|
|
from extensions.ext_database import db
|
2024-08-13 14:44:10 +08:00
|
|
|
|
|
|
|
from .types import StringUUID
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
class ProviderType(Enum):
|
2024-09-10 17:08:06 +08:00
|
|
|
CUSTOM = "custom"
|
|
|
|
SYSTEM = "system"
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def value_of(value):
|
2023-08-12 00:57:00 +08:00
|
|
|
for member in ProviderType:
|
2023-05-15 08:51:32 +08:00
|
|
|
if member.value == value:
|
|
|
|
return member
|
|
|
|
raise ValueError(f"No matching enum found for value '{value}'")
|
|
|
|
|
|
|
|
|
|
|
|
class ProviderQuotaType(Enum):
|
2024-09-10 17:08:06 +08:00
|
|
|
PAID = "paid"
|
2023-08-12 00:57:00 +08:00
|
|
|
"""hosted paid quota"""
|
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
FREE = "free"
|
2023-08-12 00:57:00 +08:00
|
|
|
"""third-party free quota"""
|
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
TRIAL = "trial"
|
2023-08-12 00:57:00 +08:00
|
|
|
"""hosted trial quota"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def value_of(value):
|
|
|
|
for member in ProviderQuotaType:
|
|
|
|
if member.value == value:
|
|
|
|
return member
|
|
|
|
raise ValueError(f"No matching enum found for value '{value}'")
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Provider(db.Model):
|
|
|
|
"""
|
|
|
|
Provider model representing the API providers and their configurations.
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
|
|
|
|
__tablename__ = "providers"
|
2023-05-15 08:51:32 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="provider_pkey"),
|
|
|
|
db.Index("provider_tenant_id_provider_idx", "tenant_id", "provider_name"),
|
|
|
|
db.UniqueConstraint(
|
|
|
|
"tenant_id", "provider_name", "provider_type", "quota_type", name="unique_provider_name_type_quota"
|
|
|
|
),
|
2023-05-15 08:51:32 +08:00
|
|
|
)
|
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-04-29 11:58:17 +08:00
|
|
|
tenant_id = db.Column(StringUUID, nullable=False)
|
2024-06-05 00:13:04 +08:00
|
|
|
provider_name = db.Column(db.String(255), nullable=False)
|
2023-05-15 08:51:32 +08:00
|
|
|
provider_type = db.Column(db.String(40), nullable=False, server_default=db.text("'custom'::character varying"))
|
|
|
|
encrypted_config = db.Column(db.Text, nullable=True)
|
2024-09-10 17:08:06 +08:00
|
|
|
is_valid = db.Column(db.Boolean, nullable=False, server_default=db.text("false"))
|
2023-05-15 08:51:32 +08:00
|
|
|
last_used = db.Column(db.DateTime, nullable=True)
|
|
|
|
|
|
|
|
quota_type = db.Column(db.String(40), nullable=True, server_default=db.text("''::character varying"))
|
2023-08-12 00:57:00 +08:00
|
|
|
quota_limit = db.Column(db.BigInteger, nullable=True)
|
|
|
|
quota_used = db.Column(db.BigInteger, default=0)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
|
|
|
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
2024-09-12 14:00:36 +08:00
|
|
|
return (
|
|
|
|
f"<Provider(id={self.id}, tenant_id={self.tenant_id}, provider_name='{self.provider_name}',"
|
|
|
|
f" provider_type='{self.provider_type}')>"
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def token_is_set(self):
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
Returns True if the encrypted_config is not None, indicating that the token is set.
|
|
|
|
"""
|
2023-05-15 08:51:32 +08:00
|
|
|
return self.encrypted_config is not None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_enabled(self):
|
|
|
|
"""
|
|
|
|
Returns True if the provider is enabled.
|
|
|
|
"""
|
|
|
|
if self.provider_type == ProviderType.SYSTEM.value:
|
|
|
|
return self.is_valid
|
|
|
|
else:
|
|
|
|
return self.is_valid and self.token_is_set
|
2023-08-12 00:57:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ProviderModel(db.Model):
|
|
|
|
"""
|
|
|
|
Provider model representing the API provider_models and their configurations.
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
|
|
|
|
__tablename__ = "provider_models"
|
2023-08-12 00:57:00 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="provider_model_pkey"),
|
|
|
|
db.Index("provider_model_tenant_id_provider_idx", "tenant_id", "provider_name"),
|
|
|
|
db.UniqueConstraint(
|
|
|
|
"tenant_id", "provider_name", "model_name", "model_type", name="unique_provider_model_name"
|
|
|
|
),
|
2023-08-12 00:57:00 +08:00
|
|
|
)
|
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-04-29 11:58:17 +08:00
|
|
|
tenant_id = db.Column(StringUUID, nullable=False)
|
2024-06-05 00:13:04 +08:00
|
|
|
provider_name = db.Column(db.String(255), nullable=False)
|
2024-01-02 23:42:00 +08:00
|
|
|
model_name = db.Column(db.String(255), nullable=False)
|
2023-08-12 00:57:00 +08:00
|
|
|
model_type = db.Column(db.String(40), nullable=False)
|
|
|
|
encrypted_config = db.Column(db.Text, nullable=True)
|
2024-09-10 17:08:06 +08:00
|
|
|
is_valid = db.Column(db.Boolean, nullable=False, server_default=db.text("false"))
|
|
|
|
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
|
|
|
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
2023-08-12 00:57:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TenantDefaultModel(db.Model):
|
2024-09-10 17:08:06 +08:00
|
|
|
__tablename__ = "tenant_default_models"
|
2023-08-12 00:57:00 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="tenant_default_model_pkey"),
|
|
|
|
db.Index("tenant_default_model_tenant_id_provider_type_idx", "tenant_id", "provider_name", "model_type"),
|
2023-08-12 00:57:00 +08:00
|
|
|
)
|
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-04-29 11:58:17 +08:00
|
|
|
tenant_id = db.Column(StringUUID, nullable=False)
|
2024-06-05 00:13:04 +08:00
|
|
|
provider_name = db.Column(db.String(255), nullable=False)
|
2024-05-10 18:12:18 +08:00
|
|
|
model_name = db.Column(db.String(255), nullable=False)
|
2023-08-12 00:57:00 +08:00
|
|
|
model_type = db.Column(db.String(40), nullable=False)
|
2024-09-10 17:08:06 +08:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
|
|
|
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
2023-08-12 00:57:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TenantPreferredModelProvider(db.Model):
|
2024-09-10 17:08:06 +08:00
|
|
|
__tablename__ = "tenant_preferred_model_providers"
|
2023-08-12 00:57:00 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="tenant_preferred_model_provider_pkey"),
|
|
|
|
db.Index("tenant_preferred_model_provider_tenant_provider_idx", "tenant_id", "provider_name"),
|
2023-08-12 00:57:00 +08:00
|
|
|
)
|
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-04-29 11:58:17 +08:00
|
|
|
tenant_id = db.Column(StringUUID, nullable=False)
|
2024-06-05 00:13:04 +08:00
|
|
|
provider_name = db.Column(db.String(255), nullable=False)
|
2023-08-12 00:57:00 +08:00
|
|
|
preferred_provider_type = db.Column(db.String(40), nullable=False)
|
2024-09-10 17:08:06 +08:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
|
|
|
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
2023-08-12 00:57:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ProviderOrder(db.Model):
|
2024-09-10 17:08:06 +08:00
|
|
|
__tablename__ = "provider_orders"
|
2023-08-12 00:57:00 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="provider_order_pkey"),
|
|
|
|
db.Index("provider_order_tenant_provider_idx", "tenant_id", "provider_name"),
|
2023-08-12 00:57:00 +08:00
|
|
|
)
|
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-04-29 11:58:17 +08:00
|
|
|
tenant_id = db.Column(StringUUID, nullable=False)
|
2024-06-05 00:13:04 +08:00
|
|
|
provider_name = db.Column(db.String(255), nullable=False)
|
2024-04-29 11:58:17 +08:00
|
|
|
account_id = db.Column(StringUUID, nullable=False)
|
2023-08-12 00:57:00 +08:00
|
|
|
payment_product_id = db.Column(db.String(191), nullable=False)
|
|
|
|
payment_id = db.Column(db.String(191))
|
|
|
|
transaction_id = db.Column(db.String(191))
|
2024-09-10 17:08:06 +08:00
|
|
|
quantity = db.Column(db.Integer, nullable=False, server_default=db.text("1"))
|
2023-08-12 00:57:00 +08:00
|
|
|
currency = db.Column(db.String(40))
|
|
|
|
total_amount = db.Column(db.Integer)
|
|
|
|
payment_status = db.Column(db.String(40), nullable=False, server_default=db.text("'wait_pay'::character varying"))
|
|
|
|
paid_at = db.Column(db.DateTime)
|
|
|
|
pay_failed_at = db.Column(db.DateTime)
|
|
|
|
refunded_at = db.Column(db.DateTime)
|
2024-09-10 17:08:06 +08:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
|
|
|
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
2024-06-05 00:13:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ProviderModelSetting(db.Model):
|
|
|
|
"""
|
|
|
|
Provider model settings for record the model enabled status and load balancing status.
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
|
|
|
|
__tablename__ = "provider_model_settings"
|
2024-06-05 00:13:04 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="provider_model_setting_pkey"),
|
|
|
|
db.Index("provider_model_setting_tenant_provider_model_idx", "tenant_id", "provider_name", "model_type"),
|
2024-06-05 00:13:04 +08:00
|
|
|
)
|
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-06-05 00:13:04 +08:00
|
|
|
tenant_id = db.Column(StringUUID, nullable=False)
|
|
|
|
provider_name = db.Column(db.String(255), nullable=False)
|
|
|
|
model_name = db.Column(db.String(255), nullable=False)
|
|
|
|
model_type = db.Column(db.String(40), nullable=False)
|
2024-09-10 17:08:06 +08:00
|
|
|
enabled = db.Column(db.Boolean, nullable=False, server_default=db.text("true"))
|
|
|
|
load_balancing_enabled = db.Column(db.Boolean, nullable=False, server_default=db.text("false"))
|
|
|
|
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
|
|
|
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
2024-06-05 00:13:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
class LoadBalancingModelConfig(db.Model):
|
|
|
|
"""
|
|
|
|
Configurations for load balancing models.
|
|
|
|
"""
|
2024-09-10 17:08:06 +08:00
|
|
|
|
|
|
|
__tablename__ = "load_balancing_model_configs"
|
2024-06-05 00:13:04 +08:00
|
|
|
__table_args__ = (
|
2024-09-10 17:08:06 +08:00
|
|
|
db.PrimaryKeyConstraint("id", name="load_balancing_model_config_pkey"),
|
|
|
|
db.Index("load_balancing_model_config_tenant_provider_model_idx", "tenant_id", "provider_name", "model_type"),
|
2024-06-05 00:13:04 +08:00
|
|
|
)
|
|
|
|
|
2024-09-10 17:08:06 +08:00
|
|
|
id = db.Column(StringUUID, server_default=db.text("uuid_generate_v4()"))
|
2024-06-05 00:13:04 +08:00
|
|
|
tenant_id = db.Column(StringUUID, nullable=False)
|
|
|
|
provider_name = db.Column(db.String(255), nullable=False)
|
|
|
|
model_name = db.Column(db.String(255), nullable=False)
|
|
|
|
model_type = db.Column(db.String(40), nullable=False)
|
|
|
|
name = db.Column(db.String(255), nullable=False)
|
|
|
|
encrypted_config = db.Column(db.Text, nullable=True)
|
2024-09-10 17:08:06 +08:00
|
|
|
enabled = db.Column(db.Boolean, nullable=False, server_default=db.text("true"))
|
|
|
|
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|
|
|
|
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text("CURRENT_TIMESTAMP(0)"))
|