jtoms 4 лет назад
Родитель
Сommit
0cf4cc7ab9

+ 185 - 133
src/main/java/com/zhiqiyun/open/mvc/controller/AppKeyInfoController.java

@@ -29,6 +29,7 @@ import org.springframework.web.bind.annotation.RestController;
 
 
 import javax.validation.Valid;
 import javax.validation.Valid;
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
 
 
@@ -36,137 +37,188 @@ import java.util.Map;
 @RequestMapping("/appKey")
 @RequestMapping("/appKey")
 public class AppKeyInfoController {
 public class AppKeyInfoController {
 
 
-	@Autowired
-	private AppKeyInfoService appKeyInfoService;
-
-	@Autowired
-	private SequenceService sequenceService;
-
-	@Autowired
-	private ApiRequestLogService apiRequestLogService;
-
-	@Autowired
-	private RouterService routerService;
-
-	@Permission(value = "app.key.find", tags = "查询AppKey")
-	@PostMapping("/findPage")
-	public Result findPage(@RequestBody QueryAppKeyInfoParams params) {
-
-		QueryWrapper<AppKeyInfo> queryWrapper = new QueryWrapper<>();
-		if (params.getId() != null) {
-			queryWrapper.eq("id", params.getId());
-		}
-		if (params.getIsEnable() != null) {
-			queryWrapper.eq("is_enable", params.getIsEnable());
-		}
-		if (StringUtils.isNotBlank(params.getRemark())) {
-			queryWrapper.like("remark", params.getRemark());
-		}
-
-		Page<AppKeyInfo> page = params.getPage();
-		page.addOrder(OrderItem.desc("created_time"));
-
-		Page<AppKeyInfo> resultData = this.appKeyInfoService.page(page, queryWrapper);
-
-		return Result.instance(Result.Code.SUCCESS).setData(resultData);
-	}
-
-	@Permission(value = "app.key.find", tags = "Api调用日志查询")
-	@PostMapping("/findRequestLogsPage")
-	public Result findRequestLogsPage(@RequestBody QueryApiRequestLogParams params) {
-
-		QueryWrapper<ApiRequestLog> queryWrapper = new QueryWrapper<>();
-		if (StringUtils.isNotBlank(params.getAppKey())) {
-			queryWrapper.eq("app_key", params.getAppKey());
-		}
-		if (StringUtils.isNotBlank(params.getMethod())) {
-			queryWrapper.eq("method", params.getMethod());
-		}
-		if (StringUtils.isNotBlank(params.getVersion())) {
-			queryWrapper.eq("version", params.getVersion());
-		}
-		if (StringUtils.isNotBlank(params.getClientIp())) {
-			queryWrapper.eq("client_ip", params.getClientIp());
-		}
-		if (params.getServiceBeginTime() != null) {
-			queryWrapper.between("service_begin_time", params.getServiceBeginTime().getStatDate(), params.getServiceBeginTime().getEndDate());
-		}
-
-
-		Page<ApiRequestLog> page = params.getPage();
-		page.addOrder(OrderItem.desc("service_begin_time"));
-
-		Page<ApiRequestLog> resultData = this.apiRequestLogService.page(page, queryWrapper);
-
-		return Result.instance(Result.Code.SUCCESS).setData(resultData);
-	}
-
-	@Permission(value = "app.key.add", tags = "新建AppKey")
-	@PostMapping("/save")
-	public Result save(@Valid @RequestBody SaveAppKeyInfoParam param) throws Exception {
-
-		Map<String, ServiceMethodHandler> serviceMethodHandlerMap = this.routerService.getOapContext().getAllServiceMethodHandlers();
-
-		List<String> listAllServices = new ArrayList<>();
-		for (ServiceMethodHandler handler : serviceMethodHandlerMap.values()) {
-			listAllServices.add(String.format("%s:%s", handler.getServiceMethodDefinition().getMethod(), handler.getServiceMethodDefinition().getVersion()));
-		}
-
-		AppKeyInfo appKeyInfo = new AppKeyInfo();
-		BeanUtils.copyProperties(param, appKeyInfo);
-
-		Long id = this.sequenceService.nextId();
-		appKeyInfo.setId(id);
-		Rsa.RsaKeys rsaKeys = Rsa.genKeyPair();
-		appKeyInfo.setPublicKey(rsaKeys.getPublicKey());
-		appKeyInfo.setPrivateKey(rsaKeys.getPrivateKey());
-		appKeyInfo.setCreatedTime(DateUtil.current());
-		appKeyInfo.setIsEnable(YN.Y);
-		appKeyInfo.getPermissions().removeIf(permission -> !listAllServices.contains(permission));
-
-		this.appKeyInfoService.save(appKeyInfo);
-
-		return Result.instance(Result.Code.MESSAGE_SUCCESS);
-	}
-
-	@Permission(value = "app.key.edit", tags = "更新AppKey")
-	@PostMapping("/updateById")
-	public Result updateById(Long id, @Valid @RequestBody SaveAppKeyInfoParam param) {
-		Map<String, ServiceMethodHandler> serviceMethodHandlerMap = this.routerService.getOapContext().getAllServiceMethodHandlers();
-
-		List<String> listAllServices = new ArrayList<>();
-		for (ServiceMethodHandler handler : serviceMethodHandlerMap.values()) {
-			listAllServices.add(String.format("%s:%s", handler.getServiceMethodDefinition().getMethod(), handler.getServiceMethodDefinition().getVersion()));
-		}
-
-		AppKeyInfo appKeyInfo = new AppKeyInfo();
-		BeanUtils.copyProperties(param, appKeyInfo);
-		appKeyInfo.setId(id);
-		appKeyInfo.getPermissions().removeIf(permission -> !listAllServices.contains(permission));
-		this.appKeyInfoService.refreshCache(id.toString());
-		this.appKeyInfoService.updateById(appKeyInfo);
-
-
-		return Result.instance(Result.Code.MESSAGE_SUCCESS);
-	}
-
-	@Permission(value = "app.key.disable", tags = "禁用AppKey")
-	@PostMapping("/disableByIds")
-	public Result disableByIds(@RequestBody List<Long> ids) {
-		UpdateWrapper<AppKeyInfo> updateWrapper = new UpdateWrapper<>();
-		updateWrapper.in("id", ids);
-		updateWrapper.set("is_enable", YN.N);
-		this.appKeyInfoService.update(updateWrapper);
-		return Result.instance(Result.Code.MESSAGE_SUCCESS);
-	}
-
-	@Permission(value = "app.key.enable", tags = "启用AppKey")
-	@PostMapping("/enableByIds")
-	public Result enableByIds(@RequestBody List<Long> ids) {
-		UpdateWrapper<AppKeyInfo> updateWrapper = new UpdateWrapper<>();
-		updateWrapper.in("id", ids);
-		updateWrapper.set("is_enable", YN.Y);
-		this.appKeyInfoService.update(updateWrapper);
-		return Result.instance(Result.Code.MESSAGE_SUCCESS);
-	}
+    @Autowired
+    private AppKeyInfoService appKeyInfoService;
+
+    @Autowired
+    private SequenceService sequenceService;
+
+    @Autowired
+    private ApiRequestLogService apiRequestLogService;
+
+    @Autowired
+    private RouterService routerService;
+
+    @Permission(value = "app.key.find", tags = "查询AppKey")
+    @PostMapping("/findPage")
+    public Result findPage(@RequestBody QueryAppKeyInfoParams params) {
+
+        QueryWrapper<AppKeyInfo> queryWrapper = new QueryWrapper<>();
+        if (params.getId() != null) {
+            queryWrapper.eq("id", params.getId());
+        }
+        if (params.getIsEnable() != null) {
+            queryWrapper.eq("is_enable", params.getIsEnable());
+        }
+        if (StringUtils.isNotBlank(params.getRemark())) {
+            queryWrapper.like("remark", params.getRemark());
+        }
+
+        Page<AppKeyInfo> page = params.getPage();
+        page.addOrder(OrderItem.desc("created_time"));
+
+        Page<AppKeyInfo> resultData = this.appKeyInfoService.page(page, queryWrapper);
+
+        return Result.instance(Result.Code.SUCCESS).setData(resultData);
+    }
+
+
+    @Permission(value = "app.key.statistics", tags = "Api接口统计")
+    @PostMapping("/statistics")
+    public Result statistics() {
+        QueryWrapper<ApiRequestLog> wrapper = new QueryWrapper<>();
+        wrapper.select("app_key appKey", "count(id) value");
+        wrapper.groupBy("app_key");
+        wrapper.orderByDesc("value");
+        wrapper.last("limit 8");
+        List<Map<String, Object>> requestAppKeyData = this.apiRequestLogService.listMaps(wrapper);
+        for (Map<String, Object> dataMap : requestAppKeyData) {
+            AppKeyInfo appKeyInfo = this.appKeyInfoService.findByAppKey(dataMap.get("appKey").toString());
+            if (appKeyInfo == null) {
+                dataMap.put("name", "未知应用");
+            } else {
+                dataMap.put("name", appKeyInfo.getName());
+            }
+        }
+
+        wrapper = new QueryWrapper<>();
+        wrapper.select("DATE_FORMAT(service_begin_time, '%Y-%m-%d') d", "count(id) value");
+        wrapper.groupBy("d");
+        wrapper.orderByAsc("d");
+        wrapper.last("limit 15");
+        List<Map<String, Object>> requestDayData = this.apiRequestLogService.listMaps(wrapper);
+
+
+        wrapper = new QueryWrapper<>();
+        wrapper.select("method", "count(id) value");
+        wrapper.groupBy("method");
+        wrapper.orderByDesc("value");
+        wrapper.last("limit 15");
+        List<Map<String, Object>> requestMethodData = this.apiRequestLogService.listMaps(wrapper);
+        for (Map<String, Object> dataMap : requestMethodData) {
+            String method = dataMap.getOrDefault("method", "").toString();
+
+            if (StringUtils.isBlank(method)) {
+                dataMap.put("name", "未知应用");
+            } else {
+                dataMap.put("name", method);
+            }
+        }
+
+        Map<String, Object> dataMap = new HashMap<>();
+        dataMap.put("requestAppKeyData", requestAppKeyData);
+        dataMap.put("requestDayData", requestDayData);
+        dataMap.put("requestMethodData", requestMethodData);
+
+        return Result.instance(Result.Code.MESSAGE_SUCCESS).setData(dataMap);
+    }
+
+    @Permission(value = "app.key.find", tags = "Api调用日志查询")
+    @PostMapping("/findRequestLogsPage")
+    public Result findRequestLogsPage(@RequestBody QueryApiRequestLogParams params) {
+
+        QueryWrapper<ApiRequestLog> queryWrapper = new QueryWrapper<>();
+        if (StringUtils.isNotBlank(params.getAppKey())) {
+            queryWrapper.eq("app_key", params.getAppKey());
+        }
+        if (StringUtils.isNotBlank(params.getMethod())) {
+            queryWrapper.eq("method", params.getMethod());
+        }
+        if (StringUtils.isNotBlank(params.getVersion())) {
+            queryWrapper.eq("version", params.getVersion());
+        }
+        if (StringUtils.isNotBlank(params.getClientIp())) {
+            queryWrapper.eq("client_ip", params.getClientIp());
+        }
+        if (params.getServiceBeginTime() != null) {
+            queryWrapper.between("service_begin_time", params.getServiceBeginTime().getStatDate(), params.getServiceBeginTime().getEndDate());
+        }
+
+
+        Page<ApiRequestLog> page = params.getPage();
+        page.addOrder(OrderItem.desc("service_begin_time"));
+
+        Page<ApiRequestLog> resultData = this.apiRequestLogService.page(page, queryWrapper);
+
+        return Result.instance(Result.Code.SUCCESS).setData(resultData);
+    }
+
+    @Permission(value = "app.key.add", tags = "新建AppKey")
+    @PostMapping("/save")
+    public Result save(@Valid @RequestBody SaveAppKeyInfoParam param) throws Exception {
+
+        Map<String, ServiceMethodHandler> serviceMethodHandlerMap = this.routerService.getOapContext().getAllServiceMethodHandlers();
+
+        List<String> listAllServices = new ArrayList<>();
+        for (ServiceMethodHandler handler : serviceMethodHandlerMap.values()) {
+            listAllServices.add(String.format("%s:%s", handler.getServiceMethodDefinition().getMethod(), handler.getServiceMethodDefinition().getVersion()));
+        }
+
+        AppKeyInfo appKeyInfo = new AppKeyInfo();
+        BeanUtils.copyProperties(param, appKeyInfo);
+
+        Long id = this.sequenceService.nextId();
+        appKeyInfo.setId(id);
+        Rsa.RsaKeys rsaKeys = Rsa.genKeyPair();
+        appKeyInfo.setPublicKey(rsaKeys.getPublicKey());
+        appKeyInfo.setPrivateKey(rsaKeys.getPrivateKey());
+        appKeyInfo.setCreatedTime(DateUtil.current());
+        appKeyInfo.setIsEnable(YN.Y);
+        appKeyInfo.getPermissions().removeIf(permission -> !listAllServices.contains(permission));
+
+        this.appKeyInfoService.save(appKeyInfo);
+
+        return Result.instance(Result.Code.MESSAGE_SUCCESS);
+    }
+
+    @Permission(value = "app.key.edit", tags = "更新AppKey")
+    @PostMapping("/updateById")
+    public Result updateById(Long id, @Valid @RequestBody SaveAppKeyInfoParam param) {
+        Map<String, ServiceMethodHandler> serviceMethodHandlerMap = this.routerService.getOapContext().getAllServiceMethodHandlers();
+
+        List<String> listAllServices = new ArrayList<>();
+        for (ServiceMethodHandler handler : serviceMethodHandlerMap.values()) {
+            listAllServices.add(String.format("%s:%s", handler.getServiceMethodDefinition().getMethod(), handler.getServiceMethodDefinition().getVersion()));
+        }
+
+        AppKeyInfo appKeyInfo = new AppKeyInfo();
+        BeanUtils.copyProperties(param, appKeyInfo);
+        appKeyInfo.setId(id);
+        appKeyInfo.getPermissions().removeIf(permission -> !listAllServices.contains(permission));
+        this.appKeyInfoService.refreshCache(id.toString());
+        this.appKeyInfoService.updateById(appKeyInfo);
+
+
+        return Result.instance(Result.Code.MESSAGE_SUCCESS);
+    }
+
+    @Permission(value = "app.key.disable", tags = "禁用AppKey")
+    @PostMapping("/disableByIds")
+    public Result disableByIds(@RequestBody List<Long> ids) {
+        UpdateWrapper<AppKeyInfo> updateWrapper = new UpdateWrapper<>();
+        updateWrapper.in("id", ids);
+        updateWrapper.set("is_enable", YN.N);
+        this.appKeyInfoService.update(updateWrapper);
+        return Result.instance(Result.Code.MESSAGE_SUCCESS);
+    }
+
+    @Permission(value = "app.key.enable", tags = "启用AppKey")
+    @PostMapping("/enableByIds")
+    public Result enableByIds(@RequestBody List<Long> ids) {
+        UpdateWrapper<AppKeyInfo> updateWrapper = new UpdateWrapper<>();
+        updateWrapper.in("id", ids);
+        updateWrapper.set("is_enable", YN.Y);
+        this.appKeyInfoService.update(updateWrapper);
+        return Result.instance(Result.Code.MESSAGE_SUCCESS);
+    }
 }
 }

+ 8 - 0
src/main/resources/db/migration/V1.1.4__update_auth.sql

@@ -0,0 +1,8 @@
+REPLACE
+INTO `authority_info` (`id`, `parent_id`, `name`, `remark`)
+VALUES (1300, 0, 'app.key', '业务数据开放管理'),
+        (1307, 1300, 'app.key.statistics', '统计');
+
+
+INSERT INTO `role_authority` (`id`, `authority_id`, `role_id`)
+VALUES (NULL, 1307, 1000);