diff --git a/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/controllers/ApiUserController.java b/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/controllers/ApiUserController.java index ca2a516e3..54be64383 100644 --- a/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/controllers/ApiUserController.java +++ b/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/controllers/ApiUserController.java @@ -73,6 +73,7 @@ public class ApiUserController { private ApiInvokeStatisticalService apiInvokeStatisticalService; private ApiVisitCountService apiVisitCountService; private ApiCountCityService apiCountCityService; + /** * 所有api用户的信息展示 * @@ -1095,91 +1096,110 @@ public class ApiUserController { if (endDate != null) { model.setEndDate(DateUtil.formatToYYYYMMDD(endDate)); } + Date nowDate = DateUtil.parseStringToDate(DateUtil.formatToYYYYMMDD(new Date())); + Date lastWeek = DateUtil.add(DateUtil.formatToYYYYMMDD(new Date()), -7); + Date lastMonth = DateUtil.add(DateUtil.formatToYYYYMMDD(new Date()), -30); long diffInMillies = Math.abs(endDate.getTime() - startDate.getTime()); // 将毫秒转换为天数 long days = diffInMillies / (1000 * 60 * 60 * 24); // 使用long来计算天数 - // 只查询正式账号 - List queryParams = Arrays.asList(new QueryParam("isTest", 0)); + + // 只查询正式账号、未过期、未禁用 + List queryParams = Arrays.asList(new QueryParam("isTest", 0), + new QueryParam("status", 0), + new QueryParam("deadTime", QueryParam.GREATER_THAN_EQUAL_OPERATOR, new Date())); List apiUsers = this.apiUserService.findApiUsersByParamsSortDate(queryParams); - // 获取正式账号的ids - List ids = new ArrayList(); - for (ApiUser apiUser : apiUsers) { - ids.add((int) apiUser.getId()); - } - // 只获取正式用户的 - List analyAll = openUriAnalyService.findOpenUriAnalyLogs(model.getUserId(), null, startDate, endDate); - List analys = new ArrayList(); - for (OpenUriAnaly analy : analyAll) { - if (ids.contains(analy.getApiUserId())) { - analys.add(analy); - } - } - List userIds = new ArrayList(); - // 获取有访问记录的用户ID - for (OpenUriAnaly analy : analys) { - if (!userIds.contains(analy.getApiUserId())) { - userIds.add(analy.getApiUserId()); - } - } + + //所有API + List allApi = openResourceService.findAll(); + // 所有API的URI与name对应Map + Map apiMap = converMapData(allApi); List bosAll = new ArrayList(); - if (CollectionUtils.isNotEmpty(userIds)) { + if (CollectionUtils.isNotEmpty(apiUsers)) { // 根据访问遍历用户 - for (Integer userId : userIds) { - long id = (long) userId; + for (ApiUser apiUser : apiUsers) { + long id = apiUser.getId(); // 获取每个用户的访问 - List analyUser = openUriAnalyService.findOpenUriAnalyLogs(id, null, startDate, endDate); ApiUserAuthority authority = apiUserAuthorityService.findByUserId(id); + // 用户有的api + List userApi = authority.getOpenResourseUris(); Map countMap = authority.getTotalCountMap(); - // 获取 deadline - ApiUser apiUser = apiUserService.findApiUserById(id); // 获取用户名 User user = userService.findById(id); - List bos = null; - bos = createAnalyBosByLink(analyUser); long date = DateUtil.parseStringToDate("2016-11-23").getTime(); if (startDate.getTime() >= date && endDate.getTime() > date) { List apiInvokeStatisticalList = apiInvokeStatisticalService.findApiInvokeStatisticalList(id, startDate, endDate); - if (bos != null && apiInvokeStatisticalList.size() > 0) { - Map> resultMap = getCountByUri(apiInvokeStatisticalList); - for (StatAnalyBo boTemp : bos) { - boTemp.setPurpose(apiUser.getPurpose()); - String url = boTemp.getKey(); - boTemp.setSuccessEffectiveTime(resultMap.get(url).get("successEffectiveCount")); - boTemp.setSuccessInvalidTime(resultMap.get(url).get("successInvalidCount")); - boTemp.setDefeatTime(resultMap.get(url).get("defeatCount")); - if (apiUser != null) { - boTemp.setDeadline(apiUser.getDeadTime()); - } - long curVistedCount = apiVisitCountService.findApiVisitCountByDay(id, url, DateUtil.truncateDay(new Date())); - long historyCount = apiVisitCountService.findApiVisitAllCount(id, url, DateUtil.truncateDay(new Date())); - if (countMap != null && countMap.containsKey(url)) { - Integer totalCount = countMap.get(url); - long usedCount = curVistedCount + historyCount; - boTemp.setSurplusCallCount(Integer.valueOf((int) (totalCount - usedCount))); - } - if (user != null) { - boTemp.setName(user.getDisplayName()); - boTemp.setUserId((int) user.getId()); - } + List weekApiInvokeStatisticalList; + if (lastWeek == startDate && nowDate == endDate) { + weekApiInvokeStatisticalList = apiInvokeStatisticalList; + } else { + weekApiInvokeStatisticalList = apiInvokeStatisticalService.findApiInvokeStatisticalList(id, lastWeek, nowDate); + } + List monthApiInvokeStatisticalList; + if (lastMonth == startDate && nowDate == endDate) { + monthApiInvokeStatisticalList = apiInvokeStatisticalList; + } else { + monthApiInvokeStatisticalList = apiInvokeStatisticalService.findApiInvokeStatisticalList(id, lastMonth, nowDate); + } + Map> resultMap = getCountByUri(apiInvokeStatisticalList); + Map> weekResultMap = getCountByUri(weekApiInvokeStatisticalList); + Map> monthResultMap = getCountByUri(monthApiInvokeStatisticalList); - List apiVisitCounts = apiVisitCountService.findAllApiVisitCount(id, boTemp.getKey(), startDate, endDate); - int total = 0; - for (ApiVisitCount apiVisitCount : apiVisitCounts) { - total += apiVisitCount.getVisitCount(); - } - boTemp.setBillingCount(total); - double dailyUse = boTemp.getCount() * 1.0 / days; - boTemp.setDailyUse(dailyUse); + // 遍历用户所有的api + for (String url : userApi) { + // 统计用过的api + String urlName = apiMap.get(url); + StatAnalyBo bo = new StatAnalyBo(); + if (resultMap.containsKey(url)) { + // 自定义时间的数据 + bo.setSuccessEffectiveTime(resultMap.get(url).get("successEffectiveCount")); + bo.setSuccessInvalidTime(resultMap.get(url).get("successInvalidCount")); + bo.setDefeatTime(resultMap.get(url).get("defeatCount")); + bo.setCount(resultMap.get(url).get("totalCount")); + double dailyUse = bo.getCount() * 1.0 / days; + bo.setDailyUse(dailyUse); } + if (weekResultMap.containsKey(url)) { + // 近一周数据 + bo.setWeekSuccessEffectiveTime(weekResultMap.get(url).get("successEffectiveCount")); + bo.setWeekSuccessInvalidTime(weekResultMap.get(url).get("successInvalidCount")); + bo.setWeekDefeatTime(weekResultMap.get(url).get("defeatCount")); + bo.setWeekCount(weekResultMap.get(url).get("totalCount")); + double dailyUse = bo.getWeekCount() * 1.0 / 7; + bo.setWeekDailyUse(dailyUse); + } + if (monthResultMap.containsKey(url)) { + // 成功有效次数 + bo.setMonthSuccessEffectiveTime(monthResultMap.get(url).get("successEffectiveCount")); + // 成功无效次数 + bo.setMonthSuccessInvalidTime(monthResultMap.get(url).get("successInvalidCount")); + // 失败次数 + bo.setMonthDefeatTime(monthResultMap.get(url).get("defeatCount")); + bo.setMonthCount(monthResultMap.get(url).get("totalCount")); + double dailyUse = bo.getMonthCount() * 1.0 / 30; + bo.setMonthDailyUse(dailyUse); + } + + bo.setUrl(url); + bo.setUrlName(urlName); + bo.setDeadline(apiUser.getDeadTime()); + bo.setUserId((int) id); + bo.setUserName(user.getDisplayName()); + //剩余调用量 + long curVistedCount = apiVisitCountService.findApiVisitCountByDay(id, url, DateUtil.truncateDay(new Date())); + long historyCount = apiVisitCountService.findApiVisitAllCount(id, url, DateUtil.truncateDay(new Date())); + if (countMap != null && countMap.containsKey(url)) { + Integer totalCount = countMap.get(url); + long usedCount = curVistedCount + historyCount; + bo.setSurplusCallCount(Integer.valueOf((int) (totalCount - usedCount))); + } + bosAll.add(bo); } } - bosAll.addAll(bos); - } + this.buildReport(bosAll, response, startDate, endDate); } - this.buildReport(bosAll, response, startDate, endDate); model.setViewType("stream"); return model; } @@ -1191,39 +1211,52 @@ public class ApiUserController { for (StatAnalyBo bo : bos) { List row = new ArrayList(); row.add(bo.getUserId() != null ? bo.getUserId().toString() : ""); - row.add(bo.getName() != null ? bo.getName().toString() : ""); - if (bo.getPurpose() != null) { - Integer purpose = bo.getPurpose(); - if (purpose == 1) { - row.add("API"); - } else if (purpose == 2) { - row.add("PDS"); - } else if (purpose == 4) { - row.add("房生活"); - } - }else { + row.add(bo.getUserName() != null ? bo.getUserName().toString() : ""); + row.add(bo.getDeadline() != null ? DateUtils.format(bo.getDeadline(), "yyyy/MM/dd") : ""); + row.add(bo.getUrl() != null ? bo.getUrl().toString() : ""); + row.add(bo.getUrlName() != null ? bo.getUrlName().toString() : ""); + row.add(bo.getSurplusCallCount() != null ? bo.getSurplusCallCount().toString() : ""); + // 上周 + row.add(bo.getWeekCount() != null ? bo.getWeekCount().toString() : ""); + if (bo.getWeekDailyUse() != null) { + // 格式化为保留一位小数的字符串 + String formattedDailyUse = String.format("%.1f", bo.getWeekDailyUse()); + row.add(formattedDailyUse); + } else { row.add(""); } - row.add(bo.getKey() != null ? bo.getKey().toString() : ""); - row.add(bo.getSurplusCallCount() != null ? bo.getSurplusCallCount().toString() : ""); - row.add(bo.getDeadline() != null ? DateUtils.format(bo.getDeadline(), "yyyy/MM/dd") : ""); + row.add(bo.getWeekSuccessEffectiveTime() != null ? bo.getWeekSuccessEffectiveTime().toString() : ""); + row.add(bo.getWeekSuccessInvalidTime() != null ? bo.getWeekSuccessInvalidTime().toString() : ""); + row.add(bo.getWeekDefeatTime() != null ? bo.getWeekDefeatTime().toString() : ""); + // 上个月 + row.add(bo.getMonthCount() != null ? bo.getMonthCount().toString() : ""); + if (bo.getMonthDailyUse() != null) { + // 格式化为保留一位小数的字符串 + String formattedDailyUse = String.format("%.1f", bo.getMonthDailyUse()); + row.add(formattedDailyUse); + } else { + row.add(""); + } + row.add(bo.getMonthSuccessEffectiveTime() != null ? bo.getMonthSuccessEffectiveTime().toString() : ""); + row.add(bo.getMonthSuccessInvalidTime() != null ? bo.getMonthSuccessInvalidTime().toString() : ""); + row.add(bo.getMonthDefeatTime() != null ? bo.getMonthDefeatTime().toString() : ""); + // 自定义时间 row.add(bo.getCount() != null ? bo.getCount().toString() : ""); - if (bo.getDailyUse() !=null){ + if (bo.getDailyUse() != null) { // 格式化为保留一位小数的字符串 String formattedDailyUse = String.format("%.1f", bo.getDailyUse()); row.add(formattedDailyUse); - }else { + } else { row.add(""); } - row.add(bo.getPercent() != null ? bo.getPercent().toString() : ""); row.add(bo.getSuccessEffectiveTime() != null ? bo.getSuccessEffectiveTime().toString() : ""); row.add(bo.getSuccessInvalidTime() != null ? bo.getSuccessInvalidTime().toString() : ""); row.add(bo.getDefeatTime() != null ? bo.getDefeatTime().toString() : ""); - row.add(bo.getBillingCount() != null ? bo.getBillingCount().toString() : ""); dataList.add(row); } - String fileName = "用户账号使用统计(" + DateUtils.format(startDate, "yyyy/MM/dd") + "—" + DateUtils.format(endDate, "yyyy/MM/dd") + ").xlsx"; + String dateStr ="("+ DateUtils.format(startDate, "yyyy/MM/dd") + "—" + DateUtils.format(endDate, "yyyy/MM/dd") +")"; + String fileName = "用户账号使用统计" + dateStr + ".xlsx"; // ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build(); String displayName = URLEncoder.encode(fileName, "UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=\"" + displayName + "\""); @@ -1231,8 +1264,11 @@ public class ApiUserController { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("用户账号使用统计"); List head = Arrays.asList( - "用户ID", "账号", "账号用途", "接口", "剩余使用次数", "合同到期时间", "访问次数", "日均访问量", - "占比(%)", "成功并有效", "成功并无效", "失败次数", "计费次数"); + "用户ID", "账号", "合同到期时间", "接口", "接口名称", "剩余使用次数", + "访问次数(近一周)", "日均访问量(近一周)","成功并有效(近一周)", "成功并无效(近一周)","失败次数(近一周)", + "访问次数(近一月)", "日均访问量(近一月)","成功并有效(近一月)", "成功并无效(近一月)","失败次数(近一月)", + "访问次数"+dateStr, "日均访问量"+dateStr,"成功并有效"+dateStr, "成功并无效"+dateStr,"失败次数"+dateStr + ); // 创建表头 Row headRow = sheet.createRow(0); for (int i = 0; i < head.size(); i++) { @@ -1852,6 +1888,7 @@ public class ApiUserController { Integer successEffectiveCount = detailMap.get("successEffectiveCount"); Integer successInvalidCount = detailMap.get("successInvalidCount"); Integer defeatCount = detailMap.get("defeatCount"); + Integer totalCount = detailMap.get("totalCount"); if (successEffectiveCount != null) { successEffectiveCount = successEffectiveCount + apiInvokeStatistical.getSuccessEffectiveTime(); } else { @@ -1868,10 +1905,16 @@ public class ApiUserController { } else { defeatCount = apiInvokeStatistical.getDefeatTime(); } + if (totalCount != null) { + totalCount = totalCount + apiInvokeStatistical.getTotalTime(); + } else { + totalCount = apiInvokeStatistical.getTotalTime(); + } detailMap.put("successEffectiveCount", successEffectiveCount); detailMap.put("successInvalidCount", successInvalidCount); detailMap.put("defeatCount", defeatCount); + detailMap.put("totalCount", totalCount); } return resultMap; diff --git a/projects/fangjia-bkoff/src/main/webapp/apiUser/list.ftl b/projects/fangjia-bkoff/src/main/webapp/apiUser/list.ftl index 8a2361bc7..fb5a7a7e0 100644 --- a/projects/fangjia-bkoff/src/main/webapp/apiUser/list.ftl +++ b/projects/fangjia-bkoff/src/main/webapp/apiUser/list.ftl @@ -56,9 +56,9 @@ 鏄电О - - 璐︽埛鐢ㄩ�� - +<#-- --> +<#-- 璐︽埛鐢ㄩ��--> +<#-- --> username @@ -100,15 +100,15 @@ ${bo.displayName!} - - <#if bo.purpose?? && bo.purpose == 1> - API - <#elseif bo.purpose?? && bo.purpose == 2> - PDS - <#elseif bo.purpose?? && bo.purpose == 4> - 鎴跨敓娲� - - +<#-- --> +<#-- <#if bo.purpose?? && bo.purpose == 1>--> +<#-- API--> +<#-- <#elseif bo.purpose?? && bo.purpose == 2>--> +<#-- PDS--> +<#-- <#elseif bo.purpose?? && bo.purpose == 4>--> +<#-- 鎴跨敓娲�--> +<#-- --> +<#-- --> ${bo.userName!} diff --git a/projects/fangjia-service/src/main/java/com/fangjia/service/open/StatAnalyBo.java b/projects/fangjia-service/src/main/java/com/fangjia/service/open/StatAnalyBo.java index a638d713e..49a889927 100644 --- a/projects/fangjia-service/src/main/java/com/fangjia/service/open/StatAnalyBo.java +++ b/projects/fangjia-service/src/main/java/com/fangjia/service/open/StatAnalyBo.java @@ -6,11 +6,19 @@ public class StatAnalyBo { private String key; private Integer count; + private Integer weekCount; + private Integer monthCount; private Double percent; private Integer userId; private Integer successEffectiveTime; + private Integer weekSuccessEffectiveTime; + private Integer monthSuccessEffectiveTime; private Integer successInvalidTime; + private Integer weekSuccessInvalidTime; + private Integer monthSuccessInvalidTime; private Integer defeatTime; + private Integer weekDefeatTime; + private Integer monthDefeatTime; private Integer successTime; private Integer billingCount; private Integer purpose; @@ -21,58 +29,133 @@ public class StatAnalyBo { // 新增url和name private String url; + private String urlName; - private String name; + private String userName; // 添加日均使用量 private Double dailyUse; + private Double weekDailyUse; + private Double monthDailyUse; + public String getKey() { return key; } + public void setKey(String key) { this.key = key; } + public Integer getCount() { return count; } + public void setCount(Integer count) { this.count = count; } + + public Integer getWeekCount() { + return weekCount; + } + + public void setWeekCount(Integer weekCount) { + this.weekCount = weekCount; + } + + public Integer getMonthCount() { + return monthCount; + } + + public void setMonthCount(Integer monthCount) { + this.monthCount = monthCount; + } + public Double getPercent() { return percent; } + public void setPercent(Double percent) { this.percent = percent; } + public Integer getUserId() { return userId; } + public void setUserId(Integer userId) { this.userId = userId; } + public Integer getSuccessEffectiveTime() { return successEffectiveTime; } + public void setSuccessEffectiveTime(Integer successEffectiveTime) { this.successEffectiveTime = successEffectiveTime; } + + public Integer getWeekSuccessEffectiveTime() { + return weekSuccessEffectiveTime; + } + + public void setWeekSuccessEffectiveTime(Integer weekSuccessEffectiveTime) { + this.weekSuccessEffectiveTime = weekSuccessEffectiveTime; + } + + public Integer getMonthSuccessEffectiveTime() { + return monthSuccessEffectiveTime; + } + + public void setMonthSuccessEffectiveTime(Integer monthSuccessEffectiveTime) { + this.monthSuccessEffectiveTime = monthSuccessEffectiveTime; + } + public Integer getSuccessInvalidTime() { return successInvalidTime; } + public void setSuccessInvalidTime(Integer successInvalidTime) { this.successInvalidTime = successInvalidTime; } + + public Integer getWeekSuccessInvalidTime() { + return weekSuccessInvalidTime; + } + + public void setWeekSuccessInvalidTime(Integer weekSuccessInvalidTime) { + this.weekSuccessInvalidTime = weekSuccessInvalidTime; + } + + public Integer getMonthSuccessInvalidTime() { + return monthSuccessInvalidTime; + } + + public void setMonthSuccessInvalidTime(Integer monthSuccessInvalidTime) { + this.monthSuccessInvalidTime = monthSuccessInvalidTime; + } + public Integer getDefeatTime() { return defeatTime; } + public void setDefeatTime(Integer defeatTime) { this.defeatTime = defeatTime; } - public Integer getBillingCount() { - return billingCount; + + public Integer getWeekDefeatTime() { + return weekDefeatTime; } - public void setBillingCount(Integer billingCount) { - this.billingCount = billingCount; + + public void setWeekDefeatTime(Integer weekDefeatTime) { + this.weekDefeatTime = weekDefeatTime; + } + + public Integer getMonthDefeatTime() { + return monthDefeatTime; + } + + public void setMonthDefeatTime(Integer monthDefeatTime) { + this.monthDefeatTime = monthDefeatTime; } public Integer getSuccessTime() { @@ -83,12 +166,12 @@ public class StatAnalyBo { this.successTime = successTime; } - public Double getSuccessRate() { - return successRate; + public Integer getBillingCount() { + return billingCount; } - public void setSuccessRate(Double successRate) { - this.successRate = successRate; + public void setBillingCount(Integer billingCount) { + this.billingCount = billingCount; } public Integer getPurpose() { @@ -99,6 +182,14 @@ public class StatAnalyBo { this.purpose = purpose; } + public Double getSuccessRate() { + return successRate; + } + + public void setSuccessRate(Double successRate) { + this.successRate = successRate; + } + public Integer getSurplusCallCount() { return surplusCallCount; } @@ -123,12 +214,20 @@ public class StatAnalyBo { this.url = url; } - public String getName() { - return name; + public String getUrlName() { + return urlName; } - public void setName(String name) { - this.name = name; + public void setUrlName(String urlName) { + this.urlName = urlName; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; } public Double getDailyUse() { @@ -138,4 +237,20 @@ public class StatAnalyBo { public void setDailyUse(Double dailyUse) { this.dailyUse = dailyUse; } + + public Double getWeekDailyUse() { + return weekDailyUse; + } + + public void setWeekDailyUse(Double weekDailyUse) { + this.weekDailyUse = weekDailyUse; + } + + public Double getMonthDailyUse() { + return monthDailyUse; + } + + public void setMonthDailyUse(Double monthDailyUse) { + this.monthDailyUse = monthDailyUse; + } }