|
|
package com.monitor.influxdb.mapper.impl;
|
|
|
|
|
|
import com.monitor.influxdb.InfluxDBQuery;
|
|
|
import com.monitor.influxdb.contants.InfluxDBContants;
|
|
|
import com.monitor.influxdb.mapper.ServiceAccessMapper;
|
|
|
import com.monitor.model.request.NewJavaApiInfoReq;
|
|
|
import com.monitor.model.response.NewJavaApiDetailInfoRep;
|
|
|
import com.monitor.model.response.NewJavaApiInfoRep;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.influxdb.dto.QueryResult;
|
|
|
import org.joda.time.DateTime;
|
|
|
import org.joda.time.DateTimeZone;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/10/20.
|
|
|
*/
|
|
|
@Component
|
|
|
public class ServiceAccessMapperImpl extends InfluxDBQuery implements ServiceAccessMapper {
|
|
|
Logger log = LoggerFactory.getLogger(ServiceAccessMapperImpl.class);
|
|
|
|
|
|
|
|
|
//根据context获取总响应次数和平均耗时
|
|
|
@Override
|
|
|
public Map<String,NewJavaApiInfoRep> getBaseDataByContext(String context,String startDateStr,String endDateStr) {
|
|
|
|
|
|
Map<String,NewJavaApiInfoRep> map = new HashMap<>();
|
|
|
String sql = String.format("select count(cost),mean(cost) from service_access where context = '%s' and time > '%s' and time < '%s' group by hostAddress",context,startDateStr,endDateStr);
|
|
|
// String sql = "select count(cost),mean(cost) from service_access group by hostAddress";
|
|
|
|
|
|
map.putAll(getDataByContext(InfluxDBContants.AWS,sql));
|
|
|
map.putAll(getDataByContext(InfluxDBContants.Q_CLOUD,sql));
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
private Map<String,NewJavaApiInfoRep> getDataByContext(String source,String sql) {
|
|
|
Map<String,NewJavaApiInfoRep> map = new HashMap<>();
|
|
|
QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT);
|
|
|
QueryResult.Result rel = queryResult.getResults().get(0);
|
|
|
List<QueryResult.Series> listSeries = rel.getSeries();
|
|
|
if(listSeries == null)
|
|
|
return map;
|
|
|
for(QueryResult.Series series : listSeries){
|
|
|
String hostAddress = series.getTags().get("hostAddress");
|
|
|
Double count = (Double)series.getValues().get(0).get(1);
|
|
|
Double mean = (Double)series.getValues().get(0).get(2);
|
|
|
NewJavaApiInfoRep newJavaApiInfoRep = new NewJavaApiInfoRep();
|
|
|
newJavaApiInfoRep.setTotalCount(count.intValue());
|
|
|
newJavaApiInfoRep.setAvgCost(mean.intValue());
|
|
|
map.put(hostAddress,newJavaApiInfoRep);
|
|
|
}
|
|
|
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public Map<String,List<String>> getTimeoutInfo(String context, String startDateStr, String endDateStr) {
|
|
|
Map<String,List<String>> map = new HashMap();
|
|
|
String sql = String.format("select ip,stack from service_access where context='%s' and cost > 200 and time > '%s' and time < '%s'",context,startDateStr,endDateStr);
|
|
|
// String sql = "select ip,stack from service_access where context='gateway' and cost > 200";
|
|
|
map.putAll(getTimeoutInfo(InfluxDBContants.AWS,sql));
|
|
|
map.putAll(getTimeoutInfo(InfluxDBContants.Q_CLOUD,sql));
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
private Map<String,List<String>> getTimeoutInfo(String source,String sql) {
|
|
|
Map<String,List<String>> map = new HashMap();
|
|
|
QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT);
|
|
|
QueryResult.Result rel = queryResult.getResults().get(0);
|
|
|
List<QueryResult.Series> listSeries = rel.getSeries();
|
|
|
if(listSeries == null)
|
|
|
return map;
|
|
|
QueryResult.Series series = listSeries.get(0);
|
|
|
List<List<Object>> values = series.getValues();
|
|
|
|
|
|
for (List<Object> objects : values){
|
|
|
if(objects.get(1) == null || objects.get(2) == null)
|
|
|
continue;
|
|
|
String ip = (String)objects.get(1);
|
|
|
String stack = (String)objects.get(2);
|
|
|
List<String> stackList = map.get(ip);
|
|
|
if(stackList == null){
|
|
|
stackList = new ArrayList<>();
|
|
|
stackList.add(stack);
|
|
|
map.put(ip,stackList);
|
|
|
}else{
|
|
|
stackList.add(stack);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public Map<String,List> getGraphInfo(String context){
|
|
|
Map<String,List> map = new HashMap();
|
|
|
String sql = String.format("SELECT mean(cost) FROM service_access WHERE time > now() - 10m and context = '%s' GROUP BY hostAddress,time(1m) fill(null)",context);
|
|
|
// String sql = "select mean(cost) from service_access where time > '2016-10-20 02:25:00' and time < '2016-10-20 02:51:00' and context = 'gateway' GROUP BY hostAddress,time(1m) fill(null)";
|
|
|
map.putAll(getGraphInfo(InfluxDBContants.AWS,sql));
|
|
|
map.putAll(getGraphInfo(InfluxDBContants.Q_CLOUD,sql));
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
|
|
|
private Map<String,List> getGraphInfo(String source,String sql) {
|
|
|
Map<String,List> resultMap = new HashMap<>();
|
|
|
|
|
|
QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT);
|
|
|
QueryResult.Result rel = queryResult.getResults().get(0);
|
|
|
List<QueryResult.Series> listSeries = rel.getSeries();
|
|
|
if(listSeries == null)
|
|
|
return resultMap;
|
|
|
|
|
|
for(QueryResult.Series series : listSeries){
|
|
|
String hostAddress = series.getTags().get("hostAddress");
|
|
|
List valuesList = (List)series.getValues();
|
|
|
|
|
|
List<String> timeList = new ArrayList<>();
|
|
|
List<Integer> meanList = new ArrayList<>();
|
|
|
|
|
|
List list = new ArrayList<>();
|
|
|
for(int i=0;i<valuesList.size();i++){
|
|
|
List avgValueList = (List)valuesList.get(i);
|
|
|
String time = (String)avgValueList.get(0);
|
|
|
DateTime one = DateTime.parse(time);
|
|
|
one = one.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Shanghai")));
|
|
|
String formatTime = one.toString("HH:mm");
|
|
|
timeList.add(formatTime);
|
|
|
if(avgValueList.get(1) == null){
|
|
|
meanList.add(0);
|
|
|
}else{
|
|
|
meanList.add(((Double)avgValueList.get(1)).intValue());
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
list.add(timeList);
|
|
|
list.add(meanList);
|
|
|
|
|
|
resultMap.put(hostAddress,list);
|
|
|
}
|
|
|
return resultMap;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public List<NewJavaApiDetailInfoRep> getDataByContextAndIP(NewJavaApiInfoReq req) {
|
|
|
List<NewJavaApiDetailInfoRep> list = new ArrayList<>();
|
|
|
String source = "";
|
|
|
String sql = "";
|
|
|
if(req.getCloudType()==2){
|
|
|
source = InfluxDBContants.Q_CLOUD;
|
|
|
}else{
|
|
|
source = InfluxDBContants.AWS;
|
|
|
}
|
|
|
|
|
|
if(StringUtils.isNotBlank(req.getIp())){
|
|
|
sql = String.format("select count(cost),mean(cost) from service_access where context = '%s' and hostAddress = '%s' group by event",req.getServiceName(),req.getIp());
|
|
|
}else{
|
|
|
sql = String.format("select count(cost),mean(cost) from service_access where context = '%s' group by event",req.getServiceName());
|
|
|
}
|
|
|
|
|
|
QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT);
|
|
|
QueryResult.Result rel = queryResult.getResults().get(0);
|
|
|
List<QueryResult.Series> listSeries = rel.getSeries();
|
|
|
if(listSeries == null)
|
|
|
return list;
|
|
|
for(QueryResult.Series series : listSeries){
|
|
|
NewJavaApiDetailInfoRep rep = new NewJavaApiDetailInfoRep();
|
|
|
String apiName = series.getTags().get("event");
|
|
|
Double count = (Double)series.getValues().get(0).get(1);
|
|
|
Double mean = (Double)series.getValues().get(0).get(2);
|
|
|
rep.setApiName(apiName);
|
|
|
rep.setTotalCount(count.intValue());
|
|
|
rep.setAvgCost(mean.intValue());
|
|
|
list.add(rep);
|
|
|
}
|
|
|
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public Map<String,List<String>> getTimeoutInfoByContextAndIp(NewJavaApiInfoReq req) {
|
|
|
String source = "";
|
|
|
String sql = "";
|
|
|
if(req.getCloudType()==2){
|
|
|
source = InfluxDBContants.Q_CLOUD;
|
|
|
}else{
|
|
|
source = InfluxDBContants.AWS;
|
|
|
}
|
|
|
|
|
|
if(StringUtils.isNotBlank(req.getIp())){
|
|
|
sql = String.format("select event,stack from service_access where context='%s' and cost > 200 and ip = '%s'",req.getServiceName(),req.getIp());
|
|
|
}else{
|
|
|
sql = String.format("select event,stack from service_access where context='%s' and cost > 200 ",req.getServiceName());
|
|
|
}
|
|
|
|
|
|
|
|
|
Map<String,List<String>> map = new HashMap();
|
|
|
QueryResult queryResult = query(source, sql, InfluxDBContants.YOHO_EVENT);
|
|
|
QueryResult.Result rel = queryResult.getResults().get(0);
|
|
|
List<QueryResult.Series> listSeries = rel.getSeries();
|
|
|
if(listSeries == null)
|
|
|
return map;
|
|
|
QueryResult.Series series = listSeries.get(0);
|
|
|
List<List<Object>> values = series.getValues();
|
|
|
|
|
|
for (List<Object> objects : values){
|
|
|
if(objects.get(1) == null || objects.get(2) == null)
|
|
|
continue;
|
|
|
String event = (String)objects.get(1);
|
|
|
String stack = (String)objects.get(2);
|
|
|
List<String> stackList = map.get(event);
|
|
|
if(stackList == null){
|
|
|
stackList = new ArrayList<>();
|
|
|
stackList.add(stack);
|
|
|
map.put(event,stackList);
|
|
|
}else{
|
|
|
stackList.add(stack);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|