Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
588f2770b7 | ||
|
|
ad743d6ab7 | ||
|
|
6145d0b005 | ||
|
|
d147fcb070 | ||
|
|
6196b93e97 | ||
|
|
b51933d625 | ||
|
|
40ff118798 | ||
|
|
d470677abe | ||
|
|
6eac3c6100 | ||
|
|
d22513730a | ||
|
|
613cece94b | ||
|
|
5cd1c12323 | ||
|
|
dcf73b25bf | ||
|
|
64a67e9c8d | ||
|
|
b6160af3c5 | ||
|
|
2ea41dfb75 | ||
|
|
cbcc419f0b | ||
|
|
687949beb2 | ||
|
|
fedaa5f5df | ||
|
|
c272c87e1f | ||
|
|
8a052ff910 | ||
|
|
d1c7a4b8c0 | ||
|
|
1a104338e8 | ||
|
|
5dd47b2d01 | ||
|
|
a28de60b1c | ||
|
|
e4a807b2a0 | ||
|
|
d0e7ed2771 | ||
|
|
05c0088f1b | ||
|
|
7f66cfd21e | ||
|
|
05b48f3656 | ||
|
|
07fe83b7b5 | ||
|
|
5fa1cd741e | ||
|
|
3e1d986c1c | ||
|
|
2cf95fd1c1 | ||
|
|
09b6811391 | ||
|
|
08d326dc1f | ||
|
|
fb23d62fb2 | ||
|
|
79400146ea | ||
|
|
503b3bf078 | ||
|
|
eb4ecff6ed | ||
|
|
7f8947f070 | ||
|
|
6dca9b08cc | ||
|
|
06034cf46e | ||
|
|
ffdf931cad | ||
|
|
24e59f18d0 | ||
|
|
5a538863b1 | ||
|
|
f468fe270f | ||
|
|
4cc8b87de5 | ||
|
|
f6e0130035 | ||
|
|
6f882845c2 | ||
|
|
5654f83644 | ||
|
|
6d15420b08 | ||
|
|
5469eca450 | ||
|
|
94fd46c8ca | ||
|
|
57bd964d22 | ||
|
|
aac3a187d1 |
No files matched your search
+10
-1
@@ -26,8 +26,17 @@ RABBITMQ_USER=rabbitmq
|
||||
RABBITMQ_PASSWORD=rabbitmq_Gs123dA
|
||||
RABBITMQ_VHOST=/
|
||||
|
||||
# Billing queue / retry settings
|
||||
BILLING_QUEUE_NAME=billing.api.calls
|
||||
# Max retry attempts (excluding initial consumption)
|
||||
BILLING_RETRY_TIMES=3
|
||||
# Base delay seconds for exponential backoff: base * 2^retry_count
|
||||
BILLING_RETRY_BASE_DELAY_SECONDS=30
|
||||
# Max delay seconds cap
|
||||
BILLING_RETRY_MAX_DELAY_SECONDS=1800
|
||||
|
||||
# Payment config encryption key (Base64-encoded 32-byte random value for AES-GCM)
|
||||
# Quick generation: python -c "import os,base64; print(base64.b64encode(os.urandom(32)).decode())"
|
||||
PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA=
|
||||
# Optional: key ID used to support key rotation identification
|
||||
PAYMENT_CONFIG_KEY_ID=default
|
||||
PAYMENT_CONFIG_KEY_ID=default
|
||||
+1
-1
@@ -9,7 +9,7 @@
|
||||
!.yarn/plugins
|
||||
!.yarn/releases
|
||||
!.yarn/versions
|
||||
|
||||
/pnpm-lock.yaml
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {
|
||||
reactStrictMode: false,
|
||||
// Enable SSR
|
||||
output: "standalone",
|
||||
|
||||
// Optimize images
|
||||
images: {
|
||||
domains: [],
|
||||
unoptimized: false, // Enable image optimization for SSR
|
||||
},
|
||||
|
||||
// Webpack configuration
|
||||
webpack: (config) => {
|
||||
return config;
|
||||
},
|
||||
|
||||
// Redirect configuration
|
||||
async redirects() {
|
||||
return [];
|
||||
},
|
||||
async rewrites() {
|
||||
return [
|
||||
// {
|
||||
// source: "/api/:path*",
|
||||
// destination: `${process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8001"}/api/:path*`,
|
||||
// },
|
||||
{
|
||||
source: "/uploads/:path*",
|
||||
destination: `${
|
||||
process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8001"
|
||||
}/uploads/:path*`,
|
||||
},
|
||||
];
|
||||
},
|
||||
|
||||
// Headers for better performance
|
||||
async headers() {
|
||||
return [
|
||||
{
|
||||
source: "/(.*)",
|
||||
headers: [
|
||||
{
|
||||
key: "X-Content-Type-Options",
|
||||
value: "nosniff",
|
||||
},
|
||||
{
|
||||
key: "X-XSS-Protection",
|
||||
value: "1; mode=block",
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = nextConfig;
|
||||
@@ -1,10 +1,15 @@
|
||||
import type { NextConfig } from "next";
|
||||
|
||||
const nextConfig: NextConfig = {
|
||||
reactStrictMode: false,
|
||||
output: "standalone",
|
||||
|
||||
experimental: {
|
||||
optimizePackageImports: ["@nextui-org/react"],
|
||||
},
|
||||
|
||||
transpilePackages: ["@nextui-org/react", "@nextui-org/theme"],
|
||||
|
||||
images: {
|
||||
remotePatterns: [
|
||||
{
|
||||
@@ -12,6 +17,42 @@ const nextConfig: NextConfig = {
|
||||
hostname: "**",
|
||||
},
|
||||
],
|
||||
unoptimized: false,
|
||||
},
|
||||
|
||||
// Redirect configuration
|
||||
async redirects() {
|
||||
return [];
|
||||
},
|
||||
|
||||
async rewrites() {
|
||||
return [
|
||||
{
|
||||
source: "/uploads/:path*",
|
||||
destination: `${
|
||||
process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8001"
|
||||
}/uploads/:path*`,
|
||||
},
|
||||
];
|
||||
},
|
||||
|
||||
// Headers for better performance
|
||||
async headers() {
|
||||
return [
|
||||
{
|
||||
source: "/(.*)",
|
||||
headers: [
|
||||
{
|
||||
key: "X-Content-Type-Options",
|
||||
value: "nosniff",
|
||||
},
|
||||
{
|
||||
key: "X-XSS-Protection",
|
||||
value: "1; mode=block",
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
"immer": "^10.0.3",
|
||||
"lottie-react": "^2.4.1",
|
||||
"lucide-react": "^0.475.0",
|
||||
"next": "^15.5.9",
|
||||
"next": "^16.1.5",
|
||||
"react": "^19.1.4",
|
||||
"react-dom": "^19.1.4",
|
||||
"react-hook-form": "^7.49.3",
|
||||
@@ -55,7 +55,7 @@
|
||||
"@types/three": "^0.177.0",
|
||||
"autoprefixer": "^10.4.17",
|
||||
"eslint": "^8.56.0",
|
||||
"eslint-config-next": "15.5.9",
|
||||
"eslint-config-next": "16.1.5",
|
||||
"postcss": "^8.4.33",
|
||||
"tailwindcss": "^3.4.17",
|
||||
"typescript": "^5.3.3"
|
||||
|
||||
Generated
-18565
File diff suppressed because it is too large.
Load diff
@@ -12,7 +12,7 @@ export async function revalidatePlatformConfig() {
|
||||
revalidatePath("/", "layout");
|
||||
|
||||
// Revalidate host-specific cache tag
|
||||
revalidateTag(`platform-config-${host}`);
|
||||
revalidateTag(`platform-config-${host}`, 'max');
|
||||
|
||||
console.info(
|
||||
`Platform config cache revalidated for host: platform-config-${host}`
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
input::placeholder,
|
||||
textarea::placeholder {
|
||||
--nextui-foreground-500: 240 5.03% 64.9%;
|
||||
}
|
||||
/* Import theme styles */
|
||||
@import "../styles/themes.css";
|
||||
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
/* Import theme styles */
|
||||
@import "../styles/themes.css";
|
||||
input::placeholder,
|
||||
textarea::placeholder {
|
||||
--nextui-foreground-500: 240 5.03% 64.9%;
|
||||
}
|
||||
|
||||
html,
|
||||
body {
|
||||
|
||||
+19
-5
@@ -1,7 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2017",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"lib": [
|
||||
"dom",
|
||||
"dom.iterable",
|
||||
"esnext"
|
||||
],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
@@ -11,7 +15,7 @@
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"jsx": "react-jsx",
|
||||
"incremental": true,
|
||||
"plugins": [
|
||||
{
|
||||
@@ -19,9 +23,19 @@
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
"@/*": [
|
||||
"./src/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
"include": [
|
||||
"next-env.d.ts",
|
||||
"**/*.ts",
|
||||
"**/*.tsx",
|
||||
".next/types/**/*.ts",
|
||||
".next/dev/types/**/*.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"version": "v1.2",
|
||||
"releaseDate": "2025-12-22",
|
||||
"version": "v1.4",
|
||||
"releaseDate": "2026-02-23",
|
||||
"description": "This is the latest release of the application.",
|
||||
"downloads": {
|
||||
"program": {
|
||||
"xpack_mcp_market": {
|
||||
"version": "v1.2.1",
|
||||
"version": "v1.4.1",
|
||||
"docker": "xpackai/xpack-mcp-market",
|
||||
"tar": "xpack-mcp-market.tar.gz",
|
||||
"url": "https://public.eolinker.com/xpack/images/xpack-mcp-market.${VERSION}.${ARCH}.tar.gz"
|
||||
|
||||
@@ -21,9 +21,14 @@ class BillingMessageConsumer:
|
||||
|
||||
def __init__(self):
|
||||
self.queue_name = os.getenv("BILLING_QUEUE_NAME") or "billing.api.calls"
|
||||
self.retry_queue_name = f"{self.queue_name}.retry"
|
||||
self.connection = None
|
||||
self.channel = None
|
||||
self.consuming = False
|
||||
# retry times from env, default 3
|
||||
self.max_retries = int(os.getenv("BILLING_RETRY_TIMES", "3"))
|
||||
self.retry_base_delay_seconds = int(os.getenv("BILLING_RETRY_BASE_DELAY_SECONDS", "30"))
|
||||
self.retry_max_delay_seconds = int(os.getenv("BILLING_RETRY_MAX_DELAY_SECONDS", "1800"))
|
||||
self._setup_connection()
|
||||
|
||||
def _setup_connection(self):
|
||||
@@ -48,6 +53,14 @@ class BillingMessageConsumer:
|
||||
|
||||
# Declare queue
|
||||
self.channel.queue_declare(queue=self.queue_name, durable=True)
|
||||
self.channel.queue_declare(
|
||||
queue=self.retry_queue_name,
|
||||
durable=True,
|
||||
arguments={
|
||||
"x-dead-letter-exchange": "",
|
||||
"x-dead-letter-routing-key": self.queue_name,
|
||||
},
|
||||
)
|
||||
|
||||
# Set QoS, only process one message at a time
|
||||
self.channel.basic_qos(prefetch_count=1)
|
||||
@@ -124,6 +137,29 @@ class BillingMessageConsumer:
|
||||
body: Message body
|
||||
"""
|
||||
message_data = None
|
||||
settled = False
|
||||
raw_body = body.decode("utf-8", errors="replace") if isinstance(body, (bytes, bytearray)) else str(body)
|
||||
|
||||
def _safe_ack() -> None:
|
||||
nonlocal settled
|
||||
if settled:
|
||||
return
|
||||
settled = True
|
||||
try:
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
except Exception:
|
||||
logger.error("Failed to ack message", exc_info=True)
|
||||
|
||||
def _safe_nack(requeue: bool) -> None:
|
||||
nonlocal settled
|
||||
if settled:
|
||||
return
|
||||
settled = True
|
||||
try:
|
||||
channel.basic_nack(delivery_tag=method.delivery_tag, requeue=requeue)
|
||||
except Exception:
|
||||
logger.error("Failed to nack message", exc_info=True)
|
||||
|
||||
try:
|
||||
# Parse message
|
||||
message_data = json.loads(body)
|
||||
@@ -137,27 +173,68 @@ class BillingMessageConsumer:
|
||||
|
||||
if success:
|
||||
# Ack if processed successfully
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
_safe_ack()
|
||||
logger.info(f"Message processed and acknowledged: {message_data.get('user_id')}")
|
||||
else:
|
||||
# If failed, ack and drop (avoid infinite retry)
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
logger.error(f"Message processing failed, message dropped: {message_data.get('user_id')}")
|
||||
# Retry with limited attempts
|
||||
retry_count = self._get_retry_count(properties)
|
||||
if retry_count < self.max_retries:
|
||||
try:
|
||||
delay_ms = self._get_retry_delay_ms(retry_count)
|
||||
self._publish_delayed_retry(channel, message_data, retry_count + 1, delay_ms)
|
||||
_safe_ack()
|
||||
logger.warning(
|
||||
f"Message processing failed, scheduled retry {retry_count + 1}/{self.max_retries} after {delay_ms}ms: {message_data.get('user_id')}, tool={message_data.get('tool_name')}, "
|
||||
)
|
||||
except Exception as pub_err:
|
||||
logger.error(f"Failed to requeue message for retry: {pub_err}", exc_info=True)
|
||||
# Requeue original message
|
||||
_safe_nack(requeue=True)
|
||||
else:
|
||||
# Exceeded retries; ack and drop
|
||||
_safe_ack()
|
||||
self._log_retry_exceeded(
|
||||
reason="handler_returned_false",
|
||||
retry_count=retry_count,
|
||||
properties=properties,
|
||||
method=method,
|
||||
message_data=message_data,
|
||||
raw_body=raw_body,
|
||||
)
|
||||
|
||||
finally:
|
||||
db.close()
|
||||
try:
|
||||
db.close()
|
||||
except Exception:
|
||||
logger.error("Failed to close db session", exc_info=True)
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"Message format error: {str(e)}, body: {body}")
|
||||
# Format error, ack directly (do not requeue)
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
_safe_ack()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Exception occurred while processing message: {str(e)}", exc_info=True)
|
||||
# On exception, ack and drop (avoid infinite retry)
|
||||
try:
|
||||
channel.basic_ack(delivery_tag=method.delivery_tag)
|
||||
logger.error(f"Exception handling failed, message dropped")
|
||||
retry_count = self._get_retry_count(properties)
|
||||
if message_data is not None and retry_count < self.max_retries:
|
||||
delay_ms = self._get_retry_delay_ms(retry_count)
|
||||
self._publish_delayed_retry(channel, message_data, retry_count + 1, delay_ms, error=str(e))
|
||||
_safe_ack()
|
||||
logger.warning(
|
||||
f"Exception during processing, scheduled retry {retry_count + 1}/{self.max_retries} after {delay_ms}ms: {message_data.get('user_id')}"
|
||||
)
|
||||
else:
|
||||
_safe_ack()
|
||||
self._log_retry_exceeded(
|
||||
reason="exception",
|
||||
retry_count=retry_count,
|
||||
properties=properties,
|
||||
method=method,
|
||||
message_data=message_data,
|
||||
error=str(e),
|
||||
raw_body=raw_body,
|
||||
)
|
||||
except:
|
||||
# If even ack fails, log error but do not raise
|
||||
logger.error("Unable to acknowledge message", exc_info=True)
|
||||
@@ -173,9 +250,62 @@ class BillingMessageConsumer:
|
||||
int: Retry count
|
||||
"""
|
||||
if properties and properties.headers:
|
||||
return properties.headers.get("x-retry-count", 0)
|
||||
raw = properties.headers.get("x-retry-count", 0)
|
||||
try:
|
||||
return int(raw)
|
||||
except Exception:
|
||||
return 0
|
||||
return 0
|
||||
|
||||
def _get_retry_delay_ms(self, current_retry_count: int) -> int:
|
||||
delay_seconds = self.retry_base_delay_seconds * (2 ** current_retry_count)
|
||||
if delay_seconds > self.retry_max_delay_seconds:
|
||||
delay_seconds = self.retry_max_delay_seconds
|
||||
return int(delay_seconds * 1000)
|
||||
|
||||
def _publish_delayed_retry(self, channel, message_data: dict, next_retry_count: int, delay_ms: int, error: str | None = None) -> None:
|
||||
headers: dict[str, str | int] = {"x-retry-count": next_retry_count}
|
||||
if error:
|
||||
headers["x-last-error"] = error
|
||||
props = pika.BasicProperties(
|
||||
delivery_mode=2,
|
||||
headers=headers,
|
||||
expiration=str(delay_ms),
|
||||
)
|
||||
channel.basic_publish(exchange="", routing_key=self.retry_queue_name, body=json.dumps(message_data), properties=props)
|
||||
|
||||
def _log_retry_exceeded(self, reason: str, retry_count: int, properties, method, message_data: dict | None, error: str | None = None, raw_body: str | None = None) -> None:
|
||||
headers = {}
|
||||
if properties and properties.headers:
|
||||
try:
|
||||
headers = dict(properties.headers)
|
||||
except Exception:
|
||||
headers = {}
|
||||
event = {
|
||||
"reason": reason,
|
||||
"retry_count": retry_count,
|
||||
"max_retries": self.max_retries,
|
||||
"queue": self.queue_name,
|
||||
"routing_key": getattr(method, "routing_key", None),
|
||||
"delivery_tag": getattr(method, "delivery_tag", None),
|
||||
"headers": headers,
|
||||
"user_id": (message_data or {}).get("user_id"),
|
||||
"service_id": (message_data or {}).get("service_id"),
|
||||
"api_id": (message_data or {}).get("api_id"),
|
||||
"tool_name": (message_data or {}).get("tool_name"),
|
||||
"call_start_time": (message_data or {}).get("call_start_time"),
|
||||
"call_end_time": (message_data or {}).get("call_end_time"),
|
||||
}
|
||||
if error:
|
||||
event["error"] = error
|
||||
if raw_body is not None:
|
||||
event["raw_body"] = raw_body
|
||||
try:
|
||||
payload = json.dumps(event, ensure_ascii=False, default=str)
|
||||
except Exception:
|
||||
payload = repr(event)
|
||||
logger.error(f"ALERT billing message retries exceeded: {payload}")
|
||||
|
||||
|
||||
def start_billing_consumer():
|
||||
"""Start billing message consumer"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func
|
||||
from datetime import datetime, date
|
||||
@@ -7,6 +7,10 @@ from yarl import Query
|
||||
from services.common.database import get_db
|
||||
from services.common.utils.response_utils import ResponseUtils
|
||||
from services.admin_service.services.stats_service import StatsService
|
||||
from services.admin_service.utils.user_utils import UserUtils
|
||||
from services.common import error_msg
|
||||
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -15,10 +19,13 @@ def get_stats_service(db: Session = Depends(get_db)) -> StatsService:
|
||||
|
||||
@router.get("/stats/analytics")
|
||||
async def stats(
|
||||
request: Request,
|
||||
start: int= 0,
|
||||
end: int=0,
|
||||
stats_service: StatsService = Depends(get_stats_service)
|
||||
):
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
if end == 0:
|
||||
end = int(datetime.now().timestamp())
|
||||
if start == 0:
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
# Read config from sys_config table, key is configured in services/admin_service/constants/sys_config_key.py
|
||||
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
import json
|
||||
from sqlalchemy.orm import Session
|
||||
from services.common.database import get_db
|
||||
from services.common.utils.response_utils import ResponseUtils
|
||||
from services.admin_service.utils.user_utils import UserUtils
|
||||
from services.common import error_msg
|
||||
from services.admin_service.services.sys_config_service import SysConfigService
|
||||
from services.admin_service.constants.sys_config_key import *
|
||||
from services.admin_service.services.payment_channel_service import PaymentChannelService
|
||||
@@ -16,6 +18,7 @@ router = APIRouter()
|
||||
|
||||
@router.get("/config", summary="Get configuration (no login required)", tags=["common"])
|
||||
def get_config(
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get platform configuration settings without authentication."""
|
||||
@@ -81,8 +84,10 @@ def get_config(
|
||||
return ResponseUtils.error(message="Failed to get configuration")
|
||||
|
||||
@router.get("/homepage", summary="Get homepage configuration (no login required)", tags=["common"])
|
||||
def get_homepage_config(db: Session = Depends(get_db)):
|
||||
"""Get homepage configuration settings without authentication."""
|
||||
def get_homepage_config(
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
try:
|
||||
# Create service instance
|
||||
sys_config_service = SysConfigService(db)
|
||||
|
||||
@@ -90,7 +90,9 @@ def update_mcp_service_info(request: Request, body: dict = Body(...), mcp_manage
|
||||
|
||||
|
||||
@router.delete("/service", summary="Delete MCP service")
|
||||
def delete_mcp_service(body: dict = Body(...), mcp_manager_service: McpManagerService = Depends(get_mcp_manager)):
|
||||
def delete_mcp_service(request: Request, body: dict = Body(...), mcp_manager_service: McpManagerService = Depends(get_mcp_manager)):
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
"""Delete MCP service by ID."""
|
||||
id = body.get("id")
|
||||
if not id:
|
||||
@@ -102,11 +104,14 @@ def delete_mcp_service(body: dict = Body(...), mcp_manager_service: McpManagerSe
|
||||
|
||||
@router.post("/openapi_parse", summary="openapi import", response_model=dict)
|
||||
async def openapi_parse(
|
||||
request: Request,
|
||||
url: Optional[HttpUrl] = Form(None, description="OpenAPI document URL (optional)"),
|
||||
file: Optional[UploadFile] = File(None, description="OpenAPI document file (JSON/YAML, optional)"),
|
||||
mcp_manager_service: McpManagerService = Depends(get_mcp_manager),
|
||||
):
|
||||
"""Parse and import OpenAPI document from URL or file upload."""
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
try:
|
||||
if url:
|
||||
url_str = str(url)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
from os import stat
|
||||
from typing import Optional, List
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from services.common.models.user_wallet_history import TransactionType
|
||||
from fastapi import APIRouter, Depends, Query, Request
|
||||
from sqlalchemy import table
|
||||
from sqlalchemy.orm import Session
|
||||
from services.common.database import get_db
|
||||
from services.common.utils.response_utils import ResponseUtils
|
||||
from services.admin_service.services.user_service import UserService
|
||||
from services.admin_service.services.user_wallet_history_service import UserWalletHistoryService
|
||||
from services.admin_service.utils.user_utils import UserUtils
|
||||
from services.common import error_msg
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -22,6 +22,7 @@ def get_user_service(db: Session = Depends(get_db)) -> UserService:
|
||||
|
||||
@router.get("/list", summary="Get user order list")
|
||||
def get_user_order_list(
|
||||
request: Request,
|
||||
page: int = Query(1, description="Current page number"),
|
||||
page_size: int = Query(15, description="Number of items per page"),
|
||||
keyword: str = Query("", description="Search keyword for user email or name"),
|
||||
@@ -30,6 +31,8 @@ def get_user_order_list(
|
||||
user_service: UserService = Depends(get_user_service),
|
||||
user_wallet_history_service: UserWalletHistoryService = Depends(get_user_wallet_history_service),
|
||||
):
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
"""Get paginated list of user order history."""
|
||||
offset = (page - 1) * page_size
|
||||
if filter_payment_type:
|
||||
@@ -65,6 +68,7 @@ def get_user_order_list(
|
||||
|
||||
@router.get("/list/by_user", summary="Get user order list by user id")
|
||||
def get_user_order_list_by_user_id(
|
||||
request: Request,
|
||||
user_id: str = Query(..., description="User ID"),
|
||||
page: int = Query(1, description="Current page number"),
|
||||
page_size: int = Query(15, description="Number of items per page"),
|
||||
@@ -73,6 +77,8 @@ def get_user_order_list_by_user_id(
|
||||
user_service: UserService = Depends(get_user_service),
|
||||
user_wallet_history_service: UserWalletHistoryService = Depends(get_user_wallet_history_service),
|
||||
):
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
"""Get paginated list of user order history by user id."""
|
||||
user = user_service.get_by_id(user_id)
|
||||
if not user:
|
||||
|
||||
@@ -7,6 +7,8 @@ from services.common.utils.response_utils import ResponseUtils
|
||||
from services.admin_service.services.payment_service import PaymentService
|
||||
from services.admin_service.services.user_wallet_history_service import UserWalletHistoryService
|
||||
from services.admin_service.tasks.order_monitor_task import OrderMonitorTask
|
||||
from services.admin_service.utils.user_utils import UserUtils
|
||||
from services.common import error_msg
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@@ -32,6 +34,8 @@ def create_payment_link(
|
||||
payment: PaymentService = Depends(get_payment),
|
||||
):
|
||||
"""Create Stripe payment link for the user."""
|
||||
if not UserUtils.is_normal_user(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
user = request.scope.get("user")
|
||||
if not user:
|
||||
return ResponseUtils.error(message="User not found", code=404)
|
||||
@@ -86,10 +90,13 @@ async def callback_stripe(
|
||||
|
||||
@router.get("/order_status", response_model=dict)
|
||||
async def order_status(
|
||||
request: Request,
|
||||
payment_id: str,
|
||||
get_user_wallet_history: UserWalletHistoryService = Depends(get_user_wallet_history),
|
||||
):
|
||||
"""Check payment order completion status."""
|
||||
if not UserUtils.is_normal_user(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
if get_user_wallet_history.check_order_complete(payment_id):
|
||||
return ResponseUtils.success({"status": 1})
|
||||
return ResponseUtils.success({"status": 0})
|
||||
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, Depends, Query, Body
|
||||
from fastapi import APIRouter, Depends, Query, Body, Request
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from services.common.database import get_db
|
||||
@@ -6,7 +6,8 @@ from services.common.utils.response_utils import ResponseUtils
|
||||
from services.admin_service.services.payment_channel_service import PaymentChannelService
|
||||
from services.common.utils.secure_config import decrypt_config, encrypt_config
|
||||
from services.admin_service.tasks.order_monitor_task import OrderMonitorTask
|
||||
import json
|
||||
from services.admin_service.utils.user_utils import UserUtils
|
||||
from services.common import error_msg
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -17,10 +18,13 @@ def get_payment_channel(db: Session = Depends(get_db)) -> PaymentChannelService:
|
||||
|
||||
@router.get("/list")
|
||||
async def payment_channel_list(
|
||||
request: Request,
|
||||
page: int = Query(1, description="Current page number"),
|
||||
page_size: int = Query(15, description="Number of items per page"),
|
||||
payment_channel_service: PaymentChannelService = Depends(get_payment_channel),
|
||||
):
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
"""Get paginated list of payment channels."""
|
||||
total, list = payment_channel_service.list()
|
||||
if total == 0:
|
||||
@@ -45,9 +49,12 @@ async def payment_channel_list(
|
||||
|
||||
@router.put("/enable")
|
||||
async def payment_channel_enable(
|
||||
request: Request,
|
||||
payment_channel_service: PaymentChannelService = Depends(get_payment_channel),
|
||||
body: dict = Body(..., description="Payment channel configuration"),
|
||||
):
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
"""Enable a payment channel by ID."""
|
||||
id = body.get("id")
|
||||
if not id:
|
||||
@@ -103,9 +110,12 @@ async def payment_channel_disable(
|
||||
|
||||
@router.put("/info")
|
||||
async def payment_channel_config(
|
||||
request: Request,
|
||||
payment_channel_service: PaymentChannelService = Depends(get_payment_channel),
|
||||
body: dict = Body(..., description="Payment channel configuration"),
|
||||
):
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
"""Update payment channel configuration."""
|
||||
id = body.get("id")
|
||||
if not id:
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
Provides endpoints to read and update platform configurations, homepage content,
|
||||
and to test email settings. All responses and comments are standardized in English.
|
||||
"""
|
||||
from fastapi import APIRouter, Depends, Body
|
||||
from fastapi import APIRouter, Depends, Body, Request
|
||||
import json
|
||||
from sqlalchemy.orm import Session
|
||||
from services.common.database import get_db
|
||||
@@ -11,6 +11,8 @@ from services.common.utils.response_utils import ResponseUtils
|
||||
from services.common.utils.email_utils import EmailUtils
|
||||
from services.admin_service.services.sys_config_service import SysConfigService
|
||||
from services.admin_service.services.user_service import UserService
|
||||
from services.admin_service.utils.user_utils import UserUtils
|
||||
from services.common import error_msg
|
||||
from services.admin_service.services.platform_report_service import PlatformReportService
|
||||
from services.admin_service.constants import sys_config_key
|
||||
from services.common.logging_config import get_logger
|
||||
@@ -91,9 +93,12 @@ def update_homepage(
|
||||
|
||||
@router.get("/info")
|
||||
def get_sysconfig(
|
||||
request: Request,
|
||||
sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
|
||||
user_service: UserService = Depends(get_user_service),
|
||||
):
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
"""Get all system configuration settings."""
|
||||
admin_user = user_service.get_admin_user()
|
||||
# Batch read configurations (small table)
|
||||
@@ -173,10 +178,13 @@ def get_sysconfig(
|
||||
|
||||
@router.put("/info")
|
||||
def set_sysconfig(
|
||||
request: Request,
|
||||
body: dict = Body(...),
|
||||
user_service: UserService = Depends(get_user_service),
|
||||
sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
|
||||
):
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
"""Update system configuration settings."""
|
||||
try:
|
||||
platform_name = ""
|
||||
@@ -334,7 +342,7 @@ def set_sysconfig(
|
||||
# Log error but continue with main operation - reporting failure should not affect configuration update
|
||||
logger.warning(f"Failed to initiate platform reporting: {e}")
|
||||
|
||||
return get_sysconfig(sysconfig_service, user_service)
|
||||
return get_sysconfig(request, sysconfig_service, user_service)
|
||||
|
||||
except Exception as e:
|
||||
return ResponseUtils.error(f"Failed to update system configuration: {str(e)}")
|
||||
|
||||
@@ -3,7 +3,10 @@ import hashlib
|
||||
import os
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
from fastapi import Request
|
||||
from services.common.utils.response_utils import ResponseUtils
|
||||
from services.admin_service.utils.user_utils import UserUtils
|
||||
from services.common import error_msg
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -14,6 +17,7 @@ and store files under date-based directories.
|
||||
"""
|
||||
@router.post("/image")
|
||||
async def upload(
|
||||
request: Request,
|
||||
img: UploadFile = File(None),
|
||||
sha256: str = Form(None),
|
||||
):
|
||||
@@ -24,6 +28,8 @@ async def upload(
|
||||
- Saves the file under `uploads/images/<YYYYMMDD>/`
|
||||
- Returns the relative `file_path`
|
||||
"""
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
# 1. Get uploaded file name
|
||||
if not img:
|
||||
return ResponseUtils.error(message="No file provided", code=400)
|
||||
|
||||
@@ -11,6 +11,9 @@ from services.admin_service.services.user_task_service import UserTaskService
|
||||
from services.admin_service.services.auth_service import AuthService
|
||||
from services.admin_service.services.resource_group_service import ResourceGroupService
|
||||
from services.admin_service.services.user_wallet_history_service import UserWalletHistoryService
|
||||
from services.common import error_msg
|
||||
|
||||
|
||||
|
||||
from typing import Optional,List
|
||||
|
||||
|
||||
@@ -6,6 +6,9 @@ from services.common.response.apikey_response import ApikeyResponse
|
||||
from services.admin_service.services.user_apikey_service import UserApiKeyService
|
||||
from services.admin_service.utils.user_utils import UserUtils
|
||||
from services.common.models.user_apikey import UserApiKey
|
||||
from services.common import error_msg
|
||||
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -29,6 +32,8 @@ def convert_to_apikey_response(user_apikey: UserApiKey) -> ApikeyResponse:
|
||||
@router.post("/info", summary="add apikey")
|
||||
def add_apikey(request: Request, apikey_service: UserApiKeyService = Depends(get_apikey_service), body: dict = Body(...)):
|
||||
"""Create a new API key for the user."""
|
||||
if not UserUtils.is_normal_user(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
user_id = UserUtils.get_request_user_id(request)
|
||||
name = body.get("name")
|
||||
if name is None:
|
||||
@@ -46,6 +51,8 @@ def add_apikey(request: Request, apikey_service: UserApiKeyService = Depends(get
|
||||
@router.get("/list", summary="get user apikey list")
|
||||
def get_user_apikey_list(request: Request, apikey_service: UserApiKeyService = Depends(get_apikey_service)):
|
||||
"""Get list of API keys for the current user."""
|
||||
if not UserUtils.is_normal_user(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
user_id = UserUtils.get_request_user_id(request)
|
||||
apikey_list = apikey_service.get_by_user_id(user_id)
|
||||
data = [convert_to_apikey_response(item) for item in apikey_list]
|
||||
@@ -55,6 +62,8 @@ def get_user_apikey_list(request: Request, apikey_service: UserApiKeyService = D
|
||||
@router.delete("/info", summary="delete user apikey")
|
||||
def delete_apikey(request: Request, apikey_service: UserApiKeyService = Depends(get_apikey_service), body: dict = Body(...)):
|
||||
"""Delete an API key by ID for the current user."""
|
||||
if not UserUtils.is_normal_user(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
user_id = UserUtils.get_request_user_id(request)
|
||||
id = body.get("apikey_id")
|
||||
if id is None:
|
||||
@@ -68,6 +77,8 @@ def delete_apikey(request: Request, apikey_service: UserApiKeyService = Depends(
|
||||
@router.put("/info", summary="modify user apikey")
|
||||
def modify_apikey(request: Request, apikey_service: UserApiKeyService = Depends(get_apikey_service), body: dict = Body(...)):
|
||||
"""Update API key information for the current user."""
|
||||
if not UserUtils.is_normal_user(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
user_id = UserUtils.get_request_user_id(request)
|
||||
id = body.get("apikey_id")
|
||||
name = body.get("name")
|
||||
|
||||
@@ -63,11 +63,13 @@ async def update_user_resource_group(
|
||||
|
||||
@router.delete("/account", summary="Delete user account")
|
||||
async def delete_user(
|
||||
request: Request,
|
||||
id: str,
|
||||
user_service: UserService = Depends(get_user_service),
|
||||
):
|
||||
"""Delete user by ID."""
|
||||
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
# Validate user ID parameter
|
||||
ValidationUtils.require_non_empty_string(id, "id")
|
||||
|
||||
@@ -88,6 +90,7 @@ async def delete_user(
|
||||
|
||||
@router.get("/account/list", summary="Get user list with pagination")
|
||||
async def get_user_list(
|
||||
request: Request,
|
||||
page: int = Query(1, description="Page number (starts from 1)"),
|
||||
page_size: int = Query(15, description="Number of items per page"),
|
||||
keyword: str = Query(None, description="Search keyword for email or name"),
|
||||
@@ -95,7 +98,8 @@ async def get_user_list(
|
||||
user_wallet_service: UserWalletService = Depends(get_user_wallet_service),
|
||||
):
|
||||
"""Get paginated list of users with wallet information."""
|
||||
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
# Validate pagination parameters
|
||||
validated_page, validated_page_size = ValidationUtils.validate_pagination(page, page_size)
|
||||
|
||||
@@ -126,6 +130,8 @@ async def recharge_balance(
|
||||
request: Request,
|
||||
payment_service: PaymentService = Depends(get_payment_service),
|
||||
):
|
||||
if not UserUtils.is_admin(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
body = await request.body()
|
||||
body_str = body.decode('utf-8')
|
||||
timestamp=request.headers.get("x-xpack-timestamp")
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
User statistics controller
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi import APIRouter, Depends, Query, Request
|
||||
from sqlalchemy.orm import Session
|
||||
from services.common.database import get_db
|
||||
from services.common.utils.response_utils import ResponseUtils
|
||||
from services.admin_service.services.user_stats_service import UserStatsService
|
||||
from services.admin_service.utils.user_utils import UserUtils
|
||||
from services.common.logging_config import get_logger
|
||||
from services.common import error_msg
|
||||
|
||||
logger = get_logger(__name__)
|
||||
router = APIRouter()
|
||||
@@ -15,10 +17,13 @@ router = APIRouter()
|
||||
|
||||
@router.get("/key_call_tool")
|
||||
async def get_key_call_tool(
|
||||
request: Request,
|
||||
apikey_id: str = Query(..., description="apikey ID (not the apikey itself)"),
|
||||
last_day: int = Query(30, description="Days to query, default 30"),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
if not UserUtils.is_normal_user(request):
|
||||
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
||||
"""
|
||||
Get tool call statistics by apikey
|
||||
"""
|
||||
@@ -51,12 +56,12 @@ async def get_key_call_tool(
|
||||
|
||||
except ValueError as e:
|
||||
# Handle business logic errors (invalid apikey_id)
|
||||
error_msg = str(e)
|
||||
logger.error(f"Business logic error: {error_msg}")
|
||||
return ResponseUtils.error(error_msg, code=400)
|
||||
err_msg = str(e)
|
||||
logger.error(f"Business logic error: {err_msg}")
|
||||
return ResponseUtils.error(err_msg, code=400)
|
||||
|
||||
except Exception as e:
|
||||
# Handle unexpected errors
|
||||
error_msg = f"Failed to retrieve call tool statistics: {str(e)}"
|
||||
logger.error(f"Unexpected error in get_key_call_tool_count: {error_msg}", exc_info=True)
|
||||
err_msg = f"Failed to retrieve call tool statistics: {str(e)}"
|
||||
logger.error(f"Unexpected error in get_key_call_tool_count: {err_msg}", exc_info=True)
|
||||
return ResponseUtils.error("Internal server error occurred while retrieving statistics", code=500)
|
||||
@@ -11,17 +11,20 @@ class McpCallLogRepository:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def create(self, call_log: McpCallLog) -> McpCallLog:
|
||||
def create(self, call_log: McpCallLog, commit: bool = True) -> McpCallLog:
|
||||
self.db.add(call_log)
|
||||
self.db.commit()
|
||||
self.db.refresh(call_log)
|
||||
if commit:
|
||||
self.db.commit()
|
||||
self.db.refresh(call_log)
|
||||
else:
|
||||
self.db.flush()
|
||||
return call_log
|
||||
|
||||
def get_by_id(self, log_id: str) -> Optional[McpCallLog]:
|
||||
return self.db.query(McpCallLog).filter(McpCallLog.id == log_id).first()
|
||||
|
||||
def update_status(
|
||||
self, log_id: str, status: ProcessStatus, error_msg: Optional[str] = None, wallet_history_id: Optional[str] = None
|
||||
self, log_id: str, status: ProcessStatus, error_msg: Optional[str] = None, wallet_history_id: Optional[str] = None, commit: bool = True
|
||||
) -> bool:
|
||||
log_record = self.get_by_id(log_id)
|
||||
if log_record:
|
||||
@@ -30,7 +33,10 @@ class McpCallLogRepository:
|
||||
log_record.error_msg = error_msg
|
||||
if wallet_history_id:
|
||||
log_record.wallet_history_id = wallet_history_id
|
||||
self.db.commit()
|
||||
if commit:
|
||||
self.db.commit()
|
||||
else:
|
||||
self.db.flush()
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from datetime import date, datetime, timedelta
|
||||
from typing import Optional, List, Tuple, cast
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func, literal_column
|
||||
from sqlalchemy import Boolean, func, literal_column
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.dialects.mysql import insert as mysql_insert
|
||||
from sqlalchemy.sql.schema import Table
|
||||
@@ -91,14 +91,12 @@ class StatsMcpServiceDateRepository:
|
||||
total = query.scalar()
|
||||
return int(total or 0)
|
||||
|
||||
def increment(self, service_id: str, stats_date: datetime, inc: int = 1) -> None:
|
||||
def increment(self, service_id: str, stats_date: datetime, inc: int = 1, commit: bool = True):
|
||||
"""Atomically increment call_count for (service_id, stats_date) under high concurrency.
|
||||
|
||||
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__)
|
||||
@@ -114,7 +112,10 @@ class StatsMcpServiceDateRepository:
|
||||
updated_at=func.current_timestamp(),
|
||||
)
|
||||
self.db.execute(ondup)
|
||||
self.db.commit()
|
||||
if commit:
|
||||
self.db.commit()
|
||||
else:
|
||||
self.db.flush()
|
||||
|
||||
def increment_fallback(self, service_id: str, stats_date: datetime, inc: int = 1) -> None:
|
||||
"""Fallback increment strategy without dialect-specific upsert.
|
||||
|
||||
@@ -15,8 +15,8 @@ class UserRepository:
|
||||
def get_by_email(self, email: str) -> Optional[User]:
|
||||
return self.db.query(User).filter(User.email == email).first()
|
||||
|
||||
def get_by_account(self, name: str) -> Optional[User]:
|
||||
return self.db.query(User).filter(User.name == name).first()
|
||||
def get_by_account(self, name: str, role_id: int = 1) -> Optional[User]:
|
||||
return self.db.query(User).filter(User.name == name, User.role_id == role_id).first()
|
||||
|
||||
def update_password(self,user_id:str, password:str) -> Optional[User]:
|
||||
user = self.get_by_id(user_id)
|
||||
|
||||
@@ -296,7 +296,7 @@ class UserWalletHistoryRepository:
|
||||
)
|
||||
return total, history
|
||||
|
||||
def add_consume_record(self, wallet_history: UserWalletHistory) -> Optional[UserWalletHistory]:
|
||||
def add_consume_record(self, wallet_history: UserWalletHistory, commit: bool = True) -> Optional[UserWalletHistory]:
|
||||
"""
|
||||
Add consume record to database.
|
||||
|
||||
@@ -310,12 +310,18 @@ class UserWalletHistoryRepository:
|
||||
# Add the wallet history record to database
|
||||
# The created_at and updated_at fields should be set by the caller
|
||||
self.db.add(wallet_history)
|
||||
self.db.commit()
|
||||
self.db.refresh(wallet_history)
|
||||
if commit:
|
||||
self.db.commit()
|
||||
self.db.refresh(wallet_history)
|
||||
else:
|
||||
self.db.flush()
|
||||
return wallet_history
|
||||
except Exception as e:
|
||||
self.logging.error(f"Failed to add consume record: {str(e)}")
|
||||
self.db.rollback()
|
||||
try:
|
||||
self.db.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
def order_list(self, payment_method: str, status: int, start: Optional[datetime] = None, end: Optional[datetime] = None) -> List[UserWalletHistory]:
|
||||
@@ -435,4 +441,4 @@ class UserWalletHistoryRepository:
|
||||
result.append({"stats_day": current_day, "count": amounts_by_day.get(current_day, 0.0)})
|
||||
current_day += timedelta(days=1)
|
||||
|
||||
return result
|
||||
return result
|
||||
@@ -32,13 +32,16 @@ class UserWalletRepository:
|
||||
"""Get user wallet with row-level lock for update"""
|
||||
return self.db.query(UserWallet).filter(UserWallet.user_id == user_id).with_for_update().first()
|
||||
|
||||
def update_balance(self, user_id: str, new_balance: float) -> bool:
|
||||
def update_balance(self, user_id: str, new_balance: float, commit: bool = True) -> bool:
|
||||
"""Update user balance"""
|
||||
wallet = self.get_by_user_id(user_id)
|
||||
if wallet:
|
||||
wallet.balance = new_balance
|
||||
wallet.updated_at = datetime.now(timezone.utc)
|
||||
self.db.commit()
|
||||
if commit:
|
||||
self.db.commit()
|
||||
else:
|
||||
self.db.flush()
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ Billing message handler service
|
||||
"""
|
||||
|
||||
import json
|
||||
from re import finditer
|
||||
import uuid
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
@@ -44,41 +45,60 @@ class BillingMessageHandler:
|
||||
Returns:
|
||||
bool: Whether processing was successful
|
||||
"""
|
||||
call_log_id: Optional[str] = None
|
||||
tx = self.db.begin()
|
||||
try:
|
||||
# Parse message
|
||||
billing_message = self._parse_message(message_data)
|
||||
if not billing_message:
|
||||
tx.rollback()
|
||||
return False
|
||||
|
||||
# Create API call log
|
||||
call_log_id = self._create_call_log(billing_message)
|
||||
if not call_log_id:
|
||||
tx.rollback()
|
||||
return False
|
||||
|
||||
# Update stats_mcp_service_date record
|
||||
# Bucket stats by hour (UTC), aligned to full hours
|
||||
|
||||
start_dt = billing_message.call_start_time
|
||||
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)
|
||||
stats_date = start_utc.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
|
||||
self.stats_repo.increment(billing_message.service_id, stats_date, 1, commit=False)
|
||||
|
||||
# Process billing logic
|
||||
if billing_message.call_success and billing_message.unit_price > 0:
|
||||
success = self._process_billing(billing_message, call_log_id)
|
||||
success = self._process_billing(billing_message, call_log_id, commit=False)
|
||||
if not success:
|
||||
# Update record status to failed
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing failed")
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing failed", commit=False)
|
||||
tx.rollback()
|
||||
return False
|
||||
|
||||
# Update record status to processed
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED)
|
||||
logger.info(f"Billing message processed successfully - User ID: {billing_message.user_id}, Tool: {billing_message.tool_name}")
|
||||
return True
|
||||
|
||||
if not self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, commit=False):
|
||||
tx.rollback()
|
||||
return False
|
||||
tx.commit()
|
||||
logger.info(f"Billing message processed successfully - User ID: {billing_message.user_id}, Tool: {billing_message.tool_name}")
|
||||
# Update wallet cache after successful commit
|
||||
try:
|
||||
wallet = self.wallet_repo.get_by_user_id(billing_message.user_id)
|
||||
if wallet:
|
||||
self._update_wallet_cache(billing_message.user_id, Decimal(str(wallet.balance)))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to process billing message: {str(e)}", exc_info=True)
|
||||
try:
|
||||
tx.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
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
|
||||
finally:
|
||||
if tx.is_active:
|
||||
tx.rollback()
|
||||
|
||||
def _parse_message(self, message_data: dict) -> Optional[BillingMessage]:
|
||||
"""
|
||||
@@ -146,14 +166,14 @@ class BillingMessageHandler:
|
||||
updated_at=datetime.now(timezone.utc),
|
||||
)
|
||||
|
||||
created_log = self.call_log_repo.create(call_log)
|
||||
created_log = self.call_log_repo.create(call_log, commit=False)
|
||||
return created_log.id
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to create API call log record: {str(e)}")
|
||||
return None
|
||||
|
||||
def _process_billing(self, billing_message: BillingMessage, call_log_id: str) -> bool:
|
||||
def _process_billing(self, billing_message: BillingMessage, call_log_id: str, commit: bool = True) -> bool:
|
||||
"""
|
||||
Process actual billing logic
|
||||
|
||||
@@ -183,7 +203,7 @@ class BillingMessageHandler:
|
||||
|
||||
# Execute deduction
|
||||
new_balance = current_balance - amount
|
||||
success = self.wallet_repo.update_balance(user_id, float(new_balance))
|
||||
success = self.wallet_repo.update_balance(user_id, float(new_balance), commit=commit)
|
||||
if not success:
|
||||
logger.error(f"Failed to update user wallet balance - User ID: {user_id}")
|
||||
return False
|
||||
@@ -206,22 +226,15 @@ class BillingMessageHandler:
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
created_history = self.wallet_history_repo.add_consume_record(wallet_history)
|
||||
created_history = self.wallet_history_repo.add_consume_record(wallet_history, commit=commit)
|
||||
if not created_history:
|
||||
logger.error(f"Failed to create wallet history record - User ID: {user_id}")
|
||||
# Rollback balance update
|
||||
self.wallet_repo.update_balance(user_id, float(current_balance))
|
||||
return False
|
||||
|
||||
logger.info(f"Wallet history record created successfully - User ID: {user_id}, History ID: {history_id}, Created at: {now}")
|
||||
|
||||
# Update API call record's actual deduction amount and associated history record ID
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, None, history_id)
|
||||
|
||||
|
||||
|
||||
# Update wallet balance cache in Redis
|
||||
self._update_wallet_cache(user_id, new_balance)
|
||||
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, None, history_id, commit=commit)
|
||||
|
||||
logger.info(
|
||||
f"Billing processing completed successfully - User ID: {user_id}, Amount deducted: {amount}, Balance: {current_balance} -> {new_balance}"
|
||||
|
||||
@@ -25,3 +25,9 @@ class UserUtils:
|
||||
"""Check if current user is admin"""
|
||||
user = UserUtils.get_request_user(request)
|
||||
return user and user.role_id == 1 if user else False
|
||||
|
||||
@staticmethod
|
||||
def is_normal_user(request) -> bool:
|
||||
"""Check if current user is normal user"""
|
||||
user = UserUtils.get_request_user(request)
|
||||
return user and user.role_id == 2 if user else False
|
||||
@@ -37,7 +37,7 @@ class UserApiKeyRepository:
|
||||
return cached_model
|
||||
|
||||
# Query from database if not in cache
|
||||
user_apikey = self.db.query(UserApiKey).filter(UserApiKey.apikey == apikey).first()
|
||||
user_apikey = self.db.query(UserApiKey).filter(UserApiKey.apikey == apikey, UserApiKey.is_deleted == 0).first()
|
||||
if user_apikey:
|
||||
# Cache the result using new SQLAlchemy-specific method
|
||||
CacheUtils.set_sqlalchemy_cache(cache_key, user_apikey, 300)
|
||||
|
||||
@@ -45,7 +45,9 @@ class RedisEventStore(EventStore):
|
||||
|
||||
async def store_event(self, stream_id: StreamId, message: JSONRPCMessage) -> EventId:
|
||||
key = self._key(stream_id)
|
||||
payload = json.dumps(message.model_dump(by_alias=True, exclude_none=True))
|
||||
payload = json.dumps(
|
||||
message.model_dump(by_alias=True, exclude_none=True) if message is not None else {}
|
||||
)
|
||||
# Append to Redis list via thread wrapper to avoid blocking
|
||||
seq: int = await asyncio.to_thread(self._rpush, key, payload)
|
||||
# rpush returns new length; sequence index is length-1
|
||||
|
||||
@@ -30,7 +30,7 @@ class RabbitMQClient:
|
||||
logger.error(f"Failed to establish RabbitMQ connection: {str(e)}")
|
||||
raise
|
||||
|
||||
def publish(self, queue: str, message: str, persistent: bool = True):
|
||||
def publish(self, queue: str, message: str, persistent: bool = True, headers: dict | None = None):
|
||||
"""Publish message to queue"""
|
||||
try:
|
||||
# Ensure connection is available
|
||||
@@ -46,7 +46,9 @@ class RabbitMQClient:
|
||||
# Publish message
|
||||
properties = None
|
||||
if persistent:
|
||||
properties = pika.BasicProperties(delivery_mode=2)
|
||||
properties = pika.BasicProperties(delivery_mode=2, headers=headers or None)
|
||||
elif headers:
|
||||
properties = pika.BasicProperties(headers=headers)
|
||||
|
||||
self.channel.basic_publish(exchange="", routing_key=queue, body=message, properties=properties)
|
||||
logger.info(f"Message successfully published to queue: {queue}, message length: {len(message)} bytes")
|
||||
@@ -61,7 +63,10 @@ class RabbitMQClient:
|
||||
|
||||
# Retry sending
|
||||
self.channel.queue_declare(queue=queue, durable=True)
|
||||
properties = pika.BasicProperties(delivery_mode=2) if persistent else None
|
||||
if persistent:
|
||||
properties = pika.BasicProperties(delivery_mode=2, headers=headers or None)
|
||||
else:
|
||||
properties = pika.BasicProperties(headers=headers or None)
|
||||
self.channel.basic_publish(exchange="", routing_key=queue, body=message, properties=properties)
|
||||
logger.info(f"Message retry successful to queue {queue}")
|
||||
except Exception as retry_error:
|
||||
|
||||
@@ -33,7 +33,6 @@ def verify_token(token: str, db: Session) -> Optional[User]:
|
||||
set_model_cache(token_cache_key, user_access_token, TOKEN_CACHE_EXPIRE_TIME)
|
||||
|
||||
logging.info(f"query token info")
|
||||
|
||||
# Check if token is expired
|
||||
expire_at = user_access_token.expire_at
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user