|
|
@@ -0,0 +1,825 @@
|
|
|
+package com.energy.manage.service.service.ranking.impl;
|
|
|
+
|
|
|
+import com.energy.manage.service.service.ranking.DiagnosisService;
|
|
|
+import com.energy.manage.service.util.GzlyTxtParser;
|
|
|
+import com.energy.manage.service.util.IPUtils;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import io.minio.GetObjectArgs;
|
|
|
+import io.minio.ListObjectsArgs;
|
|
|
+import io.minio.MinioClient;
|
|
|
+import io.minio.Result;
|
|
|
+import io.minio.messages.Item;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.URI;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 诊断结果取数实现。定位 MinIO 最新批次文件并解析。
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class DiagnosisServiceImpl implements DiagnosisService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MinioClient minioClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("analyseJdbcTemplate")
|
|
|
+ private JdbcTemplate analyseJdbcTemplate;
|
|
|
+
|
|
|
+ @Value("${minio.diagnosisBucket:gzly-analyse}")
|
|
|
+ private String bucket;
|
|
|
+
|
|
|
+ private final ObjectMapper mapper = new ObjectMapper();
|
|
|
+
|
|
|
+ /** 算法表路径列只允许这些功能编号,避免把请求参数拼进列名。 */
|
|
|
+ private static final Set<String> FUNCTION_CODES = new HashSet<>(Arrays.asList(
|
|
|
+ "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10", "A11", "A12",
|
|
|
+ "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "Y01", "Y02"));
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> projectFunction(String fieldCode, String machineTypeCode,
|
|
|
+ String month, String functionCode) {
|
|
|
+ Map<String, Object> resp = baseResp(fieldCode, machineTypeCode, month, functionCode, null);
|
|
|
+ Located graphLoc = locate(fieldCode, machineTypeCode, null, functionCode, month, true);
|
|
|
+ Located txtLoc = locate(fieldCode, machineTypeCode, null, functionCode, month, false);
|
|
|
+ resp.put("found", graphLoc.path != null || txtLoc.path != null);
|
|
|
+ resp.put("graphPath", graphLoc.path);
|
|
|
+ resp.put("txtPath", txtLoc.path);
|
|
|
+ resp.put("graphUrl", publicFileUrl(graphLoc));
|
|
|
+ resp.put("txtUrl", publicFileUrl(txtLoc));
|
|
|
+
|
|
|
+ Map<String, Object> graph = readGraph(graphLoc, resp);
|
|
|
+ if (graph != null) {
|
|
|
+ resp.put("graphJson", graph);
|
|
|
+ resp.put("graph", graph);
|
|
|
+ resp.put("graphStatus", graph.get("graph_status"));
|
|
|
+ resp.put("identity", graph.get("identity"));
|
|
|
+ resp.put("period", graph.get("period"));
|
|
|
+ Object series = graph.get("series");
|
|
|
+ if (series instanceof List) {
|
|
|
+ List<Map<String, Object>> out = new ArrayList<>();
|
|
|
+ for (Object s : (List<?>) series) {
|
|
|
+ if (!(s instanceof Map)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Map<String, Object> item = (Map<String, Object>) s;
|
|
|
+ Map<String, Object> one = new LinkedHashMap<>();
|
|
|
+ one.put("engineCode", item.get("engine_code"));
|
|
|
+ Object datasets = item.get("datasets");
|
|
|
+ if (datasets instanceof Map) {
|
|
|
+ assemble((Map<String, Object>) datasets, one);
|
|
|
+ }
|
|
|
+ out.add(one);
|
|
|
+ }
|
|
|
+ resp.put("series", out);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resp.put("graphJson", null);
|
|
|
+ }
|
|
|
+ String rawTxt = readLocated(txtLoc, resp, "txtError");
|
|
|
+ resp.put("conclusionTxt", rawTxt);
|
|
|
+ if (rawTxt != null) {
|
|
|
+ Map<String, Object> txt = GzlyTxtParser.parse(rawTxt);
|
|
|
+ resp.put("conclusion", conclusionNode(txt, rawTxt));
|
|
|
+ resp.put("coverage", txt.get("coverage"));
|
|
|
+ resp.put("provenance", txt.get("provenance"));
|
|
|
+ if (resp.get("identity") == null) {
|
|
|
+ resp.put("identity", txt.get("identity"));
|
|
|
+ }
|
|
|
+ if (resp.get("period") == null) {
|
|
|
+ resp.put("period", txt.get("period"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<Map<String, Object>> criteria = new ArrayList<>();
|
|
|
+ List<Map<String, Object>> turbines = new ArrayList<>();
|
|
|
+ resp.put("metrics", loadTurbineMetrics(fieldCode, machineTypeCode, month, functionCode, criteria, turbines));
|
|
|
+ Map<String, Object> table = new LinkedHashMap<>();
|
|
|
+ table.put("id", "project_criteria_row");
|
|
|
+ table.put("rows", criteria);
|
|
|
+ resp.put("tables", Collections.singletonList(table));
|
|
|
+ resp.put("turbines", turbines);
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> turbineFunction(String fieldCode, String machineTypeCode,
|
|
|
+ String month, String functionCode, String engineCode) {
|
|
|
+ Map<String, Object> resp = baseResp(fieldCode, machineTypeCode, month, functionCode, engineCode);
|
|
|
+ Located graphLoc = locate(fieldCode, machineTypeCode, engineCode, functionCode, month, true);
|
|
|
+ Located txtLoc = locate(fieldCode, machineTypeCode, engineCode, functionCode, month, false);
|
|
|
+ resp.put("found", graphLoc.path != null || txtLoc.path != null);
|
|
|
+ resp.put("graphPath", graphLoc.path);
|
|
|
+ resp.put("txtPath", txtLoc.path);
|
|
|
+ resp.put("graphUrl", publicFileUrl(graphLoc));
|
|
|
+ resp.put("txtUrl", publicFileUrl(txtLoc));
|
|
|
+
|
|
|
+ Map<String, Object> graph = readGraph(graphLoc, resp);
|
|
|
+ if (graph != null) {
|
|
|
+ resp.put("graphJson", graph);
|
|
|
+ resp.put("graph", graph);
|
|
|
+ resp.put("graphStatus", graph.get("graph_status"));
|
|
|
+ resp.put("identity", graph.get("identity"));
|
|
|
+ resp.put("period", graph.get("period"));
|
|
|
+ Object datasets = graph.get("datasets");
|
|
|
+ if (datasets instanceof Map) {
|
|
|
+ assemble((Map<String, Object>) datasets, resp);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ resp.put("graphJson", null);
|
|
|
+ }
|
|
|
+ String rawTxt = readLocated(txtLoc, resp, "txtError");
|
|
|
+ resp.put("conclusionTxt", rawTxt);
|
|
|
+ if (rawTxt != null) {
|
|
|
+ Map<String, Object> txt = GzlyTxtParser.parse(rawTxt);
|
|
|
+ resp.put("conclusion", conclusionNode(txt, rawTxt));
|
|
|
+ resp.put("status", txt.get("status"));
|
|
|
+ resp.put("result", txt.get("result"));
|
|
|
+ resp.put("provenance", txt.get("provenance"));
|
|
|
+ if (resp.get("identity") == null) {
|
|
|
+ resp.put("identity", txt.get("identity"));
|
|
|
+ }
|
|
|
+ if (resp.get("period") == null) {
|
|
|
+ resp.put("period", txt.get("period"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> baseResp(String fieldCode, String machineTypeCode, String month,
|
|
|
+ String functionCode, String engineCode) {
|
|
|
+ Map<String, Object> resp = new LinkedHashMap<>();
|
|
|
+ resp.put("fieldCode", fieldCode);
|
|
|
+ resp.put("machineTypeCode", machineTypeCode);
|
|
|
+ resp.put("month", month);
|
|
|
+ resp.put("functionCode", functionCode);
|
|
|
+ if (engineCode != null) {
|
|
|
+ resp.put("engineCode", engineCode);
|
|
|
+ }
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 优先用算法表的路径列定位文件,列为空时再按桶内命名规则列举。
|
|
|
+ * graph=true 取 *_graph_json_path,否则取 *_conclusion_txt_path。
|
|
|
+ */
|
|
|
+ private Located locate(String fieldCode, String machineTypeCode, String engineCode,
|
|
|
+ String functionCode, String month, boolean graph) {
|
|
|
+ String func = functionCode == null ? "" : functionCode.trim().toUpperCase();
|
|
|
+ String ext = graph ? "graph.json" : "conclusion.txt";
|
|
|
+ String column = func.toLowerCase() + (graph ? "_graph_json_path" : "_conclusion_txt_path");
|
|
|
+ if (FUNCTION_CODES.contains(func) && month != null && month.matches("\\d{4}-\\d{2}")) {
|
|
|
+ try {
|
|
|
+ boolean turbine = engineCode != null && !engineCode.isEmpty();
|
|
|
+ String table = turbine ? "turbine_monthly_result" : "project_model_monthly_result";
|
|
|
+ StringBuilder sql = new StringBuilder("SELECT ").append(column)
|
|
|
+ .append(" AS p FROM ").append(table)
|
|
|
+ .append(" WHERE field_code = ? AND machine_type_code = ? AND period_start = ?");
|
|
|
+ List<Object> args = new ArrayList<>();
|
|
|
+ args.add(fieldCode);
|
|
|
+ args.add(machineTypeCode);
|
|
|
+ args.add(month + "-01");
|
|
|
+ if (turbine) {
|
|
|
+ sql.append(" AND engine_code = ?");
|
|
|
+ args.add(engineCode);
|
|
|
+ }
|
|
|
+ sql.append(" LIMIT 1");
|
|
|
+ List<Map<String, Object>> rows = analyseJdbcTemplate.queryForList(sql.toString(), args.toArray());
|
|
|
+ if (!rows.isEmpty() && rows.get(0).get("p") != null) {
|
|
|
+ String path = String.valueOf(rows.get(0).get("p")).trim();
|
|
|
+ if (!path.isEmpty() && !"null".equalsIgnoreCase(path)) {
|
|
|
+ return new Located(path, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("lookup {} path failed for {} {}: {}", ext, fieldCode, func, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String listed = findObject(fieldCode, machineTypeCode, engineCode, functionCode, month, ext);
|
|
|
+ return new Located(listed, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> readGraph(Located loc, Map<String, Object> resp) {
|
|
|
+ String raw = readLocated(loc, resp, "graphError");
|
|
|
+ if (raw == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return mapper.readValue(raw, LinkedHashMap.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ resp.put("graphError", "图 JSON 解析失败: " + e.getMessage());
|
|
|
+ log.warn("parse graph failed for {}: {}", loc.path, e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String readLocated(Located loc, Map<String, Object> resp, String errorKey) {
|
|
|
+ if (loc == null || loc.path == null || loc.path.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String raw = loc.fromDb ? readByFullPath(loc.path) : readText(loc.path);
|
|
|
+ if (raw == null) {
|
|
|
+ resp.put(errorKey, "已定位路径但读取失败: " + loc.path);
|
|
|
+ }
|
|
|
+ return raw;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final class Located {
|
|
|
+ private final String path;
|
|
|
+ private final boolean fromDb;
|
|
|
+
|
|
|
+ private Located(String path, boolean fromDb) {
|
|
|
+ this.path = path;
|
|
|
+ this.fromDb = fromDb;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把图 JSON 的扁平点号键 datasets(如 viz.windhist / algorithm.history / WGEN.xxx.history / source_extra)
|
|
|
+ * 拆成 resp.viz、resp.algData、resp.channels、resp.sourceExtra。
|
|
|
+ */
|
|
|
+ private void assemble(Map<String, Object> datasets, Map<String, Object> resp) {
|
|
|
+ Map<String, Object> viz = new LinkedHashMap<>();
|
|
|
+ Map<String, Object> algData = new LinkedHashMap<>();
|
|
|
+ Map<String, Map<String, Object>> channelMap = new LinkedHashMap<>();
|
|
|
+ for (Map.Entry<String, Object> e : datasets.entrySet()) {
|
|
|
+ String key = e.getKey();
|
|
|
+ Object value = e.getValue();
|
|
|
+ if (key == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (key.startsWith("viz.")) {
|
|
|
+ nest(viz, key.substring("viz.".length()), value);
|
|
|
+ } else if (key.startsWith("algorithm.")) {
|
|
|
+ nest(algData, key.substring("algorithm.".length()), value);
|
|
|
+ } else if ("source_extra".equals(key)) {
|
|
|
+ resp.put("sourceExtra", value);
|
|
|
+ viz.put("source_extra", value);
|
|
|
+ } else {
|
|
|
+ // 通道键:namespace.channelCode.field(如 WGEN.TemGenDriEnd.history)
|
|
|
+ int dot = key.lastIndexOf('.');
|
|
|
+ if (dot > 0) {
|
|
|
+ String channelCode = key.substring(0, dot);
|
|
|
+ String field = key.substring(dot + 1);
|
|
|
+ channelMap.computeIfAbsent(channelCode, k -> new LinkedHashMap<>()).put(field, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ resp.put("viz", viz);
|
|
|
+ resp.put("algData", algData);
|
|
|
+ if (!channelMap.isEmpty()) {
|
|
|
+ List<Map<String, Object>> channels = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, Map<String, Object>> e : channelMap.entrySet()) {
|
|
|
+ Map<String, Object> ch = new LinkedHashMap<>();
|
|
|
+ ch.put("code", e.getKey());
|
|
|
+ ch.putAll(e.getValue());
|
|
|
+ channels.add(ch);
|
|
|
+ }
|
|
|
+ resp.put("channels", channels);
|
|
|
+ algData.put("channels", channels);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 把 "curves.A04" 这样的相对点号路径按段嵌套进 dst。 */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private void nest(Map<String, Object> dst, String dottedKey, Object value) {
|
|
|
+ String[] parts = dottedKey.split("\\.");
|
|
|
+ Map<String, Object> cur = dst;
|
|
|
+ for (int i = 0; i < parts.length - 1; i++) {
|
|
|
+ Object nxt = cur.get(parts[i]);
|
|
|
+ if (!(nxt instanceof Map)) {
|
|
|
+ nxt = new LinkedHashMap<>();
|
|
|
+ cur.put(parts[i], nxt);
|
|
|
+ }
|
|
|
+ cur = (Map<String, Object>) nxt;
|
|
|
+ }
|
|
|
+ cur.put(parts[parts.length - 1], value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定位指定范围最新的对象。engineCode 为 null 表示项目级(machine_type_code 下直接放文件)。
|
|
|
+ */
|
|
|
+ private String findObject(String fieldCode, String machineTypeCode, String engineCode,
|
|
|
+ String functionCode, String month, String ext) {
|
|
|
+ List<String> dates = listDatePrefixes();
|
|
|
+ Collections.sort(dates, Collections.reverseOrder());
|
|
|
+ String filePrefix = functionCode + "_" + month + "_";
|
|
|
+ String fileSuffix = "." + ext;
|
|
|
+ for (String date : dates) {
|
|
|
+ StringBuilder prefix = new StringBuilder(date).append('/')
|
|
|
+ .append(fieldCode).append('/').append(machineTypeCode).append('/');
|
|
|
+ if (engineCode != null && !engineCode.isEmpty()) {
|
|
|
+ prefix.append(engineCode).append('/');
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Iterable<Result<Item>> results = minioClient.listObjects(
|
|
|
+ ListObjectsArgs.builder().bucket(bucket).prefix(prefix.toString()).recursive(true).build());
|
|
|
+ for (Result<Item> r : results) {
|
|
|
+ Item item = r.get();
|
|
|
+ String name = item.objectName();
|
|
|
+ String file = name.substring(name.lastIndexOf('/') + 1);
|
|
|
+ if (file.startsWith(filePrefix) && file.endsWith(fileSuffix)) {
|
|
|
+ return name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("listObjects failed for prefix={}: {}", prefix, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> listDatePrefixes() {
|
|
|
+ List<String> dates = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ Iterable<Result<Item>> results = minioClient.listObjects(
|
|
|
+ ListObjectsArgs.builder().bucket(bucket).delimiter("/").build());
|
|
|
+ for (Result<Item> r : results) {
|
|
|
+ Item item = r.get();
|
|
|
+ if (item.isDir()) {
|
|
|
+ String name = item.objectName();
|
|
|
+ dates.add(name.endsWith("/") ? name.substring(0, name.length() - 1) : name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("listDatePrefixes failed: {}", e.getMessage());
|
|
|
+ }
|
|
|
+ return dates;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String readText(String objectName) {
|
|
|
+ try {
|
|
|
+ return readObject(objectName);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("readText failed for {}: {}", objectName, e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 逐机指标:按报告月查 turbine_monthly_result 的 {func}_conclusion_txt_path,解析 result.value/unit/status。 */
|
|
|
+ private List<Map<String, Object>> loadTurbineMetrics(String fieldCode, String machineTypeCode,
|
|
|
+ String month, String functionCode,
|
|
|
+ List<Map<String, Object>> criteria,
|
|
|
+ List<Map<String, Object>> turbines) {
|
|
|
+ List<Map<String, Object>> out = new ArrayList<>();
|
|
|
+ String func = functionCode == null ? "" : functionCode.trim().toUpperCase();
|
|
|
+ if (!FUNCTION_CODES.contains(func) || month == null || !month.matches("\\d{4}-\\d{2}")) {
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+ String col = func.toLowerCase() + "_conclusion_txt_path";
|
|
|
+ try {
|
|
|
+ List<Map<String, Object>> rows = analyseJdbcTemplate.queryForList(
|
|
|
+ "SELECT engine_code, " + col + " AS txt_path FROM turbine_monthly_result " +
|
|
|
+ "WHERE field_code = ? AND machine_type_code = ? AND period_start = ?",
|
|
|
+ fieldCode, machineTypeCode, month + "-01");
|
|
|
+ for (Map<String, Object> row : rows) {
|
|
|
+ Object ec = row.get("engine_code");
|
|
|
+ Object path = row.get("txt_path");
|
|
|
+ if (ec == null || path == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Map<String, Object> txt = GzlyTxtParser.parse(readByFullPath(String.valueOf(path)));
|
|
|
+ Map<String, Object> result = (Map<String, Object>) txt.get("result");
|
|
|
+ Map<String, Object> status = (Map<String, Object>) txt.get("status");
|
|
|
+ Map<String, Object> m = new LinkedHashMap<>();
|
|
|
+ m.put("engineCode", String.valueOf(ec));
|
|
|
+ m.put("value", result == null ? null : result.get("value"));
|
|
|
+ m.put("unit", result == null ? null : result.get("unit"));
|
|
|
+ m.put("status", status == null ? null : status.get("source_status"));
|
|
|
+ Map<String, Object> display = result == null ? null : (Map<String, Object>) result.get("display");
|
|
|
+ if (display != null) {
|
|
|
+ m.put("metric", display.get("metric"));
|
|
|
+ }
|
|
|
+ out.add(m);
|
|
|
+ Map<String, Object> one = criteriaRow(txt, machineTypeCode, String.valueOf(ec));
|
|
|
+ if (criteria != null && one != null) {
|
|
|
+ criteria.add(one);
|
|
|
+ }
|
|
|
+ if (turbines != null) {
|
|
|
+ List<Map<String, Object>> channels = new ArrayList<>();
|
|
|
+ collectChannels(txt, channels, String.valueOf(ec));
|
|
|
+ Map<String, Object> turbine = new LinkedHashMap<>();
|
|
|
+ turbine.put("engineCode", String.valueOf(ec));
|
|
|
+ turbine.put("machineTypeCode", machineTypeCode);
|
|
|
+ turbine.put("criteria", one);
|
|
|
+ turbine.put("channels", channels);
|
|
|
+ turbines.add(turbine);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("loadTurbineMetrics failed for {} {} {}: {}",
|
|
|
+ fieldCode, machineTypeCode, functionCode, e.getMessage());
|
|
|
+ }
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 从风机 TXT 收集温度矩阵单元格。行对象、按通道代码嵌套的对象、列式数组都收。 */
|
|
|
+ private void collectChannels(Object node, List<Map<String, Object>> out, String engine) {
|
|
|
+ if (node instanceof Map) {
|
|
|
+ Map<String, Object> map = (Map<String, Object>) node;
|
|
|
+ if (map.get("channel_code") instanceof List) {
|
|
|
+ addColumnarChannels(map, out, engine);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (isChannelCell(map)) {
|
|
|
+ out.add(channelCell(map, engine, null));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
+ Object child = entry.getValue();
|
|
|
+ if (child instanceof Map && isChannelCell((Map<String, Object>) child)
|
|
|
+ && !((Map<?, ?>) child).containsKey("channel_code")) {
|
|
|
+ out.add(channelCell((Map<String, Object>) child, engine, entry.getKey()));
|
|
|
+ } else {
|
|
|
+ collectChannels(child, out, engine);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (node instanceof List) {
|
|
|
+ for (Object child : (List<?>) node) {
|
|
|
+ collectChannels(child, out, engine);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isChannelCell(Map<String, Object> map) {
|
|
|
+ if (map.containsKey("mean_residual_c")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ boolean named = map.containsKey("channel_code") || map.containsKey("channel_name");
|
|
|
+ return named && (map.containsKey("status") || map.containsKey("status_label")
|
|
|
+ || map.containsKey("classification"));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addColumnarChannels(Map<String, Object> map, List<Map<String, Object>> out, String engine) {
|
|
|
+ List<?> codes = (List<?>) map.get("channel_code");
|
|
|
+ List<?> names = map.get("channel_name") instanceof List ? (List<?>) map.get("channel_name") : null;
|
|
|
+ List<?> values = map.get("mean_residual_c") instanceof List ? (List<?>) map.get("mean_residual_c") : null;
|
|
|
+ List<?> statuses = null;
|
|
|
+ if (map.get("status_label") instanceof List) {
|
|
|
+ statuses = (List<?>) map.get("status_label");
|
|
|
+ } else if (map.get("classification") instanceof List) {
|
|
|
+ statuses = (List<?>) map.get("classification");
|
|
|
+ } else if (map.get("status") instanceof List) {
|
|
|
+ statuses = (List<?>) map.get("status");
|
|
|
+ }
|
|
|
+ for (int i = 0; i < codes.size(); i++) {
|
|
|
+ Map<String, Object> cell = new LinkedHashMap<>();
|
|
|
+ Object code = codes.get(i);
|
|
|
+ cell.put("engineCode", engine);
|
|
|
+ cell.put("channelCode", code);
|
|
|
+ Object name = names != null && i < names.size() ? names.get(i) : null;
|
|
|
+ cell.put("channelName", name != null ? name : code);
|
|
|
+ cell.put("value", values != null && i < values.size() ? values.get(i) : null);
|
|
|
+ cell.put("status", statuses != null && i < statuses.size() ? statuses.get(i) : null);
|
|
|
+ out.add(cell);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> channelCell(Map<String, Object> map, String engine, String fallbackCode) {
|
|
|
+ Object code = map.get("channel_code");
|
|
|
+ if (code == null) {
|
|
|
+ code = fallbackCode;
|
|
|
+ }
|
|
|
+ Object name = map.get("channel_name");
|
|
|
+ Object status = map.get("status_label");
|
|
|
+ if (status == null) {
|
|
|
+ status = map.get("classification");
|
|
|
+ }
|
|
|
+ if (status == null) {
|
|
|
+ status = map.get("status");
|
|
|
+ }
|
|
|
+ if (status instanceof Map) {
|
|
|
+ Object nested = ((Map<?, ?>) status).get("status_label");
|
|
|
+ if (nested == null) {
|
|
|
+ nested = ((Map<?, ?>) status).get("source_status");
|
|
|
+ }
|
|
|
+ status = nested;
|
|
|
+ }
|
|
|
+ Map<String, Object> cell = new LinkedHashMap<>();
|
|
|
+ cell.put("engineCode", map.get("engine_code") != null ? map.get("engine_code") : engine);
|
|
|
+ cell.put("channelCode", code);
|
|
|
+ cell.put("channelName", name != null ? name : code);
|
|
|
+ cell.put("value", map.get("mean_residual_c"));
|
|
|
+ cell.put("status", status);
|
|
|
+ return cell;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 项目结论:优先结论段的 text,其次 TXT 里的 /text 行,最后才是纯文本文件。 */
|
|
|
+ private Map<String, Object> conclusionNode(Map<String, Object> parsed, String raw) {
|
|
|
+ Map<String, Object> view = new LinkedHashMap<>();
|
|
|
+ if (parsed != null) {
|
|
|
+ Object section = parsed.get("conclusion");
|
|
|
+ if (section instanceof Map) {
|
|
|
+ view.putAll((Map<String, Object>) section);
|
|
|
+ } else if (section instanceof String) {
|
|
|
+ view.put("text", section);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!(view.get("text") instanceof String) || String.valueOf(view.get("text")).trim().isEmpty()) {
|
|
|
+ String fromTree = parsed == null ? null : firstString(parsed.get("conclusion"), "text");
|
|
|
+ if (fromTree == null && parsed != null) {
|
|
|
+ fromTree = firstString(parsed, "text");
|
|
|
+ }
|
|
|
+ if (fromTree != null) {
|
|
|
+ view.put("text", fromTree);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!(view.get("text") instanceof String) || String.valueOf(view.get("text")).trim().isEmpty()) {
|
|
|
+ String fromRaw = textFromRaw(raw);
|
|
|
+ if (fromRaw != null) {
|
|
|
+ view.put("text", fromRaw);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return view;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String textFromRaw(String raw) {
|
|
|
+ if (raw == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String trimmed = raw.trim();
|
|
|
+ if (trimmed.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (trimmed.contains("@value") && trimmed.contains("\t")) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (String line : trimmed.split("\n")) {
|
|
|
+ String[] cols = line.split("\t", -1);
|
|
|
+ if (cols.length < 4 || !"@value".equals(cols[0].trim())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String path = cols[1].trim().toLowerCase();
|
|
|
+ if (!"string".equals(cols[2].trim())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String value = cols[3];
|
|
|
+ if (value.isEmpty() || "\\N".equals(value)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (path.endsWith("/text") || path.endsWith("/summary") || path.endsWith("/advice")
|
|
|
+ || path.endsWith("/finding") || path.endsWith("/description")) {
|
|
|
+ if (sb.length() > 0) {
|
|
|
+ sb.append('\n');
|
|
|
+ }
|
|
|
+ sb.append(value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.length() == 0 ? null : sb.toString();
|
|
|
+ }
|
|
|
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return trimmed;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 一台风机的结论 TXT 收成逐机表的一行。没有检测结果和实际情况时不造行。 */
|
|
|
+ private Map<String, Object> criteriaRow(Map<String, Object> parsed, String machineTypeCode, String engineCode) {
|
|
|
+ if (parsed == null || parsed.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String status = firstString(parsed, "status_label");
|
|
|
+ if (status == null) {
|
|
|
+ status = firstString(parsed, "source_status");
|
|
|
+ }
|
|
|
+ String actual = firstString(parsed, "actual");
|
|
|
+ String actualLabel = firstString(parsed, "actual_label");
|
|
|
+ String criterion = firstString(parsed, "criterion");
|
|
|
+ String explanation = firstString(parsed, "explanation");
|
|
|
+ if (status == null && actual == null && criterion == null && explanation == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Map<String, Object> row = new LinkedHashMap<>();
|
|
|
+ String engine = firstString(parsed, "engine_code");
|
|
|
+ row.put("engine_code", engine != null ? engine : engineCode);
|
|
|
+ String model = firstString(parsed, "machine_type_code");
|
|
|
+ row.put("machine_type_code", model != null ? model : machineTypeCode);
|
|
|
+ row.put("status_label", status);
|
|
|
+ if (actualLabel != null && (actual == null || !actual.contains(actualLabel))) {
|
|
|
+ row.put("actual", actual == null ? actualLabel : actual + "\n" + actualLabel);
|
|
|
+ } else {
|
|
|
+ row.put("actual", actual);
|
|
|
+ }
|
|
|
+ row.put("criterion", criterion);
|
|
|
+ row.put("explanation", explanation);
|
|
|
+ return row;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String firstString(Object node, String key) {
|
|
|
+ if (node instanceof Map) {
|
|
|
+ Map<String, Object> map = (Map<String, Object>) node;
|
|
|
+ Object direct = map.get(key);
|
|
|
+ if (direct instanceof String || direct instanceof Number || direct instanceof Boolean) {
|
|
|
+ String text = String.valueOf(direct).trim();
|
|
|
+ if (!text.isEmpty()) {
|
|
|
+ return text;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Object child : map.values()) {
|
|
|
+ String found = firstString(child, key);
|
|
|
+ if (found != null) {
|
|
|
+ return found;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (node instanceof List) {
|
|
|
+ for (Object child : (List<?>) node) {
|
|
|
+ String found = firstString(child, key);
|
|
|
+ if (found != null) {
|
|
|
+ return found;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拼出浏览器可直接读取的完整地址。
|
|
|
+ * 本机和内网访问使用 http://192.168.50.233:6900,外网访问使用 http://106.120.102.238:26900。
|
|
|
+ */
|
|
|
+ private String publicFileUrl(Located loc) {
|
|
|
+ if (loc == null || loc.path == null || loc.path.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String stored = loc.path.trim();
|
|
|
+ if (stored.startsWith("http://") || stored.startsWith("https://")) {
|
|
|
+ return rewriteHost(stored);
|
|
|
+ }
|
|
|
+ String bucketName;
|
|
|
+ String objectKey;
|
|
|
+ int slash = stored.indexOf('/');
|
|
|
+ String first = slash > 0 ? stored.substring(0, slash) : "";
|
|
|
+ boolean dateKey = first.matches("\\d{8}|\\d{4}-\\d{2}-\\d{2}");
|
|
|
+ if (loc.fromDb && slash > 0 && !dateKey) {
|
|
|
+ bucketName = first;
|
|
|
+ objectKey = stored.substring(slash + 1);
|
|
|
+ } else {
|
|
|
+ bucketName = bucket;
|
|
|
+ objectKey = stored;
|
|
|
+ }
|
|
|
+ String base = dynamicBase();
|
|
|
+ if (base == null || base.isEmpty() || objectKey.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return base + "/" + bucketName + "/" + objectKey;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final String INTRANET_FILE_BASE = "http://192.168.50.233:6900";
|
|
|
+ private static final String EXTERNAL_FILE_BASE = "http://106.120.102.238:26900";
|
|
|
+ private static final String EXTERNAL_FILE_HOST = "106.120.102.238";
|
|
|
+
|
|
|
+ private String rewriteHost(String absoluteUrl) {
|
|
|
+ try {
|
|
|
+ URI uri = URI.create(absoluteUrl);
|
|
|
+ String path = uri.getRawPath() == null ? "" : uri.getRawPath();
|
|
|
+ String query = uri.getRawQuery() == null ? "" : "?" + uri.getRawQuery();
|
|
|
+ return dynamicBase() + path + query;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("rewrite file url failed for {}: {}", absoluteUrl, e.getMessage());
|
|
|
+ return absoluteUrl;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String dynamicBase() {
|
|
|
+ return externalRequest() ? EXTERNAL_FILE_BASE : INTRANET_FILE_BASE;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 请求来自公网地址时走外网 MinIO,本机回环和局域网地址走内网 MinIO。 */
|
|
|
+ private boolean externalRequest() {
|
|
|
+ HttpServletRequest request = currentRequest();
|
|
|
+ if (request == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String[] candidates = new String[] {
|
|
|
+ request.getHeader("X-Forwarded-Host"),
|
|
|
+ request.getHeader("Host"),
|
|
|
+ request.getHeader("Origin"),
|
|
|
+ request.getHeader("Referer"),
|
|
|
+ request.getServerName(),
|
|
|
+ IPUtils.getIpAddr(request),
|
|
|
+ };
|
|
|
+ for (String raw : candidates) {
|
|
|
+ String host = hostOnly(raw);
|
|
|
+ if (EXTERNAL_FILE_HOST.equals(host) || isPublicAddress(host)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpServletRequest currentRequest() {
|
|
|
+ if (!(RequestContextHolder.getRequestAttributes() instanceof ServletRequestAttributes)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String hostOnly(String host) {
|
|
|
+ if (host == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String value = host.trim();
|
|
|
+ if (value.isEmpty() || "unknown".equalsIgnoreCase(value)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ int comma = value.indexOf(',');
|
|
|
+ if (comma > 0) {
|
|
|
+ value = value.substring(0, comma).trim();
|
|
|
+ }
|
|
|
+ if (value.startsWith("[")) {
|
|
|
+ int end = value.indexOf(']');
|
|
|
+ return end > 0 ? value.substring(0, end + 1) : value;
|
|
|
+ }
|
|
|
+ int colon = value.indexOf(':');
|
|
|
+ return colon > 0 ? value.substring(0, colon) : value;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isLoopback(String host) {
|
|
|
+ return "localhost".equalsIgnoreCase(host)
|
|
|
+ || "127.0.0.1".equals(host)
|
|
|
+ || "0:0:0:0:0:0:0:1".equals(host)
|
|
|
+ || "::1".equals(host);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 回环和局域网地址视为内网;其余公网 IP 视为外网。 */
|
|
|
+ private static boolean isPublicAddress(String host) {
|
|
|
+ if (host == null || host.isEmpty() || isLoopback(host) || host.indexOf(':') >= 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String[] parts = host.split("\\.");
|
|
|
+ if (parts.length != 4) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ int[] numbers = new int[4];
|
|
|
+ for (int i = 0; i < 4; i++) {
|
|
|
+ try {
|
|
|
+ numbers[i] = Integer.parseInt(parts[i]);
|
|
|
+ } catch (NumberFormatException ex) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (numbers[i] < 0 || numbers[i] > 255) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (numbers[0] == 10 || (numbers[0] == 192 && numbers[1] == 168)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (numbers[0] == 172 && numbers[1] >= 16 && numbers[1] <= 31) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 读全路径(含桶名)对象:bucket/objectKey。 */
|
|
|
+ private String readByFullPath(String fullPath) {
|
|
|
+ if (fullPath == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ int slash = fullPath.indexOf('/');
|
|
|
+ String first = slash > 0 ? fullPath.substring(0, slash) : "";
|
|
|
+ // 日期前缀是对象键(列举结果或未带桶名的路径),桶名用诊断桶;否则首段是桶名。
|
|
|
+ boolean dateKey = first.matches("\\d{8}|\\d{4}-\\d{2}-\\d{2}");
|
|
|
+ String b = slash > 0 && !dateKey ? first : bucket;
|
|
|
+ String obj = slash > 0 && !dateKey ? fullPath.substring(slash + 1) : fullPath;
|
|
|
+ try {
|
|
|
+ return readObject(b, obj);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("readByFullPath failed for {}: {}", fullPath, e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String readObject(String objectName) throws Exception {
|
|
|
+ return readObject(bucket, objectName);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String readObject(String b, String obj) throws Exception {
|
|
|
+ try (InputStream in = minioClient.getObject(
|
|
|
+ GetObjectArgs.builder().bucket(b).object(obj).build())) {
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ byte[] buf = new byte[8192];
|
|
|
+ int n;
|
|
|
+ while ((n = in.read(buf)) != -1) {
|
|
|
+ bos.write(buf, 0, n);
|
|
|
+ }
|
|
|
+ return new String(bos.toByteArray(), StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|