客户API用量导出

This commit is contained in:
majintao
2024-08-21 15:23:36 +08:00
parent 44131c3e94
commit 7ab052c966
4 changed files with 392 additions and 43 deletions
@@ -1,11 +1,13 @@
package com.fangjia.bkoff.controllers;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.fangjia.bkoff.model.ApiUserModel;
import com.fangjia.bkoff.model.JsonModel;
import com.fangjia.core.util.BeanUtils;
import com.fangjia.core.util.DataUtil;
import com.fangjia.core.util.DateUtil;
import com.fangjia.core.util.StringUtil;
import com.fangjia.core.util.*;
import com.fangjia.dal.QueryParam;
import com.fangjia.dal.mongo.fangjia.model.ServerArea;
import com.fangjia.dal.mongo.model.ApiInvokeStatistical;
import com.fangjia.dal.mongo.model.ApiUserCfg;
@@ -27,23 +29,33 @@ import com.fangjia.service.sql.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.bson.types.ObjectId;
import org.json.JSONArray;
import org.sothis.core.util.CollectionUtils;
import org.sothis.web.mvc.ActionContext;
import org.sothis.web.mvc.annotation.Param;
import scala.Int;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* api用户开通等管理controller
*
@@ -61,8 +73,6 @@ public class ApiUserController {
private ApiInvokeStatisticalService apiInvokeStatisticalService;
private ApiVisitCountService apiVisitCountService;
private ApiCountCityService apiCountCityService;
/**
* 所有api用户的信息展示
*
@@ -303,14 +313,15 @@ public class ApiUserController {
user.put("password", DigestUtils.md5Hex(model.getPassword()));
//
// // 更改用户密码
this.userService.updateById(user,userId);
this.userService.updateById(user, userId);
// 刷新缓存
this.apiUserService.clearApiUserAuthorityCache(userId);
model.setViewType("redirect");
model.setViewParam("location", "/apiUser/edit?userId="+userId);
model.setViewParam("location", "/apiUser/edit?userId=" + userId);
return model;
}
public Object editPwdAction(ApiUserModel model) {
Long userId = model.getUserId();
User user = this.userService.findById(userId);
@@ -975,7 +986,21 @@ public class ApiUserController {
} else {
cities = new ArrayList<String>();
}
List<OpenUriAnaly> analys = openUriAnalyService.findOpenUriAnalyLogs(model.getUserId(), null, startDate, endDate);
// 只查询正式账号
List<QueryParam> queryParams = Arrays.asList(new QueryParam("isTest", 0));
List<ApiUser> apiUsers = this.apiUserService.findApiUsersByParamsSortDate(queryParams);
// 获取正式账号的ids
List<Integer> ids = new ArrayList<Integer>();
for (ApiUser apiUser : apiUsers) {
ids.add((int) apiUser.getId());
}
List<OpenUriAnaly> analyAll = openUriAnalyService.findOpenUriAnalyLogs(model.getUserId(), null, startDate, endDate);
List<OpenUriAnaly> analys = new ArrayList<OpenUriAnaly>();
for (OpenUriAnaly analy : analyAll) {
if (ids.contains(analy.getApiUserId())) {
analys.add(analy);
}
}
List<StatAnalyBo> bos = null;
List<EachCityBo> eachCityBos = null;
if (model.getWay() == null) {
@@ -1016,9 +1041,7 @@ public class ApiUserController {
}
}
}
List<Long> userIds = new ArrayList<Long>();
for (StatAnalyBo boTemp : bos) {
userIds.add(Long.valueOf(boTemp.getUserId()));
List<ApiVisitCount> apiVisitCounts = apiVisitCountService.findAllApiVisitCount(Long.valueOf(boTemp.getUserId()), null, startDate, endDate);
int total = 0;
for (ApiVisitCount apiVisitCount : apiVisitCounts) {
@@ -1027,7 +1050,6 @@ public class ApiUserController {
boTemp.setBillingCount(total);
}
List<ApiUser> apiUsers = apiUserService.findApiUserByIds(userIds);
Map<Long, ApiUser> idUserMap = new HashMap<Long, ApiUser>();
for (ApiUser apiUser : apiUsers) {
idUserMap.put(apiUser.getId(), apiUser);
@@ -1051,8 +1073,199 @@ public class ApiUserController {
return model;
}
public Object exportAction(ApiUserModel model, HttpServletResponse response) throws IllegalAccessException {
Date startDate = null;
Date endDate = null;
if (StringUtils.isBlank(model.getBegDate()) && StringUtils.isBlank(model.getEndDate())) {
startDate = DateUtil.add(DateUtil.formatToYYYYMMDD(new Date()), -7);
endDate = DateUtil.parseStringToDate(DateUtil.formatToYYYYMMDD(new Date()));
} else {
if (StringUtils.isNotBlank(model.getBegDate())) {
startDate = DateUtil.parseStringToDate(model.getBegDate());
}
if (StringUtils.isNotBlank(model.getEndDate())) {
endDate = DateUtil.parseStringToDate(model.getEndDate());
}
public Object userVisitStatAction(ApiUserModel model) {
}
if (startDate != null) {
model.setBegDate(DateUtil.formatToYYYYMMDD(startDate));
}
if (endDate != null) {
model.setEndDate(DateUtil.formatToYYYYMMDD(endDate));
}
long diffInMillies = Math.abs(endDate.getTime() - startDate.getTime());
// 将毫秒转换为天数
long days = diffInMillies / (1000 * 60 * 60 * 24); // 使用long来计算天数
// 只查询正式账号
List<QueryParam> queryParams = Arrays.asList(new QueryParam("isTest", 0));
List<ApiUser> apiUsers = this.apiUserService.findApiUsersByParamsSortDate(queryParams);
// 获取正式账号的ids
List<Integer> ids = new ArrayList<Integer>();
for (ApiUser apiUser : apiUsers) {
ids.add((int) apiUser.getId());
}
// 只获取正式用户的
List<OpenUriAnaly> analyAll = openUriAnalyService.findOpenUriAnalyLogs(model.getUserId(), null, startDate, endDate);
List<OpenUriAnaly> analys = new ArrayList<OpenUriAnaly>();
for (OpenUriAnaly analy : analyAll) {
if (ids.contains(analy.getApiUserId())) {
analys.add(analy);
}
}
List<Integer> userIds = new ArrayList<Integer>();
// 获取有访问记录的用户ID
for (OpenUriAnaly analy : analys) {
if (!userIds.contains(analy.getApiUserId())) {
userIds.add(analy.getApiUserId());
}
}
List<StatAnalyBo> bosAll = new ArrayList<StatAnalyBo>();
if (CollectionUtils.isNotEmpty(userIds)) {
// 根据访问遍历用户
for (Integer userId : userIds) {
long id = (long) userId;
// 获取每个用户的访问
List<OpenUriAnaly> analyUser = openUriAnalyService.findOpenUriAnalyLogs(id, null, startDate, endDate);
ApiUserAuthority authority = apiUserAuthorityService.findByUserId(id);
Map<String, Integer> countMap = authority.getTotalCountMap();
// 获取 deadline
ApiUser apiUser = apiUserService.findApiUserById(id);
// 获取用户名
User user = userService.findById(id);
List<StatAnalyBo> bos = null;
bos = createAnalyBosByLink(analyUser);
long date = DateUtil.parseStringToDate("2016-11-23").getTime();
if (startDate.getTime() >= date && endDate.getTime() > date) {
List<ApiInvokeStatistical> apiInvokeStatisticalList = apiInvokeStatisticalService.findApiInvokeStatisticalList(id, startDate, endDate);
if (bos != null && apiInvokeStatisticalList.size() > 0) {
Map<String, Map<String, Integer>> 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<ApiVisitCount> 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);
}
}
}
bosAll.addAll(bos);
}
}
this.buildReport(bosAll, response, startDate, endDate);
model.setViewType("stream");
return model;
}
private void buildReport(List<StatAnalyBo> bos, HttpServletResponse response, Date startDate, Date endDate) {
try {
List<List<String>> dataList = new ArrayList<List<String>>();
for (StatAnalyBo bo : bos) {
List<String> row = new ArrayList<String>();
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("");
}
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.getCount() != null ? bo.getCount().toString() : "");
if (bo.getDailyUse() !=null){
// 格式化为保留一位小数的字符串
String formattedDailyUse = String.format("%.1f", bo.getDailyUse());
row.add(formattedDailyUse);
}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";
// ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
String displayName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + displayName + "\"");
response.setContentType("application/x-download;charset=utf-8");
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("用户账号使用统计");
List<String> head = Arrays.asList(
"用户ID", "账号", "账号用途", "接口", "剩余使用次数", "合同到期时间", "访问次数", "日均访问量",
"占比(%)", "成功并有效", "成功并无效", "失败次数", "计费次数");
// 创建表头
Row headRow = sheet.createRow(0);
for (int i = 0; i < head.size(); i++) {
Cell cell = headRow.createCell(i);
cell.setCellValue(head.get(i));
}
// 写入数据,从第二行
for (int i = 0; i < dataList.size(); i++) {
Row row = sheet.createRow(i + 1);
List<String> data = dataList.get(i);
for (int j = 0; j < data.size(); j++) {
Cell cell = row.createCell(j);
cell.setCellValue(data.get(j));
}
}
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
} catch (IOException e) {
}
}
List<List<String>> formatHead(String[] headArr) {
List<List<String>> head = new ArrayList<List<String>>();
for (String name : headArr) {
List<String> temp = new ArrayList<String>();
temp.add(name);
head.add(temp);
}
return head;
}
public Object userVisitStatAction(ApiUserModel model) throws IllegalAccessException {
Date startDate = null;
Date endDate = null;
if (StringUtils.isNotBlank(model.getBegDate())) {
@@ -1075,8 +1288,12 @@ public class ApiUserController {
// bos = createAnalyBosByUser(analys);
// model.setChartSeries(toChartString(bos));
// }else if(model.getWay() == 2){
Long userId = model.getUserId();
ApiUserAuthority authority = apiUserAuthorityService.findByUserId(userId);
Map<String, Integer> countMap = authority.getTotalCountMap();
ApiUser apiUser = apiUserService.findApiUserById(userId);
if (model.getWay() == 2) {
User user = userService.findById(model.getUserId());
User user = userService.findById(userId);
if (user != null) {
model.setUserName(user.getDisplayName());
}
@@ -1120,6 +1337,16 @@ public class ApiUserController {
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)));
}
}
}
}
@@ -12,9 +12,9 @@
<link rel="stylesheet" type="text/css" href="../style/main.css?v=${version}" />
<script type="text/javascript" src="../js/My97DatePicker/WdatePicker.js"></script>
<style>
label{display:inline-block}
.main-content {
padding-bottom: 0;
}
@@ -47,28 +47,51 @@
</span>
</div>
<form action="<#if way?? && way==0>../apiUser/pandect<#else>../apiUser/apiVisitStat</#if>" method="post">
<input type="hidden" name="way" value="${way!}"/>
<#if way!=3>
<label for="districtName" style="margin-left: 15px;">用户ID:</label>
<input type="text" name="userId" value="${userId!}" class="input-medium" style="margin-top:9px;" ></input>
<#else>
<label for="districtName" style="margin-left: 15px;">城市:</label>
<input type="text" class="input-medium" readonly="readonly" id="city" name="citys" value="${citys!}"/><input type="button" class="input-medium" id="butt" value="选择城市"/>
</#if>
<label for="startDate">&nbsp;&nbsp;&nbsp;&nbsp;日期:</label>
<input placeholder="开始时间" class="form-control" style="width:140px;" name="begDate" id="begDate" value="<#if begDate??>${begDate}</#if>" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" />-
<input id="endDate" placeholder="结束时间" class="form-control" style="width:140px;" name="endDate" value="${endDate!}" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>
<form id="myForm" action="<#if way?? && way==0>../apiUser/pandect<#else>../apiUser/apiVisitStat</#if>" method="post">
<input type="hidden" name="way" value="${way!}"/>
<input id="queryBtn" class ="btn btn-primary" type="submit" value="查询" />
<#if way!=3>
<label for="districtName" style="margin-left: 15px;">用户ID:</label>
<input type="text" name="userId" value="${userId!}" class="input-medium" style="margin-top:9px;" />
<#else>
<label for="districtName" style="margin-left: 15px;">城市:</label>
<input type="text" class="input-medium" readonly="readonly" id="city" name="citys" value="${citys!}"/>
<input type="button" class="input-medium" id="butt" value="选择城市"/>
</#if>
<#if way!=3>
<label for="">(备注:成功有效、成功无效、失败的次数统计从2016-11-23开始)</label>
</#if>
</form>
<label for="startDate">&nbsp;&nbsp;&nbsp;&nbsp;日期:</label>
<!-- 下拉菜单选择最近一周、最近一月或自定义 -->
<select id="dateRange" class="form-control" style="width:160px;" onchange="updateDateRange(this.value)">
<option value="custom">自定义</option>
<option value="lastWeek">最近一周</option>
<option value="lastMonth">最近一月</option>
</select>
<!-- 自定义日期输入框 -->
<input placeholder="开始时间" class="form-control" style="width:140px;" name="begDate" id="begDate"
value="<#if begDate??>${begDate}</#if>"
onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" />
-
<input id="endDate" placeholder="结束时间" class="form-control" style="width:140px;" name="endDate"
value="${endDate!}"
onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" />
<!-- 查询按钮 -->
<input id="queryBtn" class="btn btn-primary" type="submit" value="查询" onclick="submitForm('query')" />
<!-- 下载按钮 -->
<input id="downloadBtn" class="btn btn-secondary" type="submit" value="导出" onclick="submitForm('download')" />
<#if way!=3>
<label for="">(备注:成功有效、成功无效、失败的次数统计从2016-11-23开始)</label>
</#if>
</form>
<#--<input id="expect" class ="btn btn-primary" type="submit" value="导出" />-->
<a class="btn common_btn" href="importExcel?city=${citys!}&begDate=${begDate!}&endDate=${endDate!}">导出</a>
<#-- <a class="btn common_btn" href="importExcel?city=${citys!}&begDate=${begDate!}&endDate=${endDate!}">导出</a>-->
<div id="isHide" style="display: none">
<ul>
@@ -544,7 +567,7 @@
</tr>
</thead>
<tbody>
<#if statAnalyBoList??>
<#if statAnalyBoList??>
<#list statAnalyBoList as bo>
<tr>
<#if way?? && way==1>
@@ -591,7 +614,7 @@
<#else>
<tr><td ></td></tr>
</#if>
</tbody>
</table>
<div class="tabbable" style="padding-top: 60px;">
@@ -643,7 +666,7 @@
$("#statBut3").removeClass("cur");
});
$("#statBut2").click(function(){
this.addClass("cur");
$("#statBut1").removeClass("cur");
@@ -697,8 +720,8 @@
}
})
});
$(function () {
$('#container').highcharts({
@@ -734,8 +757,46 @@
});
});
</script>
function updateDateRange(option) {
const now = new Date();
const begDateInput = document.getElementById('begDate');
const endDateInput = document.getElementById('endDate');
if (option === 'lastWeek') {
const lastWeek = new Date();
lastWeek.setDate(now.getDate() - 7);
begDateInput.value = formatDate(lastWeek);
endDateInput.value = formatDate(now);
} else if (option === 'lastMonth') {
const lastMonth = new Date();
lastMonth.setMonth(now.getMonth() - 1);
begDateInput.value = formatDate(lastMonth);
endDateInput.value = formatDate(now);
} else {
begDateInput.value = '';
endDateInput.value = '';
}
}
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return year + '-' + month + '-' + day;
}
function submitForm(action) {
const form = document.getElementById('myForm');
if (action === 'query') {
debugger
form.action = "<#if way?? && way==0>../apiUser/pandect<#else>../apiUser/apiVisitStat</#if>";
} else if (action === 'download') {
form.action = "../apiUser/export";
}
form.submit();
}
</script>
</body>
</html>
</#compress>
@@ -91,6 +91,12 @@ table tr th {
<#if query=="uri">URL<#elseif query=="day">日期<#else>访问时长</#if>
</#if>
</th>
<th width="9%" colspan="1">
剩余使用次数
</th>
<th width="9%" colspan="1">
合同到期时间
</th>
<th width="9%" colspan="1">
访问次数
</th>
@@ -145,6 +151,8 @@ table tr th {
</#if>
</#if>
</td>
<td width="9%"><#if bo.surplusCallCount??>${bo.surplusCallCount}</#if></td>
<td width="9%"><#if bo.deadline??>${bo.deadline?string("yyyy-MM-dd")}</#if></td>
<td width="9%">${bo.count}</td>
<td width="9%">${bo.percent}</td>
<#if way==2 && query=='uri'><td>${resourceMap[bo.key]!}</td></#if>
@@ -1,5 +1,7 @@
package com.fangjia.service.open;
import java.util.Date;
public class StatAnalyBo {
private String key;
@@ -13,6 +15,17 @@ public class StatAnalyBo {
private Integer billingCount;
private Integer purpose;
private Double successRate;
// 新增剩余使用次数和到期时间
private Integer surplusCallCount;
private Date deadline;
// 新增url和name
private String url;
private String name;
// 添加日均使用量
private Double dailyUse;
public String getKey() {
return key;
}
@@ -85,4 +98,44 @@ public class StatAnalyBo {
public void setPurpose(Integer purpose) {
this.purpose = purpose;
}
public Integer getSurplusCallCount() {
return surplusCallCount;
}
public void setSurplusCallCount(Integer surplusCallCount) {
this.surplusCallCount = surplusCallCount;
}
public Date getDeadline() {
return deadline;
}
public void setDeadline(Date deadline) {
this.deadline = deadline;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getDailyUse() {
return dailyUse;
}
public void setDailyUse(Double dailyUse) {
this.dailyUse = dailyUse;
}
}