DataSourceController.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.agent.management.controller;
  2. import com.agent.management.common.Result;
  3. import com.agent.management.model.entity.DataSource;
  4. import com.agent.management.service.DataSourceService;
  5. import jakarta.validation.Valid;
  6. import lombok.RequiredArgsConstructor;
  7. import org.springframework.validation.annotation.Validated;
  8. import org.springframework.web.bind.annotation.*;
  9. import java.util.List;
  10. /**
  11. * 数据源 CRUD API(密码加密、不返回敏感字段)
  12. */
  13. @RestController
  14. @RequestMapping("/api/kb/datasources")
  15. @RequiredArgsConstructor
  16. @Validated
  17. public class DataSourceController {
  18. private final DataSourceService dataSourceService;
  19. private final com.agent.management.rag.structured.StructuredSchemaProfileService structuredProfiles;
  20. @GetMapping
  21. public Result<List<DataSource>> list() {
  22. return Result.success(dataSourceService.listAll());
  23. }
  24. @GetMapping("/{id}")
  25. public Result<DataSource> get(@PathVariable Long id) {
  26. return Result.success(dataSourceService.get(id));
  27. }
  28. @PostMapping
  29. public Result<DataSource> create(@Valid @RequestBody DataSource ds) {
  30. DataSource saved = dataSourceService.create(ds);
  31. structuredProfiles.prewarm(saved.getId());
  32. return Result.success(saved);
  33. }
  34. @PutMapping("/{id}")
  35. public Result<DataSource> update(@PathVariable Long id, @RequestBody DataSource ds) {
  36. DataSource saved = dataSourceService.update(id, ds);
  37. structuredProfiles.refreshAsync(id);
  38. return Result.success(saved);
  39. }
  40. @DeleteMapping("/{id}")
  41. public Result<Void> delete(@PathVariable Long id) {
  42. structuredProfiles.deleteProfiles(id);
  43. dataSourceService.delete(id);
  44. return Result.success(null);
  45. }
  46. @GetMapping("/{id}/rag-profile")
  47. public Result<java.util.Map<String,Object>> ragProfile(@PathVariable Long id) {
  48. dataSourceService.get(id);
  49. return Result.success(structuredProfiles.status(id));
  50. }
  51. @PostMapping("/{id}/rag-profile/refresh")
  52. public Result<java.util.Map<String,Object>> refreshRagProfile(@PathVariable Long id) {
  53. dataSourceService.get(id);
  54. structuredProfiles.refreshAsync(id);
  55. return Result.success(structuredProfiles.status(id));
  56. }
  57. /**
  58. * 测试连接(不持久化,可用于新增/编辑前的预检)
  59. */
  60. @PostMapping("/test")
  61. public Result<DataSourceService.TestResult> test(@RequestBody DataSource ds) {
  62. return Result.success(dataSourceService.testConnection(ds));
  63. }
  64. /**
  65. * 测试已存在数据源(持久化结果到 testStatus/testMessage)
  66. */
  67. @PostMapping("/{id}/test")
  68. public Result<DataSourceService.TestResult> testExisting(@PathVariable Long id) {
  69. return Result.success(dataSourceService.testExisting(id));
  70. }
  71. }