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/dynamicpage/DynamicIndexController.java
2018-12-21 12:56:38 +08:00

335 lines
11 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.nis.web.controller.dynamicpage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.nis.domain.Page;
import com.nis.domain.log.WelcomePageEntity;
import com.nis.domain.maat.LogRecvData;
import com.nis.util.Constants;
import com.nis.util.DateUtils;
import com.nis.util.httpclient.HttpClientUtil;
import com.nis.web.controller.BaseController;
@Controller
@RequestMapping(value = "${adminPath}/dynamicpage")
public class DynamicIndexController extends BaseController {
private final static Logger logger = Logger.getLogger(DynamicIndexController.class);
/**
* 跳转到动态欢迎界面
* dynamicpage/dynamicIndex
* @return
*/
@RequestMapping(value="dynamicIndex")
public String dynamicIndex(HttpServletRequest request){
String serviceIds = request.getParameter("sid");
String baseNum = request.getParameter("baseNum");
Object trafficAreaStat = trafficAreaStat(null, Integer.parseInt(baseNum));
Object dropStat = dropStat(null, Integer.parseInt(baseNum));
Object logStat = logStat(null, serviceIds, Integer.parseInt(baseNum));
Gson gson = new GsonBuilder().create();
String trafficJson = gson.toJson(trafficAreaStat);
String dropJson = gson.toJson(dropStat);
String logJson = gson.toJson(logStat);
request.setAttribute("trafficData", trafficJson);
request.setAttribute("dropData", dropJson);
request.setAttribute("logData", logJson);
return "/dynamicpage/dynamicIndex";
}
/**
* @param hour 取hour小时内的数据
* @return
*/
@ResponseBody
@RequestMapping(value="indexTraffic")
public Object getTrafficAreaStat(Integer hour, Integer baseNum) {
return trafficAreaStat(hour, baseNum);
}
/**
* @param hour 取hour小时内的数据
* @return
*/
@ResponseBody
@RequestMapping(value="indexDrop")
public Object getDropStat(Integer hour, Integer baseNum) {
return dropStat(hour, baseNum);
}
/**
* @param hour 取hour小时内的数据
* @return
*/
@ResponseBody
@RequestMapping(value="indexLog")
public Object getLogStat(Integer hour, String sid, Integer baseNum) {
return logStat(hour, sid, baseNum);
}
/**
* @param hour 取hour小时内的流量数据默认24小时
* @return
*/
public Object trafficAreaStat(Integer hour, Integer baseNum) {
List<Map> results = new ArrayList<>();
if (hour == null || hour <= 0) {
hour = 24;
}
Date now = new Date();
Date start = new Date(now.getTime()-(hour*60*60*1000));
String nowString = DateUtils.formatDate(now, "yyyy-MM-dd HH:mm:ss");
String startString = DateUtils.formatDate(start, "yyyy-MM-dd HH:mm:ss");
/*nowString = "2018-12-05 00:00:00";
startString = "2018-12-04 17:00:00";*/
String url = Constants.LOG_BASE_URL + Constants.TRAFFIC_AREA_STAT;
//String url = "http://localhost:8080/ntc/helloworld/test";
Map<String, Object> params = new HashMap<String, Object>();
params.put("searchReportStartTime", startString);
params.put("searchReportEndTime", nowString);
params.put("baseNum", baseNum);
try {
String recv = HttpClientUtil.getMsg(url, params, null);
if (StringUtils.isNotBlank(recv)) {
Gson gson = new GsonBuilder().create();
LogRecvData<WelcomePageEntity> fromJson = gson.fromJson(recv, new TypeToken<LogRecvData<WelcomePageEntity>>(){}.getType());
if (fromJson.getStatus().intValue() == 200) {
List<WelcomePageEntity> data = fromJson.getData().getList();//需要展示的数据
Map<String, List<List<Object>>> t = new HashMap<>();//Map<地区名, [[时间戳, 数值], [], ...]>
for (WelcomePageEntity entity : data) {
if (!StringUtils.isBlank(entity.getArea())) {
if (!t.containsKey(entity.getArea())) {
t.put(entity.getArea(), new ArrayList<List<Object>>());
}
List<Object> point = new ArrayList<>();//每个点 [时间戳, 数值]
point.add(DateUtils.parseDate(entity.getReportTime()).getTime());
point.add(entity.getSum());
List<List<Object>> points = t.get(entity.getArea());//所有点
points.add(point);
}
}
for (String area : t.keySet()) {
Map result = new HashMap();
result.put("name", area);
result.put("data", t.get(area));
results.add(result);
}
}
}
} catch (IOException e) {
logger.error("请求服务出错", e);
} catch (Exception e) {
logger.error("解析出错", e);
results.clear();
}
/*if(results != null && results.size()<1){//伪造数据
results = null;
results = new ArrayList<>();
Map result = null;//new HashMap();
for(int i=0;i<hour*12;i++){
result = null;
result = new HashMap();
result.put("name", "Alamty");
result.put("data", t.get(area));
}
}*/
return results;
}
/**
* @param hour 取hour小时内的丢弃数据默认24小时
* @return
*/
public Object dropStat(Integer hour, Integer baseNum) {
List<Map> results = new ArrayList<>();
if (hour == null || hour <= 0) {
hour = 24;
}
Date now = new Date();
Date start = new Date(now.getTime()-(hour*60*60*1000));
String nowString = DateUtils.formatDate(now, "yyyy-MM-dd HH:mm:ss");
String startString = DateUtils.formatDate(start, "yyyy-MM-dd HH:mm:ss");
String url = Constants.LOG_BASE_URL + Constants.BLOCK_AND_DROP_STAT;
//String url = "http://localhost:8080/ntc/helloworld/test2";
Map<String, Object> params = new HashMap<String, Object>();
params.put("searchReportStartTime", startString);
params.put("searchReportEndTime", nowString);
params.put("searchBusinessType", 2);
params.put("baseNum", baseNum);
try {
String recv = HttpClientUtil.getMsg(url, params, null);
if (StringUtils.isNotBlank(recv)) {
Gson gson = new GsonBuilder().create();
LogRecvData<WelcomePageEntity> fromJson = gson.fromJson(recv, new TypeToken<LogRecvData<WelcomePageEntity>>(){}.getType());
if (fromJson.getStatus().intValue() == 200) {
List<WelcomePageEntity> data = fromJson.getData().getList();//需要展示的数据
Map<String, List<Long>> t = new HashMap<>();//Map<动作, [sum, sum ...]>
for (WelcomePageEntity entity : data) {
if (!StringUtils.isBlank(entity.getLabel())) {
if (!t.containsKey(entity.getLabel())) {
t.put(entity.getLabel(), new ArrayList<Long>());
}
List<Long> point = t.get(entity.getLabel());
point.add(entity.getSum());
}
}
for (String label : t.keySet()) {
Map result = new HashMap();
result.put("name", label);
result.put("data", t.get(label));
results.add(result);
}
}
}
} catch (IOException e) {
logger.error("请求服务出错", e);
} catch (Exception e) {
logger.error("解析出错", e);
results.clear();
}
return results;
}
/**
* @param hour 取hour小时内的日志数据默认24小时
* @return
*/
public Object logStat(Integer hour, String serviceIds, Integer baseNum) {
String[] _idsArr = serviceIds.split(",");
Long[] idsArr = new Long[5];
for (int i = 0; i < 5; i++) {
idsArr[i] = Long.parseLong(_idsArr[i]);
}
List results = new ArrayList<>();
if (hour == null || hour <= 0) {
hour = 24;
}
Date now = new Date();
Date start = new Date(now.getTime()-(hour*60*60*1000));
String nowString = DateUtils.formatDate(now, "yyyy-MM-dd HH:mm:ss");
String startString = DateUtils.formatDate(start, "yyyy-MM-dd HH:mm:ss");
/*nowString = "2018-12-16 11:30:00";
startString = "2018-12-16 11:00:00";*/
String url = Constants.LOG_BASE_URL + Constants.LOG_SERVICE_TOPN;
Map<String, Object> params = new HashMap<String, Object>();
params.put("searchReportStartTime", startString);
params.put("searchReportEndTime", nowString);
params.put("searchService", serviceIds);
params.put("baseNum", baseNum);
try {
String recv = HttpClientUtil.getMsg(url, params, null);
if (StringUtils.isNotBlank(recv)) {
Gson gson = new GsonBuilder().create();
LogRecvData<WelcomePageEntity> fromJson = gson.fromJson(recv, new TypeToken<LogRecvData<WelcomePageEntity>>(){}.getType());
if (fromJson.getStatus().intValue() == 200) {
List<WelcomePageEntity> data = fromJson.getData().getList();//需要展示的数据
Map<String, List<List<Long>>> t = new HashMap<>();//Map<时间, [[业务id, sum], [业务id, sum] ...]>
for (WelcomePageEntity entity : data) {
if (!StringUtils.isBlank(entity.getReportTime())) {
if (!t.containsKey(entity.getReportTime())) {
t.put(entity.getReportTime(), new ArrayList<List<Long>>());
}
List<List<Long>> services = t.get(entity.getReportTime());
List<Long> service = new ArrayList<>();
service.add(entity.getServiceCode());
service.add(entity.getSum());
services.add(service);
}
}
//每个时间点不足5个日志的话补全至5个
for (String time : t.keySet()) {
List<List<Long>> list = t.get(time);
if (list.size() < 5 && list.size() > 0) {
Long[] idsArrT = new Long[5];
Long[] sumArrT = new Long[5];
for (int i = 0; i < list.size(); i++) {
idsArrT[i] = list.get(i).get(0);
sumArrT[i] = list.get(i).get(1);
}
Long[] substract = substract(idsArr, idsArrT);
for (int i = 0; i < substract.length; i++) {
List<Long> l = new ArrayList<>();
l.add(substract[i]);
l.add(random()*sumArrT[0]/100);
list.add(l);
}
}
}
for (String time : t.keySet()) {
results.add(t.get(time));
}
}
}
} catch (IOException e) {
logger.error("请求服务出错", e);
} catch (Exception e) {
logger.error("解析出错", e);
results.clear();
}
return results;
}
public static int random() {
Random ra =new Random();
return (ra.nextInt(10)+45)*2;
}
public static Long[] substract(Long[] arr1, Long[] arr2) {
ArrayList<Long> list = new ArrayList<Long>();
for (Long str : arr1) {
if(!list.contains(str)) {
list.add(str);
}
}
for (Long str : arr2) {
if (list.contains(str)) {
list.remove(str);
}
}
Long[] result = {};
return list.toArray(result);
}
}