|  | @ -3,11 +3,11 @@ package com.yihu.hos.broker.services;
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.core.JsonProcessingException;
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
	
		
			
				|  |  | import com.mongodb.*;
 | 
	
		
			
				|  |  | import com.yihu.hos.broker.models.BusinessLog;
 | 
	
		
			
				|  |  | import com.yihu.hos.broker.models.ServiceMetrics;
 | 
	
		
			
				|  |  | 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.broker.models.BusinessLog;
 | 
	
		
			
				|  |  | import com.yihu.hos.broker.models.ServiceMetrics;
 | 
	
		
			
				|  |  | import org.json.JSONObject;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Value;
 | 
	
	
		
			
				|  | @ -15,7 +15,10 @@ import org.springframework.data.mongodb.core.MongoOperations;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.math.BigDecimal;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by chenweida on 2016/1/27.
 | 
	
	
		
			
				|  | @ -31,61 +34,6 @@ public class ServiceMonitorService {
 | 
	
		
			
				|  |  |     private MongoOperations mongoOperations;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private DBCollection businessLog;
 | 
	
		
			
				|  |  | //    public Result mapReduce(String beginTime, String endTime) {
 | 
	
		
			
				|  |  | //        DBCollection businessLogDB = mongoOperations.getCollection(mongoOperations
 | 
	
		
			
				|  |  | //                .getCollectionName(BusinessLog.class));
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        String map = "function(){emit(this.breadcrumbId, this.fireTime);}";
 | 
	
		
			
				|  |  | //        String reduce = "function(key, values){ return values[0];}";
 | 
	
		
			
				|  |  | //        String out = reduceTableName;
 | 
	
		
			
				|  |  | //        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)))});
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        MapReduceOutput mapReduceOutput = businessLogDB.mapReduce(map,
 | 
	
		
			
				|  |  | //                reduce.toString(), out, queryObject);
 | 
	
		
			
				|  |  | //        DBCollection resultColl = mapReduceOutput.getOutputCollection();
 | 
	
		
			
				|  |  | //        DBCursor cursor = resultColl.find();
 | 
	
		
			
				|  |  | //        String result = "";
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        while (cursor.hasNext()) {
 | 
	
		
			
				|  |  | //            result += cursor.next();
 | 
	
		
			
				|  |  | //        }
 | 
	
		
			
				|  |  | //        return Result.success(result);
 | 
	
		
			
				|  |  | //    }
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //    public JSONObject aggregate(String beginTime, String endTime) {
 | 
	
		
			
				|  |  | //        DBCollection reduceDB = mongoOperations.getCollection(mongoOperations
 | 
	
		
			
				|  |  | //                .getCollectionName(ReduceResult.class));
 | 
	
		
			
				|  |  | //        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
 | 
	
		
			
				|  |  | //                new BasicDBObject[]{
 | 
	
		
			
				|  |  | //                        new BasicDBObject().append("value",
 | 
	
		
			
				|  |  | //                                new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
 | 
	
		
			
				|  |  | //                        new BasicDBObject().append("value",
 | 
	
		
			
				|  |  | //                                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("$minute", "$value"));
 | 
	
		
			
				|  |  | //        groupFields.put("pv", new BasicDBObject( "$sum", 1));
 | 
	
		
			
				|  |  | //        DBObject group = new BasicDBObject("$group", groupFields);
 | 
	
		
			
				|  |  | //
 | 
	
		
			
				|  |  | //        // build the $sort operation
 | 
	
		
			
				|  |  | //        DBObject sortFields =  new BasicDBObject( "_id", 1);
 | 
	
		
			
				|  |  | //        DBObject sort = new BasicDBObject("$sort", sortFields );
 | 
	
		
			
				|  |  | //        // run aggregation
 | 
	
		
			
				|  |  | //        AggregationOutput output = reduceDB.aggregate(match, group, sort);
 | 
	
		
			
				|  |  | //        String result = "";
 | 
	
		
			
				|  |  | //        for (DBObject dbObject : output.results()) {
 | 
	
		
			
				|  |  | //            result += dbObject.toString();
 | 
	
		
			
				|  |  | //        }
 | 
	
		
			
				|  |  | //        JSONObject jsonObject = new JSONObject(result);
 | 
	
		
			
				|  |  | //        return jsonObject;
 | 
	
		
			
				|  |  | //    }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public DBCollection getBusinessLog() {
 | 
	
		
			
				|  |  |         if (businessLog == null) {
 | 
	
	
		
			
				|  | @ -95,6 +43,7 @@ public class ServiceMonitorService {
 | 
	
		
			
				|  |  |         return businessLog;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public void bandwidth(String beginTime, String endTime, List<String> codeList) {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         DBObject match = getMatchFields(beginTime, endTime);
 | 
	
	
		
			
				|  | @ -103,7 +52,7 @@ public class ServiceMonitorService {
 | 
	
		
			
				|  |  |         DBObject serviceGroup = getServiceGroupFields();
 | 
	
		
			
				|  |  |         // run aggregation
 | 
	
		
			
				|  |  |         AggregationOutput flowOutput = getBusinessLog().aggregate(match, flowGroup, sort);
 | 
	
		
			
				|  |  |        //流程带宽
 | 
	
		
			
				|  |  |         //流程带宽
 | 
	
		
			
				|  |  |         Map<String, Integer> bandwidthMapF = new HashMap<>();
 | 
	
		
			
				|  |  |         for (DBObject dbObject : flowOutput.results()) {
 | 
	
		
			
				|  |  |             Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
 | 
	
	
		
			
				|  | @ -117,7 +66,7 @@ public class ServiceMonitorService {
 | 
	
		
			
				|  |  |                 bandwidthMapF.put(code, bodyLength);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |        //服务带宽
 | 
	
		
			
				|  |  |         //服务带宽
 | 
	
		
			
				|  |  |         // run aggregation
 | 
	
		
			
				|  |  |         AggregationOutput serviceOutput = getBusinessLog().aggregate(match, serviceGroup, sort);
 | 
	
		
			
				|  |  |         Map<String, Integer> bandwidthMapS = new HashMap<>();
 | 
	
	
		
			
				|  | @ -140,7 +89,7 @@ public class ServiceMonitorService {
 | 
	
		
			
				|  |  |                 Integer flowCalls = bandwidthMapF.get(code);
 | 
	
		
			
				|  |  |                 bandwidth = NumberUtil.divideBigDecimal(BigDecimal.valueOf(flowCalls), BigDecimal.valueOf(interval));
 | 
	
		
			
				|  |  |             } else if (!StringUtil.isEmpty(bandwidthMapS.get(code))) {
 | 
	
		
			
				|  |  |                 Integer serviceCalls = bandwidthMapS.get(code)/2;
 | 
	
		
			
				|  |  |                 Integer serviceCalls = bandwidthMapS.get(code) / 2;
 | 
	
		
			
				|  |  |                 bandwidth = NumberUtil.divideBigDecimal(BigDecimal.valueOf(serviceCalls), BigDecimal.valueOf(interval));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             saveServiceMetrics(code, "bandwidth", bandwidth.toString(), endTime);
 | 
	
	
		
			
				|  | @ -162,7 +111,7 @@ public class ServiceMonitorService {
 | 
	
		
			
				|  |  |             BasicDBObject id = (BasicDBObject) dbObject.get("_id");
 | 
	
		
			
				|  |  |             String code = StringUtil.toString(id.get("routeId"));
 | 
	
		
			
				|  |  |             Integer total = Integer.parseInt(StringUtil.toString(dbObject.get("total")));
 | 
	
		
			
				|  |  |             if (total == count/2) {
 | 
	
		
			
				|  |  |             if (total == count / 2) {
 | 
	
		
			
				|  |  |                 if (qpsMapF.containsKey(code)) {
 | 
	
		
			
				|  |  |                     Integer flowCalls = qpsMapF.get(code);
 | 
	
		
			
				|  |  |                     qpsMapF.put(code, ++flowCalls);
 | 
	
	
		
			
				|  | @ -195,7 +144,7 @@ public class ServiceMonitorService {
 | 
	
		
			
				|  |  |                 Integer flowCalls = qpsMapF.get(code);
 | 
	
		
			
				|  |  |                 qps = NumberUtil.divideBigDecimal(BigDecimal.valueOf(flowCalls), BigDecimal.valueOf(interval));
 | 
	
		
			
				|  |  |             } else if (!StringUtil.isEmpty(qpsMapS.get(code))) {
 | 
	
		
			
				|  |  |                 Integer serviceCalls = qpsMapS.get(code)/2;
 | 
	
		
			
				|  |  |                 Integer serviceCalls = qpsMapS.get(code) / 2;
 | 
	
		
			
				|  |  |                 qps = NumberUtil.divideBigDecimal(BigDecimal.valueOf(serviceCalls), BigDecimal.valueOf(interval));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             saveServiceMetrics(code, "qps", qps.toString(), endTime);
 | 
	
	
		
			
				|  | @ -215,7 +164,7 @@ public class ServiceMonitorService {
 | 
	
		
			
				|  |  |             Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
 | 
	
		
			
				|  |  |             BasicDBObject id = (BasicDBObject) dbObject.get("_id");
 | 
	
		
			
				|  |  |             String code = StringUtil.toString(id.get("routeId"));
 | 
	
		
			
				|  |  |             if (total == count/2) {
 | 
	
		
			
				|  |  |             if (total == count / 2) {
 | 
	
		
			
				|  |  |                 if (usageMapSuccessF.containsKey(code)) {
 | 
	
		
			
				|  |  |                     Integer countTemp = usageMapSuccessF.get(code);
 | 
	
		
			
				|  |  |                     usageMapSuccessF.put(code, ++countTemp);
 | 
	
	
		
			
				|  | @ -268,21 +217,21 @@ public class ServiceMonitorService {
 | 
	
		
			
				|  |  |             JSONObject result = new JSONObject();
 | 
	
		
			
				|  |  |             Integer successCountF = isNull(usageMapSuccessF.get(code));
 | 
	
		
			
				|  |  |             Integer failureCountF = isNull(usageMapFailF.get(code));
 | 
	
		
			
				|  |  |             Integer successCountS= isNull(usageMapSuccessS.get(code));
 | 
	
		
			
				|  |  |             Integer successCountS = isNull(usageMapSuccessS.get(code));
 | 
	
		
			
				|  |  |             Integer failureCountS = isNull(usageMapFailS.get(code));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             if (successCountF > 0 || failureCountF > 0) {
 | 
	
		
			
				|  |  |                 result.put("totalCount", successCountF + failureCountF);
 | 
	
		
			
				|  |  |                 result.put("successCount", successCountF);
 | 
	
		
			
				|  |  |                 result.put("failureCount",failureCountF);
 | 
	
		
			
				|  |  |                 result.put("failureCount", failureCountF);
 | 
	
		
			
				|  |  |             } else if (successCountS > 0 || failureCountS > 0) {
 | 
	
		
			
				|  |  |                 result.put("totalCount", successCountS + failureCountS);
 | 
	
		
			
				|  |  |                 result.put("successCount", successCountS);
 | 
	
		
			
				|  |  |                 result.put("failureCount",failureCountS);
 | 
	
		
			
				|  |  |                 result.put("failureCount", failureCountS);
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 result.put("totalCount", 0);
 | 
	
		
			
				|  |  |                 result.put("successCount", 0);
 | 
	
		
			
				|  |  |                 result.put("failureCount",0);
 | 
	
		
			
				|  |  |                 result.put("failureCount", 0);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             ObjectMapper objectMapper = new ObjectMapper();
 | 
	
		
			
				|  |  |             saveServiceMetrics(code, "usage", objectMapper.writeValueAsString(result.toString()), endTime);
 | 
	
	
		
			
				|  | @ -363,46 +312,46 @@ public class ServiceMonitorService {
 | 
	
		
			
				|  |  |     public DBObject getServiceGroupFields() {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // Now the $group operation
 | 
	
		
			
				|  |  |         DBObject groupFields = new BasicDBObject( "_id",
 | 
	
		
			
				|  |  |         DBObject groupFields = new BasicDBObject("_id",
 | 
	
		
			
				|  |  |                 new BasicDBObject("id", "$_id")
 | 
	
		
			
				|  |  |                         .append("code", "$code"));
 | 
	
		
			
				|  |  |         groupFields.put("count", new BasicDBObject( "$sum", 1));
 | 
	
		
			
				|  |  |         groupFields.put("bodyLength", new BasicDBObject( "$sum", "$bodyLength"));
 | 
	
		
			
				|  |  |         groupFields.put("total", new BasicDBObject( "$first", "$totalServers"));
 | 
	
		
			
				|  |  |         groupFields.put("count", new BasicDBObject("$sum", 1));
 | 
	
		
			
				|  |  |         groupFields.put("bodyLength", new BasicDBObject("$sum", "$bodyLength"));
 | 
	
		
			
				|  |  |         groupFields.put("total", new BasicDBObject("$first", "$totalServers"));
 | 
	
		
			
				|  |  |         return new BasicDBObject("$group", groupFields);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public DBObject getFlowGroupFields() {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // Now the $group operation
 | 
	
		
			
				|  |  |         DBObject groupFields = new BasicDBObject( "_id",
 | 
	
		
			
				|  |  |         DBObject groupFields = new BasicDBObject("_id",
 | 
	
		
			
				|  |  |                 new BasicDBObject("breadcrumbId", "$breadcrumbId")
 | 
	
		
			
				|  |  |                         .append("routeId", "$routeId"));
 | 
	
		
			
				|  |  |         groupFields.put("count", new BasicDBObject( "$sum", 1));
 | 
	
		
			
				|  |  |         groupFields.put("bodyLength", new BasicDBObject( "$sum", "$bodyLength"));
 | 
	
		
			
				|  |  |         groupFields.put("total", new BasicDBObject( "$first", "$totalServers"));
 | 
	
		
			
				|  |  |         groupFields.put("beginTime", new BasicDBObject( "$first", "$fireTimeSource"));
 | 
	
		
			
				|  |  |         groupFields.put("count", new BasicDBObject("$sum", 1));
 | 
	
		
			
				|  |  |         groupFields.put("bodyLength", new BasicDBObject("$sum", "$bodyLength"));
 | 
	
		
			
				|  |  |         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);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public DBObject getSortFields() {
 | 
	
		
			
				|  |  |         DBObject sortFields =  new BasicDBObject( "_id", 1);
 | 
	
		
			
				|  |  |         return new BasicDBObject("$sort", sortFields );
 | 
	
		
			
				|  |  |         DBObject sortFields = new BasicDBObject("_id", 1);
 | 
	
		
			
				|  |  |         return new BasicDBObject("$sort", sortFields);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     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;
 | 
	
		
			
				|  |  |         long interval = (to.getTime() - from.getTime()) / 1000;
 | 
	
		
			
				|  |  |         return interval;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public long getIntervalExact(String beginTime, String endTime) {
 | 
	
		
			
				|  |  |         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;
 | 
	
		
			
				|  |  |         long interval = (to.getTime() - from.getTime()) / 1000;
 | 
	
		
			
				|  |  |         return interval;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 |