Commit a783e533 authored by wangsong's avatar wangsong

add statistics api

parent f05b7b0a
package com.chineseall.eden.authcenter.agent.controller;
import cn.sh.chineseall.framework.api.MapMessage;
import cn.sh.chineseall.framework.core.util.CollectionUtils;
import cn.sh.chineseall.framework.core.util.StringUtils;
import cn.sh.chineseall.framework.lang.calendar.DateUtils;
import com.chineseall.eden.authcenter.agent.client.ClientItem;
import com.chineseall.eden.authcenter.agent.vo.StatItem;
import com.chineseall.eden.authcenter.config.entity.AuthClient;
import com.chineseall.eden.authcenter.config.service.AuthClientService;
import com.chineseall.eden.authcenter.config.service.AuthSourceService;
import com.chineseall.eden.authcenter.log.service.AuthLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
@Controller
@RequestMapping("statistics")
public class StatisticsController {
@Autowired
private AuthClientService authClientService;
@Autowired
private AuthSourceService authSourceService;
@Autowired
private AuthLogService authLogService;
/**
* 接入的应用列表
*
* @return
*/
@RequestMapping("auth_apps")
@ResponseBody
public MapMessage authApps() {
List<Map<String, Object>> result = new ArrayList<>();
List<AuthClient> clientList = authClientService.listAll();
if (CollectionUtils.isNotEmpty(clientList)){
clientList.forEach(item -> {
Map<String, Object> data = new HashMap<>();
data.put("code", item.getKey());
data.put("name", item.getName());
result.add(data);
});
}
return MapMessage.successMessage().add("data", result);
}
private Date getStartDay(Integer latestDays){
Date now = new Date();
return DateUtils.calculateDateDay(now, -latestDays);
}
/**
* APP使用情况统计
*
* @param latestDays 最近多少天的统计
* @param topN topN
* @return 指定app的认证数
*/
@RequestMapping("app")
@ResponseBody
public MapMessage applications(@RequestParam(value = "clientId", required = false) String clientId, @RequestParam(value = "latestDays", required = false) Integer latestDays, @RequestParam(value = "topN", required = false) Integer topN) {
List<StatItem> dataList = new ArrayList<>();
List<AuthClient> clientList = authClientService.listAll();
if (StringUtils.isNoneBlank(clientId)){
clientList = clientList.stream().filter(p -> Objects.equals(p.getKey(), clientId)).collect(Collectors.toList());
}
Map<String,Object> param = new HashMap<>();
param.put("loginSuccessFlag", true);
Date startDate = getStartDay(latestDays);
clientList.forEach(item -> {
StatItem statItem = new StatItem();
statItem.setCode(item.getKey());
statItem.setName(item.getName());
param.put("clientId",item.getKey());
long count = authLogService.count(param, startDate);
statItem.setCount(count);
dataList.add(statItem);
});
dataList.sort((o1, o2) -> Long.valueOf(o2.getCount() - o1.getCount()).intValue());
List<StatItem> result = dataList;
if(topN != null && topN > 0){
if(topN < result.size()){
result = result.subList(0, topN);
}
}
return MapMessage.successMessage().add("data", result);
}
/**
* 来源环境情况统计
*
* @param latestDays 最近多少天
* @return
*/
@RequestMapping("environment")
@ResponseBody
public MapMessage environments(@RequestParam(value = "clientId", required = false) String clientId, @RequestParam(value = "latestDays", required = false) Integer latestDays) {
Map<String, List<StatItem>> result = new HashMap<>();
Map<String,Object> param = new HashMap<>();
param.put("loginSuccessFlag", true);
if (clientId != null){
param.put("clientId",clientId);
}
Date startDate = getStartDay(latestDays);
long all_auth = authLogService.count(param, startDate);
List<StatItem> browserResult = new ArrayList<>();
List<String> browserList = authLogService.distinct("browser", param, startDate);
long unknownBrowser = all_auth;
if (CollectionUtils.isNotEmpty(browserList)){
for (String item : browserList) {
param.put("browser",item);
long count = authLogService.count(param, startDate);
StatItem statItem = new StatItem();
statItem.setName(item);
statItem.setCount(count);
unknownBrowser = unknownBrowser - count;
browserResult.add(statItem);
}
}
if (unknownBrowser > 0){
StatItem statItem = new StatItem();
statItem.setName("unknown");
statItem.setCount(unknownBrowser);
browserResult.add(statItem);
}
result.put("browser", browserResult);
List<StatItem> osResult = new ArrayList<>();
param.remove("browser");
List<String> osList = authLogService.distinct("os", param, startDate);
long unknownOs = all_auth;
if (CollectionUtils.isNotEmpty(osList)){
for (String item : osList) {
param.put("os",item);
long count = authLogService.count(param, startDate);
StatItem statItem = new StatItem();
statItem.setName(item);
statItem.setCount(count);
osResult.add(statItem);
unknownOs = unknownOs - count;
}
}
if (unknownOs > 0){
StatItem statItem = new StatItem();
statItem.setName("unknown");
statItem.setCount(unknownOs);
osResult.add(statItem);
}
result.put("os", osResult);
return MapMessage.successMessage().add("data", result);
}
}
......@@ -3,6 +3,7 @@ package com.chineseall.eden.authcenter.log.dao;
import cn.sh.chineseall.framework.core.repackaged.org.springframework.data.domain.PageRequest;
import cn.sh.chineseall.framework.core.repackaged.org.springframework.data.domain.Pageable;
import cn.sh.chineseall.framework.core.repackaged.org.springframework.data.domain.Sort;
import cn.sh.chineseall.framework.core.util.StringUtils;
import cn.sh.chineseall.framework.dao.core.hql.Criteria;
import cn.sh.chineseall.framework.dao.core.hql.Query;
import cn.sh.chineseall.framework.dao.mongo.dao.StaticCacheDimensionDocumentMongoDao;
......@@ -39,6 +40,9 @@ public class AuthLogDao extends StaticCacheDimensionDocumentMongoDao<AuthLog, St
if(params.get("endTime")!=null){
criteria.lte((Date) params.get("endTime"));
}
if(params.get("clientId") != null){
criteria.and("clientId").is(params.get("clientId").toString());
}
Query query = Query.query(criteria);
return count(query);
}
......@@ -60,6 +64,20 @@ public class AuthLogDao extends StaticCacheDimensionDocumentMongoDao<AuthLog, St
return super.distinct(key,new Query(criteria),String.class);
}
public List<String> distinct(String key, Map<String, Object> params, Date beginTime){
Criteria criteria = new Criteria();
if (null != params && params.size() > 0){
params.forEach((k,v)->{
criteria.and(k).is(v);
});
}
if(beginTime!=null){
criteria.and("createTime").gte(beginTime);
}
return super.distinct(key,new Query(criteria),String.class);
}
public long count(Map<String, Object> params){
Criteria criteria = new Criteria();
if (null != params && params.size() > 0){
......@@ -71,6 +89,20 @@ public class AuthLogDao extends StaticCacheDimensionDocumentMongoDao<AuthLog, St
return count(query);
}
public long count(Map<String, Object> params, Date beginTime){
Criteria criteria = new Criteria();
if (null != params && params.size() > 0){
params.forEach((k,v)->{
criteria.and(k).is(v);
});
}
if(beginTime!=null){
criteria.and("createTime").gte(beginTime);
}
Query query = Query.query(criteria);
return count(query);
}
public List<AuthLog> listLogWithPage(int pageNo, int pageSize,Map<String, Object> prams) {
Criteria criteria = new Criteria();
if (null != prams && prams.size() > 0){
......
......@@ -2,6 +2,7 @@ package com.chineseall.eden.authcenter.log.service;
import com.chineseall.eden.authcenter.log.model.AuthLog;
import java.util.Date;
import java.util.List;
import java.util.Map;
......@@ -17,7 +18,10 @@ public interface AuthLogService {
List<String> distinct(String key, Map<String, Object> prams);
List<String> distinct(String key, Map<String, Object> prams, Date beginTime);
long count(Map<String,Object> prams);
long count(Map<String,Object> prams, Date beginTime);
void replace(AuthLog authLog);
......
......@@ -57,11 +57,31 @@ public class AuthLogServiceImpl implements AuthLogService {
return result;
}
@Override
public List<String> distinct(String key, Map<String, Object> prams, Date beginTime) {
List<String> stringList = authLogDao.distinct(key, prams, beginTime);
List<String> result=new ArrayList<>();
if (CollectionUtils.isNotEmpty(stringList)){
stringList.forEach(item -> {
if (null != item){
result.add(item);
}
});
}
return result;
}
@Override
public long count(Map<String, Object> prams) {
return authLogDao.count(prams);
}
@Override
public long count(Map<String, Object> prams, Date beginTime) {
return authLogDao.count(prams, beginTime);
}
@Override
public void replace(AuthLog authLog) {
authLogDao.replace(authLog);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment