Selaa lähdekoodia

新增服务监控页面

zhenglingfeng 8 vuotta sitten
vanhempi
commit
c959fed407

+ 7 - 22
src/main/java/com/yihu/hos/monitor/controller/MonitorController.java

@ -1,7 +1,6 @@
package com.yihu.hos.monitor.controller;
import com.yihu.hos.monitor.services.MonitorService;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.monitor.service.ServerMonitorService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONArray;
@ -19,18 +18,18 @@ import javax.annotation.Resource;
 * @vsrsion 1.0
 * Created at 2016/10/26.
 */
@Controller("MonitorController")
@RequestMapping("/monitor/")
public class MonitorController {
@Controller("ServerMonitorController")
@RequestMapping("/monitor/server")
public class ServerMonitorController {
    @Resource(name = MonitorService.BEAN_ID)
    private MonitorService monitorService;
    @Resource(name = ServerMonitorService.BEAN_ID)
    private ServerMonitorService monitorService;
    //跳转到列表页
    @RequestMapping("/server/initial")
    @RequestMapping("/initial")
    public String initial(Model model) {
        model.addAttribute("contentPage", "/server/sEnvManage");
        return "partView";
@ -56,18 +55,4 @@ public class MonitorController {
        }
        return "partView";
    }
    @RequestMapping(value = "/server/usage", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取服务器使用率", produces = "application/json", notes = "获取服务器硬件使用率")
    public Result usage(
            @ApiParam(name = "type", value = "类型", required = true)
            @RequestParam(value = "type") String type,
            @ApiParam(name = "beginTime", value = "开始时间", required = true)
            @RequestParam(value = "beginTime") String beginTime,
            @ApiParam(name = "endTime", value = "结束时间", required = true)
            @RequestParam(value = "endTime") String endTime) {
        return monitorService.getMonitorInfo(type, beginTime, endTime);
    }
}

+ 98 - 0
src/main/java/com/yihu/hos/monitor/controller/ServiceMonitorController.java

@ -0,0 +1,98 @@
package com.yihu.hos.monitor.controller;
import com.yihu.hos.monitor.service.ServiceMonitorService;
import com.yihu.hos.web.framework.model.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/10/26.
 */
@Controller("ServiceMonitorController")
@RequestMapping("/monitor/service")
public class ServiceMonitorController {
    @Resource(name = ServiceMonitorService.BEAN_ID)
    private ServiceMonitorService monitorService;
    //跳转到列表页
    @RequestMapping("/initial")
    public String serviceInitial(Model model) {
        model.addAttribute("contentPage", "/service/sEnvManage");
        return "partView";
    }
//    @RequestMapping(value = "/service/bandwidth", method = RequestMethod.GET)
//    @ResponseBody
//    @ApiOperation(value = "服务指标获取", produces = "application/json", notes = "服务指标获取")
//    public String serviceMetrics(
//            Model model,
//            @ApiParam(name = "beginTime", value = "开始时间", required = true)
//            @RequestParam(value = "beginTime") String beginTime,
//            @ApiParam(name = "endTime", value = "结束时间", required = true)
//            @RequestParam(value = "endTime") String endTime) {
//        try {
//            JSONArray array = monitorService.getMonitorInfoTest(type, beginTime, endTime);
//            model.addAttribute("monitorModel", array);
//            model.addAttribute("contentPage", "server/monitor");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "partView";
//    }
    @RequestMapping(value = "/bandwidth", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取带宽", produces = "application/json", notes = "获取带宽")
    public Result bandwidth(
            @ApiParam(name = "beginTime", value = "开始时间", required = true)
            @RequestParam(value = "beginTime") String beginTime,
            @ApiParam(name = "endTime", value = "结束时间", required = true)
            @RequestParam(value = "endTime") String endTime) {
        return monitorService.bandwidth(beginTime, endTime);
    }
    @RequestMapping(value = "/qps", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取吞吐量", produces = "application/json", notes = "获取吞吐量")
    public Result qps(
            @ApiParam(name = "beginTime", value = "开始时间", required = true)
            @RequestParam(value = "beginTime") String beginTime,
            @ApiParam(name = "endTime", value = "结束时间", required = true)
            @RequestParam(value = "endTime") String endTime) {
        return monitorService.qps(beginTime, endTime);
    }
    @RequestMapping(value = "/usage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取使用率", produces = "application/json", notes = "获取使用率")
    public Result usage(
            @ApiParam(name = "beginTime", value = "开始时间", required = true)
            @RequestParam(value = "beginTime") String beginTime,
            @ApiParam(name = "endTime", value = "结束时间", required = true)
            @RequestParam(value = "endTime") String endTime) {
        return monitorService.usage(beginTime, endTime);
    }
    @RequestMapping(value = "/delay", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取延时", produces = "application/json", notes = "获取延时")
    public Result delay(
            @ApiParam(name = "beginTime", value = "开始时间", required = true)
            @RequestParam(value = "beginTime") String beginTime,
            @ApiParam(name = "endTime", value = "结束时间", required = true)
            @RequestParam(value = "endTime") String endTime) {
        return monitorService.delay(beginTime, endTime);
    }
}

+ 129 - 0
src/main/java/com/yihu/hos/monitor/model/BusinessLog.java

@ -0,0 +1,129 @@
package com.yihu.hos.monitor.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
/**
 * @created Airhead 2016/8/8.
 */
@Document(collection = "businessLog")
public class BusinessLog {
    @Id
    private String id;
    @Indexed
    private String exchangeId;
    private String correlationId;
    private String transactionKey;
    @Indexed
    private String routeId;
    @Indexed
    private String breadcrumbId;
    private Integer totalServers;
    private String camelContextId;
    private String body;
    private Integer bodyLength;
    private String fireTimeSource;
    //@Indexed(name = "fireTime_1", expireAfterSeconds = 30)
    @Indexed
    private Date fireTime;  //后期建议转成TTL
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getExchangeId() {
        return exchangeId;
    }
    public void setExchangeId(String exchangeId) {
        this.exchangeId = exchangeId;
    }
    public String getCorrelationId() {
        return correlationId;
    }
    public void setCorrelationId(String correlationId) {
        this.correlationId = correlationId;
    }
    public String getTransactionKey() {
        return transactionKey;
    }
    public void setTransactionKey(String transactionKey) {
        this.transactionKey = transactionKey;
    }
    public String getRouteId() {
        return routeId;
    }
    public void setRouteId(String routeId) {
        this.routeId = routeId;
    }
    public String getBreadcrumbId() {
        return breadcrumbId;
    }
    public void setBreadcrumbId(String breadcrumbId) {
        this.breadcrumbId = breadcrumbId;
    }
    public String getCamelContextId() {
        return camelContextId;
    }
    public void setCamelContextId(String camelContextId) {
        this.camelContextId = camelContextId;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public Date getFireTime() {
        return fireTime;
    }
    public void setFireTime(Date fireTime) {
        this.fireTime = fireTime;
    }
    public Integer getBodyLength() {
        return bodyLength;
    }
    public void setBodyLength(Integer bodyLength) {
        this.bodyLength = bodyLength;
    }
    public String getFireTimeSource() {
        return fireTimeSource;
    }
    public void setFireTimeSource(String fireTimeSource) {
        this.fireTimeSource = fireTimeSource;
    }
    public Integer getTotalServers() {
        return totalServers;
    }
    public void setTotalServers(Integer totalServers) {
        this.totalServers = totalServers;
    }
}

+ 306 - 0
src/main/java/com/yihu/hos/monitor/service/ServerMonitorService.java

@ -0,0 +1,306 @@
package com.yihu.hos.monitor.service;
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.monitor.model.BusinessLog;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.Result;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Date;
/**
 * Created by chenweida on 2016/1/27.
 */
@Service("ServerMonitorService")
public class ServerMonitorService {
    public static final String BEAN_ID = "ServerMonitorService";
    public static final String envHealth = "envHealth";
    @Autowired
    private MongoOperations mongoOperations;
    @Autowired
    private Mongo mongo;
//    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 Result getMonitorInfo(String table,String beginTime, String endTime) {
        mongoOperations = new MongoTemplate(mongo, envHealth);
        DBCollection envCollection = mongoOperations.getCollection(table);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
        JSONArray result = new JSONArray();
        DBCursor cursor = envCollection.find(queryObject);
        while(cursor.hasNext()) {
            DBObject dbObject = cursor.next();
            dbObject.removeField("_id");
            result.put(dbObject);
            System.out.println(dbObject.toString());
        }
        ActionResult actionResult = new ActionResult();
        actionResult.setData(result);
        return actionResult;
    }
    public JSONArray getMonitorInfoTest(String table,String beginTime, String endTime) {
        mongoOperations = new MongoTemplate(mongo, envHealth);
        DBCollection envCollection = mongoOperations.getCollection(table);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
        JSONArray result = new JSONArray();
        DBCursor cursor = envCollection.find(queryObject);
        while(cursor.hasNext()) {
            DBObject dbObject = cursor.next();
            dbObject.removeField("_id");
            result.put(dbObject);
            System.out.println(dbObject.toString());
        }
        return result;
    }
    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)))});
        DBObject match = new BasicDBObject("$match", queryObject);
        // Now the $group operation
        DBObject groupFields = new BasicDBObject( "_id", "$breadcrumbId");
        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 );
        // run aggregation
        AggregationOutput output = businessLog.aggregate(match, group, sort);
        Integer calls = 0;
        long interval = 0;
        for (DBObject dbObject : output.results()) {
            Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
            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;
            }
        }
        JSONObject result = new JSONObject();
        result.put("bandwidth", NumberUtil.divideBigDecimal(BigDecimal.valueOf(calls), BigDecimal.valueOf(interval)));
        return Result.success(result.toString());
    }
    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", "$breadcrumbId");
        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 );
        // run aggregation
        AggregationOutput output = businessLog.aggregate(match, group, sort);
        Integer calls = 0;
        long interval = 0;
        for (DBObject dbObject : output.results()) {
            Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
            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;
                }
            }
        }
        JSONObject result = new JSONObject();
        result.put("qps", NumberUtil.divideBigDecimal(BigDecimal.valueOf(calls), BigDecimal.valueOf(interval)));
        return Result.success(result.toString());
    }
    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", "$breadcrumbId");
        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 );
        // run aggregation
        AggregationOutput output = businessLog.aggregate(match, group, sort);
        Integer successCount = 0;
        Integer failureCount = 0;
        for (DBObject dbObject : output.results()) {
            Integer total = Integer.parseInt(StringUtil.toString(dbObject.get("total")));
            Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
            if (total == count) {
                successCount++;
            } else {
                failureCount++;
            }
        }
        JSONObject result = new JSONObject();
        result.put("totalCount", successCount + failureCount);
        result.put("successCount", successCount);
        result.put("failureCount", failureCount);
        return Result.success(result.toString());
    }
    public Result delay(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", "$breadcrumbId");
        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 );
        // run aggregation
        AggregationOutput output = businessLog.aggregate(match, group, sort);
        Integer calls = 0;
        long interval = 0;
        for (DBObject dbObject : output.results()) {
            Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
            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;
            }
        }
        JSONObject result = new JSONObject();
        result.put("delay", NumberUtil.divideBigDecimal(BigDecimal.valueOf(interval), BigDecimal.valueOf(calls)));
        return Result.success(result.toString());
    }
}

