diff --git a/src/main/java/neatlogic/module/inspect/api/report/InspectReportExportApi.java b/src/main/java/neatlogic/module/inspect/api/report/InspectReportExportApi.java index 5f7aec59e2ed05ee6731c77752e9f9ccca9ab22c..5de44e2b01291ee40fd14187b5075e3c11191ad9 100644 --- a/src/main/java/neatlogic/module/inspect/api/report/InspectReportExportApi.java +++ b/src/main/java/neatlogic/module/inspect/api/report/InspectReportExportApi.java @@ -15,7 +15,6 @@ along with this program. If not, see .*/ package neatlogic.module.inspect.api.report; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; @@ -65,9 +64,9 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase static String template; static { - try { - InputStreamReader reader = new InputStreamReader(Objects.requireNonNull(InspectReportExportApi.class.getClassLoader() - .getResourceAsStream("template/inspect-report-template.ftl")), StandardCharsets.UTF_8.name()); + try (InputStreamReader reader = new InputStreamReader(Objects.requireNonNull(InspectReportExportApi.class.getClassLoader() + .getResourceAsStream("template/inspect-report-template.ftl")), StandardCharsets.UTF_8)) { + template = IOUtils.toString(reader); } catch (Exception e) { logger.error(e.getMessage(), e); @@ -115,10 +114,14 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase if (resource != null && resource.getName() != null) { fileName = resource.getName(); } - Document reportDoc = inspectReportService.getInspectReport(resourceId, id, jobId); + Document reportDocument = inspectReportService.getInspectReport(resourceId, id, jobId); + JSONObject reportDoc = null; + if (MapUtils.isNotEmpty(reportDocument)) { + reportDoc = JSONObject.parseObject(reportDocument.toJson()); + } if (MapUtils.isNotEmpty(reportDoc)) { Map translationMap = new HashMap<>(); - JSONArray fields = JSON.parseArray(reportDoc.get("fields").toString()); + JSONArray fields = reportDoc.getJSONArray("fields"); if (CollectionUtils.isNotEmpty(fields)) { for (int i = 0; i < fields.size(); i++) { JSONObject obj = fields.getJSONObject(i); @@ -131,11 +134,11 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase JSONObject alert = null; Map alertMap = new HashMap<>(); // 记录jsonpath与告警级别之间的映射 Map alertLevelClassMap = new HashMap<>(); - Map inspectStatus = (Map) reportDoc.get("inspectStatus"); + JSONObject inspectStatus = reportDoc.getJSONObject("inspectStatus"); // 组装告警级别与cssClass之间的映射(alertLevelClassMap)和告警提示(alert) if (MapUtils.isNotEmpty(inspectStatus)) { - for (Map.Entry entry : inspectStatus.entrySet()) { - JSONObject object = JSON.parseObject(entry.getValue().toString()); + for (String key : inspectStatus.keySet()) { + JSONObject object = inspectStatus.getJSONObject(key); alertLevelClassMap.put(object.getString("value"), object.getString("cssClass")); } alert = getAlert(reportDoc, translationMap, alertMap, inspectStatus); @@ -154,7 +157,13 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase dataObj.put("lineList", lineList); dataObj.put("tableList", tableList); String execUser = reportDoc.getString("_execuser"); - Date reportTime = reportDoc.getDate("_report_time"); + Date reportTime = null; + Object reportTimeObj = reportDoc.get("_report_time"); + if (reportTimeObj instanceof Date) { + reportTime = (Date) reportTimeObj; + } else if (reportTimeObj instanceof Long) { + reportTime = new Date((Long)reportTimeObj); + } if (StringUtils.isNotBlank(execUser)) { UserVo userVo = userMapper.getUserBaseInfoByUuid(execUser); if (userVo != null) { @@ -202,10 +211,10 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase * @param inspectStatus inspectStatus * @return */ - private JSONObject getAlert(Document reportDoc, Map translationMap, Map alertMap, Map inspectStatus) { - Document inspectResult = (Document) reportDoc.get("_inspect_result"); + private JSONObject getAlert(JSONObject reportDoc, Map translationMap, Map alertMap, JSONObject inspectStatus) { + JSONObject inspectResult = reportDoc.getJSONObject("_inspect_result"); if (inspectResult != null) { - List alertFields = (List) inspectResult.get("alertFields"); + JSONArray alertFields = inspectResult.getJSONArray("alertFields"); if (CollectionUtils.isNotEmpty(alertFields)) { JSONObject alert = new JSONObject(); JSONArray headList = new JSONArray(); @@ -217,7 +226,7 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase alert.put("rowList", alertArray); for (int i = 0; i < alertFields.size(); i++) { JSONObject alertObj = new JSONObject(); - Document object = (Document) alertFields.get(i); + JSONObject object = alertFields.getJSONObject(i); String alertField = object.getString("alertField").split("\\$\\.")[1]; alertMap.put(alertField, object.getString("alertLevel").toLowerCase(Locale.ROOT)); String field; @@ -242,11 +251,17 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase field = translationMap.get(alertField); } alertObj.put("告警字段", field); - Object alertLevel = inspectStatus.get(object.getString("alertLevel").toLowerCase(Locale.ROOT)); - alertObj.put("level", object.getString("alertLevel").toLowerCase(Locale.ROOT)); - alertObj.put("告警级别", JSON.parseObject(alertLevel.toString()).getString("text")); - List ruleNames = (List) object.get("ruleNames"); - alertObj.put("告警提示", String.join("、", ruleNames)); + String level = object.getString("alertLevel").toLowerCase(Locale.ROOT); + alertObj.put("level", level); + JSONObject alertLevel = inspectStatus.getJSONObject(level); + if (MapUtils.isNotEmpty(alertLevel)) { + alertObj.put("告警级别", alertLevel.getString("text")); + } + JSONArray ruleNameArray = object.getJSONArray("ruleNames"); + if (CollectionUtils.isNotEmpty(ruleNameArray)) { + List ruleNames = ruleNameArray.toJavaList(String.class); + alertObj.put("告警提示", String.join("、", ruleNames)); + } alertArray.add(alertObj); } return alert; @@ -264,11 +279,14 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase * @param lineList 存储String、int或Array字段的list * @param tableList 存储JsonArray字段的list */ - private void getDataMap(Map reportJson, Map translationMap, Map alertMap, JSONArray lineList, JSONArray tableList) { + private void getDataMap(JSONObject reportJson, Map translationMap, Map alertMap, JSONArray lineList, JSONArray tableList) { - JSONArray fields = JSON.parseArray(reportJson.get("fields").toString()); + JSONArray fields = reportJson.getJSONArray("fields"); for (int i = 0; i < fields.size(); i++) { JSONObject fieldObj = fields.getJSONObject(i); + if (MapUtils.isEmpty(fieldObj)) { + continue; + } String key = fieldObj.getString("name"); if (key.startsWith("_")) { continue; @@ -279,40 +297,36 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase String alertLevel = alertMap.get(key); if (name != null) { if (subset instanceof List) { - if (name != null) { - JSONObject table = new JSONObject(); - List listValue = new ArrayList(); - if (value instanceof List) { - listValue = (List) value; - } else if (value instanceof Map) { - Map map = (Map) value; - listValue.add(new JSONObject(map)); - recursionForTable(table, translationMap, alertMap, key, listValue, key, fieldObj); - tableList.add(table); - continue; - } + JSONObject table = new JSONObject(); + JSONArray listValue = new JSONArray(); + if (value instanceof List) { + listValue.addAll((List) value); + } else if (value instanceof Map) { + Map map = (Map) value; + listValue.add(new JSONObject(map)); + recursionForTable(table, translationMap, alertMap, key, listValue, key, fieldObj); + tableList.add(table); + continue; + } - if (CollectionUtils.isNotEmpty(listValue)) { - if (!(listValue.get(0) instanceof Document)) { // 元素类型不是Document,说明value是非JSONObject数组 - JSONObject line = new JSONObject(); - line.put("key", name); - line.put("value", listValue.toString()); - if (alertLevel != null) { - line.put("alertLevel", alertLevel); - } - lineList.add(line); - } else { - recursionForTable(table, translationMap, alertMap, key, listValue, key, fieldObj); - tableList.add(table); + if (CollectionUtils.isNotEmpty(listValue)) { + if (!(listValue.get(0) instanceof JSONObject)) { // 元素类型不是Document,说明value是非JSONObject数组 + JSONObject line = new JSONObject(); + line.put("key", name); + line.put("value", listValue.toString()); + if (alertLevel != null) { + line.put("alertLevel", alertLevel); } + lineList.add(line); } else { - lineList.add(new JSONObject() { - { - this.put("key", name); - this.put("value", "暂无数据"); - } - }); + recursionForTable(table, translationMap, alertMap, key, listValue, key, fieldObj);// + tableList.add(table); } + } else { + JSONObject line = new JSONObject(); + line.put("key", name); + line.put("value", "暂无数据"); + lineList.add(line); } } else { if (value == null || Objects.equals(StringUtils.EMPTY, value)) { @@ -368,7 +382,7 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase * @param array 待转换的JsonArray字段 * @param alertKey jsonpath */ - private void recursionForTable(JSONObject table, Map translationMap, Map alertMap, String key, List array, String alertKey, JSONObject fieldObj) { + private void recursionForTable(JSONObject table, Map translationMap, Map alertMap, String key, JSONArray array, String alertKey, JSONObject fieldObj) { Set headSet = new LinkedHashSet<>(); JSONArray subset = fieldObj.getJSONArray("subset"); if (CollectionUtils.isEmpty(subset)) { @@ -393,16 +407,18 @@ public class InspectReportExportApi extends PrivateBinaryStreamApiComponentBase JSONArray valueList = new JSONArray(); table.put("valueList", valueList); for (int i = 0; i < array.size(); i++) { - Map object = (Map) array.get(i); + JSONObject object = array.getJSONObject(i); JSONObject row = new JSONObject(); int j = 0; for (String head : headSet) { Object obj = object.get(head); if (obj != null) { - if (obj instanceof List && ((List) obj).get(0) instanceof Map) { - List _array = (List) obj; - if (CollectionUtils.isNotEmpty(_array)) { + if (obj instanceof List && CollectionUtils.isNotEmpty((List) obj) && ((List) obj).get(0) instanceof Map) { + List list = (List) obj; + if (CollectionUtils.isNotEmpty(list)) { JSONObject _table = new JSONObject(); + JSONArray _array = new JSONArray(); + _array.addAll(list); recursionForTable(_table, translationMap, alertMap, key + "." + head, _array, (alertKey + "[" + i + "]" + "." + head), fieldObj); _table.remove("key"); row.put(headList.get(j), _table);