Merge pull request #147 from xpack-ai/open-base
The concept of a tenant is added as the foundation of the SaaS code.
This commit is contained in:
28 files changed
+295
-229
No files matched your search
@@ -1,7 +1,7 @@
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy.orm import Session
|
||||
from services.common.models.mcp_service import DropMCPService
|
||||
from typing import Dict, List
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
|
||||
"""Repository for MCP services: CRUD, pagination, and public listing."""
|
||||
@@ -22,12 +22,18 @@ class DropMcpServiceRepository:
|
||||
self.db.refresh(mcp_service)
|
||||
return mcp_service
|
||||
|
||||
def all(self) -> Dict[str, DropMCPService]:
|
||||
def all(self, tenant_id: Optional[str] = None) -> Dict[str, DropMCPService]:
|
||||
"""Retrieve all services."""
|
||||
services = self.db.query(DropMCPService).all()
|
||||
query = self.db.query(DropMCPService)
|
||||
if tenant_id:
|
||||
query = query.filter(DropMCPService.tenant_id == tenant_id)
|
||||
services = query.all()
|
||||
return {service.id: service for service in services}
|
||||
|
||||
def all_by_service_ids(self, service_ids: List[str]) -> Dict[str, DropMCPService]:
|
||||
def all_by_service_ids(self, service_ids: List[str], tenant_id: Optional[str] = None) -> Dict[str, DropMCPService]:
|
||||
"""Retrieve all services by service IDs."""
|
||||
services = self.db.query(DropMCPService).filter(DropMCPService.id.in_(service_ids)).all()
|
||||
query = self.db.query(DropMCPService).filter(DropMCPService.id.in_(service_ids))
|
||||
if tenant_id:
|
||||
query = query.filter(DropMCPService.tenant_id == tenant_id)
|
||||
services = query.all()
|
||||
return {service.id: service for service in services}
|
||||
@@ -11,9 +11,9 @@ class McpServiceRepository:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def update_enabled(self, id: str, enabled: int) -> McpService:
|
||||
def update_enabled(self, id: str, enabled: int, tenant_id: str) -> McpService:
|
||||
"""Enable or disable a service by ID; raises if not found."""
|
||||
service = self.db.query(McpService).filter(McpService.id == id).first()
|
||||
service = self.db.query(McpService).filter(McpService.id == id, McpService.tenant_id == tenant_id).first()
|
||||
if not service:
|
||||
raise ValueError("Service not found")
|
||||
service.enabled = enabled
|
||||
@@ -21,18 +21,18 @@ class McpServiceRepository:
|
||||
self.db.refresh(service)
|
||||
return service
|
||||
|
||||
def delete(self, id: str) -> Optional[McpService]:
|
||||
def delete(self, id: str, tenant_id: str) -> Optional[McpService]:
|
||||
"""Delete a service by ID; returns deleted entity or None."""
|
||||
service = self.db.query(McpService).filter(McpService.id == id).first()
|
||||
service = self.db.query(McpService).filter(McpService.id == id, McpService.tenant_id == tenant_id).first()
|
||||
if not service:
|
||||
return None
|
||||
self.db.delete(service)
|
||||
self.db.commit()
|
||||
return service
|
||||
|
||||
def update(self, mcp_service: McpService) -> McpService:
|
||||
def update(self, mcp_service: McpService, tenant_id: str) -> McpService:
|
||||
"""Update mutable fields for an existing service; returns refreshed entity."""
|
||||
existing_service = self.db.query(McpService).filter(McpService.id == mcp_service.id).first()
|
||||
existing_service = self.db.query(McpService).filter(McpService.id == mcp_service.id, McpService.tenant_id == tenant_id).first()
|
||||
if not existing_service:
|
||||
raise ValueError("Service not found")
|
||||
|
||||
@@ -40,10 +40,7 @@ class McpServiceRepository:
|
||||
existing_service.slug_name = mcp_service.slug_name
|
||||
existing_service.short_description = mcp_service.short_description
|
||||
existing_service.long_description = mcp_service.long_description
|
||||
# existing_service.auth_method = mcp_service.auth_method
|
||||
existing_service.base_url = mcp_service.base_url
|
||||
# existing_service.auth_header = mcp_service.auth_header
|
||||
# existing_service.auth_token = mcp_service.auth_token
|
||||
existing_service.headers = mcp_service.headers
|
||||
existing_service.charge_type = mcp_service.charge_type
|
||||
existing_service.price = mcp_service.price
|
||||
@@ -52,30 +49,37 @@ class McpServiceRepository:
|
||||
self.db.refresh(existing_service)
|
||||
return existing_service
|
||||
|
||||
def get_by_id(self, id: str) -> Optional[McpService]:
|
||||
def get_by_id(self, id: str, tenant_id: Optional[str] = None) -> Optional[McpService]:
|
||||
"""Get a service by primary ID."""
|
||||
return self.db.query(McpService).filter(McpService.id == id).first()
|
||||
def get_by_ids(self, ids: List[str]) -> List[McpService]:
|
||||
query = self.db.query(McpService).filter(McpService.id == id)
|
||||
if tenant_id:
|
||||
query = query.filter(McpService.tenant_id == tenant_id)
|
||||
return query.first()
|
||||
|
||||
def get_by_ids(self, ids: List[str], tenant_id: Optional[str] = None) -> List[McpService]:
|
||||
"""Get services by primary IDs."""
|
||||
return self.db.query(McpService).filter(McpService.id.in_(ids)).all()
|
||||
query = self.db.query(McpService).filter(McpService.id.in_(ids))
|
||||
if tenant_id:
|
||||
query = query.filter(McpService.tenant_id == tenant_id)
|
||||
return query.all()
|
||||
|
||||
def get_by_slug_name(self, slug_name: str) -> Optional[McpService]:
|
||||
def get_by_slug_name(self, slug_name: str, tenant_id: str) -> Optional[McpService]:
|
||||
"""Get a service by unique slug name."""
|
||||
return self.db.query(McpService).filter(McpService.slug_name == slug_name).first()
|
||||
return self.db.query(McpService).filter(McpService.slug_name == slug_name, McpService.tenant_id == tenant_id).first()
|
||||
|
||||
def get_all(self, keyword: Optional[str] = None) -> List[McpService]:
|
||||
def get_all(self, tenant_id: str, keyword: Optional[str] = None) -> List[McpService]:
|
||||
"""List all services ordered by creation time descending."""
|
||||
query = self.db.query(McpService).order_by(McpService.created_at.desc())
|
||||
if keyword:
|
||||
keyword = f"%{keyword}%"
|
||||
query = query.filter((McpService.name.like(keyword)) | (McpService.short_description.like(keyword)))
|
||||
return query.all()
|
||||
return query.filter(McpService.tenant_id == tenant_id).all()
|
||||
|
||||
def get_all_paginated(self, page: int = 1, page_size: int = 10, keyword: Optional[str] = None,filter_status: Optional[list] = None) -> Tuple[List[McpService], int]:
|
||||
def get_all_paginated(self, tenant_id: str, page: int = 1, page_size: int = 10, keyword: Optional[str] = None,filter_status: Optional[list] = None) -> Tuple[List[McpService], int]:
|
||||
"""Get service list with pagination"""
|
||||
offset = (page - 1) * page_size
|
||||
|
||||
query = self.db.query(McpService)
|
||||
query = self.db.query(McpService).filter(McpService.tenant_id == tenant_id)
|
||||
if keyword:
|
||||
keyword = f"%{keyword}%"
|
||||
query = query.filter((McpService.name.like(keyword)) | (McpService.short_description.like(keyword)))
|
||||
@@ -88,9 +92,9 @@ class McpServiceRepository:
|
||||
|
||||
return services, total
|
||||
|
||||
def get_all_not_include(self, ids: List[str]) -> List[McpService]:
|
||||
def get_all_not_include(self, ids: List[str], tenant_id: str) -> List[McpService]:
|
||||
"""Get all services not include in ids"""
|
||||
return self.db.query(McpService).order_by(McpService.created_at.desc()).filter(McpService.id.not_in(ids)).all()
|
||||
return self.db.query(McpService).order_by(McpService.created_at.desc()).filter(McpService.id.not_in(ids), McpService.tenant_id == tenant_id).all()
|
||||
|
||||
def create(self, mcp_service: McpService) -> McpService:
|
||||
"""Create a service; set timestamps if missing and persist."""
|
||||
@@ -106,11 +110,13 @@ class McpServiceRepository:
|
||||
self.db.refresh(mcp_service)
|
||||
return mcp_service
|
||||
|
||||
def get_public_services_paginated(self, keyword: str, page: int = 1, page_size: int = 10, tag: Optional[str] = None) -> Tuple[List[McpService], int]:
|
||||
def get_public_services_paginated(self, keyword: str, tenant_id: Optional[str] = None, page: int = 1, page_size: int = 10, tag: Optional[str] = None) -> Tuple[List[McpService], int]:
|
||||
"""Get public service list with pagination, supports keyword search"""
|
||||
offset = (page - 1) * page_size
|
||||
|
||||
query = self.db.query(McpService).filter(McpService.enabled == 1)
|
||||
if tenant_id:
|
||||
query = query.filter(McpService.tenant_id == tenant_id)
|
||||
|
||||
if keyword:
|
||||
keyword = f"%{keyword}%"
|
||||
|
||||
@@ -9,21 +9,24 @@ class PaymentChannelRepository:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def payment_channel_list(self) -> Tuple[int, List[PaymentChannel]]:
|
||||
def payment_channel_list(self,tenant_id: str = "default") -> Tuple[int, List[PaymentChannel]]:
|
||||
"""Return total count and all payment channels."""
|
||||
return self.db.query(PaymentChannel).count(),self.db.query(PaymentChannel).all()
|
||||
query = self.db.query(PaymentChannel).filter(PaymentChannel.tenant_id == tenant_id)
|
||||
return query.count(), query.all()
|
||||
|
||||
def payment_channel_get(self, id: str) -> Optional[PaymentChannel]:
|
||||
def payment_channel_get(self, id: str, tenant_id: str = "default") -> Optional[PaymentChannel]:
|
||||
"""Fetch a payment channel by primary ID."""
|
||||
return self.db.query(PaymentChannel).filter(PaymentChannel.id == id).first()
|
||||
query = self.db.query(PaymentChannel).filter(PaymentChannel.id == id).filter(PaymentChannel.tenant_id == tenant_id)
|
||||
return query.first()
|
||||
|
||||
def payment_channel_available_list(self) -> List[PaymentChannel]:
|
||||
def payment_channel_available_list(self, tenant_id: str = "default") -> List[PaymentChannel]:
|
||||
"""List enabled payment channels."""
|
||||
return self.db.query(PaymentChannel).filter(PaymentChannel.status == 1).all()
|
||||
query = self.db.query(PaymentChannel).filter(PaymentChannel.status == 1).filter(PaymentChannel.tenant_id == tenant_id)
|
||||
return query.all()
|
||||
|
||||
def update_status(self, id: str, status: int) -> Optional[PaymentChannel]:
|
||||
def update_status(self, id: str, status: int, tenant_id: str = "default") -> Optional[PaymentChannel]:
|
||||
"""Update channel status and refresh updated_at; returns updated entity or None."""
|
||||
payment_channel = self.payment_channel_get(id)
|
||||
payment_channel = self.payment_channel_get(id, tenant_id)
|
||||
if payment_channel:
|
||||
payment_channel.status = status
|
||||
payment_channel.updated_at = datetime.now(timezone.utc)
|
||||
@@ -31,9 +34,9 @@ class PaymentChannelRepository:
|
||||
return payment_channel
|
||||
return None
|
||||
|
||||
def update_config(self, id: str, config: str) -> Optional[PaymentChannel]:
|
||||
def update_config(self, id: str, config: str, tenant_id: str = "default") -> Optional[PaymentChannel]:
|
||||
"""Update channel config and refresh updated_at; returns updated entity or None."""
|
||||
payment_channel = self.payment_channel_get(id)
|
||||
payment_channel = self.payment_channel_get(id, tenant_id)
|
||||
if payment_channel:
|
||||
payment_channel.config = config
|
||||
payment_channel.updated_at = datetime.now(timezone.utc)
|
||||
|
||||
@@ -14,13 +14,14 @@ class StatsMcpServiceDateRepository:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def insert(self, service_id: str, stats_date: datetime, call_count: int = 1) -> StatsMcpServiceDate:
|
||||
def insert(self, service_id: str, tenant_id: str, stats_date: datetime, call_count: int = 1, ) -> StatsMcpServiceDate:
|
||||
"""Insert a new hourly stats record.
|
||||
|
||||
Raises on duplicate primary key; use update or upsert if unsure.
|
||||
"""
|
||||
record = StatsMcpServiceDate()
|
||||
record.service_id = service_id
|
||||
record.tenant_id = tenant_id
|
||||
record.stats_date = stats_date
|
||||
record.call_count = call_count
|
||||
self.db.add(record)
|
||||
@@ -28,7 +29,7 @@ class StatsMcpServiceDateRepository:
|
||||
self.db.refresh(record)
|
||||
return record
|
||||
|
||||
def update(self, service_id: str, stats_date: datetime, call_count: int) -> Optional[StatsMcpServiceDate]:
|
||||
def update(self, service_id: str, tenant_id: str, stats_date: datetime, call_count: int, ) -> Optional[StatsMcpServiceDate]:
|
||||
"""Update existing hourly stats record's call_count.
|
||||
|
||||
Returns the updated record, or None if not found.
|
||||
@@ -38,6 +39,7 @@ class StatsMcpServiceDateRepository:
|
||||
.filter(
|
||||
StatsMcpServiceDate.service_id == service_id,
|
||||
StatsMcpServiceDate.stats_date == stats_date,
|
||||
StatsMcpServiceDate.tenant_id == tenant_id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
@@ -82,7 +84,7 @@ class StatsMcpServiceDateRepository:
|
||||
"""Get total call count for a service within optional date range."""
|
||||
day_col = func.date(StatsMcpServiceDate.stats_date)
|
||||
query = self.db.query(func.coalesce(func.sum(StatsMcpServiceDate.call_count), 0)).filter(
|
||||
StatsMcpServiceDate.service_id == service_id
|
||||
StatsMcpServiceDate.service_id == service_id,
|
||||
)
|
||||
if start_date is not None:
|
||||
query = query.filter(day_col >= start_date)
|
||||
@@ -97,26 +99,26 @@ class StatsMcpServiceDateRepository:
|
||||
Implementation uses MySQL's ON DUPLICATE KEY UPDATE to avoid race conditions.
|
||||
If the row does not exist, it will be inserted with call_count=inc; otherwise, call_count is increased.
|
||||
"""
|
||||
# Logger for debug purposes
|
||||
print(f"Incrementing call count for service {service_id} on {stats_date} by {inc}")
|
||||
|
||||
# Cast mapped table for typing compatibility with mysql_insert
|
||||
table: Table = cast(Table, StatsMcpServiceDate.__table__)
|
||||
stmt = mysql_insert(table).values(
|
||||
service_id=service_id,
|
||||
stats_date=stats_date,
|
||||
call_count=inc,
|
||||
created_at=func.current_timestamp(),
|
||||
updated_at=func.current_timestamp(),
|
||||
)
|
||||
ondup = stmt.on_duplicate_key_update(
|
||||
call_count=table.c.call_count + inc,
|
||||
updated_at=func.current_timestamp(),
|
||||
)
|
||||
self.db.execute(ondup)
|
||||
self.db.commit()
|
||||
try:
|
||||
stmt = mysql_insert(table).values(
|
||||
service_id=service_id,
|
||||
stats_date=stats_date,
|
||||
call_count=inc,
|
||||
created_at=func.current_timestamp(),
|
||||
updated_at=func.current_timestamp(),
|
||||
)
|
||||
ondup = stmt.on_duplicate_key_update(
|
||||
call_count=table.c.call_count + inc,
|
||||
updated_at=func.current_timestamp(),
|
||||
)
|
||||
self.db.execute(ondup)
|
||||
self.db.commit()
|
||||
except Exception:
|
||||
self.db.rollback()
|
||||
raise
|
||||
|
||||
def increment_fallback(self, service_id: str, stats_date: datetime, inc: int = 1) -> None:
|
||||
def increment_fallback(self, service_id: str,tenant_id: str, stats_date: datetime, inc: int = 1) -> None:
|
||||
"""Fallback increment strategy without dialect-specific upsert.
|
||||
|
||||
Tries to insert; on duplicate key, rolls back and performs an in-place atomic update: call_count = call_count + inc.
|
||||
@@ -124,11 +126,12 @@ class StatsMcpServiceDateRepository:
|
||||
"""
|
||||
try:
|
||||
# Try insert first
|
||||
self.insert(service_id, stats_date, inc)
|
||||
self.insert(service_id, tenant_id, stats_date, inc)
|
||||
except IntegrityError:
|
||||
# Record exists, perform atomic UPDATE
|
||||
self.db.rollback()
|
||||
self.db.query(StatsMcpServiceDate).filter(
|
||||
StatsMcpServiceDate.tenant_id == tenant_id,
|
||||
StatsMcpServiceDate.service_id == service_id,
|
||||
StatsMcpServiceDate.stats_date == stats_date,
|
||||
).update(
|
||||
@@ -142,6 +145,7 @@ class StatsMcpServiceDateRepository:
|
||||
|
||||
def stats_call_count(
|
||||
self,
|
||||
tenant_id: str,
|
||||
start_at: Optional[datetime] = None,
|
||||
end_at: Optional[datetime] = None,
|
||||
) -> int:
|
||||
@@ -166,12 +170,17 @@ class StatsMcpServiceDateRepository:
|
||||
if end_at is not None:
|
||||
end_local = end_at.astimezone().replace(tzinfo=None) if end_at.tzinfo is not None else end_at
|
||||
query = query.filter(local_dt <= end_local)
|
||||
|
||||
|
||||
query = query.filter(
|
||||
StatsMcpServiceDate.tenant_id == tenant_id,
|
||||
)
|
||||
|
||||
total = query.scalar()
|
||||
return int(total or 0)
|
||||
|
||||
def stats_call_count_trend(
|
||||
self,
|
||||
tenant_id: str,
|
||||
start_at: Optional[datetime] = None,
|
||||
end_at: Optional[datetime] = None,
|
||||
) -> list:
|
||||
@@ -191,7 +200,7 @@ class StatsMcpServiceDateRepository:
|
||||
self.db.query(
|
||||
day_col.label("stats_day"),
|
||||
func.coalesce(func.sum(StatsMcpServiceDate.call_count), 0).label("total_count"),
|
||||
)
|
||||
).filter(StatsMcpServiceDate.tenant_id == tenant_id)
|
||||
)
|
||||
|
||||
# Apply local-time range filters to match grouping
|
||||
|
||||
@@ -11,24 +11,24 @@ class SysConfigLargeRepository:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def get_all(self, keys: List[str] = []) -> Dict[str, str]:
|
||||
def get_all(self, keys: List[str] = [], tenant_id: str = "default") -> Dict[str, str]:
|
||||
"""Return mapping of key->value, optionally filtered by provided keys."""
|
||||
if keys:
|
||||
return {item.key: item.value for item in self.db.query(SysConfigLarge).filter(SysConfigLarge.key.in_(keys)).all()}
|
||||
return {item.key: item.value for item in self.db.query(SysConfigLarge).all()}
|
||||
return {item.key: item.value for item in self.db.query(SysConfigLarge).filter(SysConfigLarge.tenant_id == tenant_id, SysConfigLarge.key.in_(keys)).all()}
|
||||
return {item.key: item.value for item in self.db.query(SysConfigLarge).filter(SysConfigLarge.tenant_id == tenant_id).all()}
|
||||
|
||||
def get_by_key(self, key: str) -> Optional[SysConfigLarge]:
|
||||
def get_by_key(self, key: str, tenant_id: str = "default") -> Optional[SysConfigLarge]:
|
||||
"""Get SysConfigLarge entity by unique key."""
|
||||
return self.db.query(SysConfigLarge).filter(SysConfigLarge.key == key).first()
|
||||
def get_value_by_key(self, key: str) -> Optional[str]:
|
||||
return self.db.query(SysConfigLarge).filter(SysConfigLarge.tenant_id == tenant_id, SysConfigLarge.key == key).first()
|
||||
def get_value_by_key(self, key: str, tenant_id: str = "default") -> Optional[str]:
|
||||
"""Get value string for given key, or None if missing."""
|
||||
sys_config = self.get_by_key(key)
|
||||
sys_config = self.get_by_key(key, tenant_id)
|
||||
if sys_config:
|
||||
return sys_config.value
|
||||
return None
|
||||
def set_value_by_key(self, key: str, value: str,description:str) -> Optional[SysConfigLarge]:
|
||||
def set_value_by_key(self, key: str, value: str,description:str, tenant_id: str = "default") -> Optional[SysConfigLarge]:
|
||||
"""Set or create value and description for a key; returns entity."""
|
||||
sys_config = self.get_by_key(key)
|
||||
sys_config = self.get_by_key(key, tenant_id)
|
||||
if sys_config:
|
||||
sys_config.value = value
|
||||
sys_config.description = description
|
||||
@@ -36,6 +36,7 @@ class SysConfigLargeRepository:
|
||||
self.db.refresh(sys_config)
|
||||
return sys_config
|
||||
sys_config = SysConfigLarge(
|
||||
tenant_id=tenant_id,
|
||||
id=str(uuid.uuid4()),
|
||||
key=key,
|
||||
value=value,
|
||||
@@ -46,9 +47,9 @@ class SysConfigLargeRepository:
|
||||
self.db.add(sys_config)
|
||||
self.db.commit()
|
||||
return sys_config
|
||||
def delete_by_key(self, key: str) -> bool:
|
||||
def delete_by_key(self, key: str, tenant_id: str = "default") -> bool:
|
||||
"""Delete config by key; returns True if deleted, else False."""
|
||||
sys_config = self.get_by_key(key)
|
||||
sys_config = self.get_by_key(key, tenant_id)
|
||||
if sys_config:
|
||||
self.db.delete(sys_config)
|
||||
self.db.commit()
|
||||
|
||||
@@ -10,23 +10,23 @@ class SysConfigRepository:
|
||||
"""Data access layer for SysConfig model."""
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
def get_all(self, keys: List[str] = []) -> Dict[str, str]:
|
||||
def get_all(self, keys: List[str] = [], tenant_id: str = "default") -> Dict[str, str]:
|
||||
"""Return mapping of key->value, optionally filtered by provided keys."""
|
||||
if keys:
|
||||
return {item.key: item.value for item in self.db.query(SysConfig).filter(SysConfig.key.in_(keys)).all()}
|
||||
return {item.key: item.value for item in self.db.query(SysConfig).all()}
|
||||
def get_by_key(self, key: str) -> Optional[SysConfig]:
|
||||
return {item.key: item.value for item in self.db.query(SysConfig).filter(SysConfig.tenant_id == tenant_id, SysConfig.key.in_(keys)).all()}
|
||||
return {item.key: item.value for item in self.db.query(SysConfig).filter(SysConfig.tenant_id == tenant_id).all()}
|
||||
def get_by_key(self, key: str, tenant_id: str = "default") -> Optional[SysConfig]:
|
||||
"""Get SysConfig entity by unique key."""
|
||||
return self.db.query(SysConfig).filter(SysConfig.key == key).first()
|
||||
def get_value_by_key(self, key: str) -> Optional[str]:
|
||||
return self.db.query(SysConfig).filter(SysConfig.tenant_id == tenant_id, SysConfig.key == key).first()
|
||||
def get_value_by_key(self, key: str, tenant_id: str = "default") -> Optional[str]:
|
||||
"""Get value string for given key, or None if missing."""
|
||||
sys_config = self.get_by_key(key)
|
||||
sys_config = self.get_by_key(key, tenant_id)
|
||||
if sys_config:
|
||||
return sys_config.value
|
||||
return None
|
||||
def set_value_by_key(self, key: str, value: str, description: str, commit: bool = True) -> Optional[SysConfig]:
|
||||
def set_value_by_key(self, key: str, value: str, description: str, tenant_id: str = "default", commit: bool = True) -> Optional[SysConfig]:
|
||||
"""Set or create value and description for a key; returns entity."""
|
||||
sys_config = self.get_by_key(key)
|
||||
sys_config = self.get_by_key(key, tenant_id)
|
||||
if sys_config:
|
||||
sys_config.value = value
|
||||
sys_config.description = description
|
||||
@@ -46,9 +46,9 @@ class SysConfigRepository:
|
||||
if commit:
|
||||
self.db.commit()
|
||||
return sys_config
|
||||
def delete_by_key(self, key: str, commit: bool = True) -> bool:
|
||||
def delete_by_key(self, key: str, tenant_id: str = "default", commit: bool = True) -> bool:
|
||||
"""Delete config by key; returns True if deleted, else False."""
|
||||
sys_config = self.get_by_key(key)
|
||||
sys_config = self.get_by_key(key, tenant_id)
|
||||
if sys_config:
|
||||
self.db.delete(sys_config)
|
||||
if commit:
|
||||
|
||||
@@ -15,15 +15,15 @@ class TempMcpServiceRepository:
|
||||
self.db.refresh(temp_mcp_service)
|
||||
return temp_mcp_service
|
||||
|
||||
def delete_by_service_id(self, service_id: str) -> None:
|
||||
def delete_by_service_id(self, service_id: str, tenant_id: str) -> None:
|
||||
"""Delete all temporary records for specified service ID"""
|
||||
self.db.query(TempMcpService).filter(TempMcpService.id == service_id).delete()
|
||||
self.db.query(TempMcpService).filter(TempMcpService.id == service_id, TempMcpService.tenant_id == tenant_id).delete()
|
||||
self.db.commit()
|
||||
|
||||
def get_by_id(self, service_id: str) -> Optional[TempMcpService]:
|
||||
def get_by_id(self, service_id: str, tenant_id: str) -> Optional[TempMcpService]:
|
||||
"""Get temporary service record by service ID"""
|
||||
return self.db.query(TempMcpService).filter(TempMcpService.id == service_id).first()
|
||||
return self.db.query(TempMcpService).filter(TempMcpService.id == service_id, TempMcpService.tenant_id == tenant_id).first()
|
||||
|
||||
def get_all_by_service_id(self, service_id: str) -> List[TempMcpService]:
|
||||
def get_all_by_service_id(self, service_id: str, tenant_id: str) -> List[TempMcpService]:
|
||||
"""Get all temporary records for specified service ID"""
|
||||
return self.db.query(TempMcpService).filter(TempMcpService.id == service_id).all()
|
||||
return self.db.query(TempMcpService).filter(TempMcpService.id == service_id, TempMcpService.tenant_id == tenant_id).all()
|
||||
@@ -323,6 +323,7 @@ class UserWalletHistoryRepository:
|
||||
Get list of orders based on filters.
|
||||
|
||||
Args:
|
||||
tenant_id: Tenant ID filter
|
||||
payment_method: Payment method filter
|
||||
status: Order status filter (0=new, 1=completed, 2=refunded)
|
||||
start: Start date for filtering
|
||||
|
||||
@@ -44,6 +44,7 @@ class BillingMessageHandler:
|
||||
Returns:
|
||||
bool: Whether processing was successful
|
||||
"""
|
||||
call_log_id: Optional[str] = None
|
||||
try:
|
||||
# Parse message
|
||||
billing_message = self._parse_message(message_data)
|
||||
@@ -61,12 +62,17 @@ class BillingMessageHandler:
|
||||
start_utc = start_dt.replace(tzinfo=timezone.utc) if start_dt.tzinfo is None else start_dt.astimezone(timezone.utc)
|
||||
# Store as naive UTC datetime to match MySQL DATETIME behavior
|
||||
stats_date = start_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None)
|
||||
self.stats_repo.increment(billing_message.service_id, stats_date, 1)
|
||||
self.stats_repo.increment_fallback(billing_message.service_id,billing_message.tenant_id, stats_date, 1)
|
||||
|
||||
# Process billing logic
|
||||
if billing_message.call_success and billing_message.unit_price > 0:
|
||||
success = self._process_billing(billing_message, call_log_id)
|
||||
if not success:
|
||||
# Ensure previous failed operations don't leave session in error state
|
||||
try:
|
||||
self.db.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
# Update record status to failed
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing failed")
|
||||
return False
|
||||
@@ -78,6 +84,16 @@ class BillingMessageHandler:
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to process billing message: {str(e)}", exc_info=True)
|
||||
try:
|
||||
self.db.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
# Best-effort mark the call log as failed when possible
|
||||
if call_log_id:
|
||||
try:
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing exception")
|
||||
except Exception:
|
||||
logger.exception("Failed to update call log status after exception")
|
||||
return False
|
||||
|
||||
def _parse_message(self, message_data: dict) -> Optional[BillingMessage]:
|
||||
@@ -99,6 +115,7 @@ class BillingMessageHandler:
|
||||
return BillingMessage(
|
||||
user_id=message_data["user_id"],
|
||||
service_id=message_data["service_id"],
|
||||
tenant_id=message_data["tenant_id"],
|
||||
api_id=message_data["api_id"],
|
||||
tool_name=message_data["tool_name"],
|
||||
input_params=message_data["input_params"],
|
||||
@@ -132,6 +149,7 @@ class BillingMessageHandler:
|
||||
id=log_id,
|
||||
user_id=billing_message.user_id,
|
||||
service_id=billing_message.service_id,
|
||||
tenant_id=billing_message.tenant_id,
|
||||
api_id=billing_message.api_id,
|
||||
tool_name=billing_message.tool_name,
|
||||
input_params=billing_message.input_params,
|
||||
@@ -230,6 +248,7 @@ class BillingMessageHandler:
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Billing processing failed - User ID: {billing_message.user_id}: {str(e)}", exc_info=True)
|
||||
self.db.rollback()
|
||||
return False
|
||||
|
||||
def _update_wallet_cache(self, user_id: str, new_balance: Decimal) -> None:
|
||||
|
||||
@@ -94,16 +94,17 @@ class McpManagerService:
|
||||
logger.warning(f"Failed to delete service price cache - Service ID: {service_id}: {str(e)}")
|
||||
|
||||
|
||||
def update_enabled(self, id: str, enabled: int) -> McpService:
|
||||
return self.mcp_service_repository.update_enabled(id, enabled)
|
||||
def update_enabled(self, id: str, enabled: int, tenant_id: str = "default") -> McpService:
|
||||
return self.mcp_service_repository.update_enabled(id, enabled, tenant_id)
|
||||
|
||||
def delete(self, id: str) -> Optional[McpService]:
|
||||
service = self.mcp_service_repository.get_by_id(id)
|
||||
def delete(self, id: str, tenant_id: str = "default") -> Optional[McpService]:
|
||||
service = self.mcp_service_repository.get_by_id(id, tenant_id)
|
||||
if not service:
|
||||
raise ValueError("Service not found")
|
||||
self._delete_service_price_cache(id)
|
||||
service = self.drop_mcp_service_repository.create(DropMCPService(
|
||||
id=id,
|
||||
tenant_id=tenant_id,
|
||||
name=service.name,
|
||||
slug_name=service.slug_name,
|
||||
short_description=service.short_description,
|
||||
@@ -111,9 +112,9 @@ class McpManagerService:
|
||||
))
|
||||
if not service:
|
||||
raise ValueError("Failed to create drop service")
|
||||
return self.mcp_service_repository.delete(id)
|
||||
return self.mcp_service_repository.delete(id, tenant_id)
|
||||
|
||||
def update(self, body: dict) -> bool:
|
||||
def update(self, body: dict, tenant_id: str = "default") -> bool:
|
||||
# Update mcp_service
|
||||
service_id = body.get("id")
|
||||
if not service_id:
|
||||
@@ -123,14 +124,14 @@ class McpManagerService:
|
||||
update_type = body.get("update_type", "default")
|
||||
|
||||
# Get existing service
|
||||
existing_service = self.mcp_service_repository.get_by_id(service_id)
|
||||
existing_service = self.mcp_service_repository.get_by_id(service_id, tenant_id=tenant_id)
|
||||
if not existing_service:
|
||||
raise ValueError("Service not found")
|
||||
|
||||
# If it's an openapi type update, need to migrate data from temporary table first
|
||||
if update_type == "openapi":
|
||||
# 1. Verify temporary data exists
|
||||
temp_service = self.temp_mcp_service_repository.get_by_id(service_id)
|
||||
temp_service = self.temp_mcp_service_repository.get_by_id(service_id,tenant_id=tenant_id)
|
||||
if not temp_service:
|
||||
raise ValueError("No temporary data found for OpenAPI update")
|
||||
|
||||
@@ -175,7 +176,7 @@ class McpManagerService:
|
||||
self.mcp_tool_api_repository.create(new_api)
|
||||
|
||||
# 5. Clean up temporary table data
|
||||
self.temp_mcp_service_repository.delete_by_service_id(service_id)
|
||||
self.temp_mcp_service_repository.delete_by_service_id(service_id,tenant_id=tenant_id)
|
||||
self.temp_mcp_tool_api_repository.delete_by_service_id(service_id)
|
||||
|
||||
# Execute regular update logic (applies to both types)
|
||||
@@ -291,12 +292,12 @@ class McpManagerService:
|
||||
|
||||
return True
|
||||
|
||||
def get_by_id(self, id: str) -> Optional[McpService]:
|
||||
return self.mcp_service_repository.get_by_id(id)
|
||||
def get_by_id(self, id: str, tenant_id: str = "default") -> Optional[McpService]:
|
||||
return self.mcp_service_repository.get_by_id(id, tenant_id=tenant_id)
|
||||
|
||||
def get_service_info(self, id: str) -> Optional[dict]:
|
||||
def get_service_info(self, id: str, tenant_id: str = "default") -> Optional[dict]:
|
||||
"""Get service details including API list"""
|
||||
service = self.mcp_service_repository.get_by_id(id)
|
||||
service = self.mcp_service_repository.get_by_id(id, tenant_id=tenant_id)
|
||||
if not service:
|
||||
return None
|
||||
|
||||
@@ -322,15 +323,15 @@ class McpManagerService:
|
||||
|
||||
return service_info
|
||||
|
||||
def get_all(self) -> List[McpService]:
|
||||
return self.mcp_service_repository.get_all()
|
||||
def get_all(self, tenant_id: str = "default") -> List[McpService]:
|
||||
return self.mcp_service_repository.get_all(tenant_id)
|
||||
|
||||
def get_all_paginated(self, page: int = 1, page_size: int = 10,keyword: Optional[str] = None,filter_status: Optional[list] = None) -> Tuple[List[McpService], int]:
|
||||
def get_all_paginated(self, tenant_id: str = "default", page: int = 1, page_size: int = 10,keyword: Optional[str] = None,filter_status: Optional[list] = None) -> Tuple[List[McpService], int]:
|
||||
"""Get service list with pagination"""
|
||||
return self.mcp_service_repository.get_all_paginated(page=page, page_size=page_size,keyword=keyword,filter_status=filter_status)
|
||||
return self.mcp_service_repository.get_all_paginated(tenant_id=tenant_id, page=page, page_size=page_size,keyword=keyword,filter_status=filter_status)
|
||||
|
||||
def get_tags_list(self, enabled_only: bool = False) -> List[str]:
|
||||
tags_strings = self.mcp_service_repository.get_tags_strings(enabled_only=enabled_only)
|
||||
def get_tags_list(self, enabled_only: bool = False) -> List[str]:
|
||||
tags_strings = self.mcp_service_repository.get_tags_strings( enabled_only=enabled_only)
|
||||
tag_counts: dict[str, int] = {}
|
||||
canonical: dict[str, str] = {}
|
||||
|
||||
@@ -354,7 +355,7 @@ class McpManagerService:
|
||||
)
|
||||
return [canonical[normalized] for normalized in sorted_normalized]
|
||||
|
||||
def create_service_from_openapi(self, openapi_data: OpenApiForAI) -> str:
|
||||
def create_service_from_openapi(self, openapi_data: OpenApiForAI, tenant_id: str = "default") -> str:
|
||||
# Convert OpenApiForAI info to McpService object and McpToolApi object list, return service ID or exception.
|
||||
try:
|
||||
# Generate service ID
|
||||
@@ -373,7 +374,7 @@ class McpManagerService:
|
||||
while True:
|
||||
# Check if slug_name already exists
|
||||
try:
|
||||
existing_service = self.mcp_service_repository.get_by_slug_name(slug_name)
|
||||
existing_service = self.mcp_service_repository.get_by_slug_name(slug_name,tenant_id=tenant_id)
|
||||
if not existing_service:
|
||||
break
|
||||
except AttributeError:
|
||||
@@ -425,7 +426,7 @@ class McpManagerService:
|
||||
logger.error(f"Failed to create service from OpenAPI: {str(e)}")
|
||||
raise ValueError(f"Failed to create service from OpenAPI: {str(e)}")
|
||||
|
||||
def update_service_from_openapi(self, service_id: str, openapi_data: OpenApiForAI) -> dict:
|
||||
def update_service_from_openapi(self, service_id: str, openapi_data: OpenApiForAI, tenant_id: str = "default") -> dict:
|
||||
"""
|
||||
Update existing service based on OpenAPI data, save updated data to temporary table
|
||||
|
||||
@@ -441,12 +442,12 @@ class McpManagerService:
|
||||
"""
|
||||
try:
|
||||
# 1. Check if original service exists
|
||||
existing_service = self.mcp_service_repository.get_by_id(service_id)
|
||||
existing_service = self.mcp_service_repository.get_by_id(service_id, tenant_id=tenant_id)
|
||||
if not existing_service:
|
||||
raise ValueError(f"Service with ID {service_id} not found")
|
||||
raise ValueError(f"Service with ID {service_id} not found, tenant_id: {tenant_id}")
|
||||
|
||||
# 2. Clean up old temporary data for this service
|
||||
self.temp_mcp_service_repository.delete_by_service_id(service_id)
|
||||
self.temp_mcp_service_repository.delete_by_service_id(service_id, tenant_id=existing_service.tenant_id)
|
||||
self.temp_mcp_tool_api_repository.delete_by_service_id(service_id)
|
||||
|
||||
# 3. Create updated temporary service record
|
||||
@@ -513,9 +514,9 @@ class McpManagerService:
|
||||
logger.error(f"Failed to update service from OpenAPI: {str(e)}")
|
||||
raise ValueError(f"Failed to update service from OpenAPI.")
|
||||
|
||||
def get_public_services_paginated(self, keyword: str, page: int = 1, page_size: int = 10, tag: Optional[str] = None) -> Tuple[List[dict], int]:
|
||||
def get_public_services_paginated(self, keyword: str, tenant_id: Optional[str] = None, page: int = 1, page_size: int = 10, tag: Optional[str] = None) -> Tuple[List[dict], int]:
|
||||
"""Get public services list with pagination, returns formatted data with API info"""
|
||||
services, total = self.mcp_service_repository.get_public_services_paginated(keyword, page, page_size, tag)
|
||||
services, total = self.mcp_service_repository.get_public_services_paginated(keyword, tenant_id, page, page_size, tag)
|
||||
|
||||
service_list = []
|
||||
for service in services:
|
||||
@@ -548,13 +549,14 @@ class McpManagerService:
|
||||
|
||||
return service_list, total
|
||||
|
||||
def get_public_service_info(self, id: str) -> Optional[dict]:
|
||||
def get_public_service_info(self, id: str, tenant_id: str = "default") -> Optional[dict]:
|
||||
"""Get public service details (only returns enabled services and APIs)"""
|
||||
service = self.mcp_service_repository.get_by_id(id)
|
||||
service = self.mcp_service_repository.get_by_id(id, tenant_id=tenant_id)
|
||||
if not service:
|
||||
service = self.mcp_service_repository.get_by_slug_name(id)
|
||||
if not service:
|
||||
return None
|
||||
return None
|
||||
# service = self.mcp_service_repository.get_by_slug_name(id)
|
||||
# if not service:
|
||||
# return None
|
||||
if service.enabled != 1:
|
||||
return None
|
||||
|
||||
|
||||
@@ -15,25 +15,25 @@ class PaymentChannelService:
|
||||
self.db = db
|
||||
self.payment_channel_repository = PaymentChannelRepository(db)
|
||||
|
||||
def list(self) -> Tuple[int, List[PaymentChannel]]:
|
||||
return self.payment_channel_repository.payment_channel_list()
|
||||
def list(self, tenant_id: str = "default") -> Tuple[int, List[PaymentChannel]]:
|
||||
return self.payment_channel_repository.payment_channel_list(tenant_id)
|
||||
|
||||
def available_list(self) -> List[PaymentChannel]:
|
||||
def available_list(self, tenant_id: str = "default") -> List[PaymentChannel]:
|
||||
"""Get list of available payment channels"""
|
||||
return self.payment_channel_repository.payment_channel_available_list()
|
||||
|
||||
def get(self, id: str) -> Optional[PaymentChannel]:
|
||||
return self.payment_channel_repository.payment_channel_get(id)
|
||||
def get(self, id: str, tenant_id: str = "default") -> Optional[PaymentChannel]:
|
||||
return self.payment_channel_repository.payment_channel_get(id, tenant_id)
|
||||
|
||||
def update_status(self, id: str, status: int) -> Optional[PaymentChannel]:
|
||||
return self.payment_channel_repository.update_status(id, status)
|
||||
def update_status(self, id: str, status: int, tenant_id: str = "default") -> Optional[PaymentChannel]:
|
||||
return self.payment_channel_repository.update_status(id, status, tenant_id)
|
||||
|
||||
def update_config(self, id: str, config: str) -> Optional[PaymentChannel]:
|
||||
return self.payment_channel_repository.update_config(id, config)
|
||||
def update_config(self, id: str, config: str, tenant_id: str = "default") -> Optional[PaymentChannel]:
|
||||
return self.payment_channel_repository.update_config(id, config, tenant_id)
|
||||
|
||||
def get_config(self, id: str) -> Optional[dict]:
|
||||
def get_config(self, id: str, tenant_id: str = "default") -> Optional[dict]:
|
||||
"""Get payment channel config by ID"""
|
||||
channel_config = self.payment_channel_repository.payment_channel_get(id)
|
||||
channel_config = self.payment_channel_repository.payment_channel_get(id, tenant_id)
|
||||
if channel_config is None:
|
||||
return None
|
||||
# Support encrypted-at-rest config
|
||||
@@ -49,10 +49,10 @@ class PaymentChannelService:
|
||||
return config
|
||||
|
||||
|
||||
def get_stripe_config(self) -> Optional[Dict[str, Any]]:
|
||||
def get_stripe_config(self, tenant_id: str = "default") -> Optional[Dict[str, Any]]:
|
||||
"""Get Stripe payment channel config"""
|
||||
try:
|
||||
stripe_channel = self.payment_channel_repository.payment_channel_get("stripe")
|
||||
stripe_channel = self.payment_channel_repository.payment_channel_get("stripe", tenant_id)
|
||||
|
||||
if not stripe_channel:
|
||||
logger.warning("Stripe payment channel config does not exist")
|
||||
|
||||
@@ -218,13 +218,13 @@ class ResourceGroupService:
|
||||
return count
|
||||
|
||||
|
||||
def bind_services(self, gid: str, service_ids: List[str]) -> int:
|
||||
def bind_services(self, gid: str, service_ids: List[str], tenant_id: str = "default") -> int:
|
||||
valid_ids: List[str] = []
|
||||
group = self.group_repo.get_by_id(gid)
|
||||
if not group:
|
||||
raise ValueError("Resource group not found")
|
||||
for sid in service_ids:
|
||||
if self.mcp_repo.get_by_id(sid):
|
||||
if self.mcp_repo.get_by_id(sid, tenant_id=tenant_id):
|
||||
valid_ids.append(sid)
|
||||
try:
|
||||
created = self.map_repo.bind_services(gid, valid_ids, commit=False)
|
||||
@@ -285,18 +285,18 @@ class ResourceGroupService:
|
||||
total += 1
|
||||
|
||||
return result,total
|
||||
def get_bind_service_ids(self, gid: str) -> List[str]:
|
||||
def get_bind_service_ids(self, gid: str, tenant_id: str = "default") -> List[str]:
|
||||
if gid == "deny-all":
|
||||
return []
|
||||
elif gid == "allow-all":
|
||||
return [s.id for s in self.mcp_repo.get_all()]
|
||||
return [s.id for s in self.mcp_repo.get_all(tenant_id=tenant_id)]
|
||||
return self.map_repo.list_service_ids(gid)
|
||||
|
||||
def get_bind_services(self, gid: str, page: int = 1, page_size: int = 10, keyword: Optional[str] = None) -> Tuple[List[dict], int]:
|
||||
def get_bind_services(self, gid: str, tenant_id: str = "default", page: int = 1, page_size: int = 10, keyword: Optional[str] = None) -> Tuple[List[dict], int]:
|
||||
if gid == "deny-all":
|
||||
return [],0
|
||||
elif gid == "allow-all":
|
||||
services,total = self.mcp_repo.get_all_paginated(page=page, page_size=page_size, keyword=keyword)
|
||||
services,total = self.mcp_repo.get_all_paginated(tenant_id=tenant_id, page=page, page_size=page_size, keyword=keyword)
|
||||
return [
|
||||
{
|
||||
"id": s.id,
|
||||
@@ -318,7 +318,7 @@ class ResourceGroupService:
|
||||
],total
|
||||
if keyword:
|
||||
|
||||
services = self.mcp_repo.get_all(keyword=keyword)
|
||||
services = self.mcp_repo.get_all(tenant_id=tenant_id, keyword=keyword)
|
||||
if not services:
|
||||
return [],0
|
||||
service_map = {s.id: s for s in services}
|
||||
@@ -345,7 +345,7 @@ class ResourceGroupService:
|
||||
],total
|
||||
else:
|
||||
sids,total = self.map_repo.list_service_ids_paginated(gid, page=page, page_size=page_size)
|
||||
services = self.mcp_repo.get_by_ids(ids=sids)
|
||||
services = self.mcp_repo.get_by_ids(ids=sids, tenant_id=tenant_id)
|
||||
service_map = {s.id: s for s in services}
|
||||
return [
|
||||
{
|
||||
@@ -366,11 +366,11 @@ class ResourceGroupService:
|
||||
}
|
||||
for s in (service_map[sid] for sid in sids if sid in service_map)
|
||||
],total
|
||||
def get_unbind_services(self, gid: str) -> List[dict]:
|
||||
def get_unbind_services(self, gid: str,tenant_id: str = "default") -> List[dict]:
|
||||
if gid == "deny-all" or gid == "allow-all":
|
||||
return []
|
||||
sids = self.map_repo.list_service_ids(gid)
|
||||
services = self.mcp_repo.get_all_not_include(ids=sids)
|
||||
services = self.mcp_repo.get_all_not_include(ids=sids,tenant_id=tenant_id)
|
||||
return [
|
||||
{
|
||||
"id": s.id,
|
||||
|
||||
@@ -20,7 +20,7 @@ class StatsService:
|
||||
self.mcp_service_repository = McpServiceRepository(db)
|
||||
self.drop_mcp_service_repository = DropMcpServiceRepository(db)
|
||||
|
||||
def get_registered_user_stats(self, start: datetime, end: datetime) -> dict:
|
||||
def get_registered_user_stats(self, start: datetime, end: datetime, tenant_id: str = "default") -> dict:
|
||||
"""
|
||||
Get registered stats
|
||||
|
||||
@@ -35,7 +35,7 @@ class StatsService:
|
||||
"today": self.user_repository.get_registered_user_count(today_start),
|
||||
"days": self.user_repository.get_registered_user_trend(start, end)
|
||||
}
|
||||
def get_deposit_stats(self, start: datetime, end: datetime) -> dict:
|
||||
def get_deposit_stats(self, start: datetime, end: datetime, tenant_id: str = "default") -> dict:
|
||||
"""
|
||||
Get deposit stats
|
||||
|
||||
@@ -50,7 +50,7 @@ class StatsService:
|
||||
"today": self.user_wallet_repository.stats_deposit_amount(today_start),
|
||||
"days": self.user_wallet_repository.stats_deposit_amount_trend(start,end)
|
||||
}
|
||||
def get_call_stats(self, start: datetime, end: datetime) -> dict:
|
||||
def get_call_stats(self, start: datetime, end: datetime, tenant_id: str = "default") -> dict:
|
||||
"""
|
||||
Get call stats
|
||||
|
||||
@@ -62,12 +62,12 @@ class StatsService:
|
||||
pass30 = today_start - timedelta(days=30)
|
||||
|
||||
return {
|
||||
"total": self.stats_mcp_service_date_repository.stats_call_count(start, end),
|
||||
"today": self.stats_mcp_service_date_repository.stats_call_count(today_start),
|
||||
"days": self.stats_mcp_service_date_repository.stats_call_count_trend(start, end)
|
||||
"total": self.stats_mcp_service_date_repository.stats_call_count(tenant_id,start, end),
|
||||
"today": self.stats_mcp_service_date_repository.stats_call_count(tenant_id,today_start),
|
||||
"days": self.stats_mcp_service_date_repository.stats_call_count_trend(tenant_id,start, end)
|
||||
}
|
||||
|
||||
def get_call_stats_group_by_service(self, start: datetime, end: datetime) -> list:
|
||||
def get_call_stats_group_by_service(self, start: datetime, end: datetime, tenant_id: str = "default") -> list:
|
||||
"""
|
||||
Get call stats group by service
|
||||
|
||||
@@ -79,7 +79,7 @@ class StatsService:
|
||||
stats_map = {item["service_id"]: int(item.get("count", 0)) for item in stats}
|
||||
|
||||
# Iterate all services, fill missing call count as 0, sort by calls desc
|
||||
services = self.mcp_service_repository.get_all()
|
||||
services = self.mcp_service_repository.get_all(tenant_id)
|
||||
result = []
|
||||
for service in services:
|
||||
result.append({
|
||||
@@ -98,7 +98,7 @@ class StatsService:
|
||||
if call_count != 0:
|
||||
drop_service_ids.append(key)
|
||||
# Add deleted services
|
||||
deleted_services = self.drop_mcp_service_repository.all_by_service_ids(drop_service_ids)
|
||||
deleted_services = self.drop_mcp_service_repository.all_by_service_ids(drop_service_ids, tenant_id)
|
||||
for id,item in deleted_services.items():
|
||||
result.append({
|
||||
"id": id,
|
||||
|
||||
@@ -22,19 +22,19 @@ class SysConfigService:
|
||||
logger.warning(f"Redis connection failed, cache will be disabled: {e}")
|
||||
self.redis_client = None
|
||||
|
||||
def get_all(self, keys: List[str] = [], is_large: bool= False) -> Dict[str, str]:
|
||||
def get_all(self, keys: List[str] = [], is_large: bool= False, tenant_id: str = "default") -> Dict[str, str]:
|
||||
if is_large:
|
||||
return self.sys_config_large_repository.get_all(keys)
|
||||
return self.sys_config_repository.get_all(keys)
|
||||
return self.sys_config_large_repository.get_all(keys, tenant_id)
|
||||
return self.sys_config_repository.get_all(keys, tenant_id)
|
||||
|
||||
def get_value_by_key(self, key: str, is_large:bool = False) -> str:
|
||||
def get_value_by_key(self, key: str, is_large:bool = False, tenant_id: str = "default") -> str:
|
||||
if is_large:
|
||||
sys_config = self.sys_config_large_repository.get_by_key(key)
|
||||
sys_config = self.sys_config_large_repository.get_by_key(key, tenant_id)
|
||||
if sys_config:
|
||||
return sys_config.value
|
||||
return ""
|
||||
"""Get config value, prioritize cache, fallback to database if cache miss"""
|
||||
cache_key = RedisKeys.sys_config_key(key)
|
||||
cache_key = RedisKeys.sys_config_key(key, tenant_id)
|
||||
|
||||
if self.redis_client:
|
||||
try:
|
||||
@@ -46,7 +46,7 @@ class SysConfigService:
|
||||
logger.warning(f"Failed to get cache for key {key}: {e}")
|
||||
|
||||
try:
|
||||
value = self.sys_config_repository.get_value_by_key(key)
|
||||
value = self.sys_config_repository.get_value_by_key(key, tenant_id)
|
||||
|
||||
if self.redis_client:
|
||||
try:
|
||||
@@ -61,34 +61,34 @@ class SysConfigService:
|
||||
logger.error(f"Failed to get sys_config {key} from database: {e}")
|
||||
return ""
|
||||
|
||||
def delete_by_key(self, key: str) -> bool:
|
||||
def delete_by_key(self, key: str, tenant_id: str = "default") -> bool:
|
||||
"""Delete config and clear cache"""
|
||||
result = self.sys_config_repository.delete_by_key(key)
|
||||
result = self.sys_config_repository.delete_by_key(key, tenant_id)
|
||||
if result:
|
||||
self._clear_cache(key)
|
||||
return result
|
||||
|
||||
def set_value_by_key(self, key: str, value: str, description: str, is_large:bool = False):
|
||||
def set_value_by_key(self, key: str, value: str, description: str, is_large:bool = False, tenant_id: str = "default"):
|
||||
if is_large:
|
||||
self.sys_config_large_repository.set_value_by_key(key=key, value=value, description=description)
|
||||
self.sys_config_large_repository.set_value_by_key(key=key, value=value, description=description, tenant_id=tenant_id)
|
||||
return
|
||||
"""Set config value and clear cache"""
|
||||
result = self.sys_config_repository.set_value_by_key(key=key, value=value, description=description)
|
||||
result = self.sys_config_repository.set_value_by_key(key=key, value=value, description=description, tenant_id=tenant_id)
|
||||
if result:
|
||||
self._clear_cache(key)
|
||||
self._clear_cache(key, tenant_id)
|
||||
return
|
||||
|
||||
def _clear_cache(self, key: str) -> None:
|
||||
def _clear_cache(self, key: str, tenant_id: str = "default") -> None:
|
||||
"""Clear cache for specified config"""
|
||||
if self.redis_client:
|
||||
try:
|
||||
cache_key = RedisKeys.sys_config_key(key)
|
||||
cache_key = RedisKeys.sys_config_key(key, tenant_id)
|
||||
self.redis_client.delete(cache_key)
|
||||
logger.debug(f"Cleared cache for sys_config {key}")
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to clear cache for key {key}: {e}")
|
||||
|
||||
def get_multiple_values(self, keys: List[str]) -> Dict[str, Optional[str]]:
|
||||
def get_multiple_values(self, keys: List[str], tenant_id: str = "default") -> Dict[str, Optional[str]]:
|
||||
"""Get multiple config values with optimized performance"""
|
||||
result = {}
|
||||
cache_miss_keys = []
|
||||
@@ -96,7 +96,7 @@ class SysConfigService:
|
||||
if self.redis_client and keys:
|
||||
for key in keys:
|
||||
try:
|
||||
cache_key = RedisKeys.sys_config_key(key)
|
||||
cache_key = RedisKeys.sys_config_key(key, tenant_id)
|
||||
cached_value = self.redis_client.get(cache_key)
|
||||
if cached_value is not None:
|
||||
result[key] = None if cached_value == "__NULL__" else cached_value
|
||||
@@ -112,12 +112,12 @@ class SysConfigService:
|
||||
if cache_miss_keys:
|
||||
try:
|
||||
for key in cache_miss_keys:
|
||||
value = self.sys_config_repository.get_value_by_key(key)
|
||||
value = self.sys_config_repository.get_value_by_key(key, tenant_id)
|
||||
result[key] = value
|
||||
|
||||
if self.redis_client:
|
||||
try:
|
||||
cache_key = RedisKeys.sys_config_key(key)
|
||||
cache_key = RedisKeys.sys_config_key(key, tenant_id)
|
||||
cache_value = "__NULL__" if value is None else value
|
||||
self.redis_client.set(cache_key, cache_value, ex=3600)
|
||||
logger.debug(f"Cached sys_config {key} for 1 hour")
|
||||
|
||||
@@ -5,9 +5,7 @@ Supports both service_id (UUID) and slug_name service identifiers
|
||||
|
||||
from typing import Optional
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy import false
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
import asyncio
|
||||
import time
|
||||
import hashlib
|
||||
@@ -56,8 +54,8 @@ class McpController:
|
||||
|
||||
try:
|
||||
# Extract service_id from URL path (supports both ID and slug_name)
|
||||
service_id = self._extract_service_id(request)
|
||||
if not service_id:
|
||||
service_id,tenant_id = self._extract_service_id(request)
|
||||
if not service_id or not tenant_id:
|
||||
logger.error("Missing service_id parameter or service not found")
|
||||
# For SSE connection errors, we need to send response directly via ASGI interface
|
||||
await self._send_error_response(request, 400, "Missing service_id parameter or service not found")
|
||||
@@ -81,7 +79,7 @@ class McpController:
|
||||
apikey_for_log = apikey[:10] if apikey else None
|
||||
|
||||
# Create MCP server instance (pass user_id and apikey_id for billing and logging)
|
||||
mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id)
|
||||
mcp_server = await self.server_factory.create_server(service_id,tenant_id, user_id, apikey_id)
|
||||
|
||||
# Register connection to manager
|
||||
connection_key = connection_manager.register_connection(service_id, user_id, client_ip)
|
||||
@@ -149,9 +147,9 @@ class McpController:
|
||||
|
||||
try:
|
||||
# Extract service_id from URL path (supports both ID and slug_name)
|
||||
service_id = self._extract_service_id(req)
|
||||
if not service_id:
|
||||
response_body = b"Missing service_id parameter or service not found"
|
||||
service_id,tenant_id = self._extract_service_id(req)
|
||||
if not service_id or not tenant_id:
|
||||
response_body = b"Missing service_id or tenant_id parameter or service not found"
|
||||
await send({
|
||||
'type': 'http.response.start',
|
||||
'status': 400,
|
||||
@@ -197,7 +195,7 @@ class McpController:
|
||||
return
|
||||
|
||||
# Create MCP server instance (pass user_id and apikey_id for billing and logging)
|
||||
mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id)
|
||||
mcp_server = await self.server_factory.create_server(service_id,tenant_id, user_id, apikey_id)
|
||||
|
||||
# Register connection to manager
|
||||
connection_key = connection_manager.register_connection(service_id, user_id, client_ip)
|
||||
@@ -272,9 +270,9 @@ class McpController:
|
||||
|
||||
try:
|
||||
# Extract service_id from URL path (supports both ID and slug_name)
|
||||
service_id = self._extract_service_id(req)
|
||||
if not service_id:
|
||||
response_body = b"Missing service_id parameter or service not found"
|
||||
service_id,tenant_id = self._extract_service_id(req)
|
||||
if not service_id or not tenant_id:
|
||||
response_body = b"Missing service_id or tenant_id parameter or service not found"
|
||||
await send({
|
||||
'type': 'http.response.start',
|
||||
'status': 400,
|
||||
@@ -390,7 +388,7 @@ class McpController:
|
||||
|
||||
try:
|
||||
# Get or create a persistent StreamableHTTP transport and MCP server
|
||||
transport = await self._ensure_http_session(session_key, service_id, user_id, apikey_id)
|
||||
transport = await self._ensure_http_session(session_key, service_id,tenant_id, user_id, apikey_id)
|
||||
logger.debug(f"method: {req.method}, content_type: {content_type}")
|
||||
# Note session activity
|
||||
self._note_session_activity(session_key)
|
||||
@@ -520,7 +518,7 @@ class McpController:
|
||||
logger.exception("Origin validation error")
|
||||
return False
|
||||
|
||||
async def _ensure_http_session(self, session_key: str, service_id: str, user_id: str, apikey_id: str) -> StreamableHTTPServerTransport:
|
||||
async def _ensure_http_session(self, session_key: str, service_id: str, tenant_id: str, user_id: str, apikey_id: str) -> StreamableHTTPServerTransport:
|
||||
"""Ensure a persistent StreamableHTTP transport and MCP server exist for the session.
|
||||
|
||||
- Create or reuse the transport
|
||||
@@ -539,7 +537,7 @@ class McpController:
|
||||
# If no task or the task is finished, restart
|
||||
mcp_server = self._http_servers.get(session_key)
|
||||
if mcp_server is None:
|
||||
mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id)
|
||||
mcp_server = await self.server_factory.create_server(service_id,tenant_id, user_id, apikey_id)
|
||||
self._http_servers[session_key] = mcp_server
|
||||
init_options = mcp_server.create_initialization_options()
|
||||
init_options.server_name = f"mcp-service-{service_id}"
|
||||
@@ -604,11 +602,11 @@ class McpController:
|
||||
except Exception:
|
||||
logger.exception("Error while waiting for server task to finish")
|
||||
|
||||
def _extract_service_id(self, request: Request) -> Optional[str]:
|
||||
def _extract_service_id(self, request: Request) -> tuple[Optional[str], Optional[str]]:
|
||||
"""Extract service ID from request path, supporting both ID and slug_name."""
|
||||
service_identifier = request.path_params.get("service_id")
|
||||
if not service_identifier:
|
||||
return None
|
||||
return None, None
|
||||
|
||||
# Try to find service by service_identifier, supports both ID and slug_name modes
|
||||
db = None
|
||||
@@ -623,20 +621,20 @@ class McpController:
|
||||
service = service_repository.get_by_id(service_identifier)
|
||||
if service:
|
||||
logger.debug(f"Service found (by ID): {service.name} ({service.id})")
|
||||
return service.id
|
||||
return service.id,service.tenant_id
|
||||
|
||||
# If not found by ID, try by slug_name
|
||||
service = service_repository.get_by_slug_name(service_identifier)
|
||||
if service:
|
||||
logger.debug(f"Service found (by slug_name): {service.name} ({service.id})")
|
||||
return service.id
|
||||
return service.id,service.tenant_id
|
||||
|
||||
logger.warning(f"Service not found: {service_identifier}")
|
||||
return None
|
||||
return None, None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error occurred while querying service: {str(e)}", exc_info=True)
|
||||
return None
|
||||
return None, None
|
||||
finally:
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
@@ -125,38 +125,23 @@ class BillingService:
|
||||
call_end_time: Call end time
|
||||
"""
|
||||
try:
|
||||
message = BillingMessage(
|
||||
user_id=call_log.user_id,
|
||||
service_id=call_log.service_id,
|
||||
api_id=call_log.api_id,
|
||||
tool_name=call_log.tool_name,
|
||||
input_params=call_log.input_params,
|
||||
input_token=call_log.input_token,
|
||||
output_token=call_log.output_token,
|
||||
charge_type=call_log.charge_type,
|
||||
call_success=call_success,
|
||||
unit_price=call_log.unit_price,
|
||||
call_start_time=call_log.call_start_time,
|
||||
call_end_time=call_end_time,
|
||||
apikey_id=call_log.apikey_id,
|
||||
)
|
||||
|
||||
# Serialize message
|
||||
message_json = json.dumps(
|
||||
{
|
||||
"user_id": message.user_id,
|
||||
"service_id": message.service_id,
|
||||
"api_id": message.api_id,
|
||||
"tool_name": message.tool_name,
|
||||
"input_params": message.input_params,
|
||||
"call_success": message.call_success,
|
||||
"unit_price": str(message.unit_price),
|
||||
"input_token": str(message.input_token),
|
||||
"output_token": str(message.output_token),
|
||||
"charge_type": message.charge_type,
|
||||
"call_start_time": message.call_start_time.isoformat(),
|
||||
"call_end_time": message.call_end_time.isoformat() if message.call_end_time else None,
|
||||
"apikey_id": message.apikey_id,
|
||||
"user_id": call_log.user_id,
|
||||
"service_id": call_log.service_id,
|
||||
"tenant_id": call_log.tenant_id,
|
||||
"api_id": call_log.api_id,
|
||||
"tool_name": call_log.tool_name,
|
||||
"input_params": call_log.input_params,
|
||||
"call_success": call_success,
|
||||
"unit_price": str(call_log.unit_price),
|
||||
"input_token": str(call_log.input_token),
|
||||
"output_token": str(call_log.output_token),
|
||||
"charge_type": call_log.charge_type,
|
||||
"call_start_time": call_log.call_start_time.isoformat(),
|
||||
"call_end_time": call_end_time.isoformat() if call_end_time else None,
|
||||
"apikey_id": call_log.apikey_id,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ class McpServerFactory:
|
||||
self.tool_service = McpToolService()
|
||||
self.billing_service = billing_service
|
||||
|
||||
async def create_server(self, service_id: str, user_id: Optional[str] = None, apikey_id: Optional[str] = None) -> Server:
|
||||
async def create_server(self, service_id: str,tenant_id: str, user_id: Optional[str] = None, apikey_id: Optional[str] = None) -> Server:
|
||||
"""
|
||||
Create MCP server for specified service_id
|
||||
|
||||
@@ -65,7 +65,7 @@ class McpServerFactory:
|
||||
logger.error(error_msg)
|
||||
return [types.TextContent(type="text", text=error_msg)], {}
|
||||
|
||||
return await self._handle_call_tool_with_billing(service_id, name, arguments, user_id, apikey_id)
|
||||
return await self._handle_call_tool_with_billing(service_id, tenant_id, name, arguments, user_id, apikey_id)
|
||||
|
||||
logger.info("MCP server instance created successfully")
|
||||
return app
|
||||
@@ -102,12 +102,13 @@ class McpServerFactory:
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
async def _handle_call_tool_with_billing(self, service_id: str, name: str, arguments: dict, user_id: str, apikey_id: Optional[str] = None) -> tuple[List[types.Content], dict]:
|
||||
async def _handle_call_tool_with_billing(self, service_id: str, tenant_id: str, name: str, arguments: dict, user_id: str, apikey_id: Optional[str] = None) -> tuple[List[types.Content], dict]:
|
||||
"""
|
||||
Handle tool call with billing logic
|
||||
|
||||
Args:
|
||||
service_id: Service ID
|
||||
tenant_id: Tenant ID
|
||||
name: Tool name
|
||||
arguments: Tool arguments
|
||||
user_id: User ID
|
||||
@@ -146,10 +147,10 @@ class McpServerFactory:
|
||||
# If tool API id not found, skip sending billing message to avoid FK errors
|
||||
if not api_id_for_log:
|
||||
return [types.TextContent(type="text", text=error_msg)], {}
|
||||
|
||||
call_log = ApiCallLogInfo(
|
||||
user_id=user_id,
|
||||
service_id=service_id,
|
||||
tenant_id=tenant_id,
|
||||
api_id=api_id_for_log,
|
||||
tool_name=name,
|
||||
input_params=json.dumps(arguments),
|
||||
@@ -246,6 +247,7 @@ class McpServerFactory:
|
||||
call_log = ApiCallLogInfo(
|
||||
user_id=user_id,
|
||||
service_id=service_id,
|
||||
tenant_id=tenant_id,
|
||||
api_id=tool_config.id,
|
||||
tool_name=name,
|
||||
input_params=json.dumps(arguments),
|
||||
|
||||
@@ -10,6 +10,7 @@ from typing import Optional
|
||||
class BillingMessage:
|
||||
user_id: str
|
||||
service_id: str
|
||||
tenant_id: str
|
||||
api_id: str
|
||||
tool_name: str
|
||||
input_params: str
|
||||
|
||||
@@ -25,6 +25,7 @@ class McpCallLog(Base):
|
||||
)
|
||||
user_id: Mapped[str] = mapped_column(String(36), nullable=False, comment="User ID")
|
||||
service_id: Mapped[str] = mapped_column(String(36), nullable=False, comment="MCP service ID")
|
||||
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, comment="Tenant ID")
|
||||
api_id: Mapped[str] = mapped_column(String(100), nullable=False, comment="API tool ID")
|
||||
tool_name: Mapped[str] = mapped_column(String(200), nullable=False, comment="Tool name")
|
||||
input_params: Mapped[str] = mapped_column(Text, nullable=True, comment="Call parameters")
|
||||
|
||||
@@ -14,6 +14,7 @@ class McpService(Base):
|
||||
autoincrement=False,
|
||||
comment="Primary key",
|
||||
)
|
||||
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, default="default", comment="Tenant ID")
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="Service name")
|
||||
slug_name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, comment="Unique slug name for the service")
|
||||
short_description: Mapped[str] = mapped_column(String, nullable=False, comment="Short description of the service")
|
||||
@@ -58,6 +59,7 @@ class DropMCPService(Base):
|
||||
autoincrement=False,
|
||||
comment="Primary key, service UUID",
|
||||
)
|
||||
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, default="default", comment="Tenant ID")
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="Service name")
|
||||
slug_name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, comment="Unique service identifier (slug format)")
|
||||
short_description: Mapped[str] = mapped_column(String, nullable=False, comment="Brief service description")
|
||||
|
||||
@@ -12,6 +12,7 @@ class PaymentChannel(Base):
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
"""
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, autoincrement=False, comment="Primary key")
|
||||
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, comment="Tenant ID")
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="Channel name")
|
||||
status: Mapped[int] = mapped_column(Integer, nullable=False, comment="Channel status, 0: disabled, 1: enabled")
|
||||
# Store encrypted JSON envelope for channel configuration (AES-GCM). See secure_config.py
|
||||
|
||||
@@ -15,12 +15,21 @@ class StatsMcpServiceDate(Base):
|
||||
comment="统计时间(精确到小时,UTC)",
|
||||
)
|
||||
|
||||
tenant_id: Mapped[str] = mapped_column(
|
||||
String(100),
|
||||
primary_key=True,
|
||||
nullable=False,
|
||||
default="default",
|
||||
comment="租户ID",
|
||||
)
|
||||
|
||||
service_id: Mapped[str] = mapped_column(
|
||||
String(100),
|
||||
primary_key=True,
|
||||
nullable=False,
|
||||
comment="服务ID",
|
||||
)
|
||||
|
||||
|
||||
call_count: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
|
||||
@@ -8,6 +8,12 @@ from datetime import datetime
|
||||
class SysConfig(Base):
|
||||
__tablename__ = "sys_config"
|
||||
|
||||
__table_args__ = (
|
||||
{"mysql_engine": "InnoDB", "mysql_charset": "utf8mb4"},
|
||||
{"mysql_comment": "System configuration table"},
|
||||
{"mysql_indexes": [{"name": "idx_key_tenant_id", "columns": ["key", "tenant_id"]}]},
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(
|
||||
String(36),
|
||||
primary_key=True,
|
||||
@@ -15,6 +21,7 @@ class SysConfig(Base):
|
||||
comment="Primary key",
|
||||
)
|
||||
key: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, comment="Configuration key")
|
||||
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, comment="Tenant ID")
|
||||
value: Mapped[str] = mapped_column(String, nullable=False, comment="Configuration value")
|
||||
description: Mapped[str] = mapped_column(String, nullable=False, comment="Configuration description")
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
|
||||
@@ -7,6 +7,14 @@ from datetime import datetime
|
||||
|
||||
class SysConfigLarge(Base):
|
||||
__tablename__ = "sys_config_large"
|
||||
|
||||
__table_args__ = (
|
||||
{"mysql_engine": "InnoDB", "mysql_charset": "utf8mb4"},
|
||||
{"mysql_comment": "System configuration table for large values"},
|
||||
{"mysql_indexes": [{"name": "idx_key_tenant_id", "columns": ["key", "tenant_id"]}]},
|
||||
)
|
||||
|
||||
|
||||
|
||||
id: Mapped[str] = mapped_column(
|
||||
String(36),
|
||||
@@ -15,6 +23,7 @@ class SysConfigLarge(Base):
|
||||
comment="Primary key",
|
||||
)
|
||||
key: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, comment="Configuration key")
|
||||
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, comment="Tenant ID")
|
||||
value: Mapped[str] = mapped_column(String, nullable=False, comment="Configuration value")
|
||||
description: Mapped[str] = mapped_column(String, nullable=False, comment="Configuration description")
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
|
||||
@@ -14,6 +14,7 @@ class TempMcpService(Base):
|
||||
autoincrement=False,
|
||||
comment="Primary key",
|
||||
)
|
||||
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, comment="Tenant ID")
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="Service name")
|
||||
slug_name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, comment="Unique slug name for the service")
|
||||
short_description: Mapped[str] = mapped_column(String, nullable=False, comment="Short description of the service")
|
||||
|
||||
@@ -21,6 +21,8 @@ class User(Base):
|
||||
autoincrement=False,
|
||||
comment="Primary key, auto-incremented ID",
|
||||
)
|
||||
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, comment="Tenant ID")
|
||||
|
||||
name: Mapped[str] = mapped_column(String(100), nullable=False, comment="Username")
|
||||
email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, comment="Email address")
|
||||
password: Mapped[str] = mapped_column(String(255), nullable=True, comment="Password hash")
|
||||
|
||||
@@ -32,6 +32,7 @@ class UserWalletHistory(Base):
|
||||
user_id: Mapped[str] = mapped_column(
|
||||
String(36), nullable=False, comment="User unique ID (UUID format)"
|
||||
)
|
||||
|
||||
payment_method: Mapped[PaymentMethod] = mapped_column(
|
||||
Enum(PaymentMethod, values_callable=lambda obj: [e.value for e in obj]),
|
||||
nullable=False,
|
||||
|
||||
@@ -24,9 +24,9 @@ class RedisKeys:
|
||||
return f"xpack:openapi:parse:{parse_id}"
|
||||
|
||||
@staticmethod
|
||||
def sys_config_key(config_key: str) -> str:
|
||||
def sys_config_key(config_key: str, tenant_id: str = "default") -> str:
|
||||
"""Generate system config cache key"""
|
||||
return f"xpack:sys_config:{config_key}"
|
||||
return f"xpack:sys_config:{tenant_id}:{config_key}"
|
||||
|
||||
@staticmethod
|
||||
def mcp_service_id_key(service_id: str) -> str:
|
||||
|
||||
Reference in new issue
Block a user