feat: add admin model usage audit
This commit is contained in:
1 parent
473479fc4c
commit
13f7fbbfcb
11 files changed
+566
-4
No files matched your search
@@ -309,6 +309,7 @@ public class CatalogService {
|
||||
UUID id = resultSet.getObject("id", UUID.class);
|
||||
String projectId = resultSet.getString("project_id");
|
||||
key.put("id", id);
|
||||
key.put("userId", resultSet.getObject("user_id", UUID.class));
|
||||
key.put("scopeType", projectId == null ? "personal" : "project");
|
||||
putIfNotNull(key, "projectId", projectId);
|
||||
key.put("name", resultSet.getString("name"));
|
||||
|
||||
+21
-2
@@ -3,6 +3,7 @@ package com.moyi.controlplane.gateway;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -23,11 +24,13 @@ public class GatewayController {
|
||||
private final CatalogService catalogService;
|
||||
private final OpenAiCompatibleProvider provider;
|
||||
private final GatewayGuard guard;
|
||||
private final UsageAuditService usage;
|
||||
|
||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard) {
|
||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard, UsageAuditService usage) {
|
||||
this.catalogService = catalogService;
|
||||
this.provider = provider;
|
||||
this.guard = guard;
|
||||
this.usage = usage;
|
||||
}
|
||||
|
||||
@GetMapping("/models")
|
||||
@@ -55,6 +58,7 @@ public class GatewayController {
|
||||
@RequestBody Map<String, Object> input
|
||||
) {
|
||||
Map<String, Object> key = requireKey(authorization);
|
||||
long startedAt = System.currentTimeMillis();
|
||||
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> model = catalogService.activeModel(modelId);
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||
@@ -63,7 +67,13 @@ public class GatewayController {
|
||||
if (Boolean.TRUE.equals(input.get("stream"))) throw new ApiException("STREAM_NOT_IMPLEMENTED", "流式转发将在下一节点开放", 501);
|
||||
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||
try (GatewayGuard.Slot slot = guard.acquire(model)) {
|
||||
return invoke(key, modelId, model, input);
|
||||
ResponseEntity<Map<String, Object>> result = invoke(key, modelId, model, input);
|
||||
Map<String, Object> body = result.getBody();
|
||||
usage.recordCall(UUID.fromString(String.valueOf(key.get("userId"))), UUID.fromString(String.valueOf(key.get("id"))), modelId, String.valueOf(model.get("providerId")), "succeeded", (int) (System.currentTimeMillis() - startedAt), number(body, "usage.prompt_tokens"), number(body, "usage.completion_tokens"), input, body, null, null);
|
||||
return result;
|
||||
} catch (ApiException exception) {
|
||||
usage.recordCall(UUID.fromString(String.valueOf(key.get("userId"))), UUID.fromString(String.valueOf(key.get("id"))), modelId, String.valueOf(model.get("providerId")), "failed", (int) (System.currentTimeMillis() - startedAt), 0, 0, input, null, exception.getCode(), exception.getMessage());
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,4 +113,13 @@ public class GatewayController {
|
||||
return content == null ? "" : String.valueOf(content);
|
||||
}).reduce((left, right) -> left + "\n" + right).orElse("");
|
||||
}
|
||||
|
||||
private long number(Map<String, Object> body, String path) {
|
||||
if (body == null) return 0;
|
||||
Object usageValue = body.get("usage");
|
||||
if (!(usageValue instanceof Map<?, ?> map)) return 0;
|
||||
String key = path.substring(path.indexOf('.') + 1);
|
||||
Object value = map.get(key);
|
||||
return value instanceof Number number ? number.longValue() : 0;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.OffsetDateTime;
|
||||
@@ -27,13 +28,15 @@ public class AsyncTaskService {
|
||||
private final CatalogService catalogService;
|
||||
private final int maxQueuedPerModel;
|
||||
private final GatewayGuard gatewayGuard;
|
||||
private final UsageAuditService usage;
|
||||
|
||||
public AsyncTaskService(
|
||||
JdbcTemplate jdbc,
|
||||
ObjectMapper objectMapper,
|
||||
CatalogService catalogService,
|
||||
@Value("${moyi.tasks.max-queued-per-model:100}") int maxQueuedPerModel,
|
||||
GatewayGuard gatewayGuard
|
||||
GatewayGuard gatewayGuard,
|
||||
UsageAuditService usage
|
||||
) {
|
||||
if (maxQueuedPerModel < 1) throw new IllegalArgumentException("Task queue limit must be positive");
|
||||
this.jdbc = jdbc;
|
||||
@@ -41,6 +44,7 @@ public class AsyncTaskService {
|
||||
this.catalogService = catalogService;
|
||||
this.maxQueuedPerModel = maxQueuedPerModel;
|
||||
this.gatewayGuard = gatewayGuard;
|
||||
this.usage = usage;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -64,6 +68,7 @@ public class AsyncTaskService {
|
||||
"INSERT INTO model_tasks (id, user_id, api_key_id, model_id, task_type, provider_task_id, status, progress, prompt, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 'mock', 'queued', 0, ?, ?, ?)",
|
||||
id, userId, apiKeyId, modelId, taskType, prompt, now, now
|
||||
);
|
||||
usage.recordTask(userId, apiKeyId, modelId, String.valueOf(model.get("providerId")), id, "queued", input, null, null, null);
|
||||
catalogService.touchKey(key);
|
||||
return taskView(id);
|
||||
}
|
||||
@@ -93,11 +98,13 @@ public class AsyncTaskService {
|
||||
|
||||
@Scheduled(fixedDelayString = "${moyi.tasks.worker-interval-ms:1000}")
|
||||
public void completeMockTasks() {
|
||||
List<String> taskIds = jdbc.query("SELECT id FROM model_tasks WHERE status IN ('queued', 'running') AND provider_task_id = 'mock' AND created_at < now() - INTERVAL '500 milliseconds'", (rs, row) -> rs.getString("id"));
|
||||
jdbc.update(
|
||||
"UPDATE model_tasks SET status = 'succeeded', progress = 100, result = CAST(CASE WHEN task_type = '3d' THEN ? ELSE ? END AS jsonb), updated_at = ?, completed_at = ? WHERE status IN ('queued', 'running') AND provider_task_id = 'mock' AND created_at < now() - INTERVAL '500 milliseconds'",
|
||||
"{\"format\":\"glb\",\"url\":\"https://mock.moyi.ai/tasks/result.glb\"}",
|
||||
"{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now()
|
||||
);
|
||||
taskIds.forEach(taskId -> usage.updateTask(taskId, "succeeded", Map.of("format", "result", "url", "https://mock.moyi.ai/tasks/" + taskId + "/result"), null, null));
|
||||
}
|
||||
|
||||
private Map<String, Object> requireKey(String authorization) {
|
||||
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package com.moyi.controlplane.usage;
|
||||
|
||||
import com.moyi.controlplane.auth.AuthController;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/usage")
|
||||
public class UsageAuditController {
|
||||
private final UsageAuditService usage;
|
||||
private final AuthService auth;
|
||||
|
||||
public UsageAuditController(UsageAuditService usage, AuthService auth) {
|
||||
this.usage = usage;
|
||||
this.auth = auth;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Map<String, Object> list(
|
||||
jakarta.servlet.http.HttpServletRequest request,
|
||||
@RequestParam(required = false) String q,
|
||||
@RequestParam(required = false) String keyId,
|
||||
@RequestParam(required = false) String modelId,
|
||||
@RequestParam(required = false) String status,
|
||||
@RequestParam(required = false) String eventKind,
|
||||
@RequestParam(required = false) OffsetDateTime from,
|
||||
@RequestParam(required = false) OffsetDateTime to,
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
@RequestParam(defaultValue = "50") int size
|
||||
) {
|
||||
return usage.listAdmin(AuthController.sessionToken(request), filters(q, keyId, modelId, status, eventKind, from, to, page, size), auth);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Map<String, Object> detail(@PathVariable String id, jakarta.servlet.http.HttpServletRequest request) {
|
||||
return Map.of("record", usage.detail(AuthController.sessionToken(request), id, auth));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public Map<String, Object> update(
|
||||
@PathVariable String id,
|
||||
@RequestBody Map<String, Object> input,
|
||||
jakarta.servlet.http.HttpServletRequest request
|
||||
) {
|
||||
usage.updateNote(AuthController.sessionToken(request), id, input.get("adminNote") == null ? null : String.valueOf(input.get("adminNote")), auth, AuthController.meta(request));
|
||||
return usage.detail(AuthController.sessionToken(request), id, auth);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> delete(@PathVariable String id, jakarta.servlet.http.HttpServletRequest request) {
|
||||
usage.delete(AuthController.sessionToken(request), id, auth, AuthController.meta(request));
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public Map<String, Object> deleteMany(@RequestParam String ids, jakarta.servlet.http.HttpServletRequest request) {
|
||||
int count = usage.deleteMany(AuthController.sessionToken(request), Arrays.asList(ids.split(",")), auth, AuthController.meta(request));
|
||||
return Map.of("deleted", count);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/export", produces = "text/csv")
|
||||
public ResponseEntity<String> export(
|
||||
jakarta.servlet.http.HttpServletRequest request,
|
||||
@RequestParam(required = false) String q,
|
||||
@RequestParam(required = false) String keyId,
|
||||
@RequestParam(required = false) String modelId,
|
||||
@RequestParam(required = false) String status,
|
||||
@RequestParam(required = false) String eventKind,
|
||||
@RequestParam(required = false) OffsetDateTime from,
|
||||
@RequestParam(required = false) OffsetDateTime to
|
||||
) {
|
||||
UsageAuditService.Filters filters = filters(q, keyId, modelId, status, eventKind, from, to, 1, 5000);
|
||||
String csv = usage.exportCsv(AuthController.sessionToken(request), filters, auth);
|
||||
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"moyi-usage.csv\"").contentType(MediaType.parseMediaType("text/csv; charset=UTF-8")).body("\uFEFF" + csv);
|
||||
}
|
||||
|
||||
private UsageAuditService.Filters filters(String q, String keyId, String modelId, String status, String eventKind, OffsetDateTime from, OffsetDateTime to, int page, int size) {
|
||||
return new UsageAuditService.Filters(q, keyId, modelId, status, eventKind, from, to, page, size);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
package com.moyi.controlplane.usage;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class UsageAuditService {
|
||||
private static final int MAX_PAYLOAD_CHARS = 32_000;
|
||||
private final JdbcTemplate jdbc;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public UsageAuditService(JdbcTemplate jdbc, ObjectMapper objectMapper) {
|
||||
this.jdbc = jdbc;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public void recordCall(
|
||||
UUID userId, UUID apiKeyId, String modelId, String providerId, String status,
|
||||
int latencyMs, long inputUnits, long outputUnits, Object input, Object output,
|
||||
String errorCode, String errorMessage
|
||||
) {
|
||||
record(userId, apiKeyId, modelId, providerId, "call", status, latencyMs, inputUnits, outputUnits,
|
||||
input, output, null, errorCode, errorMessage, null, null);
|
||||
}
|
||||
|
||||
public void recordTask(
|
||||
UUID userId, UUID apiKeyId, String modelId, String providerId, String taskId,
|
||||
String status, Object input, Object output, String errorCode, String errorMessage
|
||||
) {
|
||||
record(userId, apiKeyId, modelId, providerId, "task", status, null, 0, 0,
|
||||
input, output, taskId, errorCode, errorMessage, null, null);
|
||||
}
|
||||
|
||||
public void updateTask(String taskId, String status, Object output, String errorCode, String errorMessage) {
|
||||
jdbc.update(
|
||||
"UPDATE usage_events SET status = ?, output_payload = CAST(? AS jsonb), error_code = ?, error_message = ?, updated_at = ? WHERE provider_task_id = ? AND event_kind = 'task' AND deleted_at IS NULL",
|
||||
status, json(output), errorCode, errorMessage, now(), taskId
|
||||
);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Map<String, Object> listAdmin(String token, Filters filters, AuthService auth) {
|
||||
auth.requireAdmin(token);
|
||||
Query query = buildQuery(filters, false);
|
||||
List<Map<String, Object>> records = jdbc.query(query.sql(), this::mapRecord, query.params().toArray());
|
||||
Query countQuery = buildQuery(filters, true);
|
||||
Long total = jdbc.queryForObject(countQuery.sql(), Long.class, countQuery.params().toArray());
|
||||
int safeSize = filters.size();
|
||||
int safePage = filters.page();
|
||||
long totalValue = total == null ? 0 : total;
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("records", records);
|
||||
result.put("page", safePage);
|
||||
result.put("size", safeSize);
|
||||
result.put("total", totalValue);
|
||||
result.put("pages", Math.max(1, (int) Math.ceil((double) totalValue / safeSize)));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Map<String, Object> detail(String token, String id, AuthService auth) {
|
||||
auth.requireAdmin(token);
|
||||
try {
|
||||
return jdbc.queryForObject(baseSelect() + " WHERE ue.deleted_at IS NULL AND ue.id = ?", this::mapRecord, parseUuid(id));
|
||||
} catch (EmptyResultDataAccessException exception) {
|
||||
throw new ApiException("USAGE_NOT_FOUND", "调用记录不存在或已删除", 404);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateNote(String token, String id, String note, AuthService auth, AuthService.RequestMeta meta) {
|
||||
UUID adminId = auth.requireAdmin(token).id();
|
||||
String value = note == null ? "" : note.trim();
|
||||
if (value.length() > 1000) throw new ApiException("NOTE_TOO_LONG", "管理员备注不能超过 1000 个字符");
|
||||
int changed = jdbc.update("UPDATE usage_events SET admin_note = ?, updated_at = ? WHERE id = ? AND deleted_at IS NULL", blankToNull(value), now(), parseUuid(id));
|
||||
if (changed == 0) throw new ApiException("USAGE_NOT_FOUND", "调用记录不存在或已删除", 404);
|
||||
auth.audit("usage.note_updated", adminId, meta, Map.of("usageId", id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(String token, String id, AuthService auth, AuthService.RequestMeta meta) {
|
||||
UUID adminId = auth.requireAdmin(token).id();
|
||||
int changed = jdbc.update("UPDATE usage_events SET deleted_at = ?, deleted_by = ?, updated_at = ? WHERE id = ? AND deleted_at IS NULL", now(), adminId, now(), parseUuid(id));
|
||||
if (changed == 0) throw new ApiException("USAGE_NOT_FOUND", "调用记录不存在或已删除", 404);
|
||||
auth.audit("usage.deleted", adminId, meta, Map.of("usageId", id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public int deleteMany(String token, List<String> ids, AuthService auth, AuthService.RequestMeta meta) {
|
||||
UUID adminId = auth.requireAdmin(token).id();
|
||||
if (ids == null || ids.isEmpty()) return 0;
|
||||
if (ids.size() > 100) throw new ApiException("TOO_MANY_RECORDS", "单次最多删除 100 条记录");
|
||||
List<UUID> parsed = ids.stream().distinct().map(this::parseUuid).toList();
|
||||
int changed = 0;
|
||||
for (UUID id : parsed) {
|
||||
changed += jdbc.update("UPDATE usage_events SET deleted_at = ?, deleted_by = ?, updated_at = ? WHERE id = ? AND deleted_at IS NULL", now(), adminId, now(), id);
|
||||
}
|
||||
auth.audit("usage.bulk_deleted", adminId, meta, Map.of("count", changed));
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public String exportCsv(String token, Filters filters, AuthService auth) {
|
||||
auth.requireAdmin(token);
|
||||
Filters exportFilters = filters.withPage(1, Math.min(filters.size(), 5000));
|
||||
Query query = buildQuery(exportFilters, false);
|
||||
List<Map<String, Object>> records = jdbc.query(query.sql(), this::mapRecord, query.params().toArray());
|
||||
StringBuilder csv = new StringBuilder("记录 ID,时间,用户,分发 Key,模型,供应商,类型,状态,输入单位,输出单位,耗时(ms),错误,管理员备注,输入数据,输出数据\n");
|
||||
for (Map<String, Object> record : records) {
|
||||
csv.append(row(
|
||||
record.get("id"), record.get("createdAt"), record.get("userAccount"), record.get("keyName"),
|
||||
record.get("modelName"), record.get("providerName"), record.get("eventKind"), record.get("status"),
|
||||
record.get("inputUnits"), record.get("outputUnits"), record.get("latencyMs"), record.get("errorMessage"),
|
||||
record.get("adminNote"), record.get("input"), record.get("output")
|
||||
)).append('\n');
|
||||
}
|
||||
return csv.toString();
|
||||
}
|
||||
|
||||
public record Filters(
|
||||
String query, String keyId, String modelId, String status, String eventKind,
|
||||
OffsetDateTime from, OffsetDateTime to, int page, int size
|
||||
) {
|
||||
public Filters {
|
||||
page = Math.max(1, page);
|
||||
size = Math.max(1, Math.min(size, 100));
|
||||
}
|
||||
public Filters withPage(int newPage, int newSize) { return new Filters(query, keyId, modelId, status, eventKind, from, to, newPage, newSize); }
|
||||
}
|
||||
|
||||
private Query buildQuery(Filters filters, boolean count) {
|
||||
StringBuilder sql = new StringBuilder(count ? "SELECT COUNT(*) FROM usage_events ue LEFT JOIN users u ON u.id = ue.user_id LEFT JOIN platform_api_keys k ON k.id = ue.api_key_id LEFT JOIN model_catalog m ON m.id = ue.model_id LEFT JOIN model_providers p ON p.id = ue.provider_id" : baseSelect());
|
||||
if (!count) sql.append(" ");
|
||||
sql.append(" WHERE ue.deleted_at IS NULL");
|
||||
List<Object> params = new ArrayList<>();
|
||||
if (filters.query() != null && !filters.query().isBlank()) {
|
||||
sql.append(" AND (ue.id::text ILIKE ? OR u.account ILIKE ? OR u.display_name ILIKE ? OR k.name ILIKE ? OR k.key_prefix ILIKE ? OR m.public_name ILIKE ? OR m.id ILIKE ?)");
|
||||
String query = "%" + filters.query().trim() + "%";
|
||||
params.addAll(List.of(query, query, query, query, query, query, query));
|
||||
}
|
||||
if (filters.keyId() != null && !filters.keyId().isBlank()) { sql.append(" AND ue.api_key_id = ?"); params.add(parseUuid(filters.keyId())); }
|
||||
if (filters.modelId() != null && !filters.modelId().isBlank()) { sql.append(" AND ue.model_id = ?"); params.add(filters.modelId().trim()); }
|
||||
if (filters.status() != null && !filters.status().isBlank()) { sql.append(" AND ue.status = ?"); params.add(filters.status().trim()); }
|
||||
if (filters.eventKind() != null && !filters.eventKind().isBlank()) { sql.append(" AND ue.event_kind = ?"); params.add(filters.eventKind().trim()); }
|
||||
if (filters.from() != null) { sql.append(" AND ue.created_at >= ?"); params.add(filters.from()); }
|
||||
if (filters.to() != null) { sql.append(" AND ue.created_at < ?"); params.add(filters.to()); }
|
||||
if (!count) { sql.append(" ORDER BY ue.created_at DESC LIMIT ? OFFSET ?"); params.add(filters.size()); params.add((filters.page() - 1) * filters.size()); }
|
||||
return new Query(sql.toString(), params);
|
||||
}
|
||||
|
||||
private String baseSelect() {
|
||||
return "SELECT ue.*, u.account AS user_account, u.display_name AS user_name, k.name AS key_name, k.key_prefix, m.public_name AS model_name, p.name AS provider_name FROM usage_events ue LEFT JOIN users u ON u.id = ue.user_id LEFT JOIN platform_api_keys k ON k.id = ue.api_key_id LEFT JOIN model_catalog m ON m.id = ue.model_id LEFT JOIN model_providers p ON p.id = ue.provider_id";
|
||||
}
|
||||
|
||||
private Map<String, Object> mapRecord(ResultSet rs, int row) throws SQLException {
|
||||
Map<String, Object> value = new LinkedHashMap<>();
|
||||
value.put("id", rs.getObject("id", UUID.class));
|
||||
value.put("requestId", rs.getString("request_id"));
|
||||
value.put("userId", rs.getObject("user_id", UUID.class));
|
||||
value.put("userAccount", rs.getString("user_account"));
|
||||
value.put("userName", rs.getString("user_name"));
|
||||
value.put("apiKeyId", rs.getObject("api_key_id", UUID.class));
|
||||
value.put("keyName", rs.getString("key_name"));
|
||||
value.put("keyPrefix", rs.getString("key_prefix"));
|
||||
value.put("modelId", rs.getString("model_id"));
|
||||
value.put("modelName", rs.getString("model_name"));
|
||||
value.put("providerId", rs.getString("provider_id"));
|
||||
value.put("providerName", rs.getString("provider_name"));
|
||||
value.put("eventKind", rs.getString("event_kind"));
|
||||
value.put("status", rs.getString("status"));
|
||||
value.put("latencyMs", rs.getObject("latency_ms"));
|
||||
value.put("inputUnits", rs.getObject("input_units"));
|
||||
value.put("outputUnits", rs.getObject("output_units"));
|
||||
value.put("costAmount", rs.getBigDecimal("cost_amount"));
|
||||
value.put("providerTaskId", rs.getString("provider_task_id"));
|
||||
value.put("errorCode", rs.getString("error_code"));
|
||||
value.put("errorMessage", rs.getString("error_message"));
|
||||
value.put("adminNote", rs.getString("admin_note"));
|
||||
value.put("sourceIp", rs.getString("source_ip"));
|
||||
value.put("createdAt", rs.getObject("created_at", OffsetDateTime.class));
|
||||
value.put("updatedAt", rs.getObject("updated_at", OffsetDateTime.class));
|
||||
value.put("input", readPayload(rs.getString("input_payload")));
|
||||
value.put("output", readPayload(rs.getString("output_payload")));
|
||||
return value;
|
||||
}
|
||||
|
||||
private void record(UUID userId, UUID keyId, String modelId, String providerId, String kind, String status, Integer latency, long inputUnits, long outputUnits, Object input, Object output, String taskId, String errorCode, String errorMessage, String ip, String userAgent) {
|
||||
jdbc.update(
|
||||
"INSERT INTO usage_events (id, user_id, api_key_id, model_id, provider_id, event_kind, status, latency_ms, input_units, output_units, provider_task_id, input_payload, output_payload, error_code, error_message, source_ip, user_agent, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), ?, ?, CAST(? AS inet), ?, ?, ?)",
|
||||
UUID.randomUUID(), userId, keyId, modelId, providerId, kind, status, latency, inputUnits, outputUnits, taskId,
|
||||
json(input), json(output), errorCode, errorMessage, blankToNull(ip), blankToNull(userAgent), now(), now()
|
||||
);
|
||||
}
|
||||
|
||||
private String json(Object value) {
|
||||
if (value == null) return null;
|
||||
try {
|
||||
JsonNode sanitized = sanitize(objectMapper.valueToTree(value));
|
||||
String text = objectMapper.writeValueAsString(sanitized);
|
||||
return text.length() > MAX_PAYLOAD_CHARS ? text.substring(0, MAX_PAYLOAD_CHARS) + "…" : text;
|
||||
} catch (JsonProcessingException exception) { return "{\"_unserializable\":true}"; }
|
||||
}
|
||||
|
||||
private JsonNode sanitize(JsonNode node) {
|
||||
if (node == null || node.isNull()) return node;
|
||||
if (node.isTextual()) return node.textValue().length() > 8000 ? objectMapper.getNodeFactory().textNode(node.textValue().substring(0, 8000) + "…") : node;
|
||||
if (node.isArray()) {
|
||||
ArrayNode result = objectMapper.createArrayNode();
|
||||
node.forEach(item -> result.add(sanitize(item)));
|
||||
return result;
|
||||
}
|
||||
if (node.isObject()) {
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
node.fields().forEachRemaining(entry -> {
|
||||
String key = entry.getKey().toLowerCase();
|
||||
if (key.contains("authorization") || key.contains("api_key") || key.contains("apikey") || key.contains("token") || key.contains("secret") || key.contains("password")) result.put(entry.getKey(), "[REDACTED]");
|
||||
else result.set(entry.getKey(), sanitize(entry.getValue()));
|
||||
});
|
||||
return result;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private Object readPayload(String value) {
|
||||
if (value == null) return null;
|
||||
try { return objectMapper.readTree(value); } catch (JsonProcessingException exception) { return value; }
|
||||
}
|
||||
|
||||
private String row(Object... values) { List<String> cells = new ArrayList<>(); for (Object value : values) cells.add(csv(value)); return String.join(",", cells); }
|
||||
private String csv(Object value) { String text = value == null ? "" : String.valueOf(value); return "\"" + text.replace("\"", "\"\"").replace("\r", " ").replace("\n", " ") + "\""; }
|
||||
private UUID parseUuid(String value) { try { return UUID.fromString(value); } catch (Exception exception) { throw new ApiException("INVALID_ID", "记录 ID 格式无效"); } }
|
||||
private String blankToNull(String value) { return value == null || value.isBlank() ? null : value; }
|
||||
private OffsetDateTime now() { return OffsetDateTime.now(ZoneOffset.UTC); }
|
||||
private record Query(String sql, List<Object> params) {}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
ALTER TABLE usage_events
|
||||
ADD COLUMN IF NOT EXISTS request_id varchar(96),
|
||||
ADD COLUMN IF NOT EXISTS provider_id varchar(64),
|
||||
ADD COLUMN IF NOT EXISTS input_payload jsonb,
|
||||
ADD COLUMN IF NOT EXISTS output_payload jsonb,
|
||||
ADD COLUMN IF NOT EXISTS error_code varchar(96),
|
||||
ADD COLUMN IF NOT EXISTS error_message text,
|
||||
ADD COLUMN IF NOT EXISTS source_ip inet,
|
||||
ADD COLUMN IF NOT EXISTS user_agent text,
|
||||
ADD COLUMN IF NOT EXISTS admin_note varchar(1000),
|
||||
ADD COLUMN IF NOT EXISTS deleted_at timestamptz,
|
||||
ADD COLUMN IF NOT EXISTS deleted_by uuid REFERENCES users(id) ON DELETE SET NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS usage_events_key_created_idx ON usage_events(api_key_id, created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS usage_events_model_created_idx ON usage_events(model_id, created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS usage_events_status_created_idx ON usage_events(status, created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS usage_events_visible_created_idx ON usage_events(deleted_at, created_at DESC);
|
||||
Reference in new issue
Block a user