|
|
@@ -0,0 +1,157 @@
|
|
|
+package cn.seecoder.devmanage.service.impl;
|
|
|
+
|
|
|
+import cn.seecoder.devmanage.model.po.LimitRequest;
|
|
|
+import cn.seecoder.devmanage.model.vo.LimitRequestVO;
|
|
|
+import cn.seecoder.devmanage.repository.LimitRequestRepository;
|
|
|
+import cn.seecoder.devmanage.service.LimitRequestService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.data.domain.*;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class LimitRequestServiceImpl implements LimitRequestService {
|
|
|
+
|
|
|
+ private final LimitRequestRepository limitRequestRepository;
|
|
|
+ private final int[] validPageSizes = new int[]{5, 10, 20};
|
|
|
+ private final String[] HANDLED_MATCHES = {"已处理", "handled", "handle", "已审阅", "已操作"};
|
|
|
+ private final String[] UNHANDLED_MATCHES = {"未处理", "unhandled", "unhandle", "未审阅", "未操作"};
|
|
|
+ private final String[] PERMITTED_MATCHES = {"已允许", "允许", "permitted", "permit", "已获准", "获准", "已准许", "准许"};
|
|
|
+ private final String[] DENIED_MATCHES = {"未允许", "未获准", "未准许", "deny", "denied", "unpermitted", "unpermit"};
|
|
|
+ private final int ALL_REQUEST = 0;
|
|
|
+ private final int ONLY_HANDLED = 1;
|
|
|
+ private final int ONLY_UNHANDLED = 2;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public LimitRequestVO newLimitRequest(String namespace, String name, Integer maxCpu, Integer maxMem) {
|
|
|
+ String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
+ LimitRequest limitRequest =
|
|
|
+ new LimitRequest(username, namespace, name, maxMem, maxCpu, false, false, System.currentTimeMillis());
|
|
|
+ LimitRequest save = limitRequestRepository.save(limitRequest);
|
|
|
+ return new LimitRequestVO(save);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<LimitRequestVO> getPagedLimitRequests(Integer pageNo, Integer pageSize, boolean descByTime, int handledSwitch) {
|
|
|
+ if (isInvalidPageSize(pageSize)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ Page<LimitRequest> results;
|
|
|
+ Sort sort = descByTime ? Sort.by("timestamp").descending() : Sort.by("timestamp").ascending();
|
|
|
+ if (handledSwitch == ALL_REQUEST) {
|
|
|
+ results = limitRequestRepository.findAll(PageRequest.of(pageNo, pageSize, sort));
|
|
|
+ } else if (handledSwitch == ONLY_HANDLED || handledSwitch == ONLY_UNHANDLED) {
|
|
|
+ boolean handled = handledSwitch == ONLY_HANDLED;
|
|
|
+ LimitRequest probe = new LimitRequest();
|
|
|
+ probe.setHandled(handled);
|
|
|
+ ExampleMatcher matcher = ExampleMatcher.matchingAll()
|
|
|
+ .withMatcher("handled", ExampleMatcher.GenericPropertyMatchers.exact());
|
|
|
+ Example<LimitRequest> example = Example.of(probe, matcher);
|
|
|
+ results = limitRequestRepository.findAll(example, PageRequest.of(pageNo, pageSize, sort));
|
|
|
+ } else {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ List<LimitRequestVO> ans = new ArrayList<>(pageSize);
|
|
|
+ for (LimitRequest po: results) {
|
|
|
+ ans.add(new LimitRequestVO(po));
|
|
|
+ }
|
|
|
+ return ans;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<LimitRequestVO> searchPagedLimitRequests(Integer pageNo, Integer pageSize, String keyword, boolean descByTime) {
|
|
|
+ if (isInvalidPageSize(pageSize) || keyword == null) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ Sort sort = descByTime ? Sort.by("timestamp").descending() : Sort.by("timestamp").ascending();
|
|
|
+ LimitRequest probe = new LimitRequest();
|
|
|
+ probe.setRequester(keyword);
|
|
|
+ probe.setNamespace(keyword);
|
|
|
+ probe.setName(keyword);
|
|
|
+ // String keywords
|
|
|
+ ExampleMatcher matcher = ExampleMatcher.matchingAny()
|
|
|
+ .withIgnoreCase()
|
|
|
+ .withMatcher("requester", ExampleMatcher.GenericPropertyMatchers.contains())
|
|
|
+ .withMatcher("namespace", ExampleMatcher.GenericPropertyMatchers.contains())
|
|
|
+ .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
|
|
|
+ // Long keyword
|
|
|
+ if (isParsableLong(keyword)) {
|
|
|
+ probe.setId(Long.parseLong(keyword));
|
|
|
+ matcher = matcher.withMatcher("id", ExampleMatcher.GenericPropertyMatchers.exact());
|
|
|
+ }
|
|
|
+ // Integer keywords
|
|
|
+ if (isParsableInteger(keyword)) {
|
|
|
+ int intKey = Integer.parseInt(keyword);
|
|
|
+ probe.setMaxMem(intKey);
|
|
|
+ probe.setMaxCpu(intKey);
|
|
|
+ matcher = matcher
|
|
|
+ .withMatcher("maxMem", ExampleMatcher.GenericPropertyMatchers.exact())
|
|
|
+ .withMatcher("maxCpu", ExampleMatcher.GenericPropertyMatchers.exact());
|
|
|
+ }
|
|
|
+ // Handled
|
|
|
+ if (Arrays.asList(HANDLED_MATCHES).contains(keyword)) {
|
|
|
+ probe.setHandled(true);
|
|
|
+ matcher = matcher.withMatcher("handled", ExampleMatcher.GenericPropertyMatchers.exact());
|
|
|
+ } else if (Arrays.asList(UNHANDLED_MATCHES).contains(keyword)) {
|
|
|
+ probe.setHandled(false);
|
|
|
+ matcher = matcher.withMatcher("handled", ExampleMatcher.GenericPropertyMatchers.exact());
|
|
|
+ }
|
|
|
+ // Permitted
|
|
|
+ if (Arrays.asList(PERMITTED_MATCHES).contains(keyword)) {
|
|
|
+ probe.setPermitted(true);
|
|
|
+ matcher = matcher.withMatcher("permitted", ExampleMatcher.GenericPropertyMatchers.exact());
|
|
|
+ } else if (Arrays.asList(DENIED_MATCHES).contains(keyword)) {
|
|
|
+ probe.setPermitted(false);
|
|
|
+ matcher = matcher.withMatcher("permitted", ExampleMatcher.GenericPropertyMatchers.exact());
|
|
|
+ }
|
|
|
+ Example<LimitRequest> example = Example.of(probe, matcher);
|
|
|
+ Page<LimitRequest> results = limitRequestRepository.findAll(example, PageRequest.of(pageNo, pageSize, sort));
|
|
|
+ List<LimitRequestVO> ans = new ArrayList<>();
|
|
|
+ for (LimitRequest po: results) {
|
|
|
+ ans.add(new LimitRequestVO(po));
|
|
|
+ }
|
|
|
+ return ans;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public LimitRequestVO permitRequest(Long limitRequestId) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void denyRequest(Long limitRequest) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isParsableLong(String keyword) {
|
|
|
+ try {
|
|
|
+ Long.parseLong(keyword);
|
|
|
+ return true;
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isParsableInteger(String keyword) {
|
|
|
+ try {
|
|
|
+ Integer.parseInt(keyword);
|
|
|
+ return true;
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isInvalidPageSize(Integer pageSize) {
|
|
|
+ for (int validPageSize: validPageSizes) {
|
|
|
+ if (validPageSize == pageSize) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|