|
@ -14,7 +14,7 @@ import org.springframework.data.mongodb.core.MongoOperations;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by chenweida on 2016/1/27.
|
|
@ -91,7 +91,6 @@ public class ServiceMonitorService {
|
|
|
}
|
|
|
public void bandwidth(String beginTime, String endTime) {
|
|
|
|
|
|
|
|
|
DBObject match = getMatchFields(beginTime, endTime);
|
|
|
DBObject flowGroup = getFlowGroupFields();
|
|
|
DBObject sort = getSortFields();
|
|
@ -100,56 +99,74 @@ public class ServiceMonitorService {
|
|
|
AggregationOutput flowOutput = getBusinessLog().aggregate(match, flowGroup, sort);
|
|
|
// run aggregation
|
|
|
AggregationOutput serviceOutput = getBusinessLog().aggregate(match, serviceGroup, sort);
|
|
|
|
|
|
Integer calls = 0;
|
|
|
//流程带宽
|
|
|
Integer flowCalls = 0;
|
|
|
String routeId = "";
|
|
|
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"));
|
|
|
if (count >= 2) {
|
|
|
calls++;
|
|
|
flowCalls++;
|
|
|
}
|
|
|
}
|
|
|
long interval = getInterval(beginTime, endTime);
|
|
|
BigDecimal bandwidth = NumberUtil.divideBigDecimal(BigDecimal.valueOf(calls), BigDecimal.valueOf(interval));
|
|
|
saveServiceMetrics(routeId, "bandwidth", bandwidth.toString(), endTime);
|
|
|
|
|
|
Integer calls1 = 0;
|
|
|
BigDecimal flowBandwidth = NumberUtil.divideBigDecimal(BigDecimal.valueOf(flowCalls), BigDecimal.valueOf(interval));
|
|
|
saveServiceMetrics(routeId, "bandwidth", flowBandwidth.toString(), endTime);
|
|
|
//服务带宽
|
|
|
Integer serviceCalls = 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++;
|
|
|
serviceCalls++;
|
|
|
}
|
|
|
BigDecimal bandwidth1 = NumberUtil.divideBigDecimal(BigDecimal.valueOf(calls1), BigDecimal.valueOf(interval));
|
|
|
saveServiceMetrics(code, "bandwidth", bandwidth1.toString(), endTime);
|
|
|
serviceCalls = serviceCalls/2;
|
|
|
BigDecimal serviceBandwidth = NumberUtil.divideBigDecimal(BigDecimal.valueOf(serviceCalls), BigDecimal.valueOf(interval));
|
|
|
saveServiceMetrics(code, "bandwidth", serviceBandwidth.toString(), endTime);
|
|
|
}
|
|
|
|
|
|
|
|
|
public void qps(String beginTime, String endTime) {
|
|
|
DBObject match = getMatchFields(beginTime, endTime);
|
|
|
DBObject group = getFlowGroupFields();
|
|
|
DBObject flowGroup = getFlowGroupFields();
|
|
|
DBObject sort = getSortFields();
|
|
|
DBObject serviceGroup = getServiceGroupFields();
|
|
|
// run aggregation
|
|
|
AggregationOutput output = getBusinessLog().aggregate(match, group, sort);
|
|
|
Integer calls = 0;
|
|
|
AggregationOutput flowOutput = getBusinessLog().aggregate(match, flowGroup, sort);
|
|
|
// run aggregation
|
|
|
AggregationOutput serviceOutput = getBusinessLog().aggregate(match, serviceGroup, sort);
|
|
|
|
|
|
//流程qps
|
|
|
Integer flowCalls = 0;
|
|
|
String routeId = "";
|
|
|
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"));
|
|
|
if (count >= 2) {
|
|
|
Integer total = Integer.parseInt(StringUtil.toString(dbObject.get("total")));
|
|
|
if (total == count) {
|
|
|
calls++;
|
|
|
if (total == count/2) {
|
|
|
flowCalls++;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
long interval = getInterval(beginTime, endTime);
|
|
|
BigDecimal qps = NumberUtil.divideBigDecimal(BigDecimal.valueOf(calls), BigDecimal.valueOf(interval));
|
|
|
saveServiceMetrics(routeId, "qps", qps.toString(), endTime);
|
|
|
BigDecimal flowQps = NumberUtil.divideBigDecimal(BigDecimal.valueOf(flowCalls), BigDecimal.valueOf(interval));
|
|
|
saveServiceMetrics(routeId, "qps", flowQps.toString(), endTime);
|
|
|
//服务带宽
|
|
|
Integer serviceCalls = 0;
|
|
|
String code = "";
|
|
|
for (DBObject dbObject : serviceOutput.results()) {
|
|
|
BasicDBObject id = (BasicDBObject) dbObject.get("_id");
|
|
|
code = StringUtil.toString(id.get("code"));
|
|
|
serviceCalls++;
|
|
|
}
|
|
|
serviceCalls = serviceCalls/2;
|
|
|
BigDecimal serviceQps = NumberUtil.divideBigDecimal(BigDecimal.valueOf(serviceCalls), BigDecimal.valueOf(interval));
|
|
|
saveServiceMetrics(code, "bandwidth", serviceQps.toString(), endTime);
|
|
|
}
|
|
|
|
|
|
public void usage(String beginTime, String endTime) throws JsonProcessingException {
|
|
@ -157,53 +174,117 @@ public class ServiceMonitorService {
|
|
|
DBObject group = getFlowGroupFields();
|
|
|
DBObject sort = getSortFields();
|
|
|
// run aggregation
|
|
|
AggregationOutput output = getBusinessLog().aggregate(match, group, sort);
|
|
|
Integer successCount = 0;
|
|
|
Integer failureCount = 0;
|
|
|
AggregationOutput flowOutput = getBusinessLog().aggregate(match, group, sort);
|
|
|
Integer successCountF = 0;
|
|
|
Integer failureCountF = 0;
|
|
|
String routeId = "";
|
|
|
for (DBObject dbObject : output.results()) {
|
|
|
for (DBObject dbObject : flowOutput.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"));
|
|
|
if (total == count) {
|
|
|
successCount++;
|
|
|
successCountF++;
|
|
|
} else {
|
|
|
failureCount++;
|
|
|
failureCountF++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
JSONObject result = new JSONObject();
|
|
|
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);
|
|
|
|
|
|
JSONObject resultF = new JSONObject();
|
|
|
resultF.put("totalCount", successCountF + failureCountF);
|
|
|
resultF.put("successCount", successCountF);
|
|
|
resultF.put("failureCount", failureCountF);
|
|
|
saveServiceMetrics(routeId, "usage", objectMapper.writeValueAsString(resultF.toString()), endTime);
|
|
|
|
|
|
DBCursor serviceOutput = getBusinessLog().find(getQueryObject(beginTime, endTime));
|
|
|
|
|
|
Map<String, Integer> usageMap = new HashMap<>();
|
|
|
Map<String, Integer> usageFailMap = new HashMap<>();
|
|
|
Map<String, DBObject> serviceMap = new HashMap<>();
|
|
|
for (DBObject dbObject : serviceOutput.toArray()) {
|
|
|
String code = StringUtil.toString(dbObject.get("code"));
|
|
|
if (!usageMap.containsKey(code)) {
|
|
|
usageMap.put(code, 0);
|
|
|
}
|
|
|
if (serviceMap.containsKey(code)) {
|
|
|
|
|
|
serviceMap.remove(code);
|
|
|
Integer usageTemp = usageMap.get(code);
|
|
|
usageMap.put(code, ++usageTemp);
|
|
|
usageFailMap.put(code, 0);
|
|
|
} else {
|
|
|
serviceMap.put(code, dbObject);
|
|
|
Integer usageTemp = usageMap.get(code);
|
|
|
usageFailMap.put(code, ++usageTemp);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for (String code : usageMap.keySet()) {
|
|
|
JSONObject resultS = new JSONObject();
|
|
|
resultS.put("totalCount", usageMap.get(code) + usageFailMap.get(code));
|
|
|
resultS.put("successCount", usageMap.get(code));
|
|
|
resultS.put("failureCount", usageFailMap.get(code));
|
|
|
saveServiceMetrics(code, "usage", objectMapper.writeValueAsString(resultS.toString()), endTime);
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void delay(String beginTime, String endTime) {
|
|
|
DBObject match = getMatchFields(beginTime, endTime);
|
|
|
DBObject group = getFlowGroupFields();
|
|
|
DBObject flowGroup = getFlowGroupFields();
|
|
|
DBObject sort = getSortFields();
|
|
|
// run aggregation
|
|
|
AggregationOutput output = getBusinessLog().aggregate(match, group, sort);
|
|
|
Integer calls = 0;
|
|
|
AggregationOutput flowOutput = getBusinessLog().aggregate(match, flowGroup, sort);
|
|
|
|
|
|
|
|
|
BigDecimal flowDelay = BigDecimal.ZERO;
|
|
|
String routeId = "";
|
|
|
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"));
|
|
|
if (count >= 2) {
|
|
|
calls++;
|
|
|
String begin = StringUtil.toString(dbObject.get("beginTime"));
|
|
|
String end = StringUtil.toString(dbObject.get("endTime"));
|
|
|
long interval = getInterval(begin, end);
|
|
|
flowDelay = flowDelay.add(BigDecimal.valueOf(interval));
|
|
|
}
|
|
|
}
|
|
|
saveServiceMetrics(routeId, "delay", flowDelay.toString(), endTime);
|
|
|
|
|
|
long interval = getInterval(beginTime, endTime);
|
|
|
BigDecimal delay = NumberUtil.divideBigDecimal(BigDecimal.valueOf(interval), BigDecimal.valueOf(calls));
|
|
|
saveServiceMetrics(routeId, "delay", delay.toString(), endTime);
|
|
|
|
|
|
DBCursor serviceOutput = getBusinessLog().find(getQueryObject(beginTime, endTime));
|
|
|
|
|
|
Map<String, BigDecimal> delayMap = new HashMap<>();
|
|
|
Map<String, DBObject> serviceMap = new HashMap<>();
|
|
|
for (DBObject dbObject : serviceOutput.toArray()) {
|
|
|
String code = StringUtil.toString(dbObject.get("code"));
|
|
|
if (serviceMap.containsKey(code)) {
|
|
|
DBObject dbObjectTemp = serviceMap.remove(code);
|
|
|
String begin = StringUtil.toString(dbObjectTemp.get("fireTime"));
|
|
|
String end = StringUtil.toString(dbObject.get("fireTime"));
|
|
|
long interval = getInterval(begin, end);
|
|
|
if (delayMap.containsKey(code)) {
|
|
|
BigDecimal delayTemp = delayMap.get(code);
|
|
|
delayMap.put(code, delayTemp.add(BigDecimal.valueOf(interval)));
|
|
|
} else {
|
|
|
delayMap.put(code, BigDecimal.valueOf(interval));
|
|
|
}
|
|
|
} else {
|
|
|
serviceMap.put(code, dbObject);
|
|
|
}
|
|
|
}
|
|
|
for (String code : delayMap.keySet()) {
|
|
|
saveServiceMetrics(code, "delay", delayMap.get(code).toString(), endTime);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public DBObject getMatchFields(String beginTime, String endTime) {
|
|
|
return new BasicDBObject("$match", getQueryObject(beginTime, endTime));
|
|
|
}
|
|
|
|
|
|
public DBObject getQueryObject(String beginTime, String endTime) {
|
|
|
BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
|
|
|
new BasicDBObject[]{
|
|
|
new BasicDBObject().append("fireTime",
|
|
@ -211,9 +292,10 @@ public class ServiceMonitorService {
|
|
|
new BasicDBObject().append("fireTime",
|
|
|
new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
|
|
|
|
|
|
return new BasicDBObject("$match", queryObject);
|
|
|
return queryObject;
|
|
|
}
|
|
|
|
|
|
|
|
|
public DBObject getServiceGroupFields() {
|
|
|
|
|
|
// Now the $group operation
|
|
@ -233,6 +315,9 @@ public class ServiceMonitorService {
|
|
|
.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"));
|
|
|
|
|
|
return new BasicDBObject("$group", groupFields);
|
|
|
}
|
|
|
|
|
@ -242,8 +327,8 @@ public class ServiceMonitorService {
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
Date from = DateUtil.toTimestamp(beginTime, DateUtil.DEFAULT_TIMESTAMP_FORMAT);
|
|
|
Date to = DateUtil.toTimestamp(endTime, DateUtil.DEFAULT_TIMESTAMP_FORMAT);
|
|
|
long interval = (to.getTime() - from.getTime())/1000;
|
|
|
return interval;
|
|
|
}
|