-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceInsightController.java
More file actions
114 lines (101 loc) · 4.64 KB
/
ServiceInsightController.java
File metadata and controls
114 lines (101 loc) · 4.64 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
package apu.saerok_admin.web;
import apu.saerok_admin.web.serviceinsight.ServiceInsightService;
import apu.saerok_admin.web.view.Breadcrumb;
import apu.saerok_admin.web.view.ServiceInsightViewModel;
import apu.saerok_admin.web.view.ToastMessage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestClientResponseException;
@Controller
public class ServiceInsightController {
private static final Logger log = LoggerFactory.getLogger(ServiceInsightController.class);
private static final String ERROR_TOAST_ID = "toastServiceInsightError";
private final ServiceInsightService serviceInsightService;
private final ObjectMapper objectMapper;
public ServiceInsightController(ServiceInsightService serviceInsightService, ObjectMapper objectMapper) {
this.serviceInsightService = serviceInsightService;
// ObjectMapper 설정 강화
this.objectMapper = objectMapper.copy()
.registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
@GetMapping("/service-insight")
public String serviceInsight(Model model) {
model.addAttribute("pageTitle", "서비스 인사이트");
model.addAttribute("activeMenu", "serviceInsight");
model.addAttribute("breadcrumbs", List.of(
Breadcrumb.of("대시보드", "/"),
Breadcrumb.active("서비스 인사이트")
));
ensureToastMessages(model);
ServiceInsightViewModel viewModel;
try {
viewModel = serviceInsightService.loadViewModel();
log.info("Successfully loaded service insight view model with {} metrics",
viewModel.metricOptions().size());
} catch (RestClientResponseException exception) {
log.warn(
"Failed to load service insight stats. status={}, body={}",
exception.getStatusCode(),
exception.getResponseBodyAsString(),
exception
);
viewModel = serviceInsightService.defaultViewModel();
attachErrorToast(model);
} catch (RestClientException | IllegalStateException exception) {
log.warn("Failed to load service insight stats.", exception);
viewModel = serviceInsightService.defaultViewModel();
attachErrorToast(model);
}
model.addAttribute("serviceInsight", viewModel);
String chartDataJson = toJson(viewModel);
model.addAttribute("chartDataJson", chartDataJson);
log.debug("Chart data JSON length: {}", chartDataJson.length());
return "service-insight/index";
}
private void ensureToastMessages(Model model) {
if (!model.containsAttribute("toastMessages")) {
model.addAttribute("toastMessages", List.of());
}
}
private void attachErrorToast(Model model) {
ToastMessage errorToast = new ToastMessage(
ERROR_TOAST_ID,
"데이터 로드 실패",
"통계 데이터를 불러오지 못했습니다. 잠시 후 다시 시도해주세요.",
"danger",
false
);
List<ToastMessage> messages = new ArrayList<>();
Object existing = model.getAttribute("toastMessages");
if (existing instanceof List<?> list) {
for (Object item : list) {
if (item instanceof ToastMessage toastMessage && !ERROR_TOAST_ID.equals(toastMessage.id())) {
messages.add(toastMessage);
}
}
}
messages.add(errorToast);
model.addAttribute("toastMessages", List.copyOf(messages));
}
private String toJson(ServiceInsightViewModel viewModel) {
try {
String json = objectMapper.writeValueAsString(viewModel);
log.debug("Serialized view model to JSON successfully");
return json;
} catch (JsonProcessingException exception) {
log.error("Failed to serialize service insight payload.", exception);
return "{\"metricOptions\":[],\"series\":[],\"componentLabels\":{}}";
}
}
}