|
@ -1,12 +1,13 @@
|
|
|
package com.yihu.hos.services;
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.mongodb.*;
|
|
|
import com.yihu.hos.core.datatype.DateUtil;
|
|
|
import com.yihu.hos.core.datatype.NumberUtil;
|
|
|
import com.yihu.hos.core.datatype.StringUtil;
|
|
|
import com.yihu.hos.models.BusinessLog;
|
|
|
import com.yihu.hos.models.ServiceMetrics;
|
|
|
import com.yihu.hos.web.framework.model.Result;
|
|
|
import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.mongodb.core.MongoOperations;
|
|
@ -21,10 +22,9 @@ import java.util.Date;
|
|
|
@Service("ServiceMonitorService")
|
|
|
public class ServiceMonitorService {
|
|
|
public static final String BEAN_ID = "ServiceMonitorService";
|
|
|
|
|
|
@Autowired
|
|
|
private MongoOperations mongoOperations;
|
|
|
|
|
|
private DBCollection businessLog;
|
|
|
// public Result mapReduce(String beginTime, String endTime) {
|
|
|
// DBCollection businessLogDB = mongoOperations.getCollection(mongoOperations
|
|
|
// .getCollectionName(BusinessLog.class));
|
|
@ -81,170 +81,95 @@ public class ServiceMonitorService {
|
|
|
// return jsonObject;
|
|
|
// }
|
|
|
|
|
|
public Result bandwidth(String beginTime, String endTime) {
|
|
|
DBCollection businessLog = mongoOperations.getCollection(mongoOperations
|
|
|
.getCollectionName(BusinessLog.class));
|
|
|
BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
|
|
|
new BasicDBObject[]{
|
|
|
new BasicDBObject().append("fireTime",
|
|
|
new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
|
|
|
new BasicDBObject().append("fireTime",
|
|
|
new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
|
|
|
public DBCollection getBusinessLog() {
|
|
|
if (businessLog == null) {
|
|
|
businessLog = mongoOperations.getCollection(mongoOperations
|
|
|
.getCollectionName(BusinessLog.class));
|
|
|
}
|
|
|
return businessLog;
|
|
|
|
|
|
DBObject match = new BasicDBObject("$match", queryObject);
|
|
|
}
|
|
|
public void bandwidth(String beginTime, String endTime) {
|
|
|
|
|
|
// Now the $group operation
|
|
|
DBObject groupFields = new BasicDBObject( "_id",
|
|
|
new BasicDBObject("breadcrumbId", "$breadcrumbId")
|
|
|
.append("routeId", "$routeId"));
|
|
|
// groupFields.put("routeId", new BasicDBObject( "$first", "$routeId"));
|
|
|
groupFields.put("count", new BasicDBObject( "$sum", 1));
|
|
|
groupFields.put("beginTime", new BasicDBObject( "$first", "$fireTimeSource"));
|
|
|
groupFields.put("endTime", new BasicDBObject("$last", "$fireTimeSource"));
|
|
|
DBObject group = new BasicDBObject("$group", groupFields);
|
|
|
// build the $sort operation
|
|
|
DBObject sortFields = new BasicDBObject( "_id", 1);
|
|
|
DBObject sort = new BasicDBObject("$sort", sortFields );
|
|
|
|
|
|
DBObject match = getMatchFields(beginTime, endTime);
|
|
|
DBObject flowGroup = getFlowGroupFields();
|
|
|
DBObject sort = getSortFields();
|
|
|
DBObject serviceGroup = getServiceGroupFields();
|
|
|
// run aggregation
|
|
|
AggregationOutput flowOutput = getBusinessLog().aggregate(match, flowGroup, sort);
|
|
|
// run aggregation
|
|
|
AggregationOutput output = businessLog.aggregate(match, group, sort);
|
|
|
AggregationOutput serviceOutput = getBusinessLog().aggregate(match, serviceGroup, sort);
|
|
|
|
|
|
Integer calls = 0;
|
|
|
long interval = 0;
|
|
|
String routeId = "";
|
|
|
String breadcrumbId = "";
|
|
|
for (DBObject dbObject : output.results()) {
|
|
|
for (DBObject dbObject : flowOutput.results()) {
|
|
|
Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
|
|
|
BasicDBObject id = (BasicDBObject) dbObject.get("_id");
|
|
|
routeId = StringUtil.toString(id.get("routeId"));
|
|
|
breadcrumbId = StringUtil.toString(id.get("breadcrumbId"));
|
|
|
if (count >= 2) {
|
|
|
calls++;
|
|
|
String begin = StringUtil.toString(dbObject.get("beginTime"));
|
|
|
String end = StringUtil.toString(dbObject.get("endTime"));
|
|
|
Date from = DateUtil.toTimestamp(begin, DateUtil.DEFAULT_YMDHMSDATE_FORMAT);
|
|
|
Date to = DateUtil.toTimestamp(end, DateUtil.DEFAULT_YMDHMSDATE_FORMAT);
|
|
|
interval += (to.getTime() - from.getTime())/1000;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
long interval = getInterval(beginTime, endTime);
|
|
|
BigDecimal bandwidth = NumberUtil.divideBigDecimal(BigDecimal.valueOf(calls), BigDecimal.valueOf(interval));
|
|
|
saveServiceMetrics(routeId, "bandwidth", bandwidth.toString(), endTime);
|
|
|
|
|
|
ServiceMetrics serviceMetrics = new ServiceMetrics();
|
|
|
serviceMetrics.setRouteId(routeId);
|
|
|
serviceMetrics.setBreadcrumbId(breadcrumbId);
|
|
|
serviceMetrics.setType("bandwidth");
|
|
|
serviceMetrics.setValue(bandwidth.toString());
|
|
|
serviceMetrics.setCreateTime(endTime);
|
|
|
mongoOperations.save(serviceMetrics);
|
|
|
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("bandwidth", bandwidth);
|
|
|
System.out.println(result);
|
|
|
return Result.success(result.toString());
|
|
|
Integer calls1 = 0;
|
|
|
String code = "";
|
|
|
for (DBObject dbObject : serviceOutput.results()) {
|
|
|
Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
|
|
|
BasicDBObject id = (BasicDBObject) dbObject.get("_id");
|
|
|
code = StringUtil.toString(id.get("code"));
|
|
|
calls++;
|
|
|
}
|
|
|
BigDecimal bandwidth1 = NumberUtil.divideBigDecimal(BigDecimal.valueOf(calls1), BigDecimal.valueOf(interval));
|
|
|
saveServiceMetrics(code, "bandwidth", bandwidth1.toString(), endTime);
|
|
|
}
|
|
|
|
|
|
public Result qps(String beginTime, String endTime) {
|
|
|
DBCollection businessLog = mongoOperations.getCollection(mongoOperations
|
|
|
.getCollectionName(BusinessLog.class));
|
|
|
BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
|
|
|
new BasicDBObject[]{
|
|
|
new BasicDBObject().append("fireTime",
|
|
|
new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
|
|
|
new BasicDBObject().append("fireTime",
|
|
|
new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
|
|
|
|
|
|
DBObject match = new BasicDBObject("$match", queryObject);
|
|
|
|
|
|
// Now the $group operation
|
|
|
DBObject groupFields = new BasicDBObject( "_id",
|
|
|
new BasicDBObject("breadcrumbId", "$breadcrumbId")
|
|
|
.append("routeId", "$routeId"));
|
|
|
groupFields.put("count", new BasicDBObject( "$sum", 1));
|
|
|
groupFields.put("total", new BasicDBObject( "$first", "$totalServers"));
|
|
|
groupFields.put("beginTime", new BasicDBObject( "$first", "$fireTimeSource"));
|
|
|
groupFields.put("endTime", new BasicDBObject( "$last", "$fireTimeSource"));
|
|
|
DBObject group = new BasicDBObject("$group", groupFields);
|
|
|
|
|
|
// build the $sort operation
|
|
|
DBObject sortFields = new BasicDBObject( "_id", 1);
|
|
|
DBObject sort = new BasicDBObject("$sort", sortFields );
|
|
|
public void qps(String beginTime, String endTime) {
|
|
|
DBObject match = getMatchFields(beginTime, endTime);
|
|
|
DBObject group = getFlowGroupFields();
|
|
|
DBObject sort = getSortFields();
|
|
|
// run aggregation
|
|
|
AggregationOutput output = businessLog.aggregate(match, group, sort);
|
|
|
AggregationOutput output = getBusinessLog().aggregate(match, group, sort);
|
|
|
Integer calls = 0;
|
|
|
long interval = 0;
|
|
|
String routeId = "";
|
|
|
String breadcrumbId = "";
|
|
|
for (DBObject dbObject : output.results()) {
|
|
|
Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
|
|
|
BasicDBObject id = (BasicDBObject) dbObject.get("_id");
|
|
|
routeId = StringUtil.toString(id.get("routeId"));
|
|
|
breadcrumbId = StringUtil.toString(id.get("breadcrumbId"));
|
|
|
if (count >= 2) {
|
|
|
Integer total = Integer.parseInt(StringUtil.toString(dbObject.get("total")));
|
|
|
if (total == count) {
|
|
|
calls++;
|
|
|
String begin = StringUtil.toString(dbObject.get("beginTime"));
|
|
|
String end = StringUtil.toString(dbObject.get("endTime"));
|
|
|
Date from = DateUtil.toTimestamp(begin, DateUtil.DEFAULT_YMDHMSDATE_FORMAT);
|
|
|
Date to = DateUtil.toTimestamp(end, DateUtil.DEFAULT_YMDHMSDATE_FORMAT);
|
|
|
interval += (to.getTime() - from.getTime())/1000;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
long interval = getInterval(beginTime, endTime);
|
|
|
BigDecimal qps = NumberUtil.divideBigDecimal(BigDecimal.valueOf(calls), BigDecimal.valueOf(interval));
|
|
|
ServiceMetrics serviceMetrics = new ServiceMetrics();
|
|
|
serviceMetrics.setRouteId(routeId);
|
|
|
serviceMetrics.setBreadcrumbId(breadcrumbId);
|
|
|
serviceMetrics.setType("qps");
|
|
|
serviceMetrics.setValue(qps.toString());
|
|
|
serviceMetrics.setCreateTime(endTime);
|
|
|
mongoOperations.save(serviceMetrics);
|
|
|
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("qps", qps);
|
|
|
return Result.success(result.toString());
|
|
|
saveServiceMetrics(routeId, "qps", qps.toString(), endTime);
|
|
|
}
|
|
|
|
|
|
public Result usage(String beginTime, String endTime) {
|
|
|
DBCollection businessLog = mongoOperations.getCollection(mongoOperations
|
|
|
.getCollectionName(BusinessLog.class));
|
|
|
BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
|
|
|
new BasicDBObject[]{
|
|
|
new BasicDBObject().append("fireTime",
|
|
|
new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
|
|
|
new BasicDBObject().append("fireTime",
|
|
|
new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
|
|
|
|
|
|
DBObject match = new BasicDBObject("$match", queryObject);
|
|
|
|
|
|
// Now the $group operation
|
|
|
DBObject groupFields = new BasicDBObject( "_id",
|
|
|
new BasicDBObject("breadcrumbId", "$breadcrumbId")
|
|
|
.append("routeId", "$routeId"));
|
|
|
groupFields.put("count", new BasicDBObject( "$sum", 1));
|
|
|
groupFields.put("total", new BasicDBObject( "$first", "$totalServers"));
|
|
|
DBObject group = new BasicDBObject("$group", groupFields);
|
|
|
|
|
|
// build the $sort operation
|
|
|
DBObject sortFields = new BasicDBObject( "_id", 1);
|
|
|
DBObject sort = new BasicDBObject("$sort", sortFields );
|
|
|
public void usage(String beginTime, String endTime) throws JsonProcessingException {
|
|
|
DBObject match = getMatchFields(beginTime, endTime);
|
|
|
DBObject group = getFlowGroupFields();
|
|
|
DBObject sort = getSortFields();
|
|
|
// run aggregation
|
|
|
AggregationOutput output = businessLog.aggregate(match, group, sort);
|
|
|
AggregationOutput output = getBusinessLog().aggregate(match, group, sort);
|
|
|
Integer successCount = 0;
|
|
|
Integer failureCount = 0;
|
|
|
String routeId = "";
|
|
|
String breadcrumbId = "";
|
|
|
for (DBObject dbObject : output.results()) {
|
|
|
Integer total = Integer.parseInt(StringUtil.toString(dbObject.get("total")));
|
|
|
Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
|
|
|
BasicDBObject id = (BasicDBObject) dbObject.get("_id");
|
|
|
routeId = StringUtil.toString(id.get("routeId"));
|
|
|
breadcrumbId = StringUtil.toString(id.get("breadcrumbId"));
|
|
|
if (total == count) {
|
|
|
successCount++;
|
|
|
} else {
|
|
|
failureCount++;
|
|
|
failureCount++;
|
|
|
}
|
|
|
}
|
|
|
|
|
@ -252,22 +177,33 @@ public class ServiceMonitorService {
|
|
|
result.put("totalCount", successCount + failureCount);
|
|
|
result.put("successCount", successCount);
|
|
|
result.put("failureCount", failureCount);
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
saveServiceMetrics(routeId, "usage", objectMapper.writeValueAsString(result.toString()), endTime);
|
|
|
}
|
|
|
|
|
|
ServiceMetrics serviceMetrics = new ServiceMetrics();
|
|
|
serviceMetrics.setRouteId(routeId);
|
|
|
serviceMetrics.setBreadcrumbId(breadcrumbId);
|
|
|
serviceMetrics.setType("usage");
|
|
|
serviceMetrics.setValue(result.toString());
|
|
|
serviceMetrics.setCreateTime(endTime);
|
|
|
mongoOperations.save(serviceMetrics);
|
|
|
|
|
|
public void delay(String beginTime, String endTime) {
|
|
|
DBObject match = getMatchFields(beginTime, endTime);
|
|
|
DBObject group = getFlowGroupFields();
|
|
|
DBObject sort = getSortFields();
|
|
|
// run aggregation
|
|
|
AggregationOutput output = getBusinessLog().aggregate(match, group, sort);
|
|
|
Integer calls = 0;
|
|
|
String routeId = "";
|
|
|
for (DBObject dbObject : output.results()) {
|
|
|
Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
|
|
|
BasicDBObject id = (BasicDBObject) dbObject.get("_id");
|
|
|
routeId = StringUtil.toString(id.get("routeId"));
|
|
|
if (count >= 2) {
|
|
|
calls++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return Result.success(result.toString());
|
|
|
long interval = getInterval(beginTime, endTime);
|
|
|
BigDecimal delay = NumberUtil.divideBigDecimal(BigDecimal.valueOf(interval), BigDecimal.valueOf(calls));
|
|
|
saveServiceMetrics(routeId, "delay", delay.toString(), endTime);
|
|
|
}
|
|
|
|
|
|
public Result delay(String beginTime, String endTime) {
|
|
|
DBCollection businessLog = mongoOperations.getCollection(mongoOperations
|
|
|
.getCollectionName(BusinessLog.class));
|
|
|
public DBObject getMatchFields(String beginTime, String endTime) {
|
|
|
BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
|
|
|
new BasicDBObject[]{
|
|
|
new BasicDBObject().append("fireTime",
|
|
@ -275,53 +211,49 @@ public class ServiceMonitorService {
|
|
|
new BasicDBObject().append("fireTime",
|
|
|
new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
|
|
|
|
|
|
DBObject match = new BasicDBObject("$match", queryObject);
|
|
|
return new BasicDBObject("$match", queryObject);
|
|
|
}
|
|
|
|
|
|
public DBObject getServiceGroupFields() {
|
|
|
|
|
|
// Now the $group operation
|
|
|
DBObject groupFields = new BasicDBObject( "_id",
|
|
|
new BasicDBObject("id", "$_id")
|
|
|
.append("code", "$code"));
|
|
|
groupFields.put("count", new BasicDBObject( "$sum", 1));
|
|
|
groupFields.put("total", new BasicDBObject( "$first", "$totalServers"));
|
|
|
return new BasicDBObject("$group", groupFields);
|
|
|
}
|
|
|
|
|
|
public DBObject getFlowGroupFields() {
|
|
|
|
|
|
// Now the $group operation
|
|
|
DBObject groupFields = new BasicDBObject( "_id",
|
|
|
new BasicDBObject("breadcrumbId", "$breadcrumbId")
|
|
|
.append("routeId", "$routeId"));
|
|
|
groupFields.put("count", new BasicDBObject( "$sum", 1));
|
|
|
groupFields.put("beginTime", new BasicDBObject( "$first", "$fireTimeSource"));
|
|
|
groupFields.put("endTime", new BasicDBObject( "$last", "$fireTimeSource"));
|
|
|
DBObject group = new BasicDBObject("$group", groupFields);
|
|
|
groupFields.put("total", new BasicDBObject( "$first", "$totalServers"));
|
|
|
return new BasicDBObject("$group", groupFields);
|
|
|
}
|
|
|
|
|
|
// build the $sort operation
|
|
|
public DBObject getSortFields() {
|
|
|
DBObject sortFields = new BasicDBObject( "_id", 1);
|
|
|
DBObject sort = new BasicDBObject("$sort", sortFields );
|
|
|
// run aggregation
|
|
|
AggregationOutput output = businessLog.aggregate(match, group, sort);
|
|
|
Integer calls = 0;
|
|
|
long interval = 0;
|
|
|
String routeId = "";
|
|
|
String breadcrumbId = "";
|
|
|
for (DBObject dbObject : output.results()) {
|
|
|
Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
|
|
|
BasicDBObject id = (BasicDBObject) dbObject.get("_id");
|
|
|
routeId = StringUtil.toString(id.get("routeId"));
|
|
|
breadcrumbId = StringUtil.toString(id.get("breadcrumbId"));
|
|
|
if (count >= 2) {
|
|
|
calls++;
|
|
|
String begin = StringUtil.toString(dbObject.get("beginTime"));
|
|
|
String end = StringUtil.toString(dbObject.get("endTime"));
|
|
|
Date from = DateUtil.toTimestamp(begin, DateUtil.DEFAULT_YMDHMSDATE_FORMAT);
|
|
|
Date to = DateUtil.toTimestamp(end, DateUtil.DEFAULT_YMDHMSDATE_FORMAT);
|
|
|
interval += (to.getTime() - from.getTime())/1000;
|
|
|
}
|
|
|
}
|
|
|
return new BasicDBObject("$sort", sortFields );
|
|
|
}
|
|
|
|
|
|
BigDecimal delay = NumberUtil.divideBigDecimal(BigDecimal.valueOf(interval), BigDecimal.valueOf(calls));
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("delay", delay);
|
|
|
public long getInterval(String beginTime, String endTime) {
|
|
|
Date from = DateUtil.toTimestamp(beginTime, DateUtil.DEFAULT_YMDHMSDATE_FORMAT);
|
|
|
Date to = DateUtil.toTimestamp(endTime, DateUtil.DEFAULT_YMDHMSDATE_FORMAT);
|
|
|
long interval = (to.getTime() - from.getTime())/1000;
|
|
|
return interval;
|
|
|
}
|
|
|
|
|
|
public void saveServiceMetrics(String name, String type, String value, String createTime) {
|
|
|
ServiceMetrics serviceMetrics = new ServiceMetrics();
|
|
|
serviceMetrics.setRouteId(routeId);
|
|
|
serviceMetrics.setBreadcrumbId(breadcrumbId);
|
|
|
serviceMetrics.setType("delay");
|
|
|
serviceMetrics.setValue(delay.toString());
|
|
|
serviceMetrics.setCreateTime(endTime);
|
|
|
serviceMetrics.setName(name);
|
|
|
serviceMetrics.setType(type);
|
|
|
serviceMetrics.setValue(value);
|
|
|
serviceMetrics.setCreateTime(createTime);
|
|
|
mongoOperations.save(serviceMetrics);
|
|
|
|
|
|
return Result.success(result.toString());
|
|
|
}
|
|
|
}
|