|
@@ -1,7 +1,8 @@
|
|
|
package com.jjt.risk.controller;
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.jjt.biz.domain.BizObj;
|
|
|
-import com.jjt.biz.domain.BizObjData;
|
|
|
import com.jjt.biz.domain.BizObjMetrics;
|
|
|
import com.jjt.biz.domain.BizObjMetricsData;
|
|
|
import com.jjt.biz.service.IBizObjMetricsDataService;
|
|
@@ -12,12 +13,14 @@ import com.jjt.common.core.controller.BaseController;
|
|
|
import com.jjt.common.core.domain.AjaxResult;
|
|
|
import com.jjt.common.core.page.TableDataInfo;
|
|
|
import com.jjt.common.enums.BusinessType;
|
|
|
+import com.jjt.common.utils.http.HttpUtils;
|
|
|
import com.jjt.common.utils.poi.ExcelUtil;
|
|
|
import com.jjt.risk.domain.RiskAnalysis;
|
|
|
import com.jjt.risk.domain.RiskModel;
|
|
|
import com.jjt.risk.domain.RiskObj;
|
|
|
import com.jjt.risk.service.IRiskAnalysisService;
|
|
|
import com.jjt.risk.service.IRiskModelService;
|
|
|
+import com.jjt.system.service.ISysConfigService;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
@@ -26,6 +29,7 @@ import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneOffset;
|
|
@@ -55,6 +59,8 @@ public class RiskAnalysisController extends BaseController {
|
|
|
private IBizObjMetricsService objMetricsService;
|
|
|
@Resource
|
|
|
private IBizObjMetricsDataService dataService;
|
|
|
+ @Resource
|
|
|
+ private ISysConfigService configService;
|
|
|
|
|
|
/**
|
|
|
* 查询风险分析结果列表
|
|
@@ -94,35 +100,55 @@ public class RiskAnalysisController extends BaseController {
|
|
|
|
|
|
@ApiOperation("立即分析")
|
|
|
@GetMapping(value = "/atOnce/{riskId}")
|
|
|
- public AjaxResult atOnce(@PathVariable("riskId") Long riskId) {
|
|
|
+ public AjaxResult atOnce(@PathVariable("riskId") Long riskId) throws UnsupportedEncodingException {
|
|
|
+ String uri = configService.selectConfigByKey("risk.api.uri");
|
|
|
RiskModel rm = riskModelService.selectRiskModelByRiskId(riskId);
|
|
|
-
|
|
|
-
|
|
|
- String sql = "SELECT img_path FROM risk_analysis WHERE result_id=?";
|
|
|
- String ra_1 = jdbcTemplate.queryForObject(sql, String.class, -1L);
|
|
|
- String ra_2 = jdbcTemplate.queryForObject(sql, String.class, -2L);
|
|
|
List<Long> objIds = new ArrayList<>();
|
|
|
+ //找到对象
|
|
|
if ("1".equals(rm.getObjType())) {
|
|
|
+ //1.所有对象
|
|
|
List<BizObj> list = objService.selectBizObjListByMetricsId(rm.getMetricsId());
|
|
|
if (list != null) {
|
|
|
objIds = list.stream().map(BizObj::getObjId).collect(Collectors.toList());
|
|
|
}
|
|
|
} else {
|
|
|
+ //2.选择的对象
|
|
|
List<RiskObj> objList = rm.getRiskObjList();
|
|
|
if (objList != null) {
|
|
|
objIds = objList.stream().map(RiskObj::getObjId).collect(Collectors.toList());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ BizObjMetrics queryOm = new BizObjMetrics();
|
|
|
+ queryOm.setMetricsId(rm.getMetricsId());
|
|
|
+ List<BizObjMetrics> allMetrics = objMetricsService.selectBizObjMetricsList(queryOm);
|
|
|
+ Map<Long, List<BizObjMetrics>> objMap = allMetrics.stream().collect(Collectors.groupingBy(BizObjMetrics::getObjId));
|
|
|
Date time = new Date();
|
|
|
// rm.getRiskObjList()
|
|
|
Random r = new Random();
|
|
|
if ("3".equals(rm.getRiskType())) {
|
|
|
-// if()
|
|
|
+ List<Long> finalObjIds = objIds;
|
|
|
+ List<BizObjMetrics> list = allMetrics.stream().filter(om -> finalObjIds.contains(om.getObjId())).collect(Collectors.toList());
|
|
|
+ list = list.stream().sorted(Comparator.comparing(BizObjMetrics::getDValue).reversed()).collect(Collectors.toList());
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ JSONArray dataArr = new JSONArray();
|
|
|
+ JSONArray inputArr = new JSONArray();
|
|
|
+ list.forEach(bom -> {
|
|
|
+ dataArr.add(bom.getDValue().floatValue());
|
|
|
+ JSONObject temp = new JSONObject();
|
|
|
+ temp.put("id", bom.getObjMetricsId());
|
|
|
+ temp.put("name", bom.getObjName() + "-" + bom.getMetricsName());
|
|
|
+ temp.put("num", bom.getDValue().floatValue());
|
|
|
+ inputArr.add(temp);
|
|
|
+ });
|
|
|
+ jsonObject.put("data", dataArr);
|
|
|
+ JSONObject result = JSONObject.parseObject(HttpUtils.dataPost(uri + "/api/sas/diff", jsonObject));
|
|
|
RiskAnalysis ra = new RiskAnalysis();
|
|
|
ra.setRiskId(riskId);
|
|
|
ra.setAnalyseTime(time);
|
|
|
- ra.setImgPath(ra_2);
|
|
|
+ // 假设jsonArray是已有的JSONArray对象
|
|
|
+ ra.setInput(inputArr.toString());
|
|
|
+ ra.setImgPath("data:image/png;base64," + result.getJSONObject("data").getString("diff"));
|
|
|
riskAnalysisService.insertRiskAnalysis(ra);
|
|
|
return type3info(ra.getResultId());
|
|
|
} else {
|
|
@@ -135,36 +161,67 @@ public class RiskAnalysisController extends BaseController {
|
|
|
endTime = beginTime;
|
|
|
beginTime = endTime.minusDays(7);
|
|
|
Map<String, Object> lastParams = new HashMap<>(16);
|
|
|
- currParams.put("beginTime", beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
- currParams.put("endTime", endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
+ lastParams.put("beginTime", beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
+ lastParams.put("endTime", endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
|
|
|
for (Long objId : objIds) {
|
|
|
- BizObjMetrics queryOm = new BizObjMetrics();
|
|
|
- queryOm.setMetricsId(rm.getMetricsId());
|
|
|
- queryOm.setObjId(objId);
|
|
|
- List<BizObjMetrics> list = objMetricsService.selectBizObjMetricsList(queryOm);
|
|
|
+ List<BizObjMetrics> list = objMap.get(objId);
|
|
|
|
|
|
list.forEach(om -> {
|
|
|
//查询2周的数据,调用python接口,并且存储?
|
|
|
BizObjMetricsData currQ = new BizObjMetricsData();
|
|
|
+ currQ.setObjMetricsId(om.getObjMetricsId());
|
|
|
currQ.setParams(currParams);
|
|
|
BizObjMetricsData lastQ = new BizObjMetricsData();
|
|
|
+ lastQ.setObjMetricsId(om.getObjMetricsId());
|
|
|
lastQ.setParams(lastParams);
|
|
|
List<BizObjMetricsData> currList = dataService.selectBizObjMetricsDataList(currQ);
|
|
|
List<BizObjMetricsData> lastList = dataService.selectBizObjMetricsDataList(lastQ);
|
|
|
- // "level":rm.getConfidenceLevel().floatValue();
|
|
|
|
|
|
+
|
|
|
+ JSONArray currArr = new JSONArray();
|
|
|
+ JSONArray lastArr = new JSONArray();
|
|
|
+ JSONArray sortArr = new JSONArray();
|
|
|
+ //用于补数据,因为需要本周的长度与上周相等
|
|
|
+ int dataSize = currList.size();
|
|
|
+ if (lastList.size() > dataSize) {
|
|
|
+ dataSize = lastList.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < dataSize; i++) {
|
|
|
+ int currIndex = i;
|
|
|
+ int lastIndex = i;
|
|
|
+ if (currIndex >= currList.size()) {
|
|
|
+ currIndex = currIndex - currList.size();
|
|
|
+ }
|
|
|
+ if (lastIndex >= lastList.size()) {
|
|
|
+ lastIndex = lastIndex - lastList.size();
|
|
|
+ }
|
|
|
+ BizObjMetricsData curr = currList.get(currIndex);
|
|
|
+ currArr.add(curr.getdValue().floatValue());
|
|
|
+
|
|
|
+ BizObjMetricsData last = lastList.get(lastIndex);
|
|
|
+ lastArr.add(last.getdValue().floatValue());
|
|
|
+ sortArr.add(i + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("level", rm.getConfidenceLevel().floatValue());
|
|
|
+ jsonObject.put("curr", currArr);
|
|
|
+ jsonObject.put("last", lastArr);
|
|
|
+ jsonObject.put("sort", sortArr);
|
|
|
+ JSONObject result = JSONObject.parseObject(HttpUtils.dataPost(uri + "/api/sas/trend", jsonObject));
|
|
|
+ JSONObject data = result.getJSONObject("data");
|
|
|
RiskAnalysis ra = new RiskAnalysis();
|
|
|
ra.setRiskId(riskId);
|
|
|
ra.setObjId(om.getObjMetricsId());
|
|
|
- ra.setResult("趋势好转");
|
|
|
- ra.setAvgValue("98,88");
|
|
|
- ra.setNormalValid("通过");
|
|
|
- ra.setVarianceValid("通过");
|
|
|
+ ra.setInput(jsonObject.toString());
|
|
|
+ ra.setResult(data.getString("trend"));
|
|
|
+ ra.setAvgValue(data.getString("ttest"));
|
|
|
+ ra.setNormalValid(data.getString("shapiro"));
|
|
|
+ ra.setVarianceValid(data.getString("levene"));
|
|
|
ra.setConfidenceLevel(rm.getConfidenceLevel().toString());
|
|
|
- ra.setPValue(r.nextInt(100) + "");
|
|
|
ra.setAnalyseTime(time);
|
|
|
- ra.setImgPath(ra_1);
|
|
|
riskAnalysisService.insertRiskAnalysis(ra);
|
|
|
});
|
|
|
// rm.getMetricsId()
|
|
@@ -180,19 +237,9 @@ public class RiskAnalysisController extends BaseController {
|
|
|
public AjaxResult type3info(@PathVariable("resultId") Long resultId) {
|
|
|
Map<String, Object> result = new HashMap<>(16);
|
|
|
RiskAnalysis analysis = riskAnalysisService.selectRiskAnalysisByResultId(resultId);
|
|
|
- List<Map<String, Object>> list = new ArrayList();
|
|
|
- String[] names = {"基础支撑", "运行环境", "ES搜索服务", "nginx", "redis"};
|
|
|
- for (int i = 0; i < names.length; i++) {
|
|
|
- Map<String, Object> map = new HashMap<>(16);
|
|
|
- map.put("id", (i + 1));
|
|
|
- map.put("name", names[i]);
|
|
|
- long num = Long.valueOf(new Random().nextInt(200) + 10);
|
|
|
- map.put("num", num);
|
|
|
- list.add(map);
|
|
|
- }
|
|
|
|
|
|
result.put("imgPath", analysis.getImgPath());
|
|
|
- result.put("top", list);
|
|
|
+ result.put("top", JSONArray.parseArray(analysis.getInput()));
|
|
|
return success(result);
|
|
|
}
|
|
|
|