From 602b29a6b4fe90d77a598f9a12baad2e0c2b36be Mon Sep 17 00:00:00 2001 From: majintao Date: Thu, 12 Sep 2024 18:13:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EAPI=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=95=B0=E9=87=8F=E6=98=8E=E7=BB=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../open/controllers/UsersController.java | 258 ++++++++ .../com/fangjia/open/model/ApiUserModel.java | 620 ++++++++++++++++++ 2 files changed, 878 insertions(+) diff --git a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/UsersController.java b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/UsersController.java index 951c354b3..a56938da9 100644 --- a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/UsersController.java +++ b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/UsersController.java @@ -4,22 +4,29 @@ import java.util.*; import com.fangjia.core.util.AnalyticsUtil; import com.fangjia.core.util.BeanUtils; +import com.fangjia.core.util.DataUtil; import com.fangjia.core.util.DateUtil; import com.fangjia.dal.QueryParam; +import com.fangjia.dal.mongo.model.ApiInvokeStatistical; import com.fangjia.dal.mongo.model.SendSmsLog; import com.fangjia.dal.mongo.open.model.ApiUserAuthority; import com.fangjia.dal.mongo.open.model.OpenResource; +import com.fangjia.dal.mongo.tool.model.ApiVisitCount; import com.fangjia.dal.mongo.tool.model.OpenUriAnaly; +import com.fangjia.dal.sql.model.ApiUser; import com.fangjia.open.HttpStatusCodeEnum; import com.fangjia.open.interceptors.AuthenticationBean; import com.fangjia.open.model.*; import com.fangjia.open.model.weixin.SendCodeJsonModel; import com.fangjia.open.model.weixin.SendCodeModel; +import com.fangjia.service.mongo.bkoff.ApiInvokeStatisticalService; import com.fangjia.service.open.*; +import com.fangjia.service.sql.ApiUserService; import net.sf.json.JSONObject; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.StringUtils; +import org.json.JSONArray; import org.sothis.web.mvc.ActionContext; import org.sothis.web.mvc.ActionInvocation; import org.sothis.web.mvc.annotation.Param; @@ -41,6 +48,8 @@ public class UsersController { private ApiUserAuthorityService apiUserAuthorityService; private OpenResourceService openResourceService; private OpenUriAnalyService openUriAnalyService; + private ApiUserService apiUserService; + private ApiInvokeStatisticalService apiInvokeStatisticalService; /** @@ -177,6 +186,247 @@ public class UsersController { return mav; } + public Object callDetailAction(ApiUserModel model) throws IllegalAccessException { + RestfulModelAndView mav = new RestfulModelAndView(); + Date startDate = null; + Date endDate = null; + if (StringUtils.isNotBlank(model.getBegDateStr())) { + startDate = DateUtil.parseStringToDate(model.getBegDateStr()); + } + if (StringUtils.isNotBlank(model.getEndDateStr())) { + endDate = DateUtil.parseStringToDate(model.getEndDateStr()); + } + List analys = openUriAnalyService.findOpenUriAnalyLogs(model.getUserId(), model.getUri(), startDate, endDate); + List bos = null; + model.setQuery("uri"); + + Long userId = model.getUserId(); + ApiUserAuthority authority = apiUserAuthorityService.findByUserId(userId); + Map countMap = authority.getTotalCountMap(); + ApiUser apiUser = apiUserService.findApiUserById(userId); + User user = userService.findById(userId); + if (user != null) { + model.setUserName(user.getDisplayName()); + } + + if (model.getQuery().equals("uri")) { + bos = createAnalyBosByLink(analys); + model.setChartSeries(toChartString(bos)); + } + + long date = DateUtil.parseStringToDate("2016-11-23").getTime(); + if ("uri".equals(model.getQuery()) && startDate.getTime() >= date && endDate.getTime() > date) { + List apiInvokeStatisticalList = apiInvokeStatisticalService.findApiInvokeStatisticalList(model.getUserId(), startDate, endDate); + if (bos != null && apiInvokeStatisticalList.size() > 0) { + Map> resultMap = getCountByUri(apiInvokeStatisticalList); + for (StatAnalyBo boTemp : bos) { + 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(userId, url, DateUtil.truncateDay(new Date())); + long historyCount = apiVisitCountService.findApiVisitAllCount(userId, 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))); + } + } + } + } + List resources = openResourceService.findOpenResource(null); + Map urlMap = converMapData(resources); + for (StatAnalyBo boTemp : bos) { + List apiVisitCounts = apiVisitCountService.findAllApiVisitCount(model.getUserId(), boTemp.getKey(), startDate, endDate); + int total = 0; + for (ApiVisitCount apiVisitCount : apiVisitCounts) { + total += apiVisitCount.getVisitCount(); + } + boTemp.setBillingCount(total); + boTemp.setUrlName(urlMap.get(boTemp.getKey())); + boTemp.setUrl(boTemp.getKey()); + boTemp.setKey(null); + } + +// model.setResourceMap(converMapData(resources)); + model.setStatAnalyBoList(sortStatAnaly(bos)); + BasicModel basicModel = new BasicModel(200, "ok"); + basicModel.setResult(model); + mav.setModel(basicModel); + return mav; + } + + private List createAnalyBosByLink(List analys) { + + + Map ret = new HashMap(); + Map> urlCodeMap = new HashMap>(); + int total = 0; + for (OpenUriAnaly openUriAnaly : analys) { + Integer temp = ret.get(openUriAnaly.getUrl()); + if (temp == null) { + ret.put(openUriAnaly.getUrl(), openUriAnaly.getUrlClickCount()); + } else { + ret.put(openUriAnaly.getUrl(), temp + openUriAnaly.getUrlClickCount()); + } + Map codeMap = urlCodeMap.get(openUriAnaly.getUrl()); + if (codeMap == null) { + codeMap = new HashMap(); + urlCodeMap.put(openUriAnaly.getUrl(), codeMap); + } + Map analyCodeMap = openUriAnaly.getCodeMap(); + for (String code : analyCodeMap.keySet()) { + Integer count = (Integer) analyCodeMap.get(code); + if ("200".equals(code)) { + Integer successCount = codeMap.get("successCount"); + if (successCount == null) { + codeMap.put("successCount", count); + } else { + codeMap.put("successCount", successCount + count); + } + } else { + Integer defeatCount = codeMap.get("defeatCount"); + if (defeatCount == null) { + codeMap.put("defeatCount", count); + } else { + codeMap.put("defeatCount", defeatCount + count); + } + } + } + total = total + openUriAnaly.getUrlClickCount(); + } + + List bos = new ArrayList(); + int index = 0; + double totalPercent = 0; + for (String url : ret.keySet()) { + Integer count = ret.get(url); + double percent = 0; + if (index + 1 == ret.size()) { + percent = 100 - totalPercent; + } else { + double a1 = count; + double val = a1 * 10000 / (double) total; + percent = DataUtil.roundDouble(val / 100, 2); + totalPercent = totalPercent + percent; + } + StatAnalyBo bo = new StatAnalyBo(); + bo.setKey(url); + bo.setCount(count); + bo.setPercent(percent); + Map codeMap = urlCodeMap.get(url); + bo.setSuccessTime(codeMap.get("successCount")); + bo.setDefeatTime(codeMap.get("defeatCount")); + if (codeMap.get("successCount") != null) { + bo.setSuccessRate(DataUtil.roundDouble((codeMap.get("successCount") * 100) / (double) count, 2)); + } + bos.add(bo); + + } + return bos; + } + + private List sortStatAnaly(List bos) { + Collections.sort(bos, new Comparator() { + @Override + public int compare(StatAnalyBo o1, StatAnalyBo o2) { + if (o1.getCount() > o2.getCount()) { + return -1; + } else if (o1.getCount() < o2.getCount()) { + return 1; + } + return 0; + } + }); + + return bos; + } + + private Map converMapData(List resources) { + Map map = new HashMap(); + for (OpenResource resource : resources) { + map.put(resource.getUri(), resource.getName()); + } + return map; + } + + + private Map> getCountByUri(List apiInvokeStatisticalList) { + Map> resultMap = new HashMap>(); + for (ApiInvokeStatistical apiInvokeStatistical : apiInvokeStatisticalList) { + Map detailMap = resultMap.get(apiInvokeStatistical.getRequestURI()); + if (detailMap == null) { + detailMap = new HashMap(); + resultMap.put(apiInvokeStatistical.getRequestURI(), detailMap); + } + 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 { + successEffectiveCount = apiInvokeStatistical.getSuccessEffectiveTime(); + } + if (successInvalidCount != null) { + successInvalidCount = successInvalidCount + apiInvokeStatistical.getSuccessInvalidTime(); + } else { + successInvalidCount = apiInvokeStatistical.getSuccessInvalidTime(); + ; + } + if (defeatCount != null) { + defeatCount = defeatCount + apiInvokeStatistical.getDefeatTime(); + } 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; + } + + private String toChartString(List bos) { + JSONArray ja = new JSONArray(); + for (StatAnalyBo bo : bos) { + JSONArray array = new JSONArray(); + if (StringUtils.isNotBlank(bo.getKey())) { + if (bo.getKey().equals("level_0")) { + array.put("0s"); + } else if (bo.getKey().equals("level_1")) { + array.put("1s以下"); + } else if (bo.getKey().equals("level_2")) { + array.put("1-2s以下"); + } else if (bo.getKey().equals("level_3")) { + array.put("2-3s以下"); + } else if (bo.getKey().equals("level_4")) { + array.put("3s以上"); + } else if (bo.getKey().equals("level_100")) { + array.put("暂无数据"); + } else { + array.put(bo.getKey()); + } + } else { + array.put(bo.getUserId()); + } + + array.put(bo.getPercent()); + ja.put(array); + } + return ja.toString(); + } + public void setUserService(UserService userService) { this.userService = userService; } @@ -200,4 +450,12 @@ public class UsersController { public void setOpenUriAnalyService(OpenUriAnalyService openUriAnalyService) { this.openUriAnalyService = openUriAnalyService; } + + public void setApiUserService(ApiUserService apiUserService) { + this.apiUserService = apiUserService; + } + + public void setApiInvokeStatisticalService(ApiInvokeStatisticalService apiInvokeStatisticalService) { + this.apiInvokeStatisticalService = apiInvokeStatisticalService; + } } diff --git a/projects/fangjia-open/src/main/java/com/fangjia/open/model/ApiUserModel.java b/projects/fangjia-open/src/main/java/com/fangjia/open/model/ApiUserModel.java index 22540ffea..a3c228c25 100644 --- a/projects/fangjia-open/src/main/java/com/fangjia/open/model/ApiUserModel.java +++ b/projects/fangjia-open/src/main/java/com/fangjia/open/model/ApiUserModel.java @@ -1,6 +1,12 @@ package com.fangjia.open.model; +import com.fangjia.dal.mongo.model.ApiInvokeStatistical; +import com.fangjia.dal.mongo.open.model.ApiUserAuthority; +import com.fangjia.service.open.*; + import java.util.Date; +import java.util.List; +import java.util.Map; /** * API用户管理Model @@ -12,6 +18,620 @@ public class ApiUserModel extends FangjiaApiModel{ private Date endDate; private String uri; + private static final long serialVersionUID = 7044341510628330454L; + + private String begDateStr;// 查询开始日期 + private String endDateStr;// 查询结束日期 + private String statDate; + private String appKey; + private String userName; + private String displayName; + private String siteName; + private String password; + private String email; + private String ip; + private Integer isTest; + private Integer purpose; + private String msg; + private Long userId; + private String company; + private List apiUserBoList; + private List eachCityBoList; + private String limitCountMap; + private String totalCountMap; + private Integer creditLevelStatus; + private String priceType; + private Map priceRatingMap; + private Integer isMonitor; + + private String contact; + + private String phone; + + public String getPriceType() { + return priceType; + } + + public void setPriceType(String priceType) { + this.priceType = priceType; + } + + public List getEachCityBoList() { + return eachCityBoList; + } + + public void setEachCityBoList(List eachCityBoList) { + this.eachCityBoList = eachCityBoList; + } + + private Integer sum; + private Integer limit; + /*** 权限控制相关 **/ + private ApiUserBo apiUserBo; + private List authorityList; + private ApiUserAuthority authority; + private List authorityBoList; + //private List serverAreaList; + private List> serverAreaList; + private String openCitys;// 汉字说明 + private List cityFirstLetters;// 城市拼音首字母 + private String type; + private String citys;// spell + private String unCheckCitys;// 页面反选的city + private Integer status; + private boolean hasAuth; + private boolean supportLatLng; + private Integer maxFrequency; + private String frequencyPeriod; + private Integer maximum; + + private ApiInvokeStatistical apiInvokeStatistical; + private Double one; + private Double newOne; + private Double two; + private Double three; + private Double four; + private Double five; + private Date deadTime; + private String manager; + private Double evaluateRate; + + private String data; + + private Integer isCheckCount; + + private String openResourseUrisMap; + + public String getOpenResourseUrisMap() { + return openResourseUrisMap; + } + + public void setOpenResourseUrisMap(String openResourseUrisMap) { + this.openResourseUrisMap = openResourseUrisMap; + } + + public Integer getIsCheckCount() { + return isCheckCount; + } + + public void setIsCheckCount(Integer isCheckCount) { + this.isCheckCount = isCheckCount; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public String getDisplayName() { + return displayName; + } + + private String chartSeries; + + private List OpenUriAnalyBoList; + private List statAnalyBoList; + private Integer way; + private Map resourceMap; + private String query; + private Integer totalCount; + private String xAxis; + private String credits; + private String uncitys; + + public String getUncitys() { + return uncitys; + } + + public void setUncitys(String uncitys) { + this.uncitys = uncitys; + } + + public String getCredits() { + return credits; + } + + public void setCredits(String credits) { + this.credits = credits; + } + + public String getxAxis() { + return xAxis; + } + + public void setxAxis(String xAxis) { + this.xAxis = xAxis; + } + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public String getSiteName() { + return siteName; + } + + public void setSiteName(String siteName) { + this.siteName = siteName; + } + + public String getAppKey() { + return appKey; + } + + public void setAppKey(String appKey) { + this.appKey = appKey; + } + + public String getBegDateStr() { + return begDateStr; + } + + public void setBegDateStr(String begDateStr) { + this.begDateStr = begDateStr; + } + + public String getEndDateStr() { + return endDateStr; + } + + public void setEndDateStr(String endDateStr) { + this.endDateStr = endDateStr; + } + + public Long getUserId() { + return userId; + } + + public void setUserId(Long userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public List getApiUserBoList() { + return apiUserBoList; + } + + public void setApiUserBoList(List apiUserBoList) { + this.apiUserBoList = apiUserBoList; + } + + public ApiUserBo getApiUserBo() { + return apiUserBo; + } + + public void setApiUserBo(ApiUserBo apiUserBo) { + this.apiUserBo = apiUserBo; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public List getAuthorityList() { + return authorityList; + } + + public void setAuthorityList(List authorityList) { + this.authorityList = authorityList; + } + + public ApiUserAuthority getAuthority() { + return authority; + } + + public void setAuthority(ApiUserAuthority authority) { + this.authority = authority; + } + + + public boolean isHasAuth() { + return hasAuth; + } + + public void setHasAuth(boolean hasAuth) { + this.hasAuth = hasAuth; + } + + public boolean isSupportLatLng() { + return supportLatLng; + } + + public void setSupportLatLng(boolean supportLatLng) { + this.supportLatLng = supportLatLng; + } + + public List getAuthorityBoList() { + return authorityBoList; + } + + public void setAuthorityBoList(List authorityBoList) { + this.authorityBoList = authorityBoList; + } + + public List getCityFirstLetters() { + return cityFirstLetters; + } + + public void setCityFirstLetters(List cityFirstLetters) { + this.cityFirstLetters = cityFirstLetters; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + /*public List getServerAreaList() { + return serverAreaList; + } + + public void setServerAreaList(List serverAreaList) { + this.serverAreaList = serverAreaList; + }*/ + public List> getServerAreaList() { + return serverAreaList; + } + + public void setServerAreaList(List> serverAreaList) { + this.serverAreaList = serverAreaList; + } + + public String getCitys() { + return citys; + } + + public void setCitys(String citys) { + this.citys = citys; + } + + public String getOpenCitys() { + return openCitys; + } + + public void setOpenCitys(String openCitys) { + this.openCitys = openCitys; + } + + public String getUnCheckCitys() { + return unCheckCitys; + } + + public void setUnCheckCitys(String unCheckCitys) { + this.unCheckCitys = unCheckCitys; + } + + public Integer getSum() { + return sum; + } + + public void setSum(Integer sum) { + this.sum = sum; + } + + + public Integer getLimit() { + return limit; + } + + public void setLimit(Integer limit) { + this.limit = limit; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public String getCompany() { + return company; + } + + public void setCompany(String company) { + this.company = company; + } + + public Integer getMaximum() { + return maximum; + } + + public void setMaximum(Integer maximum) { + this.maximum = maximum; + } + + public String getFrequencyPeriod() { + return frequencyPeriod; + } + + public void setFrequencyPeriod(String frequencyPeriod) { + this.frequencyPeriod = frequencyPeriod; + } + + public Integer getMaxFrequency() { + return maxFrequency; + } + + public void setMaxFrequency(Integer maxFrequency) { + this.maxFrequency = maxFrequency; + } + public List getOpenUriAnalyBoList() { + return OpenUriAnalyBoList; + } + + public void setOpenUriAnalyBoList(List openUriAnalyBoList) { + OpenUriAnalyBoList = openUriAnalyBoList; + } + + public String getChartSeries() { + return chartSeries; + } + + public void setChartSeries(String chartSeries) { + this.chartSeries = chartSeries; + } + + public String getStatDate() { + return statDate; + } + + public void setStatDate(String statDate) { + this.statDate = statDate; + } + + public Integer getWay() { + return way; + } + + public void setWay(Integer way) { + this.way = way; + } + + public Map getResourceMap() { + return resourceMap; + } + + public void setResourceMap(Map resourceMap) { + this.resourceMap = resourceMap; + } + + public List getStatAnalyBoList() { + return statAnalyBoList; + } + + public void setStatAnalyBoList(List statAnalyBoList) { + this.statAnalyBoList = statAnalyBoList; + } + + public String getQuery() { + return query; + } + + public void setQuery(String query) { + this.query = query; + } + + public Integer getTotalCount() { + return totalCount; + } + + public void setTotalCount(Integer totalCount) { + this.totalCount = totalCount; + } + + public ApiInvokeStatistical getApiInvokeStatistical() { + return apiInvokeStatistical; + } + + public void setApiInvokeStatistical(ApiInvokeStatistical apiInvokeStatistical) { + this.apiInvokeStatistical = apiInvokeStatistical; + } + public Double getEvaluateRate() { + return evaluateRate; + } + + public void setEvaluateRate(Double evaluateRate) { + this.evaluateRate = evaluateRate; + } + + public Integer getIsTest() { + return isTest; + } + + public void setIsTest(Integer isTest) { + this.isTest = isTest; + } + + public String getLimitCountMap() { + return limitCountMap; + } + + public void setLimitCountMap(String limitCountMap) { + this.limitCountMap = limitCountMap; + } + + public String getTotalCountMap() { + return totalCountMap; + } + + public void setTotalCountMap(String totalCountMap) { + this.totalCountMap = totalCountMap; + } + + public Integer getPurpose() { + return purpose; + } + + public void setPurpose(Integer purpose) { + this.purpose = purpose; + } + + public Map getPriceRatingMap() { + return priceRatingMap; + } + + public void setPriceRatingMap(Map priceRatingMap) { + this.priceRatingMap = priceRatingMap; + } + + public Double getOne() { + return one; + } + + public void setOne(Double one) { + this.one = one; + } + + public Double getNewOne() { + return newOne; + } + + public void setNewOne(Double newOne) { + this.newOne = newOne; + } + + public Double getTwo() { + return two; + } + + public void setTwo(Double two) { + this.two = two; + } + + public Double getThree() { + return three; + } + + public void setThree(Double three) { + this.three = three; + } + + public Double getFour() { + return four; + } + + public void setFour(Double four) { + this.four = four; + } + + public Double getFive() { + return five; + } + + public void setFive(Double five) { + this.five = five; + } + + public Integer getCreditLevelStatus() { + return creditLevelStatus; + } + + public void setCreditLevelStatus(Integer creditLevelStatus) { + this.creditLevelStatus = creditLevelStatus; + } + + public Date getDeadTime() { + return deadTime; + } + + public void setDeadTime(Date deadTime) { + this.deadTime = deadTime; + } + + public String getManager() { + return manager; + } + + public void setManager(String manager) { + this.manager = manager; + } + + public Integer getIsMonitor() { + return isMonitor; + } + + public void setIsMonitor(Integer isMonitor) { + this.isMonitor = isMonitor; + } + + public String getContact() { + return contact; + } + + public void setContact(String contact) { + this.contact = contact; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + public Date getStartDate() { return startDate; }