diff --git a/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/daoimpl/SysLogSystemDaoImpl.java b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/daoimpl/SysLogSystemDaoImpl.java new file mode 100644 index 0000000..4b9da59 --- /dev/null +++ b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/daoimpl/SysLogSystemDaoImpl.java @@ -0,0 +1,106 @@ +package cn.estsh.i3plus.core.apiservice.daoimpl; + +import cn.estsh.i3plus.core.apiservice.dao.ISysLogSystemDao; +import cn.estsh.i3plus.platform.common.tool.StringTool; +import cn.estsh.i3plus.pojo.platform.bean.SysLogOperate; +import cn.estsh.i3plus.pojo.platform.bean.SysLogSystem; +import com.alibaba.fastjson.JSONObject; +import com.mongodb.Block; +import com.mongodb.client.AggregateIterable; +import com.mongodb.client.MongoCursor; +import io.swagger.annotations.ApiOperation; +import org.apache.jasper.tagplugins.jstl.core.ForEach; +import org.bson.Document; +import org.bson.json.JsonMode; +import org.bson.json.JsonWriterSettings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.MongoDbFactory; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @Description : + * @Reference : + * @Author : yunhao + * @CreateDate : 2018-12-11 13:24 + * @Modify: + **/ +@Service +public class SysLogSystemDaoImpl implements ISysLogSystemDao { + + public static final Logger LOGGER = LoggerFactory.getLogger(SysLogSystemDaoImpl.class); + + @Autowired + private MongoDbFactory mongoDbFactory; + + @Override + @ApiOperation(value = "查询时间段内平均请求耗时") + public Map querySysLogOperateAvgExecuteTime(String startTime, String endTime) { + List dList = new ArrayList<>(); + Block saveBlock = new Block() { + @Override + public void apply(final Document document) { + dList.add(document.toJson()); + } + }; + + Document sub_match = new Document(); + sub_match.put("leaveTime", new Document("$gte", startTime).append("$lte", endTime)); + + + Document sub_substr = new Document(); + sub_substr.put("$substr", new Object[]{"$createDatetime", 0, 16}); + + + Document sub_project = new Document(); + sub_project.put("new_time_stamp", sub_substr); + sub_project.put("executeTime", 1); + + Document sub_group = new Document(); + sub_group.put("_id", "createDatetime"); + sub_group.put("avg", new Document("$avg", "$executeTime")); + + + Document match = new Document("$match", sub_match); + Document project = new Document("$project", sub_project); + Document group = new Document("$group", sub_group); + Document sort = new Document("$sort", new Document("_id", 1)); + + List aggregateList = new ArrayList<>(); + aggregateList.add(match); +// aggregateList.add(project); + aggregateList.add(group); + aggregateList.add(sort); + + AggregateIterable findIter = mongoDbFactory.getDb() + .getCollection(StringTool.toLowerCaseFirstOne(SysLogSystem.class.getSimpleName())) + .aggregate(aggregateList); + MongoCursor cursor = findIter.iterator(); + while (cursor.hasNext()) { + Document item_doc = cursor.next(); + int _id = item_doc.getInteger("_id", 0); + int avg = item_doc.getInteger("avg", 0); + System.out.println("_id:" + _id + "abg:" + avg); + } + + return packObjectListFromDocument(dList); + } + + + private Map packObjectListFromDocument(List dList) { + //将获取的document转为对象 + List resultList = new ArrayList<>(); + // 设置为宽松模式 + JsonWriterSettings jsonWriterSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build(); + for (String d : dList) { + resultList.add(JSONObject.parseObject(d, SysLogOperate.class)); + } + + return null; + } +}