This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/java/com/nis/web/controller/configuration/LogSearchController.java

153 lines
6.1 KiB
Java

package com.nis.web.controller.configuration;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.beust.jcommander.internal.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.nis.domain.log.BaseLogEntity;
import com.nis.util.Constants;
import com.nis.util.StringUtil;
import com.nis.util.httpclient.HttpClientUtil;
import com.nis.web.controller.BaseController;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("${adminPath}/toLogSearch")
public class LogSearchController extends BaseController{
@RequestMapping(value = {"list",""})
public String LogSearch(BaseLogEntity<Object> entity, RedirectAttributes attr, HttpServletRequest request,
HttpServletResponse response) {
/**
* url: logUrl
* searchCfgId: compileId
* searchService: serviceId
*/
// 获取相应日志检索菜单URL
if(entity.getFunctionId()!=null && entity.getFunctionId().equals(635)){
entity.setFunctionId(8);//关键字配置的日志查询定位至http日志
}
String logUrl = menuService.getLogUrl(entity.getFunctionId());
// Integer serviceId = menuService.getServiceId(entity.getFunctionId(),entity.getAction());
attr.addAttribute("service", entity.getService().intValue());
attr.addAttribute("cfgId", entity.getCfgId());
attr.addAttribute("functionId", entity.getFunctionId());
attr.addAttribute("date", entity.getDate());
attr.addAttribute("isLogTotalSearch", entity.getIsLogTotalSearch());
return "redirect:"+adminPath+logUrl;
}
@RequestMapping(value = {"logTrend"})
public String logTrend(Model model,BaseLogEntity<Object> entity,String cfgId,String serviceId,RedirectAttributes attr, HttpServletRequest request,
HttpServletResponse response) {
Calendar cal = Calendar. getInstance ();
cal.setTime(new Date());
String now = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());//获取到完整的时间
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
String oneHoursAgo = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());
model.addAttribute("beginDate", oneHoursAgo);
model.addAttribute("endDate", now);
model.addAttribute("cfgId", cfgId);
model.addAttribute("serviceId", serviceId);
return "/cfg/logCfgTrendList";
}
@RequestMapping(value="actionLogTrend")
@ResponseBody
public List actionTrans(String cfgId,String beginDate,String endDate,String serviceId){
Map<String, Object> fromJsonList = new HashMap<String, Object>();
List resultList = new ArrayList();
String url = Constants.LOG_BASE_URL+Constants.NTC_PZ_REPORT;
url=url+"?pageSize=-1&searchBusinessType=2&searchCfgId="+cfgId;
if(!StringUtil.isEmpty(serviceId)){
url=url+"&searchService="+serviceId;
}
try {
//String url="http://192.168.10.204:9999/galaxy-service/service/log/v1/ntcPzReport?searchBusinessType=2&searchReportStartTime=2018-12-29%2000:00:00&searchReportEndTime=2018-12-30%2000:00:00";
url = urlAddDate(url,beginDate,endDate);
//String json=ConfigServiceUtil.getReport(url.toString(), null);
String json = HttpClientUtil.get(url);
Gson gson = new GsonBuilder().create();
fromJsonList = gson.fromJson(json, new TypeToken<Map>(){}.getType());
fromJsonList=(Map<String, Object>) fromJsonList.get("data");
logger.debug("日志趋势数据"+fromJsonList);
JSONObject obj=JSONObject.fromObject(json);
resultList =getList(fromJsonList);
} catch (Exception e) {
e.printStackTrace();
resultList.add(Maps.newHashMap("error","request_service_failed"));
}
return resultList;
}
public List getList(Map<String, Object> dateList){
List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
try {
List<Map<String, Object>> mapList=(List<Map<String, Object>>) dateList.get("list");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map<String, Object> maps=new HashMap<String,Object>();
List<Double[]> list=new ArrayList<Double[]>();
Double total=0.0;
for (Map<String, Object> map : mapList) {
Double[] logs=new Double[2];
Double sum=Double.valueOf(String.valueOf(map.get("sum")));
Double date=Double.valueOf(sdf.parse(String.valueOf(map.get("reportTime"))).getTime());
total+=sum;
logs[0]=date;
logs[1]=sum;
list.add(logs);
}
maps.put("sum",total);
maps.put("result", list);
resultList.add(maps);
} catch (Exception e) {
e.printStackTrace();
}
return resultList;
}
/**
*
*
* url路径时间参数格式化
* @param url
* @param beginDate
* @param endDate
* @return
* @throws URISyntaxException
*/
public String urlAddDate(String url,String beginDate,String endDate) throws URISyntaxException{
if(StringUtil.isBlank(beginDate)||StringUtil.isBlank(endDate)){
Calendar cal = Calendar. getInstance ();
cal.setTime(new Date());
endDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());//获取到完整的时间
cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
beginDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:00" ).format(cal.getTime());
}
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.addParameter("searchReportStartTime",beginDate);
uriBuilder.addParameter("searchReportEndTime",endDate);
return uriBuilder.toString();
}
}