用量与分析
ONE SDK 提供了专用的 UsageService,用于跟踪 API 消耗、监控配额和查看用量分析。您可以在仪表板中查看用量数据,也可以通过编程方式查询。
创建 UsageService
SDK 提供了两个工厂函数来创建该服务。
createUsageService
使用显式配置创建新的 UsageService 实例。
import { createUsageService } from '@one_deploy/sdk';
const usageService = createUsageService({
engineUrl: process.env.ONE_ENGINE_URL!,
clientId: process.env.ONE_CLIENT_ID!,
secretKey: process.env.ONE_SECRET_KEY!,
projectId: 'proj_abc123',
});
getUsageService
获取一个单例 UsageService 实例,它会复用当前 OneEngineClient 上下文中的配置。
import { getUsageService } from '@one_deploy/sdk';
const usageService = getUsageService();
提示
在 React 组件中使用 getUsageService() 时,服务会自动从最近的 OneProvider 继承配置。无需额外设置。
类型
UsageCategory
对正在跟踪的 API 调用类型进行分类。
type UsageCategory =
| 'auth'
| 'wallet'
| 'swap'
| 'onramp'
| 'offramp'
| 'ai_trading'
| 'forex'
| 'contracts'
| 'nfts'
| 'billing'
| 'staking'
| 'bridge'
| 'gas'
| 'price'
| 'webhooks'
| 'admin'
| 'project';
DisplayCategory
用于仪表板 UI 图表和表格的更高级别分组。
type DisplayCategory =
| 'Authentication'
| 'Wallet & Assets'
| 'Payments'
| 'Trading'
| 'Infrastructure'
| 'Management';
UsageRecord
表示一个按时间分桶的计数数据点。
interface UsageRecord {
id: string;
projectId: string;
category: UsageCategory;
count: number;
timestamp: string; // ISO-8601
metadata?: Record<string, unknown>;
}
UsageSummary
指定时间范围内项目的汇总用量总计。
interface UsageSummary {
projectId: string;
totalCalls: number;
byCategory: Record<UsageCategory, number>;
byDisplayCategory: Record<DisplayCategory, number>;
periodStart: string; // ISO-8601
periodEnd: string; // ISO-8601
quotaLimit: number;
quotaUsed: number;
quotaRemaining: number;
}
UsageActivity
用量动态的带时间戳的活动条目。
interface UsageActivity {
id: string;
projectId: string;
category: UsageCategory;
action: string; // e.g. "swap.execute", "wallet.getBalance"
timestamp: string; // ISO-8601
statusCode: number;
durationMs: number;
metadata?: Record<string, unknown>;
}
UsageResponse
用量查询方法返回的标准封装。
interface UsageResponse<T> {
success: boolean;
data?: T;
error?: string;
meta?: {
page: number;
limit: number;
total: number;
};
}