|  | @ -0,0 +1,83 @@
 | 
	
		
			
				|  |  | package com.yihu.hos.services;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.mongodb.*;
 | 
	
		
			
				|  |  | import com.yihu.hos.common.configuration.GatewayConfiguration;
 | 
	
		
			
				|  |  | import com.yihu.hos.core.constants.ExceptionConstant;
 | 
	
		
			
				|  |  | import com.yihu.hos.core.datatype.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.hos.core.exception.ESBException;
 | 
	
		
			
				|  |  | import com.yihu.hos.core.http.HttpClientUtil;
 | 
	
		
			
				|  |  | import com.yihu.hos.models.BusinessLog;
 | 
	
		
			
				|  |  | import com.yihu.hos.models.GatewayRequestResult;
 | 
	
		
			
				|  |  | import com.yihu.hos.models.QPS;
 | 
	
		
			
				|  |  | import com.yihu.hos.web.framework.model.Result;
 | 
	
		
			
				|  |  | import net.sf.json.JSONObject;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.data.mongodb.core.MongoOperations;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.util.StringUtils;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by chenweida on 2016/1/27.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service("ServerService")
 | 
	
		
			
				|  |  | public class ServerService {
 | 
	
		
			
				|  |  |     public static final String BEAN_ID = "ServerService";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private MongoOperations mongoOperations;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     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 = "qps";
 | 
	
		
			
				|  |  |         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 Result aggregate(String beginTime, String endTime) {
 | 
	
		
			
				|  |  |         DBCollection qpsDB = mongoOperations.getCollection(mongoOperations
 | 
	
		
			
				|  |  |                 .getCollectionName(QPS.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 = qpsDB.aggregate(match, group, sort);
 | 
	
		
			
				|  |  |         String result = "";
 | 
	
		
			
				|  |  |         for (DBObject dbObject : output.results()) {
 | 
	
		
			
				|  |  |             result += dbObject.toString();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return Result.success(result);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |