|
1 | 1 | package io.sentrius.agent.services; |
2 | 2 |
|
| 3 | +import io.sentrius.sso.core.dto.UserDTO; |
| 4 | +import io.sentrius.sso.core.dto.agents.AgentExecution; |
3 | 5 | import io.sentrius.sso.core.dto.feedback.AgentFeedbackDTO; |
4 | 6 | import io.sentrius.sso.core.dto.feedback.FeedbackSubmissionDTO; |
| 7 | +import io.sentrius.sso.core.exceptions.ZtatException; |
5 | 8 | import io.sentrius.sso.core.feedback.FeedbackType; |
| 9 | +import io.sentrius.sso.core.services.agents.AgentClientService; |
| 10 | +import io.sentrius.sso.core.services.agents.AgentExecutionService; |
| 11 | +import io.sentrius.sso.core.services.agents.ZeroTrustClientService; |
| 12 | +import io.sentrius.sso.core.utils.JsonUtil; |
6 | 13 | import lombok.extern.slf4j.Slf4j; |
| 14 | +import org.springframework.beans.factory.annotation.Value; |
7 | 15 | import org.springframework.http.*; |
8 | 16 | import org.springframework.stereotype.Service; |
9 | 17 | import org.springframework.web.client.RestTemplate; |
|
18 | 26 | @Slf4j |
19 | 27 | @Service |
20 | 28 | public class AgentFeedbackClient { |
21 | | - |
22 | | - private final RestTemplate restTemplate; |
23 | | - private final String apiBaseUrl; |
24 | | - private final TokenProvider tokenProvider; |
25 | | - |
26 | | - public AgentFeedbackClient(RestTemplate restTemplate, String apiBaseUrl, TokenProvider tokenProvider) { |
27 | | - this.restTemplate = restTemplate; |
28 | | - this.apiBaseUrl = apiBaseUrl; |
29 | | - this.tokenProvider = tokenProvider; |
| 29 | + |
| 30 | + private final AgentExecution agentExecution; |
| 31 | + @Value("${agent.api.url:http://localhost:8080}") |
| 32 | + private String apiUrl; |
| 33 | + |
| 34 | + @Value("${agents.analytics.name:analytics-agent}") |
| 35 | + private String agentName; |
| 36 | + |
| 37 | + private final AgentExecutionService agentExecutionService; |
| 38 | + private final ZeroTrustClientService zeroTrustClientService; |
| 39 | + |
| 40 | + public AgentFeedbackClient(AgentExecutionService agentExecutionService, ZeroTrustClientService zeroTrustClientService) { |
| 41 | + this.agentExecutionService = agentExecutionService; |
| 42 | + this.zeroTrustClientService = zeroTrustClientService; |
| 43 | + |
| 44 | + UserDTO user = UserDTO.builder() |
| 45 | + .username(agentName) |
| 46 | + .build(); |
| 47 | + |
| 48 | + agentExecution = agentExecutionService.getAgentExecution(user); |
30 | 49 | } |
31 | | - |
| 50 | + |
32 | 51 | /** |
33 | 52 | * Submit feedback for this agent. |
34 | 53 | */ |
35 | 54 | public AgentFeedbackDTO submitFeedback( |
36 | | - String agentId, |
37 | | - FeedbackType feedbackType, |
38 | | - String feedbackText, |
39 | | - String behaviorCategory, |
40 | | - String context) { |
41 | | - |
42 | | - String url = apiBaseUrl + "/api/v1/feedback/submit"; |
43 | | - |
| 55 | + String agentId, |
| 56 | + FeedbackType feedbackType, |
| 57 | + String feedbackText, |
| 58 | + String behaviorCategory, |
| 59 | + String context) throws ZtatException { |
| 60 | + |
| 61 | + String url = "/api/v1/feedback/submit"; |
| 62 | + |
44 | 63 | FeedbackSubmissionDTO submission = FeedbackSubmissionDTO.builder() |
45 | 64 | .agentId(agentId) |
46 | 65 | .feedbackType(feedbackType) |
47 | 66 | .feedbackText(feedbackText) |
48 | 67 | .behaviorCategory(behaviorCategory) |
49 | 68 | .context(context) |
50 | 69 | .build(); |
51 | | - |
52 | | - HttpHeaders headers = new HttpHeaders(); |
53 | | - headers.setContentType(MediaType.APPLICATION_JSON); |
54 | | - headers.setBearerAuth(tokenProvider.getToken()); |
55 | | - |
56 | | - HttpEntity<FeedbackSubmissionDTO> request = new HttpEntity<>(submission, headers); |
57 | | - |
58 | | - try { |
59 | | - ResponseEntity<AgentFeedbackDTO> response = restTemplate.exchange( |
60 | | - url, HttpMethod.POST, request, AgentFeedbackDTO.class |
61 | | - ); |
62 | | - |
63 | | - log.info("Feedback submitted: agentId={}, type={}, id={}", |
64 | | - agentId, feedbackType, response.getBody().getId()); |
65 | | - |
66 | | - return response.getBody(); |
67 | | - } catch (Exception e) { |
68 | | - log.error("Failed to submit feedback: {}", e.getMessage(), e); |
69 | | - throw new RuntimeException("Failed to submit feedback", e); |
70 | | - } |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + var resp = zeroTrustClientService.callPostOnApi(url, submission); |
| 74 | + |
| 75 | + AgentFeedbackDTO responseBody = JsonUtil.MAPPER.convertValue(resp, AgentFeedbackDTO.class); |
| 76 | + |
| 77 | + log.info("Feedback submitted: agentId={}, type={}, id={}", |
| 78 | + agentId, feedbackType, responseBody.getId()); |
| 79 | + |
| 80 | + return responseBody; |
71 | 81 | } |
72 | | - |
| 82 | + |
73 | 83 | /** |
74 | 84 | * Get feedback statistics for this agent. |
75 | 85 | */ |
76 | | - public Map<String, Object> getFeedbackStatistics(String agentId, int days) { |
77 | | - String url = apiBaseUrl + "/api/v1/feedback/agent/" + agentId + "/statistics?days=" + days; |
78 | | - |
79 | | - HttpHeaders headers = new HttpHeaders(); |
80 | | - headers.setBearerAuth(tokenProvider.getToken()); |
81 | | - |
82 | | - HttpEntity<Void> request = new HttpEntity<>(headers); |
83 | | - |
84 | | - try { |
85 | | - ResponseEntity<Map> response = restTemplate.exchange( |
86 | | - url, HttpMethod.GET, request, Map.class |
87 | | - ); |
88 | | - |
89 | | - return (Map<String, Object>) response.getBody(); |
90 | | - } catch (Exception e) { |
91 | | - log.error("Failed to get feedback statistics: {}", e.getMessage(), e); |
92 | | - throw new RuntimeException("Failed to get feedback statistics", e); |
93 | | - } |
| 86 | + public Map<String, Object> getFeedbackStatistics(String agentId, int days) throws ZtatException { |
| 87 | + String url = "/api/v1/feedback/agent/" + agentId + "/statistics?days=" + days; |
| 88 | + |
| 89 | + String resp = zeroTrustClientService.callAuthenticatedGetOnApi(apiUrl, url); |
| 90 | + return JsonUtil.MAPPER.convertValue(resp, Map.class); |
94 | 91 | } |
95 | | - |
| 92 | + |
96 | 93 | /** |
97 | 94 | * Get feedback score for this agent (0-100). |
98 | 95 | */ |
99 | | - public Double getFeedbackScore(String agentId) { |
| 96 | + public Double getFeedbackScore(String agentId) throws ZtatException { |
100 | 97 | Map<String, Object> stats = getFeedbackStatistics(agentId, 30); |
101 | 98 | Object scoreObj = stats.get("feedback_score"); |
102 | | - |
| 99 | + |
103 | 100 | if (scoreObj instanceof Number) { |
104 | 101 | return ((Number) scoreObj).doubleValue(); |
105 | 102 | } |
106 | | - |
| 103 | + |
107 | 104 | return 50.0; // Default neutral score |
108 | 105 | } |
109 | | - |
| 106 | + |
110 | 107 | /** |
111 | 108 | * Interface for providing authentication tokens. |
112 | 109 | */ |
|
0 commit comments