Skip to content

Commit

Permalink
[Release 모니터링 API 수정 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
1jeongg authored Oct 4, 2024
2 parents 70cb667 + a071300 commit e9273cb
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public enum BaseException {
POWER_SAVING_MODE_CHANGE_FAIL("파워 세이빙 모드로 바꾸는 데 실패했습니다.", HttpStatus.INTERNAL_SERVER_ERROR),
SSH_CONNECTION_FAIL("SSH 연결에 실패했습니다.", HttpStatus.INTERNAL_SERVER_ERROR),

// 모니터링
MONITOR_PERMISSION_NONE("모니터링 권한이 없습니다.", HttpStatus.FORBIDDEN),

;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public ResponseEntity<?> monitorById(@RequestParam(value = "routerId") Long rout
return ResponseEntity.ok().body(ApiUtils.success(response));
}

// 관리자가 속한 라우터 그룹의 모든 라우터 조회
@GetMapping("/routers")
public ResponseEntity<?> findAllRouter(@AuthenticationPrincipal CustomUserDetails userDetails) {
MonitorResponse.FindAllRouter response = monitoringService.findAllRouter(userDetails.getUser());
return ResponseEntity.ok().body(ApiUtils.success(response));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ public record MonitorGraph(
) {
}

public record FindAllRouter (
List<MonitorRouter> routerList
) {
}

public record MonitorRouter(
Long routerId,
String name,
String instance
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.wasin.wasin.domain._const.Metric;
import com.wasin.wasin.domain.dto.MonitorResponse;
import com.wasin.wasin.domain.dto.RouterResponse;
import com.wasin.wasin.domain.entity.Router;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -42,4 +43,16 @@ private List<MonitorResponse.MonitorMetric> getMetricList() {
)
).toList();
}

public MonitorResponse.FindAllRouter toRouterListDTO(List<Router> routerList) {
return new MonitorResponse.FindAllRouter(
routerList.stream().map(it ->
new MonitorResponse.MonitorRouter(
it.getId(),
it.getName(),
it.getInstance()
)
).toList()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.wasin.wasin.domain.validation;

import com.wasin.wasin._core.exception.BaseException;
import com.wasin.wasin._core.exception.error.ForbiddenException;
import com.wasin.wasin.domain.entity.Router;
import com.wasin.wasin.domain.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MonitoringValidation {


public void checkRouterExistInGroup(Router router, User user) {
Long routerCompanyId = router.getCompany().getId();
Long userCompanyId = user.getCompany().getId();
if (!routerCompanyId.equals(userCompanyId)) {
throw new ForbiddenException(BaseException.MONITOR_PERMISSION_NONE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
public interface MonitoringService {

MonitorResponse.FindById monitorById(Long routerId, Long metricId, Long time, User user);

MonitorResponse.FindAllRouter findAllRouter(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,58 @@
import com.wasin.wasin.domain._const.Metric;
import com.wasin.wasin.domain.dto.MonitorResponse;
import com.wasin.wasin.domain.dto.RouterResponse;
import com.wasin.wasin.domain.entity.Company;
import com.wasin.wasin.domain.entity.Router;
import com.wasin.wasin.domain.entity.User;
import com.wasin.wasin.domain.mapper.MonitoringMapper;
import com.wasin.wasin.domain.validation.MonitoringValidation;
import com.wasin.wasin.repository.RouterJPARepository;
import com.wasin.wasin.repository.UserJPARepository;
import com.wasin.wasin.service.MonitoringService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
public class MonitoringServiceImpl implements MonitoringService {

private final UserJPARepository userJPARepository;
private final RouterJPARepository routerJPARepository;
private final MonitoringApiUtil monitoringApiUtil;
private final MonitoringMapper monitoringMapper;
private final MonitoringValidation monitoringValidation;

public MonitorResponse.FindById monitorById(Long routerId, Long metricId, Long time, User user) {
// default values
public MonitorResponse.FindById monitorById(Long routerId, Long metricId, Long time, User _user) {
if (metricId == null) metricId = 0L;
if (time == null) time = 60L;

Metric metric = Metric.findByMetricId(metricId);
Router router = findByRouterId(routerId);
User user = findUserByUserId(_user.getId());

monitoringValidation.checkRouterExistInGroup(router, user);
RouterResponse.MonitorResult results = monitoringApiUtil.getMetric(metric, router, time);

return monitoringMapper.resultToDTO(results, metricId, time);
}

public MonitorResponse.FindAllRouter findAllRouter(User _user) {
User user = findUserByUserId(_user.getId());
Company company = user.getCompany();
List<Router> routerList = routerJPARepository.findAllRouterByCompanyId(company.getId());
return monitoringMapper.toRouterListDTO(routerList);
}

private User findUserByUserId(Long userId) {
return userJPARepository.findActiveUserWithCompanyById(userId).orElseThrow(
() -> new NotFoundException(BaseException.USER_NOT_FOUND)
);
}

private Router findByRouterId(Long routerId) {
return routerJPARepository.findById(routerId).orElseThrow(
() -> new NotFoundException(BaseException.ROUTER_NOT_EXIST_IN_DB)
Expand Down

0 comments on commit e9273cb

Please sign in to comment.