+ 306 - 0
src/main/java/com/yihu/hos/monitor/service/ServiceMonitorService.java

@ -0,0 +1,306 @@
package com.yihu.hos.monitor.service;
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.monitor.model.BusinessLog;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.Result;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Date;
/**
 * Created by chenweida on 2016/1/27.
 */
@Service("ServiceMonitorService")
public class ServiceMonitorService {
    public static final String BEAN_ID = "ServiceMonitorService";
    public static final String envHealth = "envHealth";
    @Autowired
    private MongoOperations mongoOperations;
    @Autowired
    private Mongo mongo;
//    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 Result getMonitorInfo(String table,String beginTime, String endTime) {
        mongoOperations = new MongoTemplate(mongo, envHealth);
        DBCollection envCollection = mongoOperations.getCollection(table);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
        JSONArray result = new JSONArray();
        DBCursor cursor = envCollection.find(queryObject);
        while(cursor.hasNext()) {
            DBObject dbObject = cursor.next();
            dbObject.removeField("_id");
            result.put(dbObject);
            System.out.println(dbObject.toString());
        }
        ActionResult actionResult = new ActionResult();
        actionResult.setData(result);
        return actionResult;
    }
    public JSONArray getMonitorInfoTest(String table,String beginTime, String endTime) {
        mongoOperations = new MongoTemplate(mongo, envHealth);
        DBCollection envCollection = mongoOperations.getCollection(table);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
        JSONArray result = new JSONArray();
        DBCursor cursor = envCollection.find(queryObject);
        while(cursor.hasNext()) {
            DBObject dbObject = cursor.next();
            dbObject.removeField("_id");
            result.put(dbObject);
            System.out.println(dbObject.toString());
        }
        return result;
    }
    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)))});
        DBObject match = new BasicDBObject("$match", queryObject);
        // Now the $group operation
        DBObject groupFields = new BasicDBObject( "_id", "$breadcrumbId");
        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 );
        // run aggregation
        AggregationOutput output = businessLog.aggregate(match, group, sort);
        Integer calls = 0;
        long interval = 0;
        for (DBObject dbObject : output.results()) {
            Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
            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;
            }
        }
        JSONObject result = new JSONObject();
        result.put("bandwidth", NumberUtil.divideBigDecimal(BigDecimal.valueOf(calls), BigDecimal.valueOf(interval)));
        return Result.success(result.toString());
    }
    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", "$breadcrumbId");
        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 );
        // run aggregation
        AggregationOutput output = businessLog.aggregate(match, group, sort);
        Integer calls = 0;
        long interval = 0;
        for (DBObject dbObject : output.results()) {
            Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
            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;
                }
            }
        }
        JSONObject result = new JSONObject();
        result.put("qps", NumberUtil.divideBigDecimal(BigDecimal.valueOf(calls), BigDecimal.valueOf(interval)));
        return Result.success(result.toString());
    }
    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", "$breadcrumbId");
        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 );
        // run aggregation
        AggregationOutput output = businessLog.aggregate(match, group, sort);
        Integer successCount = 0;
        Integer failureCount = 0;
        for (DBObject dbObject : output.results()) {
            Integer total = Integer.parseInt(StringUtil.toString(dbObject.get("total")));
            Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
            if (total == count) {
                successCount++;
            } else {
                failureCount++;
            }
        }
        JSONObject result = new JSONObject();
        result.put("totalCount", successCount + failureCount);
        result.put("successCount", successCount);
        result.put("failureCount", failureCount);
        return Result.success(result.toString());
    }
    public Result delay(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", "$breadcrumbId");
        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 );
        // run aggregation
        AggregationOutput output = businessLog.aggregate(match, group, sort);
        Integer calls = 0;
        long interval = 0;
        for (DBObject dbObject : output.results()) {
            Integer count = Integer.parseInt(StringUtil.toString(dbObject.get("count")));
            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;
            }
        }
        JSONObject result = new JSONObject();
        result.put("delay", NumberUtil.divideBigDecimal(BigDecimal.valueOf(interval), BigDecimal.valueOf(calls)));
        return Result.success(result.toString());
    }
}

