接口优化
This commit is contained in:
@@ -1,20 +1,17 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.emotion.common.PageResult;
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.PageRequest;
|
||||
import com.emotion.dto.response.BaseResponse;
|
||||
import com.emotion.entity.CozeApiCall;
|
||||
import com.emotion.dto.request.coze.CozeApiCallPageRequest;
|
||||
import com.emotion.dto.request.coze.CozeApiCallCreateRequest;
|
||||
import com.emotion.dto.request.coze.CozeApiCallUpdateRequest;
|
||||
import com.emotion.dto.response.coze.CozeApiCallResponse;
|
||||
import com.emotion.service.CozeApiCallService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* Coze API调用记录控制器
|
||||
@@ -29,229 +26,54 @@ public class CozeApiCallController {
|
||||
@Autowired
|
||||
private CozeApiCallService cozeApiCallService;
|
||||
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
/**
|
||||
* 分页查询API调用记录
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
public Result<PageResult<CozeApiCallResponse>> getPage(@Validated PageRequest request) {
|
||||
IPage<CozeApiCall> page = cozeApiCallService.getPage(request);
|
||||
List<CozeApiCallResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<CozeApiCallResponse> pageResult = new PageResult<>();
|
||||
pageResult.setCurrent(page.getCurrent());
|
||||
pageResult.setSize(page.getSize());
|
||||
pageResult.setTotal(page.getTotal());
|
||||
pageResult.setPages(page.getPages());
|
||||
pageResult.setRecords(responses);
|
||||
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据会话ID分页查询API调用记录
|
||||
*/
|
||||
@GetMapping("/conversation/{conversationId}/page")
|
||||
public Result<PageResult<CozeApiCallResponse>> getPageByConversationId(@PathVariable String conversationId, @Validated PageRequest request) {
|
||||
IPage<CozeApiCall> page = cozeApiCallService.getPageByConversationId(request, conversationId);
|
||||
List<CozeApiCallResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<CozeApiCallResponse> pageResult = new PageResult<>();
|
||||
pageResult.setCurrent(page.getCurrent());
|
||||
pageResult.setSize(page.getSize());
|
||||
pageResult.setTotal(page.getTotal());
|
||||
pageResult.setPages(page.getPages());
|
||||
pageResult.setRecords(responses);
|
||||
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID分页查询API调用记录
|
||||
*/
|
||||
@GetMapping("/user/{userId}/page")
|
||||
public Result<PageResult<CozeApiCallResponse>> getPageByUserId(@PathVariable String userId, @Validated PageRequest request) {
|
||||
IPage<CozeApiCall> page = cozeApiCallService.getPageByUserId(request, userId);
|
||||
List<CozeApiCallResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<CozeApiCallResponse> pageResult = new PageResult<>();
|
||||
pageResult.setCurrent(page.getCurrent());
|
||||
pageResult.setSize(page.getSize());
|
||||
pageResult.setTotal(page.getTotal());
|
||||
pageResult.setPages(page.getPages());
|
||||
pageResult.setRecords(responses);
|
||||
|
||||
@GetMapping(value = "/page")
|
||||
public Result<PageResult<CozeApiCallResponse>> getCozeApiCallPage(@Validated CozeApiCallPageRequest request) {
|
||||
PageResult<CozeApiCallResponse> pageResult = cozeApiCallService.getPage(request);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取API调用记录
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Result<CozeApiCallResponse> getById(@PathVariable String id) {
|
||||
CozeApiCall apiCall = cozeApiCallService.getById(id);
|
||||
if (apiCall == null) {
|
||||
@GetMapping(value = "/detail")
|
||||
public Result<CozeApiCallResponse> getCozeApiCallById(@RequestParam String id) {
|
||||
CozeApiCallResponse response = cozeApiCallService.getById(id);
|
||||
if (response == null) {
|
||||
return Result.notFound("API调用记录不存在");
|
||||
}
|
||||
return Result.success(convertToResponse(apiCall));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据Bot ID查询API调用记录
|
||||
*/
|
||||
@GetMapping("/bot/{botId}")
|
||||
public Result<List<CozeApiCallResponse>> getByBotId(@PathVariable String botId) {
|
||||
List<CozeApiCall> apiCalls = cozeApiCallService.getByBotId(botId);
|
||||
List<CozeApiCallResponse> responses = apiCalls.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据状态查询API调用记录
|
||||
*/
|
||||
@GetMapping("/status/{status}")
|
||||
public Result<List<CozeApiCallResponse>> getByStatus(@PathVariable String status) {
|
||||
List<CozeApiCall> apiCalls = cozeApiCallService.getByStatus(status);
|
||||
List<CozeApiCallResponse> responses = apiCalls.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据请求类型查询API调用记录
|
||||
*/
|
||||
@GetMapping("/request-type/{requestType}")
|
||||
public Result<List<CozeApiCallResponse>> getByRequestType(@PathVariable String requestType) {
|
||||
List<CozeApiCall> apiCalls = cozeApiCallService.getByRequestType(requestType);
|
||||
List<CozeApiCallResponse> responses = apiCalls.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计用户的API调用次数
|
||||
*/
|
||||
@GetMapping("/user/{userId}/count")
|
||||
public Result<Long> countByUserId(@PathVariable String userId) {
|
||||
Long count = cozeApiCallService.countByUserId(userId);
|
||||
return Result.success(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计Bot的API调用次数
|
||||
*/
|
||||
@GetMapping("/bot/{botId}/count")
|
||||
public Result<Long> countByBotId(@PathVariable String botId) {
|
||||
Long count = cozeApiCallService.countByBotId(botId);
|
||||
return Result.success(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计指定状态的API调用次数
|
||||
*/
|
||||
@GetMapping("/status/{status}/count")
|
||||
public Result<Long> countByStatus(@PathVariable String status) {
|
||||
Long count = cozeApiCallService.countByStatus(status);
|
||||
return Result.success(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计用户的Token使用量
|
||||
*/
|
||||
@GetMapping("/user/{userId}/tokens")
|
||||
public Result<Long> sumTokensByUserId(@PathVariable String userId) {
|
||||
Long totalTokens = cozeApiCallService.sumTokensByUserId(userId);
|
||||
return Result.success(totalTokens);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计用户的API调用费用
|
||||
*/
|
||||
@GetMapping("/user/{userId}/cost")
|
||||
public Result<java.math.BigDecimal> sumCostByUserId(@PathVariable String userId) {
|
||||
java.math.BigDecimal totalCost = cozeApiCallService.sumCostByUserId(userId);
|
||||
return Result.success(totalCost);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询失败的API调用记录
|
||||
*/
|
||||
@GetMapping("/failed")
|
||||
public Result<List<CozeApiCallResponse>> getFailedCalls() {
|
||||
List<CozeApiCall> apiCalls = cozeApiCallService.getFailedCalls();
|
||||
List<CozeApiCallResponse> responses = apiCalls.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询超时的API调用记录
|
||||
*/
|
||||
@GetMapping("/timeout")
|
||||
public Result<List<CozeApiCallResponse>> getTimeoutCalls() {
|
||||
List<CozeApiCall> apiCalls = cozeApiCallService.getTimeoutCalls();
|
||||
List<CozeApiCallResponse> responses = apiCalls.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据追踪ID查询API调用记录
|
||||
*/
|
||||
@GetMapping("/trace/{traceId}")
|
||||
public Result<CozeApiCallResponse> getByTraceId(@PathVariable String traceId) {
|
||||
CozeApiCall apiCall = cozeApiCallService.getByTraceId(traceId);
|
||||
if (apiCall == null) {
|
||||
return Result.notFound("API调用记录不存在");
|
||||
}
|
||||
return Result.success(convertToResponse(apiCall));
|
||||
return Result.success(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建API调用记录
|
||||
*/
|
||||
@PostMapping
|
||||
public Result<CozeApiCallResponse> create(@RequestBody CozeApiCall apiCall) {
|
||||
boolean saved = cozeApiCallService.save(apiCall);
|
||||
if (!saved) {
|
||||
return Result.error("创建失败");
|
||||
}
|
||||
return Result.success(convertToResponse(apiCall));
|
||||
@PostMapping(value = "/create")
|
||||
public Result<CozeApiCallResponse> createCozeApiCall(@RequestBody @Validated CozeApiCallCreateRequest request) {
|
||||
CozeApiCallResponse response = cozeApiCallService.create(request);
|
||||
return Result.success(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新API调用记录
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
public Result<CozeApiCallResponse> update(@PathVariable String id, @RequestBody CozeApiCall apiCall) {
|
||||
apiCall.setId(id);
|
||||
boolean updated = cozeApiCallService.updateById(apiCall);
|
||||
if (!updated) {
|
||||
return Result.error("更新失败");
|
||||
@PutMapping(value = "/update")
|
||||
public Result<CozeApiCallResponse> updateCozeApiCall(@RequestBody @Validated CozeApiCallUpdateRequest request) {
|
||||
CozeApiCallResponse response = cozeApiCallService.update(request);
|
||||
if (response == null) {
|
||||
return Result.error("更新失败,记录不存在");
|
||||
}
|
||||
CozeApiCall updatedApiCall = cozeApiCallService.getById(id);
|
||||
return Result.success(convertToResponse(updatedApiCall));
|
||||
return Result.success(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除API调用记录
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Void> delete(@PathVariable String id) {
|
||||
boolean deleted = cozeApiCallService.removeById(id);
|
||||
@DeleteMapping(value = "/delete")
|
||||
public Result<Void> deleteCozeApiCall(@RequestParam String id) {
|
||||
boolean deleted = cozeApiCallService.delete(id);
|
||||
if (!deleted) {
|
||||
return Result.error("删除失败");
|
||||
}
|
||||
@@ -259,49 +81,47 @@ public class CozeApiCallController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为响应对象
|
||||
* 统计用户的API调用次数
|
||||
*/
|
||||
private CozeApiCallResponse convertToResponse(CozeApiCall apiCall) {
|
||||
CozeApiCallResponse response = new CozeApiCallResponse();
|
||||
BeanUtils.copyProperties(apiCall, response);
|
||||
response.setId(apiCall.getId());
|
||||
if (apiCall.getCreateTime() != null) {
|
||||
response.setCreateTime(apiCall.getCreateTime().format(DATE_TIME_FORMATTER));
|
||||
}
|
||||
if (apiCall.getUpdateTime() != null) {
|
||||
response.setUpdateTime(apiCall.getUpdateTime().format(DATE_TIME_FORMATTER));
|
||||
}
|
||||
if (apiCall.getStartTime() != null) {
|
||||
response.setStartTime(apiCall.getStartTime().format(DATE_TIME_FORMATTER));
|
||||
}
|
||||
if (apiCall.getEndTime() != null) {
|
||||
response.setEndTime(apiCall.getEndTime().format(DATE_TIME_FORMATTER));
|
||||
}
|
||||
return response;
|
||||
@GetMapping(value = "/countByUser")
|
||||
public Result<Long> countByUserId(@RequestParam String userId) {
|
||||
Long count = cozeApiCallService.countByUserId(userId);
|
||||
return Result.success(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* API调用记录响应类
|
||||
* 统计Bot的API调用次数
|
||||
*/
|
||||
@lombok.Data
|
||||
@lombok.EqualsAndHashCode(callSuper = true)
|
||||
public static class CozeApiCallResponse extends BaseResponse {
|
||||
private String conversationId;
|
||||
private String messageId;
|
||||
private String userId;
|
||||
private String botId;
|
||||
private String requestType;
|
||||
private String requestUrl;
|
||||
private String requestBody;
|
||||
private Integer responseStatus;
|
||||
private String responseBody;
|
||||
private String aiReply;
|
||||
private Integer totalTokens;
|
||||
private java.math.BigDecimal cost;
|
||||
private String status;
|
||||
private String finalStatus;
|
||||
private String startTime;
|
||||
private String endTime;
|
||||
private String traceId;
|
||||
@GetMapping(value = "/countByBot")
|
||||
public Result<Long> countByBotId(@RequestParam String botId) {
|
||||
Long count = cozeApiCallService.countByBotId(botId);
|
||||
return Result.success(count);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计指定状态的API调用次数
|
||||
*/
|
||||
@GetMapping(value = "/countByStatus")
|
||||
public Result<Long> countByStatus(@RequestParam String status) {
|
||||
Long count = cozeApiCallService.countByStatus(status);
|
||||
return Result.success(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计用户的Token使用量
|
||||
*/
|
||||
@GetMapping(value = "/tokensByUser")
|
||||
public Result<Long> sumTokensByUserId(@RequestParam String userId) {
|
||||
Long totalTokens = cozeApiCallService.sumTokensByUserId(userId);
|
||||
return Result.success(totalTokens);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计用户的API调用费用
|
||||
*/
|
||||
@GetMapping(value = "/costByUser")
|
||||
public Result<BigDecimal> sumCostByUserId(@RequestParam String userId) {
|
||||
BigDecimal totalCost = cozeApiCallService.sumCostByUserId(userId);
|
||||
return Result.success(totalCost);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user