|
|
@@ -1,16 +1,22 @@
|
|
|
package cn.reghao.autodop.dmaster.sys.db;
|
|
|
|
|
|
+import cn.reghao.autodop.dmaster.sys.entity.AppRuntimeLog;
|
|
|
+import cn.reghao.autodop.dmaster.sys.vo.AppRuntimeLogVO;
|
|
|
+import cn.reghao.autodop.dmaster.sys.vo.RuntimeLogVO;
|
|
|
import com.mongodb.BasicDBObject;
|
|
|
import com.mongodb.client.AggregateIterable;
|
|
|
import org.bson.Document;
|
|
|
-import org.bson.types.ObjectId;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -24,36 +30,70 @@ public class MongoQuery {
|
|
|
this.mongoTemplate = mongoTemplate;
|
|
|
}
|
|
|
|
|
|
- public void aggregate(String col) {
|
|
|
- String groupById = "$answerId";
|
|
|
- Document groupObject = new Document("_id", groupById);
|
|
|
- groupObject.put("total", new BasicDBObject("$sum", 1));
|
|
|
- groupObject.put("data", new BasicDBObject("$push", "$$ROOT"));
|
|
|
- Document queryGroup = new Document("$group", groupObject);
|
|
|
+ public List<RuntimeLogVO> aggregate(String col, Map<String, String> groupByIds) {
|
|
|
+ Document groupBy = new Document();
|
|
|
+ groupBy.putAll(groupByIds);
|
|
|
|
|
|
- Document sortObject = new Document("$sort", new BasicDBObject("total", -1));
|
|
|
- Document limitObject = new Document("$limit", 10000);
|
|
|
+ Document groupByObject = new Document("_id", groupBy);
|
|
|
+ groupByObject.put("machineIpv4", new BasicDBObject("$first", "$machineIpv4"));
|
|
|
+ groupByObject.put("total", new BasicDBObject("$sum", 1));
|
|
|
|
|
|
+ Document queryObject = new Document("$group", groupByObject);
|
|
|
List<Document> pipeline = new ArrayList<>();
|
|
|
- pipeline.add(queryGroup);
|
|
|
- pipeline.add(sortObject);
|
|
|
- pipeline.add(limitObject);
|
|
|
+ pipeline.add(queryObject);
|
|
|
|
|
|
+ List<RuntimeLogVO> list = new ArrayList<>();
|
|
|
AggregateIterable<Document> result = mongoTemplate.getCollection(col).aggregate(pipeline).allowDiskUse(true);
|
|
|
for (Document document : result) {
|
|
|
- List<Document> list = document.getList("data", Document.class);
|
|
|
- int size = list.size();
|
|
|
- // 删除重复的数据
|
|
|
- if (size > 1) {
|
|
|
- for (int i = 1; i < size; i++) {
|
|
|
- Document doc = list.get(i);
|
|
|
- ObjectId objectId = doc.get("_id", ObjectId.class);
|
|
|
-
|
|
|
- Query query = new Query();
|
|
|
- query.addCriteria(Criteria.where("_id").is(objectId));
|
|
|
- //mongoTemplate.remove(query, col);
|
|
|
- }
|
|
|
- }
|
|
|
+ Document doc = document.get("_id", Document.class);
|
|
|
+ String machineId = doc.get("machineId", String.class);
|
|
|
+ String appId = doc.get("appId", String.class);
|
|
|
+ String machineIpv4 = document.get("machineIpv4", String.class);
|
|
|
+ Integer total = document.get("total", Integer.class);
|
|
|
+ list.add(new RuntimeLogVO(machineId, machineIpv4, appId));
|
|
|
}
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<AppRuntimeLogVO> aggregate1(String col, String machineId, String appId) {
|
|
|
+ Map<String, String> matchMap = new HashMap<>();
|
|
|
+ matchMap.put("machineId", machineId);
|
|
|
+ matchMap.put("appId", appId);
|
|
|
+ Document matchObject = new Document("$match", matchMap);
|
|
|
+
|
|
|
+ Map<String, String> groupMap = new HashMap<>();
|
|
|
+ groupMap.put("format", "%Y-%m-%d");
|
|
|
+ groupMap.put("date", "$createTime");
|
|
|
+ Document groupBy = new Document("yearMonthDay",
|
|
|
+ new BasicDBObject("$dateToString", new BasicDBObject(groupMap)));
|
|
|
+ Document groupByObject = new Document("_id", groupBy);
|
|
|
+ groupByObject.put("total", new BasicDBObject("$sum", 1));
|
|
|
+ Document groupObject = new Document("$group", groupByObject);
|
|
|
+
|
|
|
+ List<Document> pipeline = new ArrayList<>();
|
|
|
+ pipeline.add(matchObject);
|
|
|
+ pipeline.add(groupObject);
|
|
|
+
|
|
|
+ List<AppRuntimeLogVO> list = new ArrayList<>();
|
|
|
+ AggregateIterable<Document> result = mongoTemplate.getCollection(col).aggregate(pipeline).allowDiskUse(true);
|
|
|
+ for (Document document : result) {
|
|
|
+ Document doc = document.get("_id", Document.class);
|
|
|
+ String yearMonthDay = doc.get("yearMonthDay", String.class);
|
|
|
+ Integer total = document.get("total", Integer.class);
|
|
|
+ list.add(new AppRuntimeLogVO(machineId, appId, yearMonthDay, total));
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<AppRuntimeLog> query(String machineId, String appId, String yearMonthDay) {
|
|
|
+ LocalDate localDate = LocalDate.parse(yearMonthDay, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
|
|
+ LocalDate localDate1 = localDate.plusDays(1);
|
|
|
+
|
|
|
+ Query query = new Query();
|
|
|
+ query.addCriteria(Criteria.where("machineId").is(machineId));
|
|
|
+ query.addCriteria(Criteria.where("appId").is(appId));
|
|
|
+ query.addCriteria(Criteria.where("createTime").gte(localDate.atStartOfDay())
|
|
|
+ .andOperator(Criteria.where("createTime").lt(localDate1.atStartOfDay())));
|
|
|
+ return mongoTemplate.find(query, AppRuntimeLog.class);
|
|
|
}
|
|
|
}
|