+ 0 - 134
src/main/java/com/yihu/hos/monitor/services/MonitorService.java

@ -1,134 +0,0 @@
package com.yihu.hos.monitor.services;
import com.mongodb.*;
import com.yihu.hos.core.datatype.DateUtil;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.Result;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
/**
 * Created by chenweida on 2016/1/27.
 */
@Service("MonitorService")
public class MonitorService {
    public static final String BEAN_ID = "MonitorService";
    public static final String envHealth = "envHealth";
    @Autowired
    private MongoOperations mongoOperations;
    @Autowired
    private Mongo mongo;
//    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 Result getMonitorInfo(String table,String beginTime, String endTime) {
        mongoOperations = new MongoTemplate(mongo, envHealth);
        DBCollection envCollection = mongoOperations.getCollection(table);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
        JSONArray result = new JSONArray();
        DBCursor cursor = envCollection.find(queryObject);
        while(cursor.hasNext()) {
            DBObject dbObject = cursor.next();
            dbObject.removeField("_id");
            result.put(dbObject);
            System.out.println(dbObject.toString());
        }
        ActionResult actionResult = new ActionResult();
        actionResult.setData(result);
        return actionResult;
    }
    public JSONArray getMonitorInfoTest(String table,String beginTime, String endTime) {
        mongoOperations = new MongoTemplate(mongo, envHealth);
        DBCollection envCollection = mongoOperations.getCollection(table);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.GTE, DateUtil.toTimestamp(beginTime))),
                        new BasicDBObject().append("create_time",
                                new BasicDBObject().append(QueryOperators.LT, DateUtil.toTimestamp(endTime)))});
        JSONArray result = new JSONArray();
        DBCursor cursor = envCollection.find(queryObject);
        while(cursor.hasNext()) {
            DBObject dbObject = cursor.next();
            dbObject.removeField("_id");
            result.put(dbObject);
            System.out.println(dbObject.toString());
        }
        return result;
    }
}

+ 2 - 1
src/main/webapp/WEB-INF/ehr/jsp/common/indexJs.jsp

@ -98,7 +98,8 @@
                {id: 98, pid: 9, text: '系统参数', url: '${contextRoot}/param/initial'},
                {id: 99, pid: 9, text: '<spring:message code="title.app.manage"/>', url: '${contextRoot}/app/initial'},
                {id: 100, pid: 9, text: '<spring:message code="title.flow.manage"/>', url: '${contextRoot}/flow/initial'},
                {id: 101, pid: 9, text: '系统监控', url: '${contextRoot}/monitor/server/initial'}
                {id: 101, pid: 9, text: '系统监控', url: '${contextRoot}/monitor/server/initial'},
                {id: 102, pid: 9, text: '服务监控', url: '${contextRoot}/monitor/service/initial'}
            ];
            me.menuTree = $('#ulTree').ligerTree({
                data: menu,