Commit 4ebf3eae authored by wangsong's avatar wangsong

add max hour data

parent 0c1a6c5b
......@@ -10,6 +10,7 @@ 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.model.AuthLogHourCount;
import com.chineseall.eden.authcenter.log.service.AuthLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
......@@ -304,10 +305,10 @@ public class StatisticsController {
public MapMessage authPickTimes(@RequestParam(value = "clientId", required = false) String clientId, @RequestParam(value = "latestDays", defaultValue = "7") Integer latestDays) {
Map<String,Object> param = new HashMap<>();
param.put("loginSuccessFlag", true);
Map<String,Object> commonParam = new HashMap<>();
commonParam.put("loginSuccessFlag", true);
if (clientId != null){
param.put("clientId",clientId);
commonParam.put("clientId",clientId);
}
Date startDate = getStartDay(latestDays);
......@@ -315,6 +316,8 @@ public class StatisticsController {
List<Integer> hourList = new ArrayList<>();
List<Long> hourCountList = new ArrayList<>();
Map<String, Object> param = new HashMap<>();
param.putAll(commonParam);
for(int i = 0 ; i < 24; i++){
param.put("hour", i);
long count = authLogService.count(param, startDate);
......@@ -322,7 +325,13 @@ public class StatisticsController {
hourCountList.add(Double.valueOf(MathUtils.doubleDivide(count, latestDays, 0)).longValue());
}
return MapMessage.successMessage().add("hourList", hourList).add("dataList", hourCountList);
List<AuthLogHourCount> hourTopList = authLogService.hourMaxCount(commonParam, startDate, 1);
AuthLogHourCount max = null;
if(CollectionUtils.isNotEmpty(hourTopList)){
max = hourTopList.get(0);
}
return MapMessage.successMessage().add("hourList", hourList).add("dataList", hourCountList).add("maxHour", max);
}
}
......@@ -5,14 +5,25 @@ import cn.sh.chineseall.framework.core.repackaged.org.springframework.data.domai
import cn.sh.chineseall.framework.core.repackaged.org.springframework.data.domain.Sort;
import cn.sh.chineseall.framework.core.util.CollectionUtils;
import cn.sh.chineseall.framework.core.util.StringUtils;
import cn.sh.chineseall.framework.dao.core.annotation.mongo.DocumentCollection;
import cn.sh.chineseall.framework.dao.core.annotation.mongo.DocumentDatabase;
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;
import cn.sh.chineseall.framework.dao.mongo.hql.MongoCriteriaTranslator;
import com.chineseall.eden.authcenter.log.model.AuthLog;
import com.chineseall.eden.authcenter.log.model.AuthLogHourCount;
import com.mongodb.MongoClient;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.*;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
@Component
public class AuthLogDao extends StaticCacheDimensionDocumentMongoDao<AuthLog, String> {
......@@ -134,4 +145,44 @@ public class AuthLogDao extends StaticCacheDimensionDocumentMongoDao<AuthLog, St
Pageable pageable = new PageRequest(pageNo-1, pageSize, sort);
return query(query.with(pageable));
}
public List<AuthLogHourCount> hourMaxCount(Map<String, Object> matchParams, Date beginTime, int topN){
DocumentDatabase documentDatabase = (DocumentDatabase)this.getDocumentClass().getAnnotation(DocumentDatabase.class);
DocumentCollection documentCollection = (DocumentCollection)this.getDocumentClass().getAnnotation(DocumentCollection.class);
MongoClient mongoClient = super.getMongoClient();
MongoDatabase database = mongoClient.getDatabase(documentDatabase.database());
MongoCollection<BsonDocument> collection = database.getCollection(documentCollection.collection(), BsonDocument.class);
Criteria criteria = new Criteria();
if (null != matchParams && matchParams.size() > 0){
matchParams.forEach((k,v)->{
criteria.and(k).is(v);
});
}
if(beginTime!=null){
criteria.and("createTime").gte(beginTime);
}
List<Bson> aggregateList = new ArrayList<>();
BsonDocument matchDocument = MongoCriteriaTranslator.INSTANCE.translate(criteria);
aggregateList.add(Aggregates.match(matchDocument));
Document groupBy = new Document().append("yearMonthDay", "$yearMonthDay").append("hour", "$hour");
aggregateList.add(Aggregates.group(groupBy, Accumulators.sum("count", 1)));
Document project = new Document().append("_id", 0).append("yearMonthDay", "$_id.yearMonthDay").append("hour", "$_id.hour").append("count", "$count");
aggregateList.add(Aggregates.project(project));
aggregateList.add(Aggregates.sort(Sorts.descending("count")));
aggregateList.add(Aggregates.limit(topN));
AggregateIterable<AuthLogHourCount> iterable = collection.aggregate(aggregateList, AuthLogHourCount.class);
List<AuthLogHourCount> dataList = new ArrayList<>();
for(AuthLogHourCount item : iterable){
dataList.add(item);
}
return dataList;
}
}
package com.chineseall.eden.authcenter.log.model;
import java.io.Serializable;
public class AuthLogHourCount implements Serializable {
private String yearMonthDay;
private Integer hour;
private Long count;
}
package com.chineseall.eden.authcenter.log.service;
import com.chineseall.eden.authcenter.log.model.AuthLog;
import com.chineseall.eden.authcenter.log.model.AuthLogHourCount;
import java.util.Date;
import java.util.List;
......@@ -28,4 +29,6 @@ public interface AuthLogService {
void replace(AuthLog authLog);
public List<AuthLog> listLogWithPage(int pageNo, int pageSize,Map<String, Object> prams);
List<AuthLogHourCount> hourMaxCount(Map<String, Object> matchParams, Date beginTime, int topN);
}
......@@ -3,6 +3,7 @@ package com.chineseall.eden.authcenter.log.service.impl;
import cn.sh.chineseall.framework.core.util.CollectionUtils;
import com.chineseall.eden.authcenter.log.dao.AuthLogDao;
import com.chineseall.eden.authcenter.log.model.AuthLog;
import com.chineseall.eden.authcenter.log.model.AuthLogHourCount;
import com.chineseall.eden.authcenter.log.service.AuthLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -96,4 +97,9 @@ public class AuthLogServiceImpl implements AuthLogService {
public List<AuthLog> listLogWithPage(int pageNo, int pageSize,Map<String, Object> prams) {
return authLogDao.listLogWithPage(pageNo,pageSize,prams);
}
@Override
public List<AuthLogHourCount> hourMaxCount(Map<String, Object> matchParams, Date beginTime, int topN) {
return authLogDao.hourMaxCount(matchParams, beginTime, topN);
}
}
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