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