feat(console): add deletion lifecycle, usage history and balance adjustments
This commit is contained in:
1 parent
32e977df5c
commit
7360cf3ebe
18 files changed
+832
-68
No files matched your search
@@ -7,6 +7,7 @@ import java.util.Map;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -91,6 +92,36 @@ public class CatalogController {
|
||||
return Map.of("providers", catalogService.listProviders(true));
|
||||
}
|
||||
|
||||
@PatchMapping("/keys/{id}/status")
|
||||
public ResponseEntity<Void> keyStatus(@PathVariable String id, @RequestBody Map<String,String> input, jakarta.servlet.http.HttpServletRequest request) {
|
||||
catalogService.setKeyStatus(AuthController.sessionToken(request),id,input.getOrDefault("status",""),authService);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/keys/{id}")
|
||||
public ResponseEntity<Void> deleteKey(@PathVariable String id, jakarta.servlet.http.HttpServletRequest request) {
|
||||
catalogService.deleteKey(AuthController.sessionToken(request),id,authService);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/admin/providers/{id}")
|
||||
public ResponseEntity<Void> deleteProvider(@PathVariable String id, jakarta.servlet.http.HttpServletRequest request) {
|
||||
catalogService.deleteCatalog("channel",id,AuthController.sessionToken(request),authService);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/admin/models/{id}")
|
||||
public ResponseEntity<Void> deleteGroup(@PathVariable String id, jakarta.servlet.http.HttpServletRequest request) {
|
||||
catalogService.deleteCatalog("group",id,AuthController.sessionToken(request),authService);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/admin/model-definitions/{id}")
|
||||
public ResponseEntity<Void> deleteDefinition(@PathVariable String id, jakarta.servlet.http.HttpServletRequest request) {
|
||||
catalogService.deleteCatalog("model",id,AuthController.sessionToken(request),authService);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@PostMapping("/admin/providers")
|
||||
public ResponseEntity<Map<String, Object>> createProvider(
|
||||
@RequestBody Map<String, Object> input,
|
||||
|
||||
+59
-13
@@ -43,17 +43,17 @@ public class CatalogService {
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listProviders(boolean includeDisabled) {
|
||||
String statusClause = includeDisabled ? "" : " WHERE status = 'active'";
|
||||
String statusClause = " WHERE deleted_at IS NULL" + (includeDisabled ? "" : " AND status = 'active'");
|
||||
return jdbc.query("SELECT * FROM model_providers" + statusClause + " ORDER BY name", this::mapProvider);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listModels(boolean includeDisabled) {
|
||||
String statusClause = includeDisabled ? "" : " WHERE m.status = 'active' AND p.status = 'active' AND d.status = 'active'";
|
||||
String statusClause = " WHERE m.deleted_at IS NULL AND p.deleted_at IS NULL AND d.deleted_at IS NULL" + (includeDisabled ? "" : " AND m.status = 'active' AND p.status = 'active' AND d.status = 'active'");
|
||||
return jdbc.query("SELECT m.* FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id" + statusClause + " ORDER BY m.public_name, m.source_label", this::mapModel);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listDefinitions(boolean includeDisabled) {
|
||||
return jdbc.query("SELECT * FROM model_definitions" + (includeDisabled ? "" : " WHERE status='active'") + " ORDER BY public_name", (rs, n) -> {
|
||||
return jdbc.query("SELECT * FROM model_definitions WHERE deleted_at IS NULL" + (includeDisabled ? "" : " AND status='active'") + " ORDER BY public_name", (rs, n) -> {
|
||||
Map<String, Object> item = new LinkedHashMap<>();
|
||||
item.put("id", rs.getString("id"));
|
||||
item.put("publicName", rs.getString("public_name"));
|
||||
@@ -71,6 +71,7 @@ public class CatalogService {
|
||||
@Transactional
|
||||
public Map<String, Object> saveDefinition(String id, Map<String, Object> input, String token, AuthService auth) {
|
||||
UserView admin = auth.requireAdmin(token);
|
||||
lockCatalog();
|
||||
boolean creating = id == null;
|
||||
Map<String, Object> value = creating ? new LinkedHashMap<>() : new LinkedHashMap<>(definition(id));
|
||||
value.putAll(input);
|
||||
@@ -84,7 +85,7 @@ public class CatalogService {
|
||||
jdbc.update("INSERT INTO model_definitions (id, model_key, public_name, model_type, call_mode, description, input_types, output_types, status) VALUES (?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), ?)", id, optional(value, "modelKey", null), name, type, mode, optional(value, "description", ""), jsonArray(value.get("inputTypes")), jsonArray(value.get("outputTypes")), status);
|
||||
} else {
|
||||
jdbc.update("UPDATE model_definitions SET model_key=?, public_name=?, model_type=?, call_mode=?, description=?, input_types=CAST(? AS jsonb), output_types=CAST(? AS jsonb), status=?, updated_at=now() WHERE id=?", optional(value,"modelKey",null), name, type, mode, optional(value,"description",""), jsonArray(value.get("inputTypes")), jsonArray(value.get("outputTypes")), status, id);
|
||||
jdbc.update("UPDATE model_catalog SET model_key=?, public_name=?, model_type=?, call_mode=?, description=?, input_types=CAST(? AS jsonb), output_types=CAST(? AS jsonb), updated_at=now() WHERE logical_model_id=?", optional(value,"modelKey",null), name, type, mode, optional(value,"description",""), jsonArray(value.get("inputTypes")), jsonArray(value.get("outputTypes")), id);
|
||||
jdbc.update("UPDATE model_catalog SET model_key=?, public_name=?, model_type=?, call_mode=?, description=?, input_types=CAST(? AS jsonb), output_types=CAST(? AS jsonb), updated_at=now() WHERE logical_model_id=? AND deleted_at IS NULL", optional(value,"modelKey",null), name, type, mode, optional(value,"description",""), jsonArray(value.get("inputTypes")), jsonArray(value.get("outputTypes")), id);
|
||||
}
|
||||
auth.audit(creating ? "model_definition.created" : "model_definition.updated", admin.id(), new AuthService.RequestMeta(null,null), Map.of("modelId",id));
|
||||
return definition(id);
|
||||
@@ -103,6 +104,7 @@ public class CatalogService {
|
||||
@Transactional
|
||||
public Map<String, Object> createProvider(Map<String, Object> input, String token, AuthService auth) {
|
||||
UserView admin = auth.requireAdmin(token);
|
||||
lockCatalog();
|
||||
String code = required(input, "code").toLowerCase();
|
||||
String name = required(input, "name");
|
||||
String protocol = optional(input, "protocol", "http");
|
||||
@@ -129,6 +131,7 @@ public class CatalogService {
|
||||
@Transactional
|
||||
public Map<String, Object> updateProvider(String id, Map<String, Object> input, String token, AuthService auth) {
|
||||
UserView admin = auth.requireAdmin(token);
|
||||
lockCatalog();
|
||||
Map<String, Object> previous = findProvider(id);
|
||||
if (previous == null) throw new ApiException("PROVIDER_NOT_FOUND", "渠道不存在", 404);
|
||||
Map<String, Object> value = new LinkedHashMap<>(previous);
|
||||
@@ -158,6 +161,7 @@ public class CatalogService {
|
||||
@Transactional
|
||||
public Map<String, Object> createModel(Map<String, Object> input, String token, AuthService auth) {
|
||||
UserView admin = auth.requireAdmin(token);
|
||||
lockCatalog();
|
||||
input = new LinkedHashMap<>(input);
|
||||
String logicalId = optional(input, "logicalModelId", null);
|
||||
if (logicalId == null) logicalId = String.valueOf(saveDefinition(null, input, token, auth).get("id"));
|
||||
@@ -197,6 +201,7 @@ public class CatalogService {
|
||||
@Transactional
|
||||
public Map<String, Object> updateModel(String id, Map<String, Object> input, String token, AuthService auth) {
|
||||
UserView admin=auth.requireAdmin(token);
|
||||
lockCatalog();
|
||||
Map<String,Object> old=findModel(id);
|
||||
if (old==null) throw new ApiException("MODEL_NOT_FOUND","分组不存在",404);
|
||||
Map<String,Object> value=new LinkedHashMap<>(old);
|
||||
@@ -245,7 +250,7 @@ public class CatalogService {
|
||||
|
||||
public List<Map<String, Object>> listKeys(String token, AuthService auth) {
|
||||
UserView user = auth.requireUser(token);
|
||||
return jdbc.query("SELECT * FROM platform_api_keys WHERE user_id = ? ORDER BY created_at DESC", this::mapApiKey, user.id());
|
||||
return jdbc.query("SELECT * FROM platform_api_keys WHERE user_id = ? AND deleted_at IS NULL ORDER BY created_at DESC", this::mapApiKey, user.id());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -275,7 +280,7 @@ public class CatalogService {
|
||||
@Transactional
|
||||
public Map<String, Object> updateKeyModels(String token, String keyId, List<String> modelIds, AuthService auth) {
|
||||
UserView user = auth.requireUser(token);
|
||||
var existing = jdbc.query("SELECT * FROM platform_api_keys WHERE id=? AND user_id=? AND status='active' FOR UPDATE", this::mapApiKey, UUID.fromString(keyId), user.id());
|
||||
var existing = jdbc.query("SELECT * FROM platform_api_keys WHERE id=? AND user_id=? AND status='active' AND deleted_at IS NULL FOR UPDATE", this::mapApiKey, UUID.fromString(keyId), user.id());
|
||||
if (existing.isEmpty()) throw new ApiException("KEY_NOT_FOUND", "Key 不存在、已停用或无权修改", 404);
|
||||
List<?> priorIds = (List<?>) existing.getFirst().get("modelIds");
|
||||
List<String> uniqueIds = modelIds == null ? List.of() : modelIds.stream().filter(java.util.Objects::nonNull).map(String::trim).filter(value -> !value.isBlank()).distinct().toList();
|
||||
@@ -287,9 +292,50 @@ public class CatalogService {
|
||||
|
||||
@Transactional
|
||||
public void revokeKey(String token, String keyId, AuthService auth) {
|
||||
setKeyStatus(token, keyId, "disabled", auth);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void setKeyStatus(String token, String keyId, String status, AuthService auth) {
|
||||
if (!List.of("active", "disabled").contains(status)) throw new ApiException("INVALID_STATUS", "Key 状态无效");
|
||||
UserView user = auth.requireUser(token);
|
||||
int changed = jdbc.update("UPDATE platform_api_keys SET status = 'disabled', revoked_at = ? WHERE id = ? AND user_id = ? AND status = 'active'", now(), UUID.fromString(keyId), user.id());
|
||||
if (changed == 0) throw new ApiException("KEY_NOT_FOUND", "Key 不存在或已停用", 404);
|
||||
int changed = jdbc.update("UPDATE platform_api_keys SET status=?, revoked_at=? WHERE id=? AND user_id=? AND deleted_at IS NULL", status, "active".equals(status) ? null : now(), UUID.fromString(keyId), user.id());
|
||||
if (changed == 0) throw new ApiException("KEY_NOT_FOUND", "Key 不存在或无权操作", 404);
|
||||
auth.audit("key." + ("active".equals(status) ? "enabled" : "disabled"), user.id(), new AuthService.RequestMeta(null,null), Map.of("keyId",keyId));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteKey(String token, String keyId, AuthService auth) {
|
||||
UserView user = auth.requireUser(token);
|
||||
var states = jdbc.queryForList("SELECT status FROM platform_api_keys WHERE id=? AND user_id=? AND deleted_at IS NULL FOR UPDATE", String.class, UUID.fromString(keyId), user.id());
|
||||
if (states.isEmpty()) throw new ApiException("KEY_NOT_FOUND", "Key 不存在或无权操作", 404);
|
||||
if ("active".equals(states.getFirst())) throw new ApiException("KEY_ACTIVE", "请先停用 Key,再删除", 409);
|
||||
jdbc.update("UPDATE platform_api_keys SET deleted_at=now() WHERE id=?", UUID.fromString(keyId));
|
||||
auth.audit("key.deleted",user.id(),new AuthService.RequestMeta(null,null),Map.of("keyId",keyId));
|
||||
}
|
||||
|
||||
private void lockCatalog() {
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:catalog-lifecycle', 0))");
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteCatalog(String kind, String id, String token, AuthService auth) {
|
||||
UserView admin = auth.requireAdmin(token);
|
||||
lockCatalog();
|
||||
if ("channel".equals(kind)) {
|
||||
if (findProvider(id)==null) throw new ApiException("PROVIDER_NOT_FOUND","渠道不存在",404);
|
||||
if (jdbc.queryForObject("SELECT count(*) FROM model_catalog WHERE provider_id=? AND deleted_at IS NULL",Integer.class,id)>0)
|
||||
throw new ApiException("CHANNEL_IN_USE","渠道仍有关联分组,请先删除分组或将其改为其他渠道",409);
|
||||
jdbc.update("UPDATE model_providers SET status='disabled', deleted_at=now(), updated_at=now() WHERE id=?",id);
|
||||
} else if ("model".equals(kind)) {
|
||||
definition(id);
|
||||
jdbc.update("UPDATE model_catalog SET status='disabled', deleted_at=now(), updated_at=now() WHERE logical_model_id=? AND deleted_at IS NULL",id);
|
||||
jdbc.update("UPDATE model_definitions SET status='disabled', deleted_at=now(), updated_at=now() WHERE id=?",id);
|
||||
} else if ("group".equals(kind)) {
|
||||
if (findModel(id)==null) throw new ApiException("MODEL_NOT_FOUND","分组不存在",404);
|
||||
jdbc.update("UPDATE model_catalog SET status='disabled', deleted_at=now(), updated_at=now() WHERE id=?",id);
|
||||
} else throw new ApiException("INVALID_KIND","删除对象无效");
|
||||
auth.audit("catalog."+kind+"_deleted",admin.id(),new AuthService.RequestMeta(null,null),Map.of("id",id));
|
||||
}
|
||||
|
||||
public Map<String, Object> overview(String token, AuthService auth) {
|
||||
@@ -314,7 +360,7 @@ public class CatalogService {
|
||||
public Map<String, Object> authenticateKey(String plaintext) {
|
||||
if (plaintext == null || plaintext.isBlank()) return null;
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT k.* FROM platform_api_keys k JOIN users u ON u.id=k.user_id WHERE k.key_hash = ? AND k.status = 'active' AND u.status='active'", this::mapApiKey, sha256(plaintext.trim()));
|
||||
return jdbc.queryForObject("SELECT k.* FROM platform_api_keys k JOIN users u ON u.id=k.user_id WHERE k.key_hash = ? AND k.status = 'active' AND k.deleted_at IS NULL AND u.status='active'", this::mapApiKey, sha256(plaintext.trim()));
|
||||
} catch (EmptyResultDataAccessException exception) {
|
||||
return null;
|
||||
}
|
||||
@@ -337,7 +383,7 @@ public class CatalogService {
|
||||
}
|
||||
|
||||
public UUID keyOwnerId(UUID apiKeyId) {
|
||||
try { return jdbc.queryForObject("SELECT user_id FROM platform_api_keys WHERE id = ? AND status = 'active'", UUID.class, apiKeyId); }
|
||||
try { return jdbc.queryForObject("SELECT user_id FROM platform_api_keys WHERE id = ? AND status = 'active' AND deleted_at IS NULL", UUID.class, apiKeyId); }
|
||||
catch (EmptyResultDataAccessException exception) { throw new ApiException("INVALID_API_KEY", "API Key 无效或已停用", 401); }
|
||||
}
|
||||
|
||||
@@ -350,17 +396,17 @@ public class CatalogService {
|
||||
}
|
||||
|
||||
private Map<String, Object> findProvider(String id) {
|
||||
try { return jdbc.queryForObject("SELECT * FROM model_providers WHERE id = ?", this::mapProvider, id); }
|
||||
try { return jdbc.queryForObject("SELECT * FROM model_providers WHERE id = ? AND deleted_at IS NULL", this::mapProvider, id); }
|
||||
catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
private Map<String, Object> findModel(String id) {
|
||||
try { return jdbc.queryForObject("SELECT * FROM model_catalog WHERE id = ?", this::mapModel, id); }
|
||||
try { return jdbc.queryForObject("SELECT * FROM model_catalog WHERE id = ? AND deleted_at IS NULL", this::mapModel, id); }
|
||||
catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
private Map<String, Object> findActiveModel(String id) {
|
||||
try { return jdbc.queryForObject("SELECT m.* FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id WHERE m.id = ? AND m.status = 'active' AND p.status='active' AND d.status='active'", this::mapModel, id); }
|
||||
try { return jdbc.queryForObject("SELECT m.* FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id WHERE m.id = ? AND m.status = 'active' AND p.status='active' AND d.status='active' AND m.deleted_at IS NULL AND p.deleted_at IS NULL AND d.deleted_at IS NULL", this::mapModel, id); }
|
||||
catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
registry.addMapping("/**")
|
||||
.allowedOrigins(allowedOrigins)
|
||||
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
|
||||
.allowedHeaders("Content-Type", "Authorization")
|
||||
.allowedHeaders("Content-Type", "Authorization", "X-Moyi-Source")
|
||||
.allowCredentials(true)
|
||||
.maxAge(3600);
|
||||
}
|
||||
|
||||
+22
-4
@@ -52,8 +52,8 @@ public class UsageAuditService {
|
||||
|
||||
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
|
||||
"UPDATE usage_events SET status = ?, output_payload = CAST(? AS jsonb), error_code = ?, error_message = ?, latency_ms = CASE WHEN ? IN ('succeeded','failed','cancelled') THEN LEAST(2147483647, GREATEST(0, EXTRACT(EPOCH FROM (now()-created_at))*1000))::integer ELSE latency_ms END, updated_at = ? WHERE provider_task_id = ? AND event_kind = 'task' AND deleted_at IS NULL",
|
||||
status, json(output), errorCode, errorMessage, status, now(), taskId
|
||||
);
|
||||
}
|
||||
|
||||
@@ -211,13 +211,31 @@ public class UsageAuditService {
|
||||
}
|
||||
|
||||
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) {
|
||||
String endpoint=null, source=null;
|
||||
var attributes=org.springframework.web.context.request.RequestContextHolder.getRequestAttributes();
|
||||
if(attributes instanceof org.springframework.web.context.request.ServletRequestAttributes servlet) {
|
||||
var request=servlet.getRequest();
|
||||
endpoint=request.getRequestURI();
|
||||
userAgent=request.getHeader("User-Agent");
|
||||
source="playground".equals(request.getHeader("X-Moyi-Source")) ? "playground" : "api";
|
||||
}
|
||||
JsonNode payload=objectMapper.valueToTree(output);
|
||||
Long cached=usageNumber(payload,"/usage/prompt_tokens_details/cached_tokens");
|
||||
if(cached==null) cached=usageNumber(payload,"/usage/prompt_cache_hit_tokens");
|
||||
Long cacheWrite=usageNumber(payload,"/usage/cache_creation_input_tokens");
|
||||
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), ?, ?, ?)",
|
||||
"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, endpoint, call_mode, request_source, cached_input_units, cache_write_units) 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()
|
||||
json(input), json(output), errorCode, errorMessage, blankToNull(ip), userAgent==null?null:userAgent.substring(0,Math.min(userAgent.length(),1000)), now(), now(), endpoint, "task".equals(kind)?"async":"sync", source, cached, cacheWrite
|
||||
);
|
||||
}
|
||||
|
||||
private Long usageNumber(JsonNode payload, String path) {
|
||||
if(payload==null) return null;
|
||||
JsonNode value=payload.at(path);
|
||||
return value.isIntegralNumber() && value.canConvertToLong() && value.longValue()>=0 ? value.longValue() : null;
|
||||
}
|
||||
|
||||
private String json(Object value) {
|
||||
if (value == null) return null;
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.moyi.controlplane.usage;
|
||||
|
||||
import com.moyi.controlplane.auth.AuthController;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/usage")
|
||||
public class UserUsageController {
|
||||
private final UserUsageService usage;
|
||||
public UserUsageController(UserUsageService usage) { this.usage=usage; }
|
||||
// Shared binding keeps the list and export filters identical.
|
||||
@ModelAttribute
|
||||
public UserUsageService.Filters filters(
|
||||
@RequestParam(required=false) String keyId, @RequestParam(required=false) String modelId,
|
||||
@RequestParam(required=false) String groupId, @RequestParam(required=false) String status,
|
||||
@RequestParam(required=false) String eventKind, @RequestParam(required=false) String callMode,
|
||||
@RequestParam(required=false) String source, @RequestParam(required=false) String costState,
|
||||
@RequestParam(required=false) String userAgent, @RequestParam(required=false) OffsetDateTime from,
|
||||
@RequestParam(required=false) OffsetDateTime to, @RequestParam(defaultValue="1") int page,
|
||||
@RequestParam(defaultValue="20") int size, @RequestParam(defaultValue="time") String sort,
|
||||
@RequestParam(defaultValue="desc") String direction) {
|
||||
return new UserUsageService.Filters(keyId,modelId,groupId,status,eventKind,callMode,source,costState,userAgent,from,to,page,size,sort,direction);
|
||||
}
|
||||
@GetMapping
|
||||
public Map<String,Object> list(HttpServletRequest request,@ModelAttribute UserUsageService.Filters filters) { return usage.list(AuthController.sessionToken(request),filters); }
|
||||
@GetMapping("/options")
|
||||
public Map<String,Object> options(HttpServletRequest request) { return usage.options(AuthController.sessionToken(request)); }
|
||||
@GetMapping("/{id}")
|
||||
public Map<String,Object> detail(@PathVariable UUID id,HttpServletRequest request) { return Map.of("record",usage.detail(AuthController.sessionToken(request),id)); }
|
||||
@GetMapping("/export")
|
||||
public ResponseEntity<String> export(HttpServletRequest request,@ModelAttribute UserUsageService.Filters filters) {
|
||||
return ResponseEntity.ok().header("Content-Type","text/csv; charset=UTF-8").header("Content-Disposition","attachment; filename=moyi-usage.csv").body(usage.exportCsv(AuthController.sessionToken(request),filters));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.moyi.controlplane.usage;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Isolation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class UserUsageService {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final AuthService auth;
|
||||
private final ObjectMapper json;
|
||||
private static final String JOINS=" FROM usage_events ue 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 LEFT JOIN model_tasks t ON t.id=ue.provider_task_id";
|
||||
private static final String MODE="COALESCE(ue.call_mode, CASE WHEN ue.event_kind='task' THEN 'async' ELSE 'sync' END)";
|
||||
private static final String COLUMNS="ue.id, ue.created_at AS \"createdAt\", ue.api_key_id AS \"apiKeyId\", k.name AS \"keyName\", k.key_prefix AS \"keyPrefix\", ue.model_id AS \"modelId\", m.logical_model_id AS \"logicalModelId\", m.public_name AS \"modelName\", m.source_label AS \"groupName\", p.name AS \"providerName\", ue.event_kind AS \"eventKind\", ue.status, ue.endpoint, "+MODE+" AS \"callMode\", ue.request_source AS \"requestSource\", ue.user_agent AS \"userAgent\", ue.input_units AS \"inputUnits\", ue.output_units AS \"outputUnits\", ue.cached_input_units AS \"cachedInputUnits\", ue.cache_write_units AS \"cacheWriteUnits\", ue.cost_amount AS \"costAmount\", ue.first_token_ms AS \"firstTokenMs\", ue.reasoning_effort AS \"reasoningEffort\", ue.latency_ms AS \"latencyMs\", ue.provider_task_id AS \"taskId\", t.progress, ue.error_code AS \"errorCode\", ue.error_message AS \"errorMessage\"";
|
||||
|
||||
public UserUsageService(JdbcTemplate jdbc, AuthService auth, ObjectMapper json) { this.jdbc=jdbc; this.auth=auth; this.json=json; }
|
||||
|
||||
public record Filters(String keyId, String modelId, String groupId, String status, String eventKind, String callMode,
|
||||
String source, String costState, String userAgent, OffsetDateTime from, OffsetDateTime to,
|
||||
int page, int size, String sort, String direction) {
|
||||
public Filters {
|
||||
page=Math.max(1,page); size=Math.max(1,Math.min(100,size));
|
||||
if(from!=null && to!=null && !from.isBefore(to)) throw new ApiException("INVALID_DATE_RANGE","开始时间必须早于结束时间");
|
||||
}
|
||||
}
|
||||
private record Query(String where, List<Object> params) {}
|
||||
private Query filter(UUID userId, Filters f) {
|
||||
StringBuilder sql=new StringBuilder(" WHERE ue.user_id=? AND ue.deleted_at IS NULL");
|
||||
List<Object> args=new ArrayList<>(List.of(userId));
|
||||
if(present(f.keyId())) { sql.append(" AND ue.api_key_id::text=?"); args.add(f.keyId()); }
|
||||
add(sql,args,"m.logical_model_id",f.modelId());
|
||||
add(sql,args,"ue.model_id",f.groupId());
|
||||
add(sql,args,"ue.status",f.status()); add(sql,args,"ue.event_kind",f.eventKind());
|
||||
add(sql,args,MODE,f.callMode()); add(sql,args,"COALESCE(ue.request_source,'unknown')",f.source());
|
||||
if("recorded".equals(f.costState())) sql.append(" AND ue.cost_amount IS NOT NULL");
|
||||
else if("pending".equals(f.costState())) sql.append(" AND ue.cost_amount IS NULL");
|
||||
if(present(f.userAgent())) { sql.append(" AND ue.user_agent ILIKE ? ESCAPE '!'"); args.add("%"+f.userAgent().replace("!","!!").replace("%","!%").replace("_","!_")+"%"); }
|
||||
if(f.from()!=null) { sql.append(" AND ue.created_at>=?"); args.add(f.from()); }
|
||||
if(f.to()!=null) { sql.append(" AND ue.created_at<?"); args.add(f.to()); }
|
||||
return new Query(sql.toString(),args);
|
||||
}
|
||||
private boolean present(String value) { return value!=null && !value.isBlank(); }
|
||||
private void add(StringBuilder sql,List<Object> args,String column,String value) { if(present(value)) { sql.append(" AND ").append(column).append("=?"); args.add(value); } }
|
||||
private String order(Filters f) {
|
||||
String column=switch(f.sort()==null?"":f.sort()) { case "model" -> "m.public_name"; case "cost" -> "ue.cost_amount"; case "latency" -> "ue.latency_ms"; default -> "ue.created_at"; };
|
||||
return " ORDER BY "+column+("asc".equals(f.direction())?" ASC":" DESC")+" NULLS LAST, ue.id DESC";
|
||||
}
|
||||
|
||||
@Transactional(readOnly=true, isolation=Isolation.REPEATABLE_READ)
|
||||
public Map<String,Object> list(String token, Filters f) {
|
||||
UUID userId=auth.requireUser(token).id();
|
||||
Query q=filter(userId,f);
|
||||
Map<String,Object> stats=jdbc.queryForMap("SELECT COUNT(*) AS \"totalRequests\", COALESCE(SUM(ue.input_units),0) AS \"inputUnits\", COALESCE(SUM(ue.output_units),0) AS \"outputUnits\", SUM(ue.cached_input_units) AS \"cachedInputUnits\", SUM(ue.cache_write_units) AS \"cacheWriteUnits\", SUM(ue.cost_amount) AS \"totalCost\", COUNT(ue.cost_amount) AS \"pricedRequests\", AVG(ue.latency_ms) AS \"averageLatencyMs\""+JOINS+q.where(),q.params().toArray());
|
||||
long total=((Number)stats.get("totalRequests")).longValue();
|
||||
long pages=Math.max(1,(total+f.size()-1)/f.size());
|
||||
int page=(int)Math.min(f.page(),pages);
|
||||
List<Object> args=new ArrayList<>(q.params()); args.add(f.size()); args.add((long)(page-1)*f.size());
|
||||
var records=jdbc.queryForList("SELECT "+COLUMNS+JOINS+q.where()+order(f)+" LIMIT ? OFFSET ?",args.toArray());
|
||||
return Map.of("records",records,"summary",stats,"total",total,"pages",pages,"page",page,"size",f.size());
|
||||
}
|
||||
|
||||
public Map<String,Object> options(String token) {
|
||||
UUID userId=auth.requireUser(token).id();
|
||||
return Map.of(
|
||||
"keys",jdbc.queryForList("SELECT DISTINCT k.id, k.name, (k.deleted_at IS NOT NULL) AS deleted FROM platform_api_keys k WHERE k.user_id=? ORDER BY k.name",userId),
|
||||
"models",jdbc.queryForList("SELECT DISTINCT m.logical_model_id AS id, m.public_name AS name"+JOINS+" WHERE ue.user_id=? AND ue.deleted_at IS NULL AND m.id IS NOT NULL ORDER BY name",userId),
|
||||
"groups",jdbc.queryForList("SELECT DISTINCT m.id, m.logical_model_id AS \"modelId\", m.source_label AS name, p.name AS channel"+JOINS+" WHERE ue.user_id=? AND ue.deleted_at IS NULL AND m.id IS NOT NULL ORDER BY name",userId));
|
||||
}
|
||||
|
||||
public Map<String,Object> detail(String token, UUID id) {
|
||||
UUID userId=auth.requireUser(token).id();
|
||||
var records=jdbc.queryForList("SELECT "+COLUMNS+", ue.input_payload::text AS input, ue.output_payload::text AS output, t.result::text AS result"+JOINS+" WHERE ue.user_id=? AND ue.id=? AND ue.deleted_at IS NULL",userId,id);
|
||||
if(records.isEmpty()) throw new ApiException("USAGE_NOT_FOUND","调用记录不存在",404);
|
||||
var record=records.getFirst();
|
||||
for(String field:List.of("input","output","result")) {
|
||||
Object raw=record.get(field);
|
||||
if(raw!=null) try { record.put(field,json.readTree(raw.toString())); } catch(Exception ignored) { record.put(field,null); }
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
@Transactional(readOnly=true, isolation=Isolation.REPEATABLE_READ)
|
||||
public String exportCsv(String token, Filters f) {
|
||||
UUID userId=auth.requireUser(token).id();
|
||||
Query q=filter(userId,f);
|
||||
Long count=jdbc.queryForObject("SELECT COUNT(*)"+JOINS+q.where(),Long.class,q.params().toArray());
|
||||
if(count!=null && count>10000) throw new ApiException("EXPORT_TOO_LARGE","单次最多导出 10000 条,请缩小时间或筛选范围");
|
||||
var records=jdbc.queryForList("SELECT "+COLUMNS+JOINS+q.where()+order(f),q.params().toArray());
|
||||
StringBuilder csv=new StringBuilder("\uFEFF记录ID,时间,API Key,模型,分组,渠道,端点,类型,状态,来源,输入Token,输出Token,缓存命中Token,缓存写入Token,费用(CNY),首Token(ms),耗时(ms),User-Agent,任务ID,错误\r\n");
|
||||
for(var record:records) {
|
||||
var cells=new ArrayList<String>();
|
||||
for(String field:List.of("id","createdAt","keyName","modelName","groupName","providerName","endpoint","callMode","status","requestSource","inputUnits","outputUnits","cachedInputUnits","cacheWriteUnits","costAmount","firstTokenMs","latencyMs","userAgent","taskId","errorMessage")) cells.add(csvCell(record.get(field)));
|
||||
csv.append(String.join(",",cells)).append("\r\n");
|
||||
}
|
||||
return csv.toString();
|
||||
}
|
||||
private String csvCell(Object value) {
|
||||
String text=value==null?"":value.toString().replace('\r',' ').replace('\n',' ');
|
||||
// Keep untrusted model/key names from becoming spreadsheet formulas.
|
||||
if(text.stripLeading().matches("^[=+@\\-].*") || text.startsWith("\t")) text="'"+text;
|
||||
return "\""+text.replace("\"","\"\"")+"\"";
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,11 @@ public class WalletController {
|
||||
private final WalletService wallet;
|
||||
public WalletController(WalletService wallet) { this.wallet=wallet; }
|
||||
public record GrantRequest(UUID requestId, String amount, String reason) {}
|
||||
public record AdjustmentRequest(UUID requestId, String operation, String amount, String reason, String expectedBalance) {}
|
||||
@PostMapping("/users/{id}/wallet/adjustments")
|
||||
public Map<String,Object> adjust(@PathVariable UUID id,@RequestBody AdjustmentRequest input,HttpServletRequest request) {
|
||||
return wallet.adjust(AuthController.sessionToken(request),id,input.requestId(),input.operation(),input.amount(),input.reason(),input.expectedBalance(),AuthController.meta(request));
|
||||
}
|
||||
@GetMapping("/wallets")
|
||||
public Map<String,Object> balances(HttpServletRequest request) { return Map.of("wallets",wallet.balances(AuthController.sessionToken(request))); }
|
||||
@GetMapping("/users/{id}/wallet")
|
||||
|
||||
@@ -25,36 +25,51 @@ public class WalletService {
|
||||
|
||||
public List<Map<String,Object>> entries(String token, UUID userId) {
|
||||
auth.requireAdmin(token);
|
||||
return jdbc.queryForList("SELECT e.id, e.amount, e.balance_after AS \"balanceAfter\", e.currency, e.reason, e.created_at AS \"createdAt\", u.display_name AS actor FROM wallet_entries e JOIN users u ON u.id=e.actor_user_id WHERE e.user_id=? ORDER BY e.created_at DESC LIMIT 100",userId);
|
||||
return jdbc.queryForList("SELECT e.id, e.amount, e.operation, e.balance_before AS \"balanceBefore\", e.balance_after AS \"balanceAfter\", e.currency, e.reason, e.created_at AS \"createdAt\", u.display_name AS actor FROM wallet_entries e JOIN users u ON u.id=e.actor_user_id WHERE e.user_id=? ORDER BY e.created_at DESC LIMIT 100",userId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String,Object> grant(String token, UUID userId, UUID requestId, String amountInput, String reasonInput, AuthService.RequestMeta meta) {
|
||||
return adjust(token,userId,requestId,"increase",amountInput,reasonInput,null,meta);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String,Object> adjust(String token, UUID userId, UUID requestId, String operation, String amountInput, String reasonInput, String expectedBalanceInput, AuthService.RequestMeta meta) {
|
||||
var actor=auth.requireAdmin(token);
|
||||
if(requestId==null) throw new ApiException("INVALID_REQUEST_ID","请提供发放请求编号");
|
||||
if(requestId==null) throw new ApiException("INVALID_REQUEST_ID","请提供调整请求编号");
|
||||
if(operation==null || !List.of("increase","decrease","set").contains(operation)) throw new ApiException("INVALID_OPERATION","请选择增加、减少或设置余额");
|
||||
BigDecimal amount;
|
||||
try {
|
||||
amount=new BigDecimal(amountInput).setScale(2,RoundingMode.UNNECESSARY);
|
||||
if(amount.signum()<=0 || amount.compareTo(new BigDecimal("1000000"))>0) throw new NumberFormatException();
|
||||
} catch(Exception exception) { throw new ApiException("INVALID_AMOUNT","金额需大于 0,最多两位小数,单次不超过 100 万元"); }
|
||||
if(amount.signum()<0 || (!"set".equals(operation) && amount.signum()==0) || amount.compareTo(new BigDecimal("1000000"))>0) throw new NumberFormatException();
|
||||
} catch(Exception exception) { throw new ApiException("INVALID_AMOUNT","金额最多两位小数,不超过 100 万元;增减金额需大于 0,目标余额可为 0"); }
|
||||
BigDecimal expected=null;
|
||||
if(expectedBalanceInput!=null) {
|
||||
try { expected=new BigDecimal(expectedBalanceInput); }
|
||||
catch(Exception e) { throw new ApiException("INVALID_BALANCE","当前余额格式无效"); }
|
||||
}
|
||||
if("set".equals(operation) && expected==null) throw new ApiException("BALANCE_REQUIRED","设置余额前请刷新当前余额");
|
||||
String reason=reasonInput==null?"":reasonInput.trim();
|
||||
if(reason.isBlank() || reason.length()>500) throw new ApiException("INVALID_REASON","请填写发放原因(最多 500 字符)");
|
||||
if(reason.isBlank() || reason.length()>500) throw new ApiException("INVALID_REASON","请填写调整原因(最多 500 字符)");
|
||||
if(jdbc.queryForObject("SELECT count(*) FROM users WHERE id=? AND status='active'",Integer.class,userId)==0) throw new ApiException("USER_NOT_FOUND","用户不存在或已停用",404);
|
||||
// Lock the idempotency key before the wallet so concurrent retries cannot grant twice.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended(?, 0))",requestId.toString());
|
||||
List<Map<String,Object>> existing=jdbc.queryForList("SELECT * FROM wallet_entries WHERE request_id=?",requestId);
|
||||
if(!existing.isEmpty()) {
|
||||
var entry=existing.getFirst();
|
||||
if(!userId.equals(entry.get("user_id")) || !actor.id().equals(entry.get("actor_user_id")) || amount.compareTo((BigDecimal)entry.get("amount"))!=0 || !reason.equals(entry.get("reason"))) throw new ApiException("REQUEST_CONFLICT","请求编号已被另一笔发放使用",409);
|
||||
if(!userId.equals(entry.get("user_id")) || !actor.id().equals(entry.get("actor_user_id")) || !operation.equals(entry.get("operation")) || amount.compareTo((BigDecimal)entry.get("requested_amount"))!=0 || !reason.equals(entry.get("reason")) || (expected!=null && expected.compareTo((BigDecimal)entry.get("balance_before"))!=0)) throw new ApiException("REQUEST_CONFLICT","请求编号已被另一笔调整使用",409);
|
||||
return Map.of("id",entry.get("id"),"balance",entry.get("balance_after"),"currency","CNY","replayed",true);
|
||||
}
|
||||
jdbc.update("INSERT INTO user_wallets(user_id) VALUES (?) ON CONFLICT DO NOTHING",userId);
|
||||
BigDecimal balance=jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=? FOR UPDATE",BigDecimal.class,userId);
|
||||
BigDecimal next=balance.add(amount);
|
||||
if(expected!=null && balance.compareTo(expected)!=0) throw new ApiException("BALANCE_CHANGED","余额已变化,请刷新后重新确认调整",409);
|
||||
BigDecimal next=switch(operation) { case "decrease" -> balance.subtract(amount); case "set" -> amount; default -> balance.add(amount); };
|
||||
if(next.signum()<0) throw new ApiException("INSUFFICIENT_BALANCE","减少金额不能超过当前余额",409);
|
||||
BigDecimal delta=next.subtract(balance);
|
||||
UUID id=UUID.randomUUID();
|
||||
jdbc.update("UPDATE user_wallets SET balance=?, updated_at=now() WHERE user_id=?",next,userId);
|
||||
jdbc.update("INSERT INTO wallet_entries(id,user_id,actor_user_id,request_id,amount,balance_after,reason) VALUES (?,?,?,?,?,?,?)",id,userId,actor.id(),requestId,amount,next,reason);
|
||||
auth.audit("wallet.credit_granted",actor.id(),meta,Map.of("userId",userId,"entryId",id,"amount",amount,"currency","CNY","reason",reason));
|
||||
jdbc.update("INSERT INTO wallet_entries(id,user_id,actor_user_id,request_id,amount,balance_before,balance_after,reason,operation,requested_amount) VALUES (?,?,?,?,?,?,?,?,?,?)",id,userId,actor.id(),requestId,delta,balance,next,reason,operation,amount);
|
||||
auth.audit("increase".equals(operation)?"wallet.credit_granted":"wallet.credit_adjusted",actor.id(),meta,Map.of("userId",userId,"entryId",id,"amount",delta,"balanceBefore",balance,"balanceAfter",next,"operation",operation,"currency","CNY","reason",reason));
|
||||
return Map.of("id",id,"balance",next,"currency","CNY","replayed",false);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
-- Keep references for historical usage and in-flight tasks when removing catalog items or keys.
|
||||
ALTER TABLE model_providers ADD COLUMN deleted_at timestamptz;
|
||||
ALTER TABLE model_definitions ADD COLUMN deleted_at timestamptz;
|
||||
ALTER TABLE model_catalog ADD COLUMN deleted_at timestamptz;
|
||||
ALTER TABLE platform_api_keys ADD COLUMN deleted_at timestamptz;
|
||||
ALTER TABLE model_providers DROP CONSTRAINT model_providers_code_key;
|
||||
CREATE UNIQUE INDEX model_providers_live_code_idx ON model_providers(code) WHERE deleted_at IS NULL;
|
||||
DROP INDEX model_catalog_group_name_idx;
|
||||
CREATE UNIQUE INDEX model_catalog_group_name_idx ON model_catalog(logical_model_id, source_label) WHERE deleted_at IS NULL;
|
||||
|
||||
ALTER TABLE wallet_entries DROP CONSTRAINT wallet_entries_amount_check;
|
||||
ALTER TABLE wallet_entries ADD COLUMN operation varchar(16) NOT NULL DEFAULT 'increase'
|
||||
CHECK (operation IN ('increase', 'decrease', 'set'));
|
||||
ALTER TABLE wallet_entries ADD COLUMN requested_amount numeric(20,8);
|
||||
ALTER TABLE wallet_entries ADD COLUMN balance_before numeric(20,8);
|
||||
UPDATE wallet_entries SET requested_amount=amount, balance_before=balance_after-amount;
|
||||
ALTER TABLE wallet_entries ALTER COLUMN requested_amount SET NOT NULL;
|
||||
ALTER TABLE wallet_entries ALTER COLUMN balance_before SET NOT NULL;
|
||||
ALTER TABLE wallet_entries ADD CONSTRAINT wallet_entries_balance_check CHECK (balance_before >= 0 AND balance_after >= 0 AND balance_after=balance_before+amount);
|
||||
|
||||
ALTER TABLE usage_events ADD COLUMN endpoint varchar(160);
|
||||
ALTER TABLE usage_events ADD COLUMN call_mode varchar(16);
|
||||
ALTER TABLE usage_events ADD COLUMN request_source varchar(24);
|
||||
ALTER TABLE usage_events ADD COLUMN cached_input_units bigint;
|
||||
ALTER TABLE usage_events ADD COLUMN cache_write_units bigint;
|
||||
ALTER TABLE usage_events ADD COLUMN first_token_ms integer;
|
||||
ALTER TABLE usage_events ADD COLUMN reasoning_effort varchar(32);
|
||||
CREATE INDEX usage_events_user_key_created_idx ON usage_events(user_id, api_key_id, created_at DESC);
|
||||
Reference in new issue
Block a user