-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallAnalysisService.java
More file actions
178 lines (149 loc) · 6.87 KB
/
Copy pathCallAnalysisService.java
File metadata and controls
178 lines (149 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.crimeLink.analyzer.service;
import com.crimeLink.analyzer.util.LogSanitizer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
/**
* Service for communicating with the Call Analysis ML microservice.
* Acts as a proxy/gateway layer, forwarding requests from the Spring Boot
* monolith to the Python FastAPI microservice.
*
* Architecture: Frontend -> Spring Boot (this service) -> Python ML Service
*/
@Service
@Slf4j
public class CallAnalysisService {
private final RestTemplate restTemplate;
private final ObjectMapper objectMapper;
@Value("${python.call-analysis.url}")
private String callAnalysisServiceUrl;
public CallAnalysisService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
this.objectMapper = new ObjectMapper();
}
/**
* Analyze a single call record PDF.
*
* @param file PDF file containing call records
* @return JSON response with analysis results
*/
public JsonNode analyzeCallRecord(MultipartFile file) {
log.info("Forwarding call record analysis to ML service: {}", LogSanitizer.sanitize(file.getOriginalFilename()));
String url = callAnalysisServiceUrl + "/analyze";
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
// Extract file bytes with explicit error handling
byte[] fileBytes;
try {
fileBytes = file.getBytes();
} catch (IOException e) {
log.error("Failed to read file bytes from uploaded file '{}': {}",
LogSanitizer.sanitize(file.getOriginalFilename()), e.getMessage());
throw new RuntimeException("Failed to read uploaded file contents", e);
}
body.add("file", new ByteArrayResource(fileBytes) {
@Override
public String getFilename() {
return file.getOriginalFilename();
}
});
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<String> response = restTemplate.exchange(
url,
HttpMethod.POST,
requestEntity,
String.class
);
log.info("ML service responded with status: {}", response.getStatusCode());
return objectMapper.readTree(response.getBody());
} catch (RestClientException e) {
log.error("Failed to communicate with call analysis service: {}", e.getMessage());
throw new RuntimeException("Call analysis service unavailable: " + e.getMessage(), e);
} catch (IOException e) {
log.error("Failed to process response from ML service: {}", e.getMessage());
throw new RuntimeException("Failed to process ML service response: " + e.getMessage(), e);
}
}
/**
* Analyze multiple call record PDFs in batch.
*
* @param files List of PDF files
* @return JSON response with batch analysis results
*/
public JsonNode analyzeBatch(List<MultipartFile> files) {
log.info("Forwarding batch call analysis to ML service: {} files", files.size());
String url = callAnalysisServiceUrl + "/analyze/batch";
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
for (MultipartFile file : files) {
// Extract file bytes with explicit error handling
byte[] fileBytes;
try {
fileBytes = file.getBytes();
} catch (IOException e) {
log.error("Failed to read file bytes from uploaded file '{}': {}",
LogSanitizer.sanitize(file.getOriginalFilename()), e.getMessage());
throw new RuntimeException("Failed to read uploaded file: " + file.getOriginalFilename(), e);
}
final String originalFilename = file.getOriginalFilename();
body.add("files", new ByteArrayResource(fileBytes) {
@Override
public String getFilename() {
return originalFilename;
}
});
}
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<String> response = restTemplate.exchange(
url,
HttpMethod.POST,
requestEntity,
String.class
);
log.info("Batch analysis completed successfully");
return objectMapper.readTree(response.getBody());
} catch (RestClientException e) {
log.error("Failed to communicate with call analysis service: {}", e.getMessage());
throw new RuntimeException("Call analysis service unavailable: " + e.getMessage(), e);
} catch (IOException e) {
log.error("Failed to process files: {}", e.getMessage());
throw new RuntimeException("Failed to process files: " + e.getMessage(), e);
}
}
/**
* Check health status of the call analysis ML service.
*
* @return Health status JSON
*/
public JsonNode checkHealth() {
String url = callAnalysisServiceUrl + "/health";
try {
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
return objectMapper.readTree(response.getBody());
} catch (RestClientException e) {
log.warn("Call analysis service health check failed: {}", e.getMessage());
return objectMapper.createObjectNode()
.put("status", "unhealthy")
.put("error", e.getMessage());
} catch (IOException e) {
return objectMapper.createObjectNode()
.put("status", "unhealthy")
.put("error", "Invalid response");
}
}
}