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
+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 {
|
||||
|
||||
Reference in new issue
Block a user