|
@ -0,0 +1,204 @@
|
|
|
|
package com.yihu.admin.server.log;
|
|
|
|
|
|
|
|
import com.yihu.admin.server.properties.AdminServerProperties;
|
|
|
|
import com.yihu.admin.server.util.DateUtil;
|
|
|
|
import com.yihu.base.es.config.ElastricSearchHelper;
|
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by chenweida on 2018/5/24 0024.1
|
|
|
|
*/
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("admin_log")
|
|
|
|
public class LogEndPoint {
|
|
|
|
@Autowired
|
|
|
|
private ElastricSearchHelper elastricSearchHelper;
|
|
|
|
@Autowired
|
|
|
|
private AdminServerProperties adminServerProperties;
|
|
|
|
|
|
|
|
@GetMapping("list")
|
|
|
|
public List<LogModel> list(
|
|
|
|
@RequestParam(value = "traceId", required = false) String traceId, // GET POST DELETE PUT
|
|
|
|
@RequestParam(value = "time", required = false) Integer time, // GET POST DELETE PUT
|
|
|
|
@RequestParam(value = "page", required = true) Integer page,
|
|
|
|
@RequestParam(value = "pageSize", required = true) Integer pageSize
|
|
|
|
|
|
|
|
) {
|
|
|
|
List<LogModel> logs = new ArrayList<>();
|
|
|
|
StringBuffer sql = new StringBuffer("SELECT traceid, max(eventendtime) eventendtime ,min(eventstarttime) eventstarttime, max(excutetime) excutetime FROM " + adminServerProperties.getDefaultIndexName() + " where eventname='system_http_tracer' ");
|
|
|
|
StringBuffer whereSQL = new StringBuffer();
|
|
|
|
if (!StringUtils.isEmpty(traceId)) {
|
|
|
|
whereSQL.append(" and traceid='" + traceId + "'");
|
|
|
|
}
|
|
|
|
if (time != null && time > 0) {
|
|
|
|
whereSQL.append(" and excutetime >" + time + "");
|
|
|
|
}
|
|
|
|
StringBuffer groupby = new StringBuffer(" group by traceid order by eventstarttime desc limit " + (page * pageSize) + "," + pageSize);
|
|
|
|
String finalSql = sql.toString() + whereSQL.toString() + groupby.toString();
|
|
|
|
List<Map<String, Object>> traceidList = elastricSearchHelper.excuceSQL(finalSql);
|
|
|
|
for (int i = 0; i < traceidList.size(); i++) {
|
|
|
|
LogModel logTemp = new LogModel();
|
|
|
|
Map<String, Object> value = traceidList.get(i);
|
|
|
|
if (value.size() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
logTemp.setTraceid(value.get("traceid").toString());
|
|
|
|
Long startTime = ((Double) value.get("eventstarttime")).longValue();
|
|
|
|
logTemp.setEventstarttime(startTime);
|
|
|
|
logTemp.setEventstarttimestr(DateUtil.dateToStr(new Date(startTime), DateUtil.YYYY_MM_DD_HH_MM_SS_SSS));
|
|
|
|
Long endtime = ((Double) value.get("eventendtime")).longValue();
|
|
|
|
logTemp.setEventendtime(endtime);
|
|
|
|
logTemp.setEventendtimestr(DateUtil.dateToStr(new Date(endtime), DateUtil.YYYY_MM_DD_HH_MM_SS_SSS));
|
|
|
|
logs.add(logTemp);
|
|
|
|
}
|
|
|
|
return logs;
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("tracelist")
|
|
|
|
public List<LogModel> tracelist(
|
|
|
|
@RequestParam(value = "traceId", required = false) String traceId,
|
|
|
|
@RequestParam(value = "spanname", required = false) String spanname,
|
|
|
|
@RequestParam(value = "time", required = false) Integer time, // GET POST DELETE PUT
|
|
|
|
@RequestParam(value = "eventname", required = false) String eventname,
|
|
|
|
@RequestParam(value = "uri", required = false) String uri
|
|
|
|
) {
|
|
|
|
if (!StringUtils.isEmpty(spanname)) {
|
|
|
|
spanname = spanname.toLowerCase();
|
|
|
|
}
|
|
|
|
List<LogModel> logs = new ArrayList<>();
|
|
|
|
StringBuffer sql = new StringBuffer("SELECT * FROM " + adminServerProperties.getDefaultIndexName());
|
|
|
|
StringBuffer whereSQL = initWhere(traceId, spanname, eventname, time, uri);
|
|
|
|
|
|
|
|
StringBuffer groupBY = new StringBuffer("order by eventstarttime asc");
|
|
|
|
|
|
|
|
String finalSQL = sql.append(whereSQL).append(groupBY).toString();
|
|
|
|
|
|
|
|
List<Map<String, Object>> traceidList = elastricSearchHelper.excuceSQL(finalSQL);
|
|
|
|
for (int i = 0; i < traceidList.size(); i++) {
|
|
|
|
LogModel logTemp = new LogModel();
|
|
|
|
Map<String, Object> value = traceidList.get(i);
|
|
|
|
|
|
|
|
|
|
|
|
logTemp.setTraceid(value.get("traceid").toString());
|
|
|
|
logTemp.setSpanid(value.get("spanid").toString());
|
|
|
|
logTemp.setSpanname(value.get("spanname").toString());
|
|
|
|
logTemp.setEventname(value.get("eventname").toString());
|
|
|
|
if (value.containsKey("eventstarttime") && value.get("eventstarttime") != null) {
|
|
|
|
Long startTime = (Long) value.get("eventstarttime");
|
|
|
|
logTemp.setEventstarttime(startTime);
|
|
|
|
logTemp.setEventstarttimestr(DateUtil.dateToStr(new Date(startTime), DateUtil.YYYY_MM_DD_HH_MM_SS_SSS));
|
|
|
|
}
|
|
|
|
if (value.containsKey("eventendtime") && value.get("eventendtime") != null) {
|
|
|
|
Long endtime = (Long) value.get("eventendtime");
|
|
|
|
logTemp.setEventendtime(endtime);
|
|
|
|
logTemp.setEventendtimestr(DateUtil.dateToStr(new Date(endtime), DateUtil.YYYY_MM_DD_HH_MM_SS_SSS));
|
|
|
|
}
|
|
|
|
if (value.containsKey("method") && value.get("method") != null) {
|
|
|
|
logTemp.setMethod(value.get("method").toString());
|
|
|
|
}
|
|
|
|
if (value.containsKey("sql") && value.get("sql") != null) {
|
|
|
|
logTemp.setSql(value.get("sql").toString());
|
|
|
|
}
|
|
|
|
if (value.containsKey("ip") && value.get("ip") != null) {
|
|
|
|
logTemp.setIp(value.get("ip").toString());
|
|
|
|
}
|
|
|
|
if (value.containsKey("uri") && value.get("uri") != null) {
|
|
|
|
logTemp.setUri(value.get("uri").toString());
|
|
|
|
}
|
|
|
|
if (value.containsKey("excutetime") && value.get("excutetime") != null) {
|
|
|
|
logTemp.setExcutetime((Integer) value.get("excutetime"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//设置header
|
|
|
|
JSONObject headerJO = new JSONObject();
|
|
|
|
JSONObject requestJO = new JSONObject();
|
|
|
|
JSONObject responseJO = new JSONObject();
|
|
|
|
for (Map.Entry<String, Object> one : value.entrySet()) {
|
|
|
|
if (one.getKey().contains("header")) {
|
|
|
|
headerJO.put(one.getKey(), one.getValue());
|
|
|
|
} else if (one.getKey().contains("requestparams")) {
|
|
|
|
requestJO.put(one.getKey(), one.getValue());
|
|
|
|
} else if (one.getKey().contains("responseparams")) {
|
|
|
|
responseJO.put(one.getKey(), one.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logTemp.setHeader(headerJO.toString());
|
|
|
|
logTemp.setRequestparams(requestJO.toString());
|
|
|
|
logTemp.setResponseparams(responseJO.toString());
|
|
|
|
logs.add(logTemp);
|
|
|
|
}
|
|
|
|
return logs;
|
|
|
|
}
|
|
|
|
|
|
|
|
private StringBuffer initWhere(String traceId, String spanname, String eventname, Integer time, String uri) {
|
|
|
|
StringBuffer whereSQL = new StringBuffer();
|
|
|
|
Map<String, Object> params = new HashMap<>();
|
|
|
|
if (!StringUtils.isEmpty(traceId)) {
|
|
|
|
params.put("traceid", traceId);
|
|
|
|
}
|
|
|
|
if (!StringUtils.isEmpty(spanname)) {
|
|
|
|
params.put("spanname", spanname);
|
|
|
|
}
|
|
|
|
if (!StringUtils.isEmpty(eventname)) {
|
|
|
|
params.put("eventname", eventname);
|
|
|
|
}
|
|
|
|
if (!StringUtils.isEmpty(uri)) {
|
|
|
|
params.put("uri", uri);
|
|
|
|
}
|
|
|
|
if (time != null && time > 0) {
|
|
|
|
params.put("time", time);
|
|
|
|
}
|
|
|
|
String key = " where ";
|
|
|
|
for (Map.Entry<String, Object> one : params.entrySet()) {
|
|
|
|
if ("time".equals(one.getKey())) {
|
|
|
|
whereSQL.append(key).append("excutetime > '" + one.getValue() + "'");
|
|
|
|
} else if ("uri".equals(one.getKey())) {
|
|
|
|
whereSQL.append(key).append("uri like '%" + one.getValue() + "%'");
|
|
|
|
} {
|
|
|
|
whereSQL.append(key).append(one.getKey() + " = '" + one.getValue() + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
key = " and ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return whereSQL;
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("spanNamelist")
|
|
|
|
public List<QuotaModel> spanNamelist(
|
|
|
|
@RequestParam(value = "spanname", required = true) String spanname) {
|
|
|
|
spanname = spanname.toLowerCase();
|
|
|
|
List<QuotaModel> logs = new ArrayList<>();
|
|
|
|
StringBuffer sql = new StringBuffer("SELECT uri,sum(success) success,sum(fail) fail,avg(excutetime) avgtime ,max(excutetime) maxtime ,count(*) allcount FROM " + adminServerProperties.getDefaultIndexName() + " " +
|
|
|
|
" where spanname ='" + spanname + "' and eventname='system_http_tracer' " +
|
|
|
|
" group by uri ");
|
|
|
|
List<Map<String, Object>> traceidList = elastricSearchHelper.excuceSQL(sql.toString());
|
|
|
|
if (traceidList.size() > 0) {
|
|
|
|
for (Map<String, Object> one : traceidList) {
|
|
|
|
if (one.size() > 0) {
|
|
|
|
QuotaModel quotaModel = new QuotaModel();
|
|
|
|
quotaModel.setUri(one.get("uri").toString());
|
|
|
|
//quotaModel.setAllcount(.toString());
|
|
|
|
quotaModel.setAllcount(Double.valueOf(one.get("allcount").toString()).intValue());
|
|
|
|
quotaModel.setSuccess(Double.valueOf(one.get("success").toString()).intValue());
|
|
|
|
quotaModel.setFail(Double.valueOf(one.get("fail").toString()).intValue());
|
|
|
|
quotaModel.setAvgtime(Double.valueOf(one.get("avgtime").toString()));
|
|
|
|
quotaModel.setMaxtime(Double.valueOf(one.get("maxtime").toString()).intValue());
|
|
|
|
|
|
|
|
BeanUtils.copyProperties(one, quotaModel);
|
|
|
|
logs.add(quotaModel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return logs;
|
|
|
|
}
|
|
|
|
}
|