|
| 1 | +package com.navercorp.pinpoint.web.authorization.controller; |
| 2 | + |
| 3 | + |
| 4 | +import com.navercorp.pinpoint.common.server.response.Response; |
| 5 | +import com.navercorp.pinpoint.common.server.response.SimpleResponse; |
| 6 | +import com.navercorp.pinpoint.common.server.uid.ApplicationUid; |
| 7 | +import com.navercorp.pinpoint.common.server.uid.ServiceUid; |
| 8 | +import com.navercorp.pinpoint.common.server.util.time.Range; |
| 9 | +import com.navercorp.pinpoint.web.service.AgentListService; |
| 10 | +import com.navercorp.pinpoint.web.uid.service.ApplicationUidService; |
| 11 | +import com.navercorp.pinpoint.web.vo.agent.AgentListEntryAndStatus; |
| 12 | +import jakarta.validation.constraints.NotBlank; |
| 13 | +import jakarta.validation.constraints.PositiveOrZero; |
| 14 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 15 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 16 | +import org.springframework.web.bind.annotation.GetMapping; |
| 17 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 18 | +import org.springframework.web.bind.annotation.RequestParam; |
| 19 | +import org.springframework.web.bind.annotation.RestController; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.stream.Collectors; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +@RestController |
| 30 | +@RequestMapping("/api/uid/agentList") |
| 31 | +@ConditionalOnProperty(name = "pinpoint.web.application.uid.enable", havingValue = "true") |
| 32 | +public class UidAgentListController { |
| 33 | + |
| 34 | + private final AgentListService agentListService; |
| 35 | + private final ApplicationUidService applicationUidService; |
| 36 | + |
| 37 | + public UidAgentListController(AgentListService agentListService, ApplicationUidService applicationUidService) { |
| 38 | + this.agentListService = Objects.requireNonNull(agentListService, "agentListService"); |
| 39 | + this.applicationUidService = Objects.requireNonNull(applicationUidService, "applicationUidService"); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + @GetMapping() |
| 44 | + public List<AgentListEntryAndStatus> getAgentList(@RequestParam(value = "serviceUid", required = false, defaultValue = "0") int serviceUid, |
| 45 | + @RequestParam(value = "applicationName") @NotBlank String applicationName, |
| 46 | + @RequestParam(value = "orderBy", required = false, defaultValue = "NO_OP") String orderBy) { |
| 47 | + ServiceUid serviceUidObject = ServiceUid.of(serviceUid); |
| 48 | + |
| 49 | + ApplicationUid applicationUid = applicationUidService.getApplicationUid(serviceUidObject, applicationName); |
| 50 | + if (applicationUid == null) { |
| 51 | + return Collections.emptyList(); |
| 52 | + } |
| 53 | + |
| 54 | + return agentListService.getAgentList(serviceUidObject, applicationUid).stream() |
| 55 | + .sorted(OrderBy.of(orderBy)) |
| 56 | + .collect(Collectors.toList()); |
| 57 | + } |
| 58 | + |
| 59 | + @GetMapping(params = {"from", "to"}) |
| 60 | + public List<AgentListEntryAndStatus> getAgentList(@RequestParam(value = "serviceUid", required = false, defaultValue = "0") int serviceUid, |
| 61 | + @RequestParam(value = "applicationName") @NotBlank String applicationName, |
| 62 | + @RequestParam("from") @PositiveOrZero long from, |
| 63 | + @RequestParam("to") @PositiveOrZero long to, |
| 64 | + @RequestParam(value = "orderBy", required = false, defaultValue = "NO_OP") String orderBy) { |
| 65 | + ServiceUid serviceUidObject = ServiceUid.of(serviceUid); |
| 66 | + Range range = Range.between(from, to); |
| 67 | + |
| 68 | + ApplicationUid applicationUid = applicationUidService.getApplicationUid(serviceUidObject, applicationName); |
| 69 | + if (applicationUid == null) { |
| 70 | + return Collections.emptyList(); |
| 71 | + } |
| 72 | + return agentListService.getActiveAgentList(serviceUidObject, applicationUid, range).stream() |
| 73 | + .sorted(OrderBy.of(orderBy)) |
| 74 | + .collect(Collectors.toList()); |
| 75 | + } |
| 76 | + |
| 77 | + @DeleteMapping() |
| 78 | + public Response deleteAgents(@RequestParam(value = "serviceUid", required = false, defaultValue = "0") int serviceUid, |
| 79 | + @RequestParam(value = "applicationName") @NotBlank String applicationName) { |
| 80 | + ServiceUid serviceUidObject = ServiceUid.of(serviceUid); |
| 81 | + |
| 82 | + ApplicationUid applicationUid = applicationUidService.getApplicationUid(serviceUidObject, applicationName); |
| 83 | + if (applicationUid == null) { |
| 84 | + return SimpleResponse.ok(); |
| 85 | + } |
| 86 | + agentListService.deleteAgents(serviceUidObject, applicationUid); |
| 87 | + return SimpleResponse.ok(); |
| 88 | + } |
| 89 | + |
| 90 | + @DeleteMapping(params = {"agentId"}) |
| 91 | + public Response deleteAgent(@RequestParam(value = "serviceUid", required = false, defaultValue = "0") int serviceUid, |
| 92 | + @RequestParam(value = "applicationName") @NotBlank String applicationName, |
| 93 | + @RequestParam(value = "agentId") @NotBlank String agentId) { |
| 94 | + ServiceUid serviceUidObject = ServiceUid.of(serviceUid); |
| 95 | + |
| 96 | + ApplicationUid applicationUid = applicationUidService.getApplicationUid(serviceUidObject, applicationName); |
| 97 | + if (applicationUid == null) { |
| 98 | + return SimpleResponse.ok(); |
| 99 | + } |
| 100 | + agentListService.deleteAgent(serviceUidObject, applicationUid, agentId); |
| 101 | + return SimpleResponse.ok(); |
| 102 | + } |
| 103 | + |
| 104 | + private enum OrderBy implements Comparator<AgentListEntryAndStatus> { |
| 105 | + NO_OP(Comparator.comparing(o -> 0)), |
| 106 | + NAME_ASC(Comparator.comparing(o -> o.getAgentListEntry().getAgentName())), |
| 107 | + NAME_DESC(NAME_ASC.reversed()), |
| 108 | + ID_ASC(Comparator.comparing(o -> o.getAgentListEntry().getAgentId())), |
| 109 | + ID_DESC(ID_ASC.reversed()), |
| 110 | + START_TIME_ASC(Comparator.comparingLong(o -> o.getAgentListEntry().getStartTimestamp())), |
| 111 | + START_TIME_DESC(START_TIME_ASC.reversed()); |
| 112 | + |
| 113 | + private static final Map<String, OrderBy> MAP = |
| 114 | + Stream.of(values()).collect(Collectors.toMap(Enum::name, e -> e)); |
| 115 | + |
| 116 | + public static OrderBy of(String value) { |
| 117 | + return MAP.getOrDefault(value.toUpperCase(), NO_OP); |
| 118 | + } |
| 119 | + |
| 120 | + private final Comparator<AgentListEntryAndStatus> comparator; |
| 121 | + |
| 122 | + OrderBy(Comparator<AgentListEntryAndStatus> comparator) { |
| 123 | + this.comparator = comparator; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public int compare(AgentListEntryAndStatus o1, AgentListEntryAndStatus o2) { |
| 128 | + return comparator.compare(o1, o2); